Skip to content

Commit 5ac7606

Browse files
authored
[docs] sync docs with types (#2299)
1 parent 168af86 commit 5ac7606

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

documentation/docs/04-hooks.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,47 @@ If unimplemented, defaults to `({ request, resolve }) => resolve(request)`.
1717
```ts
1818
// handle TypeScript type definitions
1919

20-
type Headers = Record<string, string>;
20+
export type RequestHeaders = Record<string, string>;
2121

22-
type Request<Locals = Record<string, any>> = {
22+
/** Only value that can be an array is set-cookie. For everything else we assume string value */
23+
export type ResponseHeaders = Record<string, string | string[]>;
24+
25+
export type RawBody = null | Uint8Array;
26+
27+
export interface IncomingRequest {
2328
method: string;
2429
host: string;
25-
headers: Headers;
2630
path: string;
27-
params: Record<string, string>;
2831
query: URLSearchParams;
29-
rawBody: Uint8Array;
32+
headers: RequestHeaders;
33+
rawBody: RawBody;
34+
}
35+
36+
export type ParameterizedBody<Body = unknown> = Body extends FormData
37+
? ReadOnlyFormData
38+
: (string | RawBody | ReadOnlyFormData) & Body;
39+
40+
export interface ServerRequest<Locals = Record<string, any>, Body = unknown>
41+
extends IncomingRequest {
42+
params: Record<string, string>;
3043
body: ParameterizedBody<Body>;
31-
locals: Locals; // populated by hooks handle
32-
};
44+
locals: Locals;
45+
}
46+
47+
export type StrictBody = string | Uint8Array;
3348

34-
type Response = {
49+
export interface ServerResponse {
3550
status: number;
36-
headers: Headers;
37-
body?: string | Uint8Array;
38-
};
39-
40-
type Handle<Locals = Record<string, any>> = (input: {
41-
request: Request<Locals>;
42-
resolve: (request: Request<Locals>) => Response | Promise<Response>;
43-
}) => Response | Promise<Response>;
51+
headers: ResponseHeaders;
52+
body?: StrictBody;
53+
}
54+
55+
export interface Handle<Locals = Record<string, any>> {
56+
(input: {
57+
request: ServerRequest<Locals>;
58+
resolve(request: ServerRequest<Locals>): MaybePromise<ServerResponse>;
59+
}): MaybePromise<ServerResponse>;
60+
}
4461
```
4562

4663
To add custom data to the request, which is passed to endpoints, populate the `request.locals` object, as shown below.
@@ -71,7 +88,7 @@ During development, if an error occurs because of a syntax error in your Svelte
7188
If unimplemented, SvelteKit will log the error with default formatting.
7289

7390
```ts
74-
type HandleError = HandleError<Locals = Record<string, any>> {
91+
export interface HandleError<Locals = Record<string, any>> {
7592
(input: { error: Error & { frame?: string }; request: ServerRequest<Locals> }): void;
7693
}
7794
```
@@ -95,9 +112,9 @@ If unimplemented, session is `{}`.
95112
```ts
96113
// getSession TypeScript type definition
97114

98-
type GetSession<Locals = Record<string, any>, Session = any> = {
99-
(request: Request<Locals>): Session | Promise<Session>;
100-
};
115+
export interface GetSession<Locals = Record<string, any>, Session = any> {
116+
(request: ServerRequest<Locals>): MaybePromise<Session>;
117+
}
101118
```
102119

103120
```js
@@ -127,7 +144,9 @@ This function allows you to modify (or replace) a `fetch` request for an externa
127144
For example, your `load` function might make a request to a public URL like `https://api.yourapp.com` when the user performs a client-side navigation to the respective page, but during SSR it might make sense to hit the API directly (bypassing whatever proxies and load balancers sit between it and the public internet).
128145

129146
```ts
130-
type ExternalFetch = (req: Request) => Promise<Response>;
147+
export interface ExternalFetch {
148+
(req: Request): Promise<Response>;
149+
}
131150
```
132151

133152
```js

0 commit comments

Comments
 (0)