@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.
Files changed (44) hide show
  1. package/README.md +16 -0
  2. package/dist/index.js +8 -1
  3. package/dist/index.umd.min.js +1 -1
  4. package/legacy/OptionsToFunction.js +1 -1
  5. package/legacy/RestEndpoint.js +7 -2
  6. package/legacy/RestEndpointTypes.js +1 -1
  7. package/legacy/index.js +2 -1
  8. package/legacy/mapCollection.js +1 -1
  9. package/legacy/paramsToString.js +1 -1
  10. package/legacy/paramsToString.native.js +1 -1
  11. package/lib/OptionsToFunction.d.ts.map +1 -1
  12. package/lib/OptionsToFunction.js +1 -1
  13. package/lib/RestEndpoint.js +7 -2
  14. package/lib/RestEndpointTypes.d.ts +29 -7
  15. package/lib/RestEndpointTypes.d.ts.map +1 -1
  16. package/lib/RestEndpointTypes.js +1 -1
  17. package/lib/index.d.ts +1 -0
  18. package/lib/index.d.ts.map +1 -1
  19. package/lib/index.js +2 -1
  20. package/lib/mapCollection.d.ts.map +1 -1
  21. package/lib/mapCollection.js +1 -1
  22. package/lib/paramsToString.d.ts +1 -1
  23. package/lib/paramsToString.d.ts.map +1 -1
  24. package/lib/paramsToString.js +1 -1
  25. package/lib/paramsToString.native.d.ts +1 -1
  26. package/lib/paramsToString.native.d.ts.map +1 -1
  27. package/lib/paramsToString.native.js +1 -1
  28. package/package.json +2 -2
  29. package/src/OptionsToFunction.ts +44 -42
  30. package/src/RestEndpoint.js +6 -1
  31. package/src/RestEndpointTypes.ts +118 -90
  32. package/src/__tests__/RestEndpoint.ts +54 -2
  33. package/src/index.ts +1 -0
  34. package/src/mapCollection.ts +5 -4
  35. package/src/paramsToString.native.ts +6 -1
  36. package/src/paramsToString.ts +7 -2
  37. package/ts4.0/RestEndpointTypes.d.ts +29 -7
  38. package/ts4.0/index.d.ts +1 -0
  39. package/ts4.0/paramsToString.d.ts +1 -1
  40. package/ts4.0/paramsToString.native.d.ts +1 -1
  41. package/ts4.1/RestEndpointTypes.d.ts +29 -7
  42. package/ts4.1/index.d.ts +1 -0
  43. package/ts4.1/paramsToString.d.ts +1 -1
  44. package/ts4.1/paramsToString.native.d.ts +1 -1
