@data-client/rest 0.16.5 → 0.17.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.
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ Denormalize,
2
3
  EndpointExtraOptions,
3
4
  EndpointInstanceInterface,
4
5
  Schema,
@@ -14,6 +15,23 @@ import {
14
15
  import { PathArgs, SoftPathArgs } from './pathTypes.js';
15
16
  import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
16
17
 
18
+ export type ContentType = 'json' | 'blob' | 'text' | 'arrayBuffer' | 'stream';
19
+
20
+ interface ContentTypeMap {
21
+ blob: Blob;
22
+ text: string;
23
+ arrayBuffer: ArrayBuffer;
24
+ stream: ReadableStream<Uint8Array>;
25
+ json: any;
26
+ }
27
+
28
+ type ContentReturnType<C extends ContentType> = ContentTypeMap[C];
29
+
30
+ type ContentSchemaGuard<O> =
31
+ O extends { content: 'blob' | 'text' | 'arrayBuffer' | 'stream' } ?
32
+ { schema?: undefined }
33
+ : {};
34
+
17
35
  export interface RestInstanceBase<
18
36
  F extends FetchFunction = FetchFunction,
19
37
  S extends Schema | undefined = any,
@@ -48,6 +66,8 @@ export interface RestInstanceBase<
48
66
  readonly signal: AbortSignal | undefined;
49
67
  /** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
50
68
  readonly paginationField?: string;
69
+ /** @see https://dataclient.io/rest/api/RestEndpoint#content */
70
+ readonly content?: ContentType;
51
71
 
52
72
  /* fetch lifecycles */
53
73
  /* before-fetch */
@@ -105,7 +125,8 @@ export interface RestInstanceBase<
105
125
  this: E,
106
126
  options: Readonly<
107
127
  RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions
108
- >,
128
+ > &
129
+ ContentSchemaGuard<ExtendOptions>,
109
130
  ): RestExtendedEndpoint<ExtendOptions, E>;
110
131
  }
111
132
 
@@ -155,10 +176,10 @@ export interface RestInstance<
155
176
  push: AddEndpoint<
156
177
  F,
157
178
  ExtractCollection<S>['push'],
