Current section
Files
Jump to
Current section
Files
src/fetch_event.gleam
import gleam/http/response.{type Response}
import gleam/http/request.{type Request}
import gleam/javascript/promise.{type Promise}
pub type FetchEvent(body) {
FetchEvent(request: Request(body))
}
/// Fetch event handler
/// This function is called when a fetch event is triggered
/// eg. when a request is made to the server
@external(javascript, "./ffi.mjs", "fetchEventHandler")
pub fn fetch_event_handler(handler: fn(FetchEvent(body)) -> Nil) -> Nil
/// Respond with a response
/// This function is used to respond to a fetch event
@external(javascript, "./ffi.mjs", "respondWith")
pub fn respond_with(
event: FetchEvent(body),
handler: fn() -> Response(value),
) -> Nil
/// Respond with a promise
/// This function is used to respond to a fetch event with a promise
@external(javascript, "./ffi.mjs", "respondWith")
pub fn respond_with_promise(
event: FetchEvent(body),
handler: fn() -> Promise(Response(value)),
) -> Nil
/// Wait until a promise resolves
/// This function to perform asynchronous work without blocking the request/response cycle
@external(javascript, "./ffi.mjs", "waitUntil")
pub fn wait_until(event: FetchEvent(body), promise: Promise(value)) -> Nil
/// Convert a Gleam Response to a native JavaScript Response
@external(javascript, "./ffi.mjs", "toNativeResponse")
pub fn to_native_response(response: Response(value)) -> Response(value)