@data-client/rest 0.1.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 (158) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +110 -0
  3. package/dist/index.js +561 -0
  4. package/dist/index.umd.min.js +1 -0
  5. package/dist/next.js +520 -0
  6. package/dist/package.json +1 -0
  7. package/legacy/NetworkError.js +13 -0
  8. package/legacy/OptionsToFunction.js +2 -0
  9. package/legacy/RestEndpoint.js +225 -0
  10. package/legacy/RestEndpointTypeHelp.js +2 -0
  11. package/legacy/RestHelpers.js +33 -0
  12. package/legacy/createResource.js +88 -0
  13. package/legacy/extractCollection.js +14 -0
  14. package/legacy/getArrayPath.js +19 -0
  15. package/legacy/hookifyResource.js +24 -0
  16. package/legacy/index.js +10 -0
  17. package/legacy/mapCollection.js +14 -0
  18. package/legacy/next/OptionsToFunction.js +2 -0
  19. package/legacy/next/RestEndpoint.js +225 -0
  20. package/legacy/next/createResource.js +86 -0
  21. package/legacy/next/index.js +4 -0
  22. package/legacy/next/resourceTypes.js +2 -0
  23. package/legacy/paginatedCollections.js +29 -0
  24. package/legacy/paginationUpdate.js +36 -0
  25. package/legacy/paramsToString.js +6 -0
  26. package/legacy/paramsToString.native.js +9 -0
  27. package/legacy/pathTypes.js +2 -0
  28. package/legacy/test-fixtures.js +111 -0
  29. package/legacy/utiltypes.js +2 -0
  30. package/lib/NetworkError.d.ts +11 -0
  31. package/lib/NetworkError.d.ts.map +1 -0
  32. package/lib/NetworkError.js +13 -0
  33. package/lib/OptionsToFunction.d.ts +7 -0
  34. package/lib/OptionsToFunction.d.ts.map +1 -0
  35. package/lib/OptionsToFunction.js +2 -0
  36. package/lib/RestEndpoint.d.ts +548 -0
  37. package/lib/RestEndpoint.js +229 -0
  38. package/lib/RestEndpointTypeHelp.d.ts +10 -0
  39. package/lib/RestEndpointTypeHelp.d.ts.map +1 -0
  40. package/lib/RestEndpointTypeHelp.js +2 -0
  41. package/lib/RestHelpers.d.ts +7 -0
  42. package/lib/RestHelpers.d.ts.map +1 -0
  43. package/lib/RestHelpers.js +33 -0
  44. package/lib/createResource.d.ts +66 -0
  45. package/lib/createResource.d.ts.map +1 -0
  46. package/lib/createResource.js +104 -0
  47. package/lib/extractCollection.d.ts +11 -0
  48. package/lib/extractCollection.d.ts.map +1 -0
  49. package/lib/extractCollection.js +16 -0
  50. package/lib/getArrayPath.d.ts +3 -0
  51. package/lib/getArrayPath.d.ts.map +1 -0
  52. package/lib/getArrayPath.js +19 -0
  53. package/lib/hookifyResource.d.ts +14 -0
  54. package/lib/hookifyResource.d.ts.map +1 -0
  55. package/lib/hookifyResource.js +24 -0
  56. package/lib/index.d.ts +12 -0
  57. package/lib/index.d.ts.map +1 -0
  58. package/lib/index.js +10 -0
  59. package/lib/mapCollection.d.ts +16 -0
  60. package/lib/mapCollection.d.ts.map +1 -0
  61. package/lib/mapCollection.js +16 -0
  62. package/lib/next/OptionsToFunction.d.ts +7 -0
  63. package/lib/next/OptionsToFunction.d.ts.map +1 -0
  64. package/lib/next/OptionsToFunction.js +2 -0
  65. package/lib/next/RestEndpoint.d.ts +542 -0
  66. package/lib/next/RestEndpoint.js +229 -0
  67. package/lib/next/createResource.d.ts +86 -0
  68. package/lib/next/createResource.d.ts.map +1 -0
  69. package/lib/next/createResource.js +100 -0
  70. package/lib/next/index.d.ts +6 -0
  71. package/lib/next/index.d.ts.map +1 -0
  72. package/lib/next/index.js +4 -0
  73. package/lib/next/resourceTypes.d.ts +31 -0
  74. package/lib/next/resourceTypes.d.ts.map +1 -0
  75. package/lib/next/resourceTypes.js +2 -0
  76. package/lib/paginatedCollections.d.ts +3 -0
  77. package/lib/paginatedCollections.d.ts.map +1 -0
  78. package/lib/paginatedCollections.js +29 -0
  79. package/lib/paginationUpdate.d.ts +8 -0
  80. package/lib/paginationUpdate.d.ts.map +1 -0
  81. package/lib/paginationUpdate.js +40 -0
  82. package/lib/paramsToString.d.ts +2 -0
  83. package/lib/paramsToString.d.ts.map +1 -0
  84. package/lib/paramsToString.js +6 -0
  85. package/lib/paramsToString.native.d.ts +2 -0
  86. package/lib/paramsToString.native.d.ts.map +1 -0
  87. package/lib/paramsToString.native.js +9 -0
  88. package/lib/pathTypes.d.ts +20 -0
  89. package/lib/pathTypes.d.ts.map +1 -0
  90. package/lib/pathTypes.js +2 -0
  91. package/lib/test-fixtures.d.ts +141 -0
  92. package/lib/test-fixtures.d.ts.map +1 -0
  93. package/lib/test-fixtures.js +111 -0
  94. package/lib/utiltypes.d.ts +21 -0
  95. package/lib/utiltypes.d.ts.map +1 -0
  96. package/lib/utiltypes.js +2 -0
  97. package/node.mjs +1 -0
  98. package/package.json +121 -0
  99. package/src/NetworkError.ts +17 -0
  100. package/src/OptionsToFunction.ts +42 -0
  101. package/src/RestEndpoint.d.ts +548 -0
  102. package/src/RestEndpoint.js +262 -0
  103. package/src/RestEndpointTypeHelp.ts +20 -0
  104. package/src/RestHelpers.ts +48 -0
  105. package/src/__tests__/Resource.test.ts +317 -0
  106. package/src/__tests__/RestEndpoint.ts +1304 -0
  107. package/src/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
  108. package/src/__tests__/__snapshots__/createResource.test.ts.snap +5 -0
  109. package/src/__tests__/createResource.test.ts +583 -0
  110. package/src/__tests__/getArrayPath.test.ts +30 -0
  111. package/src/__tests__/hookifyResource.ts +197 -0
  112. package/src/__tests__/types.test.ts +309 -0
  113. package/src/createResource.ts +172 -0
  114. package/src/extractCollection.ts +34 -0
  115. package/src/getArrayPath.ts +20 -0
  116. package/src/hookifyResource.ts +36 -0
  117. package/src/index.ts +42 -0
  118. package/src/mapCollection.ts +54 -0
  119. package/src/next/OptionsToFunction.ts +41 -0
  120. package/src/next/RestEndpoint.d.ts +542 -0
  121. package/src/next/RestEndpoint.js +262 -0
  122. package/src/next/__tests__/RestEndpoint.ts +1333 -0
  123. package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +18 -0
  124. package/src/next/__tests__/types.test.ts +429 -0
  125. package/src/next/createResource.ts +194 -0
  126. package/src/next/index.ts +5 -0
  127. package/src/next/resourceTypes.ts +32 -0
  128. package/src/package.json +1 -0
  129. package/src/paginatedCollections.ts +32 -0
  130. package/src/paginationUpdate.ts +49 -0
  131. package/src/paramsToString.native.ts +10 -0
  132. package/src/paramsToString.ts +7 -0
  133. package/src/pathTypes.ts +58 -0
  134. package/src/test-fixtures.ts +128 -0
  135. package/src/utiltypes.ts +25 -0
  136. package/ts4.0/NetworkError.d.ts +11 -0
  137. package/ts4.0/OptionsToFunction.d.ts +16 -0
  138. package/ts4.0/RestEndpoint.d.ts +240 -0
  139. package/ts4.0/RestEndpointTypeHelp.d.ts +10 -0
  140. package/ts4.0/RestHelpers.d.ts +7 -0
  141. package/ts4.0/createResource.d.ts +66 -0
  142. package/ts4.0/extractCollection.d.ts +11 -0
  143. package/ts4.0/getArrayPath.d.ts +3 -0
  144. package/ts4.0/hookifyResource.d.ts +19 -0
  145. package/ts4.0/index.d.ts +12 -0
  146. package/ts4.0/mapCollection.d.ts +16 -0
  147. package/ts4.0/next/OptionsToFunction.d.ts +16 -0
  148. package/ts4.0/next/RestEndpoint.d.ts +226 -0
  149. package/ts4.0/next/createResource.d.ts +94 -0
  150. package/ts4.0/next/index.d.ts +6 -0
  151. package/ts4.0/next/resourceTypes.d.ts +31 -0
  152. package/ts4.0/paginatedCollections.d.ts +3 -0
  153. package/ts4.0/paginationUpdate.d.ts +10 -0
  154. package/ts4.0/paramsToString.d.ts +2 -0
  155. package/ts4.0/paramsToString.native.d.ts +2 -0
  156. package/ts4.0/pathTypes.d.ts +20 -0
  157. package/ts4.0/test-fixtures.d.ts +141 -0
  158. package/ts4.0/utiltypes.d.ts +5 -0