158
- Exclude<O, 'body' | 'method'> & {
179
+ Omit<O, 'body' | 'method'> & {
159
180
  body:
160
- | OptionsToAdderBodyArgument<O>
161
- | OptionsToAdderBodyArgument<O>[]
181
+ | OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']>
182
+ | OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']>[]
162
183
  | FormData;
163
184
  }
164
185
  >;
@@ -168,10 +189,10 @@ export interface RestInstance<
168
189
  unshift: AddEndpoint<
169
190
  F,
170
191
  ExtractCollection<S>['unshift'],
171
- Exclude<O, 'body' | 'method'> & {
192
+ Omit<O, 'body' | 'method'> & {
172
193
  body:
173
- | OptionsToAdderBodyArgument<O>
174
- | OptionsToAdderBodyArgument<O>[]
194
+ | OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']>
195
+ | OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']>[]
175
196
  | FormData;
176
197
  }
177
198
  >;
@@ -181,8 +202,13 @@ export interface RestInstance<
181
202
  assign: AddEndpoint<
182
203
  F,
183
204
  ExtractCollection<S>,
184
- Exclude<O, 'body' | 'method'> & {
185
- body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
205
+ Omit<O, 'body' | 'method'> & {
206
+ body:
207
+ | Record<
208
+ string,
209
+ OptionsToAdderBodyArgument<O, ExtractCollection<S>['assign']>
210
+ >
211
+ | FormData;
186
212
  }
187
213
  >;
188
214
  /** Remove item(s) (PATCH) from collection
@@ -191,10 +217,12 @@ export interface RestInstance<
191
217
  remove: RemoveEndpoint<
192
218
  F,
193
219
  ExtractCollection<S>['remove'],
194
- Exclude<O, 'body' | 'method'> & {
220
+ Omit<O, 'body' | 'method'> & {
195
221
  body:
196
- | OptionsToAdderBodyArgument<O>
197
- | OptionsToAdderBodyArgument<O>[]
222
+ | Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>>
223
+ | Partial<
224
+ OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>
225
+ >[]
198
226
  | FormData;
199
227
  }
200
228
  >;
@@ -206,7 +234,9 @@ export interface RestInstance<
206
234
  ExtractCollection<S>['move'],
207
235
  {
208
236
  path: 'movePath' extends keyof O ? O['movePath'] & string : O['path'];
209
- body: OptionsToAdderBodyArgument<O> | FormData;
237
+ body:
238
+ | Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['move']>>
239
+ | FormData;
210
240
  }
211
241
  >;
212
242
  }
@@ -248,7 +278,10 @@ type OptionsToRestEndpoint<
248
278
  Extract<O['sideEffect'], boolean | undefined>
249
279
  : 'method' extends keyof O ? MethodToSide<O['method']>
250
280
  : E['sideEffect'],
251
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
281
+ O['process'] extends {} ? ReturnType<O['process']>
282
+ : 'content' extends keyof O ?
283
+ ContentReturnType<O['content'] & ContentType>
284
+ : ResolveType<F>,
252
285
  {
253
286
  path: Exclude<O['path'], undefined>;
254
287
  body: 'body' extends keyof O ? O['body'] : E['body'];
@@ -276,7 +309,10 @@ type OptionsToRestEndpoint<
276
309
  Extract<O['sideEffect'], boolean | undefined>
277
310
  : 'method' extends keyof O ? MethodToSide<O['method']>
278
311
  : E['sideEffect'],
279
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
312
+ O['process'] extends {} ? ReturnType<O['process']>
313
+ : 'content' extends keyof O ?
314
+ ContentReturnType<O['content'] & ContentType>
315
+ : ResolveType<F>,
280
316
  {
281
317
  path: E['path'];
282
318
  body: O['body'];
@@ -302,7 +338,10 @@ type OptionsToRestEndpoint<
302
338
  Extract<O['sideEffect'], boolean | undefined>
303
339
  : 'method' extends keyof O ? MethodToSide<O['method']>
304
340
  : E['sideEffect'],
305
- O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
341
+ O['process'] extends {} ? ReturnType<O['process']>
342
+ : 'content' extends keyof O ?
343
+ ContentReturnType<O['content'] & ContentType>
344
+ : ResolveType<F>,
306
345
  {
307
346
  path: E['path'];
308
347
  body: E['body'];
@@ -343,9 +382,10 @@ export type RestExtendedEndpoint<
343
382
  { paginationField: E['getPage']['paginationField'] }
344
383
  : unknown),
345
384
  RestInstance<
346
- (
347
- ...args: Parameters<E>
348
- ) => O['process'] extends {} ? Promise<ReturnType<O['process']>>
385
+ (...args: Parameters<E>) => O['process'] extends {} ?
386
+ Promise<ReturnType<O['process']>>
387
+ : 'content' extends keyof O ?
388
+ Promise<ContentReturnType<O['content'] & ContentType>>
349
389
  : ReturnType<E>,
350
390
  'schema' extends keyof O ? O['schema'] : E['schema'],
351
391
  'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined>
@@ -373,6 +413,8 @@ export interface PartialRestGenerics {
373
413
  readonly paginationField?: string;
374
414
  /** @see https://dataclient.io/rest/api/RestEndpoint#process */
375
415
  process?(value: any, ...args: any): any;
416
+ /** @see https://dataclient.io/rest/api/RestEndpoint#content */
417
+ readonly content?: ContentType;
376
418
  }
377
419
  /** Generic types when constructing a RestEndpoint
378
420
  *
@@ -532,8 +574,8 @@ type InferRestMethodWhenOmitted<O extends RestGenerics> =
532
574
  type MethodArgForBodyInference<O extends RestGenerics> =
533
575
  'method' extends keyof O ? O['method'] : InferRestMethodWhenOmitted<O>;
534
576
 
535
- type OptionsToAdderBodyArgument<O extends { body?: any }> =
536
- 'body' extends keyof O ? O['body'] : any;
577
+ type OptionsToAdderBodyArgument<O extends { body?: any }, EntitySchema = any> =
578
+ 'body' extends keyof O ? O['body'] : Partial<Denormalize<EntitySchema>>;
537
579
 
538
580
  export interface RestEndpointOptions<
539
581
  F extends FetchFunction = FetchFunction,
@@ -560,6 +602,8 @@ export interface RestEndpointOptions<
560
602
  * @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
561
603
  */
562
604
  parseResponse?(response: Response): Promise<any>;
605
+ /** @see https://dataclient.io/rest/api/RestEndpoint#content */
606
+ content?: ContentType;
563
607
 
564
608
  sideEffect?: boolean | undefined;
565
609
  name?: string;
@@ -586,6 +630,8 @@ export type RestEndpointConstructorOptions<O extends RestGenerics = any> =
586
630
  : SoftPathArgs<O['path']>,
587
631
  OptionsToBodyArgument<O, MethodArgForBodyInference<O>>,
588
632
  O['process'] extends {} ? ReturnType<O['process']>
633
+ : 'content' extends keyof O ?
634
+ ContentReturnType<O['content'] & ContentType>
589
635
  : any /*Denormalize<O['schema']>*/
590
636
  >,
591
637
  O['schema']
@@ -607,6 +653,7 @@ export interface RestEndpoint<
607
653
  : PathArgs<O['path']>,
608
654
  OptionsToBodyArgument<O, MethodArgForBodyInference<O>>,
609
655
  O['process'] extends {} ? ReturnType<O['process']>
656
+ : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType>
610
657
  : any /*Denormalize<O['schema']>*/
611
658
  >,
612
659
  'schema' extends keyof O ? O['schema'] : undefined,
@@ -628,7 +675,9 @@ export interface RestEndpointConstructor {
628
675
  sideEffect,
629
676
  name,
630
677
  ...options
631
- }: RestEndpointConstructorOptions<O> & Readonly<O>): RestEndpoint<O>;
678
+ }: RestEndpointConstructorOptions<O> &
679
+ Readonly<O> &
680
+ ContentSchemaGuard<O>): RestEndpoint<O>;
632
681
  readonly prototype: RestInstanceBase;
633
682
  }
634
683