@@ -12,10 +12,10 @@ zero-fetch just add type information to native fetch, and does not add any runti
12
12
Type information is erased during compilation, so it does not affect the runtime behavior.
13
13
As a result, it does not increase bundle size and does not have any runtime dependencies.
14
14
15
- ```` typescript
15
+ ``` typescript
16
16
// fetchT is just native fetch, so it does not have any additional runtime dependencies
17
17
const fetchT = fetch as FetchT <" " , Spec >;
18
- ````
18
+ ```
19
19
20
20
:::
21
21
@@ -29,7 +29,7 @@ zero-fetch provides type information for the response data based on the API spec
29
29
type Spec = DefineApiEndpoints <{
30
30
" /users" : {
31
31
get: {
32
- responses: { 200 : { body: { names: string [] }; }; };
32
+ responses: { 200 : { body: { names: string [] } } };
33
33
};
34
34
};
35
35
}>;
@@ -42,18 +42,18 @@ const data = await res.json(); // data is { userNames: string[] }
42
42
If the response have multiple status codes, response type is union of each status code type.
43
43
44
44
``` typescript
45
- type Headers = { headers: { ' Content-Type' : ' application/json' } };
45
+ type Headers = { headers: { " Content-Type" : " application/json" } };
46
46
type Spec = DefineApiEndpoints <{
47
47
" /users" : {
48
48
get: {
49
49
responses: {
50
- 200 : { body: { names: string [] }; } & Headers ;
51
- 201 : { body: { ok: boolean }; } & Headers ;
52
- 400 : { body: { message: string ; }; } & Headers ;
53
- 500 : { body: { internalError: string ; }; } & Headers ;
50
+ 200 : { body: { names: string [] } } & Headers ;
51
+ 201 : { body: { ok: boolean } } & Headers ;
52
+ 400 : { body: { message: string } } & Headers ;
53
+ 500 : { body: { internalError: string } } & Headers ;
54
54
};
55
55
};
56
- }
56
+ };
57
57
}>;
58
58
59
59
const fetchT = fetch as FetchT <" " , Spec >;
@@ -62,12 +62,12 @@ if (!res.ok) {
62
62
// If res.ok is false, status code is 400 or 500
63
63
// So res.json() returns { message: string } | { internalError: string }
64
64
const data = await res .json ();
65
-
65
+
66
66
// Response headers are also type-checked. Content-Type is always 'application/json'
67
- const contentType: ' application/json' = res .headers .get (' Content-Type' );
67
+ const contentType: " application/json" = res .headers .get (" Content-Type" );
68
68
// and, hasContentType is inferred as true, not boolean
69
- const hasContentType: true = res .headers .has (' Content-Type' );
70
-
69
+ const hasContentType: true = res .headers .has (" Content-Type" );
70
+
71
71
return console .error (data );
72
72
}
73
73
// If res.ok is true, status code is 200 or 201
@@ -83,26 +83,24 @@ It means that you can not `append`, `set` or `delete` operation after the respon
83
83
This is a limitation of the type system, not a runtime change. If you need mutable operations, you can cast types.
84
84
85
85
``` typescript
86
- const immutableHeaders = res .headers
86
+ const immutableHeaders = res .headers ;
87
87
const mutableHeaders = res .headers as Headers ;
88
88
```
89
89
90
90
:::
91
91
92
-
93
92
### Path & Path parameters
94
93
95
94
zero-fetch accepts only the path that is defined in the API specification.
96
95
Path parameters are also supported as ` :paramName ` in the path.
97
96
98
97
``` typescript
99
-
100
98
type Spec = DefineApiEndpoints <{
101
99
" /users" : {
102
- get: { responses: { 200 : { body: { names: string [] }; }; }; };
100
+ get: { responses: { 200 : { body: { names: string [] } } } };
103
101
};
104
102
" /users/:id" : {
105
- get: { responses: { 200 : { body: { name: string }; }; }; };
103
+ get: { responses: { 200 : { body: { name: string } } } };
106
104
};
107
105
}>;
108
106
const fetchT = fetch as FetchT <" " , Spec >;
@@ -122,7 +120,7 @@ type Spec = DefineApiEndpoints<{
122
120
" /users" : {
123
121
get: {
124
122
query: { page: string };
125
- responses: { 200 : { body: { names: string [] }; }; };
123
+ responses: { 200 : { body: { names: string [] } } };
126
124
};
127
125
};
128
126
}>;
@@ -142,7 +140,7 @@ type Spec = DefineApiEndpoints<{
142
140
" /users" : {
143
141
get: {
144
142
headers: { " x-api-key" : string };
145
- responses: { 200 : { body: { names: string [] }; }; };
143
+ responses: { 200 : { body: { names: string [] } } };
146
144
};
147
145
};
148
146
}>;
@@ -158,19 +156,22 @@ zero-fetch accepts only the body that is defined in the API specification.
158
156
Please note that when converting an object to a string, you must use the ` JSONT ` type provided by typed-api-spec.
159
157
160
158
``` typescript
161
- import { JSONT } from " @mpppk /typed-api-spec/json" ;
159
+ import { JSONT } from " @notainc /typed-api-spec/json" ;
162
160
type Spec = DefineApiEndpoints <{
163
161
" /users" : {
164
162
post: {
165
163
body: { name: string };
166
- responses: { 200 : { body: { id: string }; }; };
164
+ responses: { 200 : { body: { id: string } } };
167
165
};
168
166
};
169
167
}>;
170
168
const fetchT = fetch as FetchT <" " , Spec >;
171
169
const JSONT = JSON as JSONT ;
172
170
173
- await fetchT (" /users" , { method: " POST" , body: JSONT .stringify ({ name: " name" }) }); // OK
171
+ await fetchT (" /users" , {
172
+ method: " POST" ,
173
+ body: JSONT .stringify ({ name: " name" }),
174
+ }); // OK
174
175
await fetchT (" /users" , { method: " POST" , body: JSONT .stringify ({ name: 1 }) }); // Error: Type TypedString<{ userName: number; }> is not assignable to type TypedString<{ userName: string; }>
175
176
```
176
177
@@ -181,7 +182,6 @@ We hope to address this issue in the future, but for now, you must provide an em
181
182
182
183
:::
183
184
184
-
185
185
## API
186
186
187
187
### FetchT
@@ -199,10 +199,11 @@ JSONT is a type that adds type information to native JSON.
199
199
If you want to check body parameter type, you need to use JSONT.stringify to convert object to string.
200
200
201
201
``` typescript
202
- import { JSONT } from " @mpppk /typed-api-spec/json" ;
202
+ import { JSONT } from " @notainc /typed-api-spec/json" ;
203
203
const JSONT = JSON as JSONT ;
204
204
// body parameter type will be checked by using JSONT.stringify
205
- await fetchT (" /users" , { method: " POST" , body: JSONT .stringify ({ name: " name" }) });
205
+ await fetchT (" /users" , {
206
+ method: " POST" ,
207
+ body: JSONT .stringify ({ name: " name" }),
208
+ });
206
209
```
207
-
208
-
0 commit comments