diff --git a/documentation/docs/01-routing.md b/documentation/docs/01-routing.md index 38b7a219461f..93ebcf3fc656 100644 --- a/documentation/docs/01-routing.md +++ b/documentation/docs/01-routing.md @@ -48,27 +48,29 @@ A file or directory can have multiple dynamic parts, like `[id]-[category].svelt Endpoints are modules written in `.js` (or `.ts`) files that export functions corresponding to HTTP methods. For example, our hypothetical blog page, `/blog/cool-article`, might request data from `/blog/cool-article.json`, which could be represented by a `src/routes/blog/[slug].json.js` endpoint: ```ts -type Request = { +type Headers = Record; + +type Request, Body = unknown> = { + method: string; host: string; - method: 'GET'; - headers: Record; + headers: Headers; path: string; - params: Record; + params: Record; query: URLSearchParams; rawBody: string | Uint8Array; - body: string | Uint8Array | JSONValue; - locals: Record; // see below + body: ParameterizedBody; + locals: Locals; // populated by hooks handle }; -type Response = { +type EndpointOutput = { status?: number; - headers?: Record; - body?: any; + headers?: Headers; + body?: string | Uint8Array | JSONValue; }; -type RequestHandler = { - (request: Request) => Response | Promise; -} +type RequestHandler> = ( + request: Request +) => void | EndpointOutput | Promise; ``` ```js diff --git a/documentation/docs/03-loading.md b/documentation/docs/03-loading.md index 2123917ff499..6b68e1578528 100644 --- a/documentation/docs/03-loading.md +++ b/documentation/docs/03-loading.md @@ -11,7 +11,7 @@ type LoadInput = { page: { host: string; path: string; - params: Record; + params: Record; query: URLSearchParams; }; fetch: (info: RequestInfo, init?: RequestInit) => Promise; @@ -21,7 +21,7 @@ type LoadInput = { type LoadOutput = { status?: number; - error?: Error; + error?: string | Error; redirect?: string; props?: Record; context?: Record; diff --git a/documentation/docs/04-hooks.md b/documentation/docs/04-hooks.md index cffd484a788a..e8c9187ac482 100644 --- a/documentation/docs/04-hooks.md +++ b/documentation/docs/04-hooks.md @@ -15,6 +15,8 @@ If unimplemented, defaults to `({ request, render }) => render(request)`. To add custom data to the request, which is passed to endpoints, populate the `request.locals` object, as shown below. ```ts +type Headers = Record; + type Request> = { method: string; host: string; @@ -23,19 +25,19 @@ type Request> = { params: Record; query: URLSearchParams; rawBody: string | Uint8Array; - body: string | Uint8Array | ReadOnlyFormData | JSONValue; - locals: Locals; + body: ParameterizedBody; + locals: Locals; // populated by hooks handle }; type Response = { - status?: number; - headers?: Headers; - body?: any; + status: number; + headers: Headers; + body?: string | Uint8Array; }; -type Handle> = ({ - request: Request, - render: (request: Request) => Promise +type Handle> = (input: { + request: Request; + render: (request: Request) => Response | Promise; }) => Response | Promise; ```