@@ -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
- > = 'path' extends keyof O ?
11
- RestFetch<
12
- 'searchParams' extends keyof O ?
13
- O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
14
- : PathArgs<Exclude<O['path'], undefined>>,
15
- OptionsToBodyArgument<
16
- 'body' extends keyof O ? O : E,
17
- 'method' extends keyof O ? O['method'] : E['method']
18
- >,
19
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
20
- >
21
- : 'body' extends keyof O ?
22
- RestFetch<
23
- 'searchParams' extends keyof O ?
24
- O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
25
- : PathArgs<Exclude<E['path'], undefined>>,
26
- OptionsToBodyArgument<
27
- O,
28
- 'method' extends keyof O ? O['method'] : E['method']
29
- >,
30
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
31
- >
32
- : 'searchParams' extends keyof O ?
33
- RestFetch<
34
- O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
35
- OptionsToBodyArgument<
36
- E,
37
- 'method' extends keyof O ? O['method'] : E['method']
38
- >,
39
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
40
- >
41
- : (
42
- this: ThisParameterType<F>,
43
- ...args: Parameters<F>
44
- ) => Promise<
45
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
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
- > = Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' ?
52
- 'body' extends keyof O ?
53
- O['body']
54
- : any
55
- : undefined;
52
+ > =
53
+ Method extends 'POST' | 'PUT' | 'PATCH' | 'DELETE' ?
54
+ 'body' extends keyof O ?
55
+ O['body']
56
+ : any
57
+ : undefined;
@@ -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}?${paramsToString(searchParams)}`;
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
  }
@@ -42,31 +42,53 @@ export interface RestInstanceBase<
42
42
  */
43
43
  readonly urlPrefix: string;
44
44
  readonly requestInit: RequestInit;
45
- /** @see https://dataclient.io/rest/api/RestEndpoint#method */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getHeaders */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#parseResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#process */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#testKey */
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
- > = 'path' extends keyof O ?
190
- RestType<
191
- 'searchParams' extends keyof O ?
192
- O['searchParams'] extends undefined ?
193
- PathArgs<Exclude<O['path'], undefined>>
194
- : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
195
- : PathArgs<Exclude<O['path'], undefined>>,
196
- OptionsToBodyArgument<
197
- 'body' extends keyof O ? O : E,
198
- 'method' extends keyof O ? O['method'] : E['method']
199
- >,
200
- 'schema' extends keyof O ? O['schema'] : E['schema'],
201
- 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
202
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
203
- {
204
- path: Exclude<O['path'], undefined>;
205
- body: 'body' extends keyof O ? O['body'] : E['body'];
206
- searchParams: 'searchParams' extends keyof O ? O['searchParams']
207
- : E['searchParams'];
208
- method: 'method' extends keyof O ? O['method'] : E['method'];
209
- paginationField: 'paginationField' extends keyof O ? O['paginationField']
210
- : E['paginationField'];
211
- }
212
- >
213
- : 'body' extends keyof O ?
214
- RestType<
215
- 'searchParams' extends keyof O ?
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
- : PathArgs<Exclude<E['path'], undefined>>,
220
- OptionsToBodyArgument<
221
- O,
222
- 'method' extends keyof O ? O['method'] : E['method']
223
- >,
224
- 'schema' extends keyof O ? O['schema'] : E['schema'],
225
- 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
226
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
227
- {
228
- path: E['path'];
229
- body: O['body'];
230
- searchParams: 'searchParams' extends keyof O ? O['searchParams']
231
- : E['searchParams'];
232
- method: 'method' extends keyof O ? O['method'] : E['method'];
233
- paginationField: 'paginationField' extends keyof O ? O['paginationField']
234
- : Extract<E['paginationField'], string>;
235
- }
236
- >
237
- : 'searchParams' extends keyof O ?
238
- RestType<
239
- O['searchParams'] extends undefined ?
240
- PathArgs<Exclude<O['path'], undefined>>
241
- : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
242
- OptionsToBodyArgument<
243
- E,
244
- 'method' extends keyof O ? O['method'] : E['method']
245
- >,
246
- 'schema' extends keyof O ? O['schema'] : E['schema'],
247
- 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
248
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
249
- {
250
- path: E['path'];
251
- body: E['body'];
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> = K extends keyof O ? O[K]
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';
@@ -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
- > = S extends schema.Collection ? ReturnType<M>
37
- : S extends schema.Object<infer T> ? MapCollection<M, T>
38
- : S extends { [K: string]: any } ? MapObject<M, S>
39
- : never;
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: Readonly<Record<string, string | number>>,
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 {
@@ -1,7 +1,12 @@
1
1
  export default function paramsToString(
2
- searchParams: Readonly<Record<string, string | number | boolean>>,
2
+ searchParams?:
3
+ | string
4
+ | URLSearchParams
5
+ | Record<string, string>
6
+ | string[][]
7
+ | undefined,
3
8
  ) {
4
- const params = new URLSearchParams(searchParams as any);
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#method */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getHeaders */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#parseResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#process */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#testKey */
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: Readonly<Record<string, string | number | boolean>>): string;
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: Readonly<Record<string, string | number>>): string;
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#method */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getRequestInit */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#getHeaders */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#fetchResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#parseResponse */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#process */
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
- /** @see https://dataclient.io/rest/api/RestEndpoint#testKey */
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: Readonly<Record<string, string | number | boolean>>): string;
1
+ export default function paramsToString(searchParams?: string | URLSearchParams | Record<string, string> | string[][] | undefined): string;
2
2
  //# sourceMappingURL=paramsToString.d.ts.map