-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples.ts
349 lines (287 loc) · 8.54 KB
/
examples.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* This file contains various examples together with tests for typings declarations
*/
import { createApiFetcher, fetchf } from '../../src';
import type { Endpoint } from '../../src/types';
const endpoints = {
ping: { url: 'ping' },
fetchMovies: { url: 'movies' },
fetchBooks: {
url: 'books',
unknownProperty: 'error',
},
fetchBook: {
url: 'books/:bookId',
},
};
// Example interfaces of responses, query params, and url path params
interface Book {
id: number;
title: string;
}
interface Books {
books: Book[];
totalResults: number;
}
interface Movie {
id: number;
title: string;
}
type Movies = Movie[];
interface BooksQueryParams {
all: boolean;
}
interface BookQueryParams {
newBook: boolean;
}
interface BookPathParams {
bookId: number;
}
// Automatic type inference + how to obtain config and endpoints to modify
async function example1() {
const api = createApiFetcher({
apiUrl: '',
endpoints,
});
const apiConfig = api.config;
const endpointsList = api.endpoints;
const { data } = await api.ping();
// @ts-expect-error Endpoint ping2 does not exist
await api.ping2();
console.log('Example 1', data, apiConfig, endpointsList);
}
// With passed "typeof endpoints" to createApiFetcher()
async function example2() {
const api = createApiFetcher({
apiUrl: '',
endpoints,
});
const apiConfig = api.config;
const endpointsList = api.endpoints;
api.ping();
// @ts-expect-error Endpoint ping2 does not exist
await api.ping2();
const { data } = await api.ping<{ dd: string }>();
console.log('Example 2', data, apiConfig, endpointsList);
}
// Explicit types passed to createApiFetcher()
async function example3() {
// Note how you do not need to specify all endpoints for typings to work just fine.
interface Endpoints {
fetchBook: Endpoint<Book, BookQueryParams, BookPathParams>;
fetchBooks: Endpoint<Books, BooksQueryParams>;
}
type EndpointsConfiguration = typeof endpoints;
const api = createApiFetcher<Endpoints, EndpointsConfiguration>({
apiUrl: '',
endpoints,
});
const apiConfig = api.config;
const endpointsList = api.endpoints;
// Defined in EndpointsList
const { data } = await api.ping();
// Defined in EndpointsList with query param and url path param
const { data: book } = await api.fetchBook({
params: { newBook: true },
urlPathParams: { bookId: 1 },
});
// Defined in "endpoints" but not in EndpointsList so there is no need to add "fetchMovies: Endpoint;" explicitly.
const { data: movies1 } = await api.fetchMovies();
// With dynamically inferred type
const { data: movies } = await api.fetchMovies<Movies>();
const { data: movies3 }: { data: Movies } = await api.fetchMovies<Movies>();
// With custom params not defined in any interface
const { data: movies4 } = await api.fetchMovies({
params: {
all: true,
},
});
// @ts-expect-error This will result in an error as endpoint is not defined
const { data: movies2 } = await api.nonExistentEndpoint();
interface NewBook {
alternativeInterface: string;
}
interface NewBookQueryParams {
color: string;
}
// Overwrite response of existing endpoint
const { data: book1 } = await api.fetchBook<NewBook>(
{ newBook: true },
// @ts-expect-error should verify that bookId cannot be text
{ bookId: 'text' },
);
// Overwrite response and query params of existing endpoint
const { data: book11 } = await api.fetchBook<NewBook, NewBookQueryParams>({
params: {
// @ts-expect-error Should not allow old param
newBook: true,
color: 'green',
// TODO: @ts-expect-error Should not allow non-existent param
type: 'red',
},
});
// Standard fetch with predefined response and query params
const { data: books } = await api.fetchBooks({
// TODO: @ts-expect-error Non-existent setting
test: true,
params: {
// This param exists
all: true,
// @ts-expect-error Should not allow non-existent param
randomParam: 1,
},
});
const { data: book2 } = await api.fetchBook(
{ newBook: true },
// @ts-expect-error Error as bookId is not a number
{ bookId: 'text' },
);
const { data: book3 } = await api.fetchBook({
// @ts-expect-error Error as newBook is not a boolean
params: { newBook: 'true' },
urlPathParams: { bookId: 1 },
});
console.log('Example 3', data, apiConfig, endpointsList);
console.log('Example 3', movies, movies1, movies2, movies3, movies4);
console.log(
'Example 3',
books satisfies Books,
book satisfies Book,
book1 satisfies NewBook,
book11 satisfies NewBook,
book2 satisfies Book,
book3 satisfies Book,
);
}
// createApiFetcher() - direct API request() call to a custom endpoint with flattenResponse == true
async function example4() {
interface Endpoints {
fetchBooks: Endpoint<Books, BooksQueryParams>;
}
type EndpointsConfiguration = typeof endpoints;
const api = createApiFetcher<Endpoints, EndpointsConfiguration>({
apiUrl: '',
endpoints,
flattenResponse: true,
});
// Existing endpoint generic
const { data: books } = await api.request<Books>('fetchBooks');
// Custom URL
const { data: data1 } = await api.request(
'https://example.com/api/custom-endpoint',
);
interface OtherEndpointData {
myData: true;
}
// Explicitly defined empty config
const { data: data4 } = await api.request('fetchBooks', {
params: {
anyParam: true,
},
});
// Dynamically added Response to a generic
const { data: data2 } = await api.request<OtherEndpointData>(
'https://example.com/api/custom-endpoint',
);
// Dynamically added Response to a generic using fetchf()
const { data: data3 } = await fetchf<OtherEndpointData>(
'https://example.com/api/custom-endpoint',
);
// Existing endpoint with custom params
interface DynamicQueryParams {
param1: string;
}
interface DynamicUrlParams {
urlparam2: number;
}
const { data: books2 } = await api.request<
Books,
DynamicQueryParams,
DynamicUrlParams
>('fetchBooks', {
// Native fetch() setting
cache: 'no-store',
// Extended fetch setting
cacheTime: 86000,
// TODO: @ts-expect-error Non-existent setting
something: true,
urlPathParams: {
// @ts-expect-error Non-existent param
urlparam1: '1',
urlparam2: 1,
},
params: {
param1: '1',
// @ts-expect-error Non-existent param
param2: 1,
},
});
console.log('Example 4', books satisfies Books, books2 satisfies Books);
console.log(
'Example 4',
data1,
data2 satisfies OtherEndpointData,
data3 satisfies OtherEndpointData,
data4,
);
}
// createApiFetcher() - direct API request() call to a custom endpoint with flattenResponse == false
async function example5() {
interface MyEndpoints {
fetchBooks: Endpoint<Books, BooksQueryParams>;
}
type EndpointsConfiguration = typeof endpoints;
const api = createApiFetcher<MyEndpoints, EndpointsConfiguration>({
apiUrl: '',
endpoints,
});
const { data: books2 } = await api.fetchBooks();
const { data: books } = await api.request<Books>('fetchBooks', {});
const { data: data1 } = await api.request(
'https://example.com/api/custom-endpoint',
);
// Specify generic
const { data: data2 } = await api.request<{ myData: true }>(
'https://example.com/api/custom-endpoint',
);
console.log('Example 5', books, books2);
console.log('Example 5', data1, data2);
}
// fetchf() - direct fetchf() request
async function example6() {
const { data: books } = await fetchf<Books>('fetchBooks');
const { data: data1 } = await fetchf(
'https://example.com/api/custom-endpoint',
);
// Specify generic
const { data: data2 } = await fetchf<{ myData: true }>(
'https://example.com/api/custom-endpoint',
);
// Fetch with custom settings
const { data: books2 } = await fetchf<Books>('fetchBooks', {
// Native fetch() setting
cache: 'no-store',
// Extended fetch setting
cacheTime: 86000,
// @ts-expect-error Non-existent setting
something: true,
});
console.log('Example 6', books satisfies Books, books2 satisfies Books);
console.log('Example 6', data1, data2);
}
// fetchf() - direct fetchf() request with interceptor
async function example7() {
const response = await fetchf('https://example.com/api/custom-endpoint', {
onResponse(response) {
response.data = { username: 'modified response' };
},
});
console.log('Example 7', response);
}
example1();
example2();
example3();
example4();
example5();
example6();
example7();