@data-client/rest 0.9.9 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/index.js +8 -1
- package/dist/index.umd.min.js +1 -1
- package/legacy/OptionsToFunction.js +1 -1
- package/legacy/RestEndpoint.js +7 -2
- package/legacy/RestEndpointTypes.js +1 -1
- package/legacy/index.js +2 -1
- package/legacy/mapCollection.js +1 -1
- package/legacy/paramsToString.js +1 -1
- package/legacy/paramsToString.native.js +1 -1
- package/lib/OptionsToFunction.d.ts.map +1 -1
- package/lib/OptionsToFunction.js +1 -1
- package/lib/RestEndpoint.js +7 -2
- package/lib/RestEndpointTypes.d.ts +29 -7
- package/lib/RestEndpointTypes.d.ts.map +1 -1
- package/lib/RestEndpointTypes.js +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/mapCollection.d.ts.map +1 -1
- package/lib/mapCollection.js +1 -1
- package/lib/paramsToString.d.ts +1 -1
- package/lib/paramsToString.d.ts.map +1 -1
- package/lib/paramsToString.js +1 -1
- package/lib/paramsToString.native.d.ts +1 -1
- package/lib/paramsToString.native.d.ts.map +1 -1
- package/lib/paramsToString.native.js +1 -1
- package/package.json +2 -2
- package/src/OptionsToFunction.ts +44 -42
- package/src/RestEndpoint.js +6 -1
- package/src/RestEndpointTypes.ts +118 -90
- package/src/__tests__/RestEndpoint.ts +54 -2
- package/src/index.ts +1 -0
- package/src/mapCollection.ts +5 -4
- package/src/paramsToString.native.ts +6 -1
- package/src/paramsToString.ts +7 -2
- package/ts4.0/RestEndpointTypes.d.ts +29 -7
- package/ts4.0/index.d.ts +1 -0
- package/ts4.0/paramsToString.d.ts +1 -1
- package/ts4.0/paramsToString.native.d.ts +1 -1
- package/ts4.1/RestEndpointTypes.d.ts +29 -7
- package/ts4.1/index.d.ts +1 -0
- package/ts4.1/paramsToString.d.ts +1 -1
- package/ts4.1/paramsToString.native.d.ts +1 -1
package/src/OptionsToFunction.ts
CHANGED
|
@@ -7,49 +7,51 @@ export type OptionsToFunction<
|
|
|
7
7
|
O extends PartialRestGenerics,
|
|
8
8
|
E extends { body?: any; path?: string; method?: string },
|
|
9
9
|
F extends FetchFunction,
|
|
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
|
-
|
|
10
|
+
> =
|
|
11
|
+
'path' extends keyof O ?
|
|
12
|
+
RestFetch<
|
|
13
|
+
'searchParams' extends keyof O ?
|
|
14
|
+
O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
15
|
+
: PathArgs<Exclude<O['path'], undefined>>,
|
|
16
|
+
OptionsToBodyArgument<
|
|
17
|
+
'body' extends keyof O ? O : E,
|
|
18
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
19
|
+
>,
|
|
20
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
21
|
+
>
|
|
22
|
+
: 'body' extends keyof O ?
|
|
23
|
+
RestFetch<
|
|
24
|
+
'searchParams' extends keyof O ?
|
|
25
|
+
O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
|
|
26
|
+
: PathArgs<Exclude<E['path'], undefined>>,
|
|
27
|
+
OptionsToBodyArgument<
|
|
28
|
+
O,
|
|
29
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
30
|
+
>,
|
|
31
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
32
|
+
>
|
|
33
|
+
: 'searchParams' extends keyof O ?
|
|
34
|
+
RestFetch<
|
|
35
|
+
O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
|
|
36
|
+
OptionsToBodyArgument<
|
|
37
|
+
E,
|
|
38
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
39
|
+
>,
|
|
40
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
41
|
+
>
|
|
42
|
+
: (
|
|
43
|
+
this: ThisParameterType<F>,
|
|
44
|
+
...args: Parameters<F>
|
|
45
|
+
) => Promise<
|
|
46
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
47
|
+
>;
|
|
47
48
|
|
|
48
49
|
export type OptionsToBodyArgument<
|
|
49
50
|
O extends { body?: any },
|
|
50
51
|
Method extends string | undefined,
|
|
51
|
-
> =
|
|
52
|
-
'
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
:
|
|
52
|
+
> =
|
|
53
|
+
Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' ?
|
|
54
|
+
'body' extends keyof O ?
|
|
55
|
+
O['body']
|
|
56
|
+
: any
|
|
57
|
+
: undefined;
|
package/src/RestEndpoint.js
CHANGED
|
@@ -76,11 +76,16 @@ export default class RestEndpoint extends Endpoint {
|
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
78
|
if (Object.keys(searchParams).length) {
|
|
79
|
-
return `${this.urlPrefix}${urlBase}?${
|
|
79
|
+
return `${this.urlPrefix}${urlBase}?${this.searchToString(searchParams)}`;
|
|
80
80
|
}
|
|
81
81
|
return `${this.urlPrefix}${urlBase}`;
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
+
/** Encode the url searchParams */
|
|
85
|
+
searchToString(searchParams) {
|
|
86
|
+
return paramsToString(searchParams);
|
|
87
|
+
}
|
|
88
|
+
|
|
84
89
|
getHeaders(headers) {
|
|
85
90
|
return headers;
|
|
86
91
|
}
|
package/src/RestEndpointTypes.ts
CHANGED
|
@@ -42,31 +42,53 @@ export interface RestInstanceBase<
|
|
|
42
42
|
*/
|
|
43
43
|
readonly urlPrefix: string;
|
|
44
44
|
readonly requestInit: RequestInit;
|
|
45
|
-
/**
|
|
45
|
+
/** HTTP request method
|
|
46
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#method
|
|
47
|
+
*/
|
|
46
48
|
readonly method: (O & { method: string })['method'];
|
|
47
49
|
readonly signal: AbortSignal | undefined;
|
|
50
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
|
|
48
51
|
readonly paginationField?: string;
|
|
49
52
|
|
|
50
53
|
/* fetch lifecycles */
|
|
51
54
|
/* before-fetch */
|
|
55
|
+
/** Builds the URL to fetch
|
|
56
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#url
|
|
57
|
+
*/
|
|
52
58
|
url(...args: Parameters<F>): string;
|
|
53
|
-
/**
|
|
59
|
+
/** Encode the searchParams component of the url
|
|
60
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#searchToString
|
|
61
|
+
*/
|
|
62
|
+
searchToString(searchParams: Record<string, any>): string;
|
|
63
|
+
/** Prepares RequestInit used in fetch. This is sent to fetchResponse()
|
|
64
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit
|
|
65
|
+
*/
|
|
54
66
|
getRequestInit(
|
|
55
67
|
this: any,
|
|
56
68
|
body?: RequestInit['body'] | Record<string, unknown>,
|
|
57
69
|
): Promise<RequestInit> | RequestInit;
|
|
58
|
-
/**
|
|
70
|
+
/** Called by getRequestInit to determine HTTP Headers
|
|
71
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getHeaders
|
|
72
|
+
*/
|
|
59
73
|
getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
60
74
|
/* after-fetch */
|
|
61
|
-
/**
|
|
75
|
+
/** Performs the fetch call
|
|
76
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse
|
|
77
|
+
*/
|
|
62
78
|
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
63
|
-
/**
|
|
79
|
+
/** Takes the Response and parses via .text() or .json()
|
|
80
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
|
|
81
|
+
*/
|
|
64
82
|
parseResponse(response: Response): Promise<any>;
|
|
65
|
-
/**
|
|
83
|
+
/** Perform any transforms with the parsed result.
|
|
84
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#process
|
|
85
|
+
*/
|
|
66
86
|
process(value: any, ...args: Parameters<F>): ResolveType<F>;
|
|
67
87
|
|
|
68
88
|
/* utilities */
|
|
69
|
-
/**
|
|
89
|
+
/** Returns true if the provided (fetch) key matches this endpoint.
|
|
90
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#testKey
|
|
91
|
+
*/
|
|
70
92
|
testKey(key: string): boolean;
|
|
71
93
|
|
|
72
94
|
/* extenders */
|
|
@@ -186,89 +208,95 @@ type OptionsToRestEndpoint<
|
|
|
186
208
|
O extends PartialRestGenerics,
|
|
187
209
|
E extends RestInstanceBase & { body?: any; paginationField?: string },
|
|
188
210
|
F extends FetchFunction,
|
|
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
|
-
|
|
211
|
+
> =
|
|
212
|
+
'path' extends keyof O ?
|
|
213
|
+
RestType<
|
|
214
|
+
'searchParams' extends keyof O ?
|
|
215
|
+
O['searchParams'] extends undefined ?
|
|
216
|
+
PathArgs<Exclude<O['path'], undefined>>
|
|
217
|
+
: O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
218
|
+
: PathArgs<Exclude<O['path'], undefined>>,
|
|
219
|
+
OptionsToBodyArgument<
|
|
220
|
+
'body' extends keyof O ? O : E,
|
|
221
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
222
|
+
>,
|
|
223
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
224
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
225
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
226
|
+
{
|
|
227
|
+
path: Exclude<O['path'], undefined>;
|
|
228
|
+
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
229
|
+
searchParams: 'searchParams' extends keyof O ? O['searchParams']
|
|
230
|
+
: E['searchParams'];
|
|
231
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
232
|
+
paginationField: 'paginationField' extends keyof O ?
|
|
233
|
+
O['paginationField']
|
|
234
|
+
: E['paginationField'];
|
|
235
|
+
}
|
|
236
|
+
>
|
|
237
|
+
: 'body' extends keyof O ?
|
|
238
|
+
RestType<
|
|
239
|
+
'searchParams' extends keyof O ?
|
|
240
|
+
O['searchParams'] extends undefined ?
|
|
241
|
+
PathArgs<Exclude<O['path'], undefined>>
|
|
242
|
+
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
|
|
243
|
+
: PathArgs<Exclude<E['path'], undefined>>,
|
|
244
|
+
OptionsToBodyArgument<
|
|
245
|
+
O,
|
|
246
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
247
|
+
>,
|
|
248
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
249
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
250
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
251
|
+
{
|
|
252
|
+
path: E['path'];
|
|
253
|
+
body: O['body'];
|
|
254
|
+
searchParams: 'searchParams' extends keyof O ? O['searchParams']
|
|
255
|
+
: E['searchParams'];
|
|
256
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
257
|
+
paginationField: 'paginationField' extends keyof O ?
|
|
258
|
+
O['paginationField']
|
|
259
|
+
: Extract<E['paginationField'], string>;
|
|
260
|
+
}
|
|
261
|
+
>
|
|
262
|
+
: 'searchParams' extends keyof O ?
|
|
263
|
+
RestType<
|
|
216
264
|
O['searchParams'] extends undefined ?
|
|
217
265
|
PathArgs<Exclude<O['path'], undefined>>
|
|
218
|
-
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined
|
|
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
|
-
searchParams: O['searchParams'];
|
|
253
|
-
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
254
|
-
paginationField: 'paginationField' extends keyof O ? O['paginationField']
|
|
255
|
-
: Extract<E['paginationField'], string>;
|
|
256
|
-
}
|
|
257
|
-
>
|
|
258
|
-
: RestInstance<
|
|
259
|
-
F,
|
|
260
|
-
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
261
|
-
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
262
|
-
{
|
|
263
|
-
path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
|
|
264
|
-
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
265
|
-
searchParams: 'searchParams' extends keyof O ? O['searchParams']
|
|
266
|
-
: E['searchParams'];
|
|
267
|
-
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
268
|
-
paginationField: 'paginationField' extends keyof O ? O['paginationField']
|
|
269
|
-
: E['paginationField'];
|
|
270
|
-
}
|
|
271
|
-
>;
|
|
266
|
+
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
|
|
267
|
+
OptionsToBodyArgument<
|
|
268
|
+
E,
|
|
269
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
270
|
+
>,
|
|
271
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
272
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
273
|
+
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
274
|
+
{
|
|
275
|
+
path: E['path'];
|
|
276
|
+
body: E['body'];
|
|
277
|
+
searchParams: O['searchParams'];
|
|
278
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
279
|
+
paginationField: 'paginationField' extends keyof O ?
|
|
280
|
+
O['paginationField']
|
|
281
|
+
: Extract<E['paginationField'], string>;
|
|
282
|
+
}
|
|
283
|
+
>
|
|
284
|
+
: RestInstance<
|
|
285
|
+
F,
|
|
286
|
+
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
287
|
+
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
288
|
+
{
|
|
289
|
+
path: 'path' extends keyof O ? Exclude<O['path'], undefined>
|
|
290
|
+
: E['path'];
|
|
291
|
+
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
292
|
+
searchParams: 'searchParams' extends keyof O ? O['searchParams']
|
|
293
|
+
: E['searchParams'];
|
|
294
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
295
|
+
paginationField: 'paginationField' extends keyof O ?
|
|
296
|
+
O['paginationField']
|
|
297
|
+
: E['paginationField'];
|
|
298
|
+
}
|
|
299
|
+
>;
|
|
272
300
|
|
|
273
301
|
export type RestExtendedEndpoint<
|
|
274
302
|
O extends PartialRestGenerics,
|
|
@@ -555,8 +583,8 @@ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
|
|
|
555
583
|
|
|
556
584
|
export type KeyofRestEndpoint = keyof RestInstance;
|
|
557
585
|
|
|
558
|
-
export type FromFallBack<K extends keyof E, O, E> =
|
|
559
|
-
: E[K];
|
|
586
|
+
export type FromFallBack<K extends keyof E, O, E> =
|
|
587
|
+
K extends keyof O ? O[K] : E[K];
|
|
560
588
|
|
|
561
589
|
export type FetchMutate<
|
|
562
590
|
A extends readonly any[] = // eslint-disable-next-line @typescript-eslint/ban-types
|
|
@@ -761,7 +761,7 @@ describe('RestEndpoint', () => {
|
|
|
761
761
|
() => useSuspense(getUser);
|
|
762
762
|
});
|
|
763
763
|
|
|
764
|
-
it('should work with extends', async () => {
|
|
764
|
+
it('update should work with extends', async () => {
|
|
765
765
|
mynock.put('/6/user/5').reply(200, (uri, body: any) => ({
|
|
766
766
|
id: 5,
|
|
767
767
|
username: 'charles',
|
|
@@ -792,7 +792,7 @@ describe('RestEndpoint', () => {
|
|
|
792
792
|
`);
|
|
793
793
|
});
|
|
794
794
|
|
|
795
|
-
it('should work with extends', async () => {
|
|
795
|
+
it('get should work with extends', async () => {
|
|
796
796
|
mynock.get('/6/user/5').reply(200, (uri, body: any) => ({
|
|
797
797
|
id: 5,
|
|
798
798
|
username2: 'charles',
|
|
@@ -961,7 +961,59 @@ describe('RestEndpoint', () => {
|
|
|
961
961
|
).toMatchInlineSnapshot(`"/users?bigger=true"`);
|
|
962
962
|
expect(searchParams4.url()).toMatchInlineSnapshot(`"/users"`);
|
|
963
963
|
});
|
|
964
|
+
|
|
965
|
+
it('should work with custom searchToString', async () => {
|
|
966
|
+
class SearchEndpoint<O extends RestGenerics = any> extends MyEndpoint<O> {
|
|
967
|
+
searchToString(searchParams: Record<string, any>) {
|
|
968
|
+
return super.searchToString({ ...searchParams, bob: 5 });
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
mynock.get('/6/user/5').reply(200, (uri, body: any) => ({
|
|
973
|
+
id: 5,
|
|
974
|
+
username2: 'charles',
|
|
975
|
+
...body,
|
|
976
|
+
}));
|
|
977
|
+
class User2 extends Entity {
|
|
978
|
+
readonly id: number | undefined = undefined;
|
|
979
|
+
readonly username2: string = '';
|
|
980
|
+
readonly email: string = '';
|
|
981
|
+
readonly isAdmin: boolean = false;
|
|
982
|
+
|
|
983
|
+
pk() {
|
|
984
|
+
return this.id?.toString();
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
988
|
+
const getUserBase = new SearchEndpoint({
|
|
989
|
+
method: 'GET',
|
|
990
|
+
path: 'http\\://test.com/user/:id',
|
|
991
|
+
name: 'getuser',
|
|
992
|
+
schema: User,
|
|
993
|
+
});
|
|
994
|
+
const getUser = getUserBase.extend({
|
|
995
|
+
path: 'http\\://test.com/:group/user/:id',
|
|
996
|
+
schema: User2,
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
const searchParams = getUser.extend({
|
|
1000
|
+
path: 'http\\://test.com/:group/user/:id',
|
|
1001
|
+
searchParams: {} as { isAdmin?: boolean; sort: 'asc' | 'desc' },
|
|
1002
|
+
});
|
|
1003
|
+
|
|
1004
|
+
expect(
|
|
1005
|
+
searchParams.url({
|
|
1006
|
+
group: 'hi',
|
|
1007
|
+
id: 'what',
|
|
1008
|
+
sort: 'desc',
|
|
1009
|
+
isAdmin: true,
|
|
1010
|
+
}),
|
|
1011
|
+
).toMatchInlineSnapshot(
|
|
1012
|
+
`"http://test.com/hi/user/what?bob=5&isAdmin=true&sort=desc"`,
|
|
1013
|
+
);
|
|
1014
|
+
});
|
|
964
1015
|
});
|
|
1016
|
+
|
|
965
1017
|
it('extending with name should work', () => {
|
|
966
1018
|
const endpoint = CoolerArticleResource.get.extend({ name: 'mything' });
|
|
967
1019
|
const endpoint2 = CoolerArticleResource.get.extend({ path: '/:bob' });
|
package/src/index.ts
CHANGED
|
@@ -20,6 +20,7 @@ export type {
|
|
|
20
20
|
PaginationFieldEndpoint,
|
|
21
21
|
PaginationEndpoint,
|
|
22
22
|
} from './RestEndpoint.js';
|
|
23
|
+
export { getUrlBase, getUrlTokens } from './RestHelpers.js';
|
|
23
24
|
export { default as createResource } from './createResource.js';
|
|
24
25
|
export type { Resource } from './resourceTypes.js';
|
|
25
26
|
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
package/src/mapCollection.ts
CHANGED
|
@@ -33,10 +33,11 @@ export default function mapCollection<
|
|
|
33
33
|
type MapCollection<
|
|
34
34
|
M extends <C extends schema.Collection>(collection: C) => any,
|
|
35
35
|
S extends Schema | undefined,
|
|
36
|
-
> =
|
|
37
|
-
|
|
38
|
-
: S extends
|
|
39
|
-
:
|
|
36
|
+
> =
|
|
37
|
+
S extends schema.Collection ? ReturnType<M>
|
|
38
|
+
: S extends schema.Object<infer T> ? MapCollection<M, T>
|
|
39
|
+
: S extends { [K: string]: any } ? MapObject<M, S>
|
|
40
|
+
: never;
|
|
40
41
|
|
|
41
42
|
export type MapObject<
|
|
42
43
|
M extends (collection: schema.Collection) => any,
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export default function paramsToString(
|
|
2
|
-
searchParams
|
|
2
|
+
searchParams?:
|
|
3
|
+
| string
|
|
4
|
+
| URLSearchParams
|
|
5
|
+
| Record<string, string>
|
|
6
|
+
| string[][]
|
|
7
|
+
| undefined,
|
|
3
8
|
) {
|
|
4
9
|
const params = new URLSearchParams(searchParams as any);
|
|
5
10
|
try {
|
package/src/paramsToString.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
export default function paramsToString(
|
|
2
|
-
searchParams
|
|
2
|
+
searchParams?:
|
|
3
|
+
| string
|
|
4
|
+
| URLSearchParams
|
|
5
|
+
| Record<string, string>
|
|
6
|
+
| string[][]
|
|
7
|
+
| undefined,
|
|
3
8
|
) {
|
|
4
|
-
const params = new URLSearchParams(searchParams
|
|
9
|
+
const params = new URLSearchParams(searchParams);
|
|
5
10
|
params.sort();
|
|
6
11
|
return params.toString();
|
|
7
12
|
}
|
|
@@ -24,24 +24,46 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
24
24
|
*/
|
|
25
25
|
readonly urlPrefix: string;
|
|
26
26
|
readonly requestInit: RequestInit;
|
|
27
|
-
/**
|
|
27
|
+
/** HTTP request method
|
|
28
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#method
|
|
29
|
+
*/
|
|
28
30
|
readonly method: (O & {
|
|
29
31
|
method: string;
|
|
30
32
|
})['method'];
|
|
31
33
|
readonly signal: AbortSignal | undefined;
|
|
34
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
|
|
32
35
|
readonly paginationField?: string;
|
|
36
|
+
/** Builds the URL to fetch
|
|
37
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#url
|
|
38
|
+
*/
|
|
33
39
|
url(...args: Parameters<F>): string;
|
|
34
|
-
/**
|
|
40
|
+
/** Encode the searchParams component of the url
|
|
41
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#searchToString
|
|
42
|
+
*/
|
|
43
|
+
searchToString(searchParams: Record<string, any>): string;
|
|
44
|
+
/** Prepares RequestInit used in fetch. This is sent to fetchResponse()
|
|
45
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit
|
|
46
|
+
*/
|
|
35
47
|
getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
|
|
36
|
-
/**
|
|
48
|
+
/** Called by getRequestInit to determine HTTP Headers
|
|
49
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getHeaders
|
|
50
|
+
*/
|
|
37
51
|
getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
38
|
-
/**
|
|
52
|
+
/** Performs the fetch call
|
|
53
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse
|
|
54
|
+
*/
|
|
39
55
|
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
40
|
-
/**
|
|
56
|
+
/** Takes the Response and parses via .text() or .json()
|
|
57
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
|
|
58
|
+
*/
|
|
41
59
|
parseResponse(response: Response): Promise<any>;
|
|
42
|
-
/**
|
|
60
|
+
/** Perform any transforms with the parsed result.
|
|
61
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#process
|
|
62
|
+
*/
|
|
43
63
|
process(value: any, ...args: Parameters<F>): ResolveType<F>;
|
|
44
|
-
/**
|
|
64
|
+
/** Returns true if the provided (fetch) key matches this endpoint.
|
|
65
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#testKey
|
|
66
|
+
*/
|
|
45
67
|
testKey(key: string): boolean;
|
|
46
68
|
/** Creates a child endpoint that inherits from this while overriding provided `options`.
|
|
47
69
|
* @see https://dataclient.io/rest/api/RestEndpoint#extend
|
package/ts4.0/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
2
|
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
|
|
3
|
+
export { getUrlBase, getUrlTokens } from './RestHelpers.js';
|
|
3
4
|
export { default as createResource } from './createResource.js';
|
|
4
5
|
export type { Resource } from './resourceTypes.js';
|
|
5
6
|
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export default function paramsToString(searchParams
|
|
1
|
+
export default function paramsToString(searchParams?: string | URLSearchParams | Record<string, string> | string[][] | undefined): string;
|
|
2
2
|
//# sourceMappingURL=paramsToString.d.ts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export default function paramsToString(searchParams
|
|
1
|
+
export default function paramsToString(searchParams?: string | URLSearchParams | Record<string, string> | string[][] | undefined): string;
|
|
2
2
|
//# sourceMappingURL=paramsToString.native.d.ts.map
|
|
@@ -24,24 +24,46 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
24
24
|
*/
|
|
25
25
|
readonly urlPrefix: string;
|
|
26
26
|
readonly requestInit: RequestInit;
|
|
27
|
-
/**
|
|
27
|
+
/** HTTP request method
|
|
28
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#method
|
|
29
|
+
*/
|
|
28
30
|
readonly method: (O & {
|
|
29
31
|
method: string;
|
|
30
32
|
})['method'];
|
|
31
33
|
readonly signal: AbortSignal | undefined;
|
|
34
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
|
|
32
35
|
readonly paginationField?: string;
|
|
36
|
+
/** Builds the URL to fetch
|
|
37
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#url
|
|
38
|
+
*/
|
|
33
39
|
url(...args: Parameters<F>): string;
|
|
34
|
-
/**
|
|
40
|
+
/** Encode the searchParams component of the url
|
|
41
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#searchToString
|
|
42
|
+
*/
|
|
43
|
+
searchToString(searchParams: Record<string, any>): string;
|
|
44
|
+
/** Prepares RequestInit used in fetch. This is sent to fetchResponse()
|
|
45
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit
|
|
46
|
+
*/
|
|
35
47
|
getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
|
|
36
|
-
/**
|
|
48
|
+
/** Called by getRequestInit to determine HTTP Headers
|
|
49
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#getHeaders
|
|
50
|
+
*/
|
|
37
51
|
getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
38
|
-
/**
|
|
52
|
+
/** Performs the fetch call
|
|
53
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse
|
|
54
|
+
*/
|
|
39
55
|
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
40
|
-
/**
|
|
56
|
+
/** Takes the Response and parses via .text() or .json()
|
|
57
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
|
|
58
|
+
*/
|
|
41
59
|
parseResponse(response: Response): Promise<any>;
|
|
42
|
-
/**
|
|
60
|
+
/** Perform any transforms with the parsed result.
|
|
61
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#process
|
|
62
|
+
*/
|
|
43
63
|
process(value: any, ...args: Parameters<F>): ResolveType<F>;
|
|
44
|
-
/**
|
|
64
|
+
/** Returns true if the provided (fetch) key matches this endpoint.
|
|
65
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#testKey
|
|
66
|
+
*/
|
|
45
67
|
testKey(key: string): boolean;
|
|
46
68
|
/** Creates a child endpoint that inherits from this while overriding provided `options`.
|
|
47
69
|
* @see https://dataclient.io/rest/api/RestEndpoint#extend
|
package/ts4.1/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
2
|
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
|
|
3
|
+
export { getUrlBase, getUrlTokens } from './RestHelpers.js';
|
|
3
4
|
export { default as createResource } from './createResource.js';
|
|
4
5
|
export type { Resource } from './resourceTypes.js';
|
|
5
6
|
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export default function paramsToString(searchParams
|
|
1
|
+
export default function paramsToString(searchParams?: string | URLSearchParams | Record<string, string> | string[][] | undefined): string;
|
|
2
2
|
//# sourceMappingURL=paramsToString.d.ts.map
|