@@ -17,30 +17,47 @@ If unimplemented, defaults to `({ request, resolve }) => resolve(request)`.
17
17
``` ts
18
18
// handle TypeScript type definitions
19
19
20
- type Headers = Record <string , string >;
20
+ export type RequestHeaders = Record <string , string >;
21
21
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 {
23
28
method: string ;
24
29
host: string ;
25
- headers: Headers ;
26
30
path: string ;
27
- params: Record <string , string >;
28
31
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 >;
30
43
body: ParameterizedBody <Body >;
31
- locals: Locals ; // populated by hooks handle
32
- };
44
+ locals: Locals ;
45
+ }
46
+
47
+ export type StrictBody = string | Uint8Array ;
33
48
34
- type Response = {
49
+ export interface ServerResponse {
35
50
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
+ }
44
61
```
45
62
46
63
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
71
88
If unimplemented, SvelteKit will log the error with default formatting.
72
89
73
90
``` ts
74
- type HandleError = HandleError <Locals = Record <string , any >> {
91
+ export interface HandleError <Locals = Record <string , any >> {
75
92
(input : { error: Error & { frame? : string }; request: ServerRequest <Locals > }): void ;
76
93
}
77
94
```
@@ -95,9 +112,9 @@ If unimplemented, session is `{}`.
95
112
``` ts
96
113
// getSession TypeScript type definition
97
114
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
+ }
101
118
```
102
119
103
120
``` js
@@ -127,7 +144,9 @@ This function allows you to modify (or replace) a `fetch` request for an externa
127
144
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).
128
145
129
146
``` ts
130
- type ExternalFetch = (req : Request ) => Promise <Response >;
147
+ export interface ExternalFetch {
148
+ (req : Request ): Promise <Response >;
149
+ }
131
150
```
132
151
133
152
``` js
0 commit comments