@@ -0,0 +1,548 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ import type {
3
+ EndpointExtraOptions,
4
+ EndpointInstanceInterface,
5
+ Schema,
6
+ FetchFunction,
7
+ ResolveType,
8
+ } from '@data-client/endpoint';
9
+
10
+ import type { ExtractCollection } from './extractCollection.js';
11
+ import { OptionsToFunction } from './OptionsToFunction.js';
12
+ import { PathArgs } from './pathTypes.js';
13
+ import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
14
+
15
+ export interface RestInstanceBase<
16
+ F extends FetchFunction = FetchFunction,
17
+ S extends Schema | undefined = any,
18
+ M extends true | undefined = true | undefined,
19
+ O extends {
20
+ path: string;
21
+ body?: any;
22
+ searchParams?: any;
23
+ } = { path: string },
24
+ > extends EndpointInstanceInterface<F, S, M> {
25
+ readonly body?: 'body' extends keyof O ? O['body'] : any;
26
+ readonly searchParams?: 'searchParams' extends keyof O
27
+ ? O['searchParams']
28
+ : // unknown is identity with '&' type operator
29
+ unknown;
30
+
31
+ /** Pattern to construct url based on Url Parameters
32
+ * @see https://resthooks.io/rest/api/RestEndpoint#path
33
+ */
34
+ readonly path: O['path'];
35
+ /** Prepended to all urls
36
+ * @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
37
+ */
38
+ readonly urlPrefix: string;
39
+ readonly requestInit: RequestInit;
40
+ readonly method: string;
41
+ readonly signal: AbortSignal | undefined;
42
+
43
+ /* fetch lifecycles */
44
+ /* before-fetch */
45
+ url(...args: Parameters<F>): string;
46
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
47
+ getRequestInit(
48
+ this: any,
49
+ body?: RequestInit['body'] | Record<string, unknown>,
50
+ ): RequestInit;
51
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
52
+ getHeaders(headers: HeadersInit): HeadersInit;
53
+ /* after-fetch */
54
+ /** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
55
+ fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
56
+ /** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
57
+ parseResponse(response: Response): Promise<any>;
58
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
59
+ process(value: any, ...args: Parameters<F>): ResolveType<F>;
60
+
61
+ /* utilities */
62
+ /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
63
+ testKey(key: string): boolean;
64
+
65
+ /* extenders */
66
+ // TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
67
+ // this is just a hack to handle when no members of PartialRestGenerics are present
68
+ // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
69
+ // should be used from the other (due to same problem with every member being partial)
70
+ extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(
71
+ this: E,
72
+ options: Readonly<RestEndpointExtendOptions<O, E, F> & O>,
73
+ ): RestExtendedEndpoint<O, E>;
74
+ paginated<
75
+ E extends RestInstanceBase<FetchFunction, any, undefined>,
76
+ A extends any[],
77
+ >(
78
+ this: E,
79
+ removeCursor: (...args: A) => readonly [...Parameters<E>],
80
+ ): PaginationEndpoint<E, A>;
81
+ paginated<
82
+ E extends RestInstanceBase<FetchFunction, any, undefined>,
83
+ C extends string,
84
+ >(
85
+ this: E,
86
+ cursorField: C,
87
+ ): PaginationFieldEndpoint<E, C>;
88
+ }
89
+
90
+ export interface RestInstance<
91
+ F extends FetchFunction = FetchFunction,
92
+ S extends Schema | undefined = any,
93
+ M extends true | undefined = true | undefined,
94
+ O extends {
95
+ path: string;
96
+ body?: any;
97
+ searchParams?: any;
98
+ } = { path: string },
99
+ > extends RestInstanceBase<F, S, M, O> {
100
+ push: AddEndpoint<F, S, O>;
101
+ unshift: AddEndpoint<F, S, O>;
102
+ assign: AddEndpoint<F, S, O>;
103
+ }
104
+
105
+ export type ContainsCollectionArray =
106
+ | { push: any; unshift: any }
107
+ | { [K: string]: ContainsCollectionArray }
108
+ | { schema: { [K: string]: ContainsCollectionArray } };
109
+ export type ContainsCollectionValues =
110
+ | { assign: any }
111
+ | { [K: string]: ContainsCollectionValues }
112
+ | { schema: { [K: string]: ContainsCollectionValues } };
113
+
114
+ export type RestEndpointExtendOptions<
115
+ O extends PartialRestGenerics,
116
+ E extends RestInstanceBase,
117
+ F extends FetchFunction,
118
+ > = RestEndpointOptions<
119
+ OptionsToFunction<O, E, F>,
120
+ 'schema' extends keyof O
121
+ ? Extract<O['schema'], Schema | undefined>
122
+ : E['schema']
123
+ > &
124
+ Partial<Omit<E, KeyofRestEndpoint | keyof PartialRestGenerics>>;
125
+
126
+ type OptionsToRestEndpoint<
127
+ O extends PartialRestGenerics,
128
+ E extends RestInstanceBase & { body?: any },
129
+ F extends FetchFunction,
130
+ > = 'path' extends keyof O
131
+ ? RestType<
132
+ 'searchParams' extends keyof O
133
+ ? O['searchParams'] extends undefined
134
+ ? PathArgs<Exclude<O['path'], undefined>>
135
+ : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
136
+ : PathArgs<Exclude<O['path'], undefined>>,
137
+ 'body' extends keyof O ? O['body'] : E['body'],
138
+ 'schema' extends keyof O ? O['schema'] : E['schema'],
139
+ 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
140
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
141
+ {
142
+ path: Exclude<O['path'], undefined>;
143
+ body: 'body' extends keyof O ? O['body'] : E['body'];
144
+ searchParams: 'searchParams' extends keyof O
145
+ ? O['searchParams']
146
+ : E['searchParams'];
147
+ }
148
+ >
149
+ : 'body' extends keyof O
150
+ ? RestType<
151
+ 'searchParams' extends keyof O
152
+ ? O['searchParams'] extends undefined
153
+ ? PathArgs<Exclude<O['path'], undefined>>
154
+ : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
155
+ : PathArgs<Exclude<E['path'], undefined>>,
156
+ O['body'],
157
+ 'schema' extends keyof O ? O['schema'] : E['schema'],
158
+ 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
159
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
160
+ {
161
+ path: E['path'];
162
+ body: O['body'];
163
+ searchParams: 'searchParams' extends keyof O
164
+ ? O['searchParams']
165
+ : E['searchParams'];
166
+ }
167
+ >
168
+ : 'searchParams' extends keyof O
169
+ ? RestType<
170
+ O['searchParams'] extends undefined
171
+ ? PathArgs<Exclude<O['path'], undefined>>
172
+ : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
173
+ E['body'],
174
+ 'schema' extends keyof O ? O['schema'] : E['schema'],
175
+ 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
176
+ O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
177
+ {
178
+ path: E['path'];
179
+ body: E['body'];
180
+ searchParams: O['searchParams'];
181
+ }
182
+ >
183
+ : RestInstance<
184
+ F,
185
+ 'schema' extends keyof O ? O['schema'] : E['schema'],
186
+ 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
187
+ {
188
+ path: 'path' extends keyof O
189
+ ? Exclude<O['path'], undefined>
190
+ : E['path'];
191
+ body: 'body' extends keyof O ? O['body'] : E['body'];
192
+ searchParams: 'searchParams' extends keyof O
193
+ ? O['searchParams']
194
+ : E['searchParams'];
195
+ }
196
+ >;
197
+
198
+ export type RestExtendedEndpoint<
199
+ O extends PartialRestGenerics,
200
+ E extends RestInstanceBase,
201
+ > = OptionsToRestEndpoint<
202
+ O,
203
+ E,
204
+ RestInstance<
205
+ (
206
+ ...args: Parameters<E>
207
+ ) => O['process'] extends {}
208
+ ? Promise<ReturnType<O['process']>>
209
+ : ReturnType<E>,
210
+ 'schema' extends keyof O ? O['schema'] : E['schema'],
211
+ 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']
212
+ >
213
+ > &
214
+ Omit<O, KeyofRestEndpoint> &
215
+ Omit<E, KeyofRestEndpoint | keyof O>;
216
+
217
+ export interface PartialRestGenerics {
218
+ readonly path?: string;
219
+ readonly schema?: Schema | undefined;
220
+ readonly method?: string;
221
+ /** Only used for types */
222
+ readonly body?: any;
223
+ /** Only used for types */
224
+ readonly searchParams?: any;
225
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
226
+ process?(value: any, ...args: any): any;
227
+ }
228
+ export interface RestGenerics extends PartialRestGenerics {
229
+ readonly path: string;
230
+ }
231
+
232
+ export type PaginationEndpoint<
233
+ E extends RestInstanceBase,
234
+ A extends any[],
235
+ > = RestInstance<
236
+ ParamFetchNoBody<A[0], ResolveType<E>>,
237
+ E['schema'],
238
+ E['sideEffect'],
239
+ Pick<E, 'path' | 'searchParams' | 'body'> & {
240
+ searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
241
+ }
242
+ >;
243
+ export type PaginationFieldEndpoint<
244
+ E extends RestInstanceBase,
245
+ C extends string,
246
+ > = RestInstance<
247
+ ParamFetchNoBody<
248
+ { [K in C]: string | number | boolean } & E['searchParams'] &
249
+ PathArgs<Exclude<E['path'], undefined>>,
250
+ ResolveType<E>
251
+ >,
252
+ E['schema'],
253
+ E['sideEffect'],
254
+ Pick<E, 'path' | 'searchParams' | 'body'> & {
255
+ searchParams: { [K in C]: string | number | boolean } & E['searchParams'];
256
+ }
257
+ >;
258
+ export type AddEndpoint<
259
+ F extends FetchFunction = FetchFunction,
260
+ S extends Schema | undefined = any,
261
+ O extends {
262
+ path: string;
263
+ body?: any;
264
+ searchParams?: any;
265
+ } = { path: string },
266
+ > = RestInstanceBase<
267
+ RestFetch<
268
+ 'searchParams' extends keyof O
269
+ ? O['searchParams'] extends undefined
270
+ ? PathArgs<Exclude<O['path'], undefined>>
271
+ : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
272
+ : PathArgs<Exclude<O['path'], undefined>>,
273
+ any,
274
+ ReturnType<F>
275
+ >,
276
+ ExtractCollection<S>,
277
+ true,
278
+ Omit<O, 'body'>
279
+ > & { method: 'POST' };
280
+
281
+ export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O
282
+ ? O['body']
283
+ : O['method'] extends 'POST' | 'PUT' | 'PATCH'
284
+ ? Record<string, unknown> | FormData
285
+ : undefined;
286
+
287
+ type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O
288
+ ? O
289
+ : O['method'] extends 'POST' | 'PUT' | 'PATCH'
290
+ ? O & { body: any }
291
+ : O & { body: undefined };
292
+
293
+ export interface RestEndpointOptions<
294
+ F extends FetchFunction = FetchFunction,
295
+ S extends Schema | undefined = undefined,
296
+ > extends EndpointExtraOptions<F> {
297
+ urlPrefix?: string;
298
+ requestInit?: RequestInit;
299
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
300
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
301
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
302
+ parseResponse?(response: Response): Promise<any>;
303
+
304
+ sideEffect?: true | undefined;
305
+ name?: string;
306
+ signal?: AbortSignal;
307
+ fetch?: F;
308
+ key?(...args: Parameters<F>): string;
309
+ url?(...args: Parameters<F>): string;
310
+ update?: EndpointUpdateFunction<F, S>;
311
+ }
312
+
313
+ export type RestEndpointConstructorOptions<O extends RestGenerics = any> =
314
+ RestEndpointOptions<
315
+ RestFetch<
316
+ 'searchParams' extends keyof O
317
+ ? O['searchParams'] extends undefined
318
+ ? PathArgs<O['path']>
319
+ : O['searchParams'] & PathArgs<O['path']>
320
+ : PathArgs<O['path']>,
321
+ BodyDefault<O>,
322
+ O['process'] extends {}
323
+ ? ReturnType<O['process']>
324
+ : any /*Denormalize<O['schema']>*/
325
+ >,
326
+ O['schema']
327
+ >;
328
+
329
+ export interface RestEndpointConstructor {
330
+ new <O extends RestGenerics = any>({
331
+ method,
332
+ sideEffect,
333
+ name,
334
+ ...options
335
+ }: RestEndpointConstructorOptions<O> & Readonly<O>): RestInstance<
336
+ RestFetch<
337
+ 'searchParams' extends keyof O
338
+ ? O['searchParams'] extends undefined
339
+ ? PathArgs<O['path']>
340
+ : O['searchParams'] & PathArgs<O['path']>
341
+ : PathArgs<O['path']>,
342
+ BodyDefault<O>,
343
+ O['process'] extends {}
344
+ ? ReturnType<O['process']>
345
+ : any /*Denormalize<O['schema']>*/
346
+ >,
347
+ 'schema' extends keyof O ? O['schema'] : undefined,
348
+ MethodToSide<O['method']>,
349
+ OptionsBodyDefault<O>
350
+ >;
351
+ readonly prototype: RestInstanceBase;
352
+ }
353
+
354
+ /** Simplifies endpoint definitions that follow REST patterns
355
+ *
356
+ * @see https://resthooks.io/rest/api/RestEndpoint
357
+ */
358
+ export declare let RestEndpoint: RestEndpointConstructor;
359
+ export default RestEndpoint;
360
+
361
+ export type MethodToSide<M> = M extends string
362
+ ? M extends 'GET'
363
+ ? undefined
364
+ : true
365
+ : undefined;
366
+
367
+ /** RestEndpoint types simplified */
368
+ export type RestType<
369
+ UrlParams = any,
370
+ Body = any,
371
+ S extends Schema | undefined = Schema | undefined,
372
+ M extends true | undefined = true | undefined,
373
+ R = any,
374
+ O extends {
375
+ path: string;
376
+ body?: any;
377
+ searchParams?: any;
378
+ } = { path: string },
379
+ // eslint-disable-next-line @typescript-eslint/ban-types
380
+ > = IfTypeScriptLooseNull<
381
+ RestInstance<
382
+ keyof UrlParams extends never
383
+ ? (this: EndpointInstanceInterface, body?: Body) => Promise<R>
384
+ : // even with loose null, this will only be true when all members are optional
385
+ {} extends UrlParams
386
+ ?
387
+ | ((this: EndpointInstanceInterface, body?: Body) => Promise<R>)
388
+ | ((
389
+ this: EndpointInstanceInterface,
390
+ params: UrlParams,
391
+ body?: Body,
392
+ ) => Promise<R>)
393
+ : (
394
+ this: EndpointInstanceInterface,
395
+ params: UrlParams,
396
+ body?: Body,
397
+ ) => Promise<R>,
398
+ S,
399
+ M,
400
+ O
401
+ >,
402
+ Body extends {}
403
+ ? RestTypeWithBody<UrlParams, S, M, Body, R, O>
404
+ : RestTypeNoBody<UrlParams, S, M, R, O>
405
+ >;
406
+
407
+ export type RestTypeWithBody<
408
+ UrlParams = any,
409
+ S extends Schema | undefined = Schema | undefined,
410
+ M extends true | undefined = true | undefined,
411
+ Body = any,
412
+ R = any /*Denormalize<S>*/,
413
+ O extends {
414
+ path: string;
415
+ body?: any;
416
+ searchParams?: any;
417
+ } = { path: string; body: any },
418
+ > = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
419
+
420
+ export type RestTypeNoBody<
421
+ UrlParams = any,
422
+ S extends Schema | undefined = Schema | undefined,
423
+ M extends true | undefined = true | undefined,
424
+ R = any /*Denormalize<S>*/,
425
+ O extends {
426
+ path: string;
427
+ body?: undefined;
428
+ searchParams?: any;
429
+ } = { path: string; body: undefined },
430
+ > = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
431
+
432
+ /** Simple parameters, and body fetch functions */
433
+ export type RestFetch<
434
+ UrlParams,
435
+ // eslint-disable-next-line @typescript-eslint/ban-types
436
+ Body = {},
437
+ Resolve = any,
438
+ > = IfTypeScriptLooseNull<
439
+ | ParamFetchNoBody<UrlParams, Resolve>
440
+ | ParamFetchWithBody<UrlParams, Body, Resolve>,
441
+ Body extends {}
442
+ ? ParamFetchWithBody<UrlParams, Body, Resolve>
443
+ : ParamFetchNoBody<UrlParams, Resolve>
444
+ >;
445
+
446
+ export type ParamFetchWithBody<P, B = {}, R = any> =
447
+ // we must always allow undefined in a union and give it a type without params
448
+ P extends undefined
449
+ ? (this: EndpointInstanceInterface, body: B) => Promise<R>
450
+ : // even with loose null, this will only be true when all members are optional
451
+ {} extends P
452
+ ? // this safely handles PathArgs with no members that results in a simple `unknown` type
453
+ keyof P extends never
454
+ ? (this: EndpointInstanceInterface, body: B) => Promise<R>
455
+ :
456
+ | ((
457
+ this: EndpointInstanceInterface,
458
+ params: P,
459
+ body: B,
460
+ ) => Promise<R>)
461
+ | ((this: EndpointInstanceInterface, body: B) => Promise<R>)
462
+ : (this: EndpointInstanceInterface, params: P, body: B) => Promise<R>;
463
+
464
+ export type ParamFetchNoBody<P, R = any> =
465
+ // we must always allow undefined in a union and give it a type without params
466
+ P extends undefined
467
+ ? (this: EndpointInstanceInterface) => Promise<R>
468
+ : // even with loose null, this will only be true when all members are optional
469
+ {} extends P
470
+ ? // this safely handles PathArgs with no members that results in a simple `unknown` type
471
+ keyof P extends never
472
+ ? (this: EndpointInstanceInterface) => Promise<R>
473
+ :
474
+ | ((this: EndpointInstanceInterface, params: P) => Promise<R>)
475
+ | ((this: EndpointInstanceInterface) => Promise<R>)
476
+ : (this: EndpointInstanceInterface, params: P) => Promise<R>;
477
+
478
+ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
479
+
480
+ export type KeyofRestEndpoint = keyof RestInstance;
481
+
482
+ export type FromFallBack<K extends keyof E, O, E> = K extends keyof O
483
+ ? O[K]
484
+ : E[K];
485
+
486
+ export type FetchMutate<
487
+ A extends readonly any[] = // eslint-disable-next-line @typescript-eslint/ban-types
488
+ | [any, {}]
489
+ // eslint-disable-next-line @typescript-eslint/ban-types
490
+ | [{}],
491
+ R = any,
492
+ > = (this: RestInstance, ...args: A) => Promise<R>;
493
+
494
+ export type FetchGet<A extends readonly any[] = [any], R = any> = (
495
+ this: RestInstance,
496
+ ...args: A
497
+ ) => Promise<R>;
498
+
499
+ export type GetEndpoint<
500
+ UrlParams = any,
501
+ S extends Schema | undefined = Schema | undefined,
502
+ > = RestTypeNoBody<UrlParams, S, undefined>;
503
+
504
+ export type MutateEndpoint<
505
+ UrlParams = any,
506
+ Body extends BodyInit | Record<string, any> = any,
507
+ S extends Schema | undefined = Schema | undefined,
508
+ > = RestTypeWithBody<UrlParams, S, true, Body>;
509
+
510
+ export type Defaults<O, D> = {
511
+ [K in keyof O | keyof D]: K extends keyof O
512
+ ? Exclude<O[K], undefined>
513
+ : D[Extract<K, keyof D>];
514
+ };
515
+
516
+ export type NewGetEndpoint<
517
+ O extends {
518
+ path: string;
519
+ searchParams?: any;
520
+ } = { path: string },
521
+ S extends Schema | undefined = Schema | undefined,
522
+ > = RestTypeNoBody<
523
+ 'searchParams' extends keyof O
524
+ ? O['searchParams'] & PathArgs<O['path']>
525
+ : PathArgs<O['path']>,
526
+ S,
527
+ undefined,
528
+ any,
529
+ O & { body: undefined }
530
+ >;
531
+
532
+ export type NewMutateEndpoint<
533
+ O extends {
534
+ path: string;
535
+ body?: any;
536
+ searchParams?: any;
537
+ } = { path: string; body: any },
538
+ S extends Schema | undefined = Schema | undefined,
539
+ > = RestTypeWithBody<
540
+ 'searchParams' extends keyof O
541
+ ? O['searchParams'] & PathArgs<O['path']>
542
+ : PathArgs<O['path']>,
543
+ S,
544
+ true,
545
+ O['body'],
546
+ any,
547
+ O
548
+ >;