@data-client/rest 0.16.5 → 0.16.6
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/CHANGELOG.md +54 -0
- package/dist/index.js +55 -26
- package/dist/index.umd.min.js +1 -1
- package/legacy/RestEndpoint.js +55 -27
- package/legacy/RestEndpointTypes.js +1 -1
- package/legacy/index.js +1 -1
- package/lib/RestEndpoint.js +55 -27
- package/lib/RestEndpointTypes.d.ts +39 -19
- package/lib/RestEndpointTypes.d.ts.map +1 -1
- package/lib/RestEndpointTypes.js +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/package.json +2 -2
- package/src/RestEndpoint.js +85 -34
- package/src/RestEndpointTypes.ts +71 -22
- package/src/__tests__/RestEndpoint.ts +485 -1
- package/src/__tests__/resource-construction.test.ts +47 -0
- package/src/index.ts +2 -0
- package/ts4.0/RestEndpointTypes.d.ts +39 -19
- package/ts4.0/index.d.ts +2 -1
- package/ts4.1/RestEndpointTypes.d.ts +39 -19
- package/ts4.1/index.d.ts +2 -1
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
1
|
+
import type { Denormalize, EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
2
|
import type { ExtractCollection } from './extractCollection.js';
|
|
3
3
|
import { OptionsToBodyArgument, OptionsToFunction } from './OptionsToFunction.js';
|
|
4
4
|
import { PathArgs, SoftPathArgs } from './pathTypes.js';
|
|
5
5
|
import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
|
|
6
|
+
export type ContentType = 'json' | 'blob' | 'text' | 'arrayBuffer' | 'stream';
|
|
7
|
+
interface ContentTypeMap {
|
|
8
|
+
blob: Blob;
|
|
9
|
+
text: string;
|
|
10
|
+
arrayBuffer: ArrayBuffer;
|
|
11
|
+
stream: ReadableStream<Uint8Array>;
|
|
12
|
+
json: any;
|
|
13
|
+
}
|
|
14
|
+
type ContentReturnType<C extends ContentType> = ContentTypeMap[C];
|
|
15
|
+
type ContentSchemaGuard<O> = O extends {
|
|
16
|
+
content: 'blob' | 'text' | 'arrayBuffer' | 'stream';
|
|
17
|
+
} ? {
|
|
18
|
+
schema?: undefined;
|
|
19
|
+
} : {};
|
|
6
20
|
export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends boolean | undefined = boolean | undefined, O extends {
|
|
7
21
|
path: string;
|
|
8
22
|
body?: any;
|
|
@@ -33,6 +47,8 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
33
47
|
readonly signal: AbortSignal | undefined;
|
|
34
48
|
/** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
|
|
35
49
|
readonly paginationField?: string;
|
|
50
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
51
|
+
readonly content?: ContentType;
|
|
36
52
|
/** Builds the URL to fetch
|
|
37
53
|
* @see https://dataclient.io/rest/api/RestEndpoint#url
|
|
38
54
|
*/
|
|
@@ -68,7 +84,7 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
68
84
|
/** Creates a child endpoint that inherits from this while overriding provided `options`.
|
|
69
85
|
* @see https://dataclient.io/rest/api/RestEndpoint#extend
|
|
70
86
|
*/
|
|
71
|
-
extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
|
|
87
|
+
extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions> & ContentSchemaGuard<ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
|
|
72
88
|
}
|
|
73
89
|
export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends boolean | undefined = boolean | undefined, O extends {
|
|
74
90
|
path: string;
|
|
@@ -96,33 +112,33 @@ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends
|
|
|
96
112
|
/** Create a new item (POST) and `push` to the end
|
|
97
113
|
* @see https://dataclient.io/rest/api/RestEndpoint#push
|
|
98
114
|
*/
|
|
99
|
-
push: AddEndpoint<F, ExtractCollection<S>['push'],
|
|
100
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
115
|
+
push: AddEndpoint<F, ExtractCollection<S>['push'], Omit<O, 'body' | 'method'> & {
|
|
116
|
+
body: OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']> | OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']>[] | FormData;
|
|
101
117
|
}>;
|
|
102
118
|
/** Create a new item (POST) and `unshift` to the beginning
|
|
103
119
|
* @see https://dataclient.io/rest/api/RestEndpoint#unshift
|
|
104
120
|
*/
|
|
105
|
-
unshift: AddEndpoint<F, ExtractCollection<S>['unshift'],
|
|
106
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
121
|
+
unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Omit<O, 'body' | 'method'> & {
|
|
122
|
+
body: OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']> | OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']>[] | FormData;
|
|
107
123
|
}>;
|
|
108
124
|
/** Create new item(s) (POST) and `Object.assign` merge
|
|
109
125
|
* @see https://dataclient.io/rest/api/RestEndpoint#assign
|
|
110
126
|
*/
|
|
111
|
-
assign: AddEndpoint<F, ExtractCollection<S>,
|
|
112
|
-
body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
|
|
127
|
+
assign: AddEndpoint<F, ExtractCollection<S>, Omit<O, 'body' | 'method'> & {
|
|
128
|
+
body: Record<string, OptionsToAdderBodyArgument<O, ExtractCollection<S>['assign']>> | FormData;
|
|
113
129
|
}>;
|
|
114
130
|
/** Remove item(s) (PATCH) from collection
|
|
115
131
|
* @see https://dataclient.io/rest/api/RestEndpoint#remove
|
|
116
132
|
*/
|
|
117
|
-
remove: RemoveEndpoint<F, ExtractCollection<S>['remove'],
|
|
118
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
133
|
+
remove: RemoveEndpoint<F, ExtractCollection<S>['remove'], Omit<O, 'body' | 'method'> & {
|
|
134
|
+
body: Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>> | Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>>[] | FormData;
|
|
119
135
|
}>;
|
|
120
136
|
/** Move item between collections (PATCH) - removes from old, adds to new
|
|
121
137
|
* @see https://dataclient.io/rest/api/RestEndpoint#move
|
|
122
138
|
*/
|
|
123
139
|
move: MoveEndpoint<F, ExtractCollection<S>['move'], {
|
|
124
140
|
path: 'movePath' extends keyof O ? O['movePath'] & string : O['path'];
|
|
125
|
-
body: OptionsToAdderBodyArgument<O> | FormData;
|
|
141
|
+
body: Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['move']>> | FormData;
|
|
126
142
|
}>;
|
|
127
143
|
}
|
|
128
144
|
export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
|
|
@@ -138,7 +154,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
138
154
|
O['searchParams']
|
|
139
155
|
] extends [
|
|
140
156
|
undefined
|
|
141
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, OptionsToBodyArgument<'body' extends keyof O ? O : E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
157
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, OptionsToBodyArgument<'body' extends keyof O ? O : E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
142
158
|
path: Exclude<O['path'], undefined>;
|
|
143
159
|
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
144
160
|
searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
|
|
@@ -148,7 +164,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
148
164
|
O['searchParams']
|
|
149
165
|
] extends [
|
|
150
166
|
undefined
|
|
151
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
167
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
152
168
|
path: E['path'];
|
|
153
169
|
body: O['body'];
|
|
154
170
|
searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
|
|
@@ -158,7 +174,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
158
174
|
O['searchParams']
|
|
159
175
|
] extends [
|
|
160
176
|
undefined
|
|
161
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
177
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
162
178
|
path: E['path'];
|
|
163
179
|
body: E['body'];
|
|
164
180
|
searchParams: O['searchParams'];
|
|
@@ -179,7 +195,7 @@ export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestIn
|
|
|
179
195
|
};
|
|
180
196
|
} ? {
|
|
181
197
|
paginationField: E['getPage']['paginationField'];
|
|
182
|
-
} : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
|
|
198
|
+
} : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : 'content' extends keyof O ? Promise<ContentReturnType<O['content'] & ContentType>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
|
|
183
199
|
export interface PartialRestGenerics {
|
|
184
200
|
/** @see https://dataclient.io/rest/api/RestEndpoint#path */
|
|
185
201
|
readonly path?: string;
|
|
@@ -197,6 +213,8 @@ export interface PartialRestGenerics {
|
|
|
197
213
|
readonly paginationField?: string;
|
|
198
214
|
/** @see https://dataclient.io/rest/api/RestEndpoint#process */
|
|
199
215
|
process?(value: any, ...args: any): any;
|
|
216
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
217
|
+
readonly content?: ContentType;
|
|
200
218
|
}
|
|
201
219
|
/** Generic types when constructing a RestEndpoint
|
|
202
220
|
*
|
|
@@ -291,7 +309,7 @@ type InferRestMethodWhenOmitted<O extends RestGenerics> = O extends {
|
|
|
291
309
|
type MethodArgForBodyInference<O extends RestGenerics> = 'method' extends keyof O ? O['method'] : InferRestMethodWhenOmitted<O>;
|
|
292
310
|
type OptionsToAdderBodyArgument<O extends {
|
|
293
311
|
body?: any;
|
|
294
|
-
}> = 'body' extends keyof O ? O['body'] :
|
|
312
|
+
}, EntitySchema = any> = 'body' extends keyof O ? O['body'] : Partial<Denormalize<EntitySchema>>;
|
|
295
313
|
export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
|
|
296
314
|
/** Prepended to all urls
|
|
297
315
|
* @see https://dataclient.io/rest/api/RestEndpoint#urlPrefix
|
|
@@ -314,6 +332,8 @@ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S
|
|
|
314
332
|
* @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
|
|
315
333
|
*/
|
|
316
334
|
parseResponse?(response: Response): Promise<any>;
|
|
335
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
336
|
+
content?: ContentType;
|
|
317
337
|
sideEffect?: boolean | undefined;
|
|
318
338
|
name?: string;
|
|
319
339
|
signal?: AbortSignal;
|
|
@@ -326,7 +346,7 @@ export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestE
|
|
|
326
346
|
O['searchParams']
|
|
327
347
|
] extends [
|
|
328
348
|
undefined
|
|
329
|
-
] ? SoftPathArgs<O['path']> : O['searchParams'] & SoftPathArgs<O['path']> : SoftPathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, O['schema']>;
|
|
349
|
+
] ? SoftPathArgs<O['path']> : O['searchParams'] & SoftPathArgs<O['path']> : SoftPathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : any>, O['schema']>;
|
|
330
350
|
/** Simplifies endpoint definitions that follow REST patterns
|
|
331
351
|
*
|
|
332
352
|
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
@@ -335,7 +355,7 @@ export interface RestEndpoint<O extends RestGenerics = any> extends RestInstance
|
|
|
335
355
|
O['searchParams']
|
|
336
356
|
] extends [
|
|
337
357
|
undefined
|
|
338
|
-
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<MethodArgForBodyInference<O>>, 'method' extends keyof O ? O : O & {
|
|
358
|
+
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<MethodArgForBodyInference<O>>, 'method' extends keyof O ? O : O & {
|
|
339
359
|
method: InferRestMethodWhenOmitted<O>;
|
|
340
360
|
}> {
|
|
341
361
|
}
|
|
@@ -344,7 +364,7 @@ export interface RestEndpointConstructor {
|
|
|
344
364
|
*
|
|
345
365
|
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
346
366
|
*/
|
|
347
|
-
new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O>): RestEndpoint<O>;
|
|
367
|
+
new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O> & ContentSchemaGuard<O>): RestEndpoint<O>;
|
|
348
368
|
readonly prototype: RestInstanceBase;
|
|
349
369
|
}
|
|
350
370
|
export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
|
package/ts4.0/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
|
-
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
|
|
2
|
+
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, ContentType, } from './RestEndpoint.js';
|
|
3
3
|
export type { RestEndpoint as IRestEndpoint } from './RestEndpointTypes.js';
|
|
4
4
|
export { getUrlBase, getUrlTokens } from './RestHelpers.js';
|
|
5
5
|
export { default as resource } from './resource.js';
|
|
@@ -11,6 +11,7 @@ export type { HookResource, HookableEndpointInterface, } from './hookifyResource
|
|
|
11
11
|
export { default as NetworkError } from './NetworkError.js';
|
|
12
12
|
export type { OptionsToFunction } from './OptionsToFunction.js';
|
|
13
13
|
export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
|
|
14
|
+
export type { CollectionOptions } from '@data-client/endpoint';
|
|
14
15
|
export * from '@data-client/endpoint';
|
|
15
16
|
export * from './RestEndpoint.js';
|
|
16
17
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1,8 +1,22 @@
|
|
|
1
|
-
import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
1
|
+
import type { Denormalize, EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
2
|
import type { ExtractCollection } from './extractCollection.js';
|
|
3
3
|
import { OptionsToBodyArgument, OptionsToFunction } from './OptionsToFunction.js';
|
|
4
4
|
import { PathArgs, SoftPathArgs } from './pathTypes.js';
|
|
5
5
|
import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
|
|
6
|
+
export type ContentType = 'json' | 'blob' | 'text' | 'arrayBuffer' | 'stream';
|
|
7
|
+
interface ContentTypeMap {
|
|
8
|
+
blob: Blob;
|
|
9
|
+
text: string;
|
|
10
|
+
arrayBuffer: ArrayBuffer;
|
|
11
|
+
stream: ReadableStream<Uint8Array>;
|
|
12
|
+
json: any;
|
|
13
|
+
}
|
|
14
|
+
type ContentReturnType<C extends ContentType> = ContentTypeMap[C];
|
|
15
|
+
type ContentSchemaGuard<O> = O extends {
|
|
16
|
+
content: 'blob' | 'text' | 'arrayBuffer' | 'stream';
|
|
17
|
+
} ? {
|
|
18
|
+
schema?: undefined;
|
|
19
|
+
} : {};
|
|
6
20
|
export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends boolean | undefined = boolean | undefined, O extends {
|
|
7
21
|
path: string;
|
|
8
22
|
body?: any;
|
|
@@ -33,6 +47,8 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
33
47
|
readonly signal: AbortSignal | undefined;
|
|
34
48
|
/** @see https://dataclient.io/rest/api/RestEndpoint#paginationField */
|
|
35
49
|
readonly paginationField?: string;
|
|
50
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
51
|
+
readonly content?: ContentType;
|
|
36
52
|
/** Builds the URL to fetch
|
|
37
53
|
* @see https://dataclient.io/rest/api/RestEndpoint#url
|
|
38
54
|
*/
|
|
@@ -68,7 +84,7 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
68
84
|
/** Creates a child endpoint that inherits from this while overriding provided `options`.
|
|
69
85
|
* @see https://dataclient.io/rest/api/RestEndpoint#extend
|
|
70
86
|
*/
|
|
71
|
-
extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
|
|
87
|
+
extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions> & ContentSchemaGuard<ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
|
|
72
88
|
}
|
|
73
89
|
export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends boolean | undefined = boolean | undefined, O extends {
|
|
74
90
|
path: string;
|
|
@@ -96,33 +112,33 @@ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends
|
|
|
96
112
|
/** Create a new item (POST) and `push` to the end
|
|
97
113
|
* @see https://dataclient.io/rest/api/RestEndpoint#push
|
|
98
114
|
*/
|
|
99
|
-
push: AddEndpoint<F, ExtractCollection<S>['push'],
|
|
100
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
115
|
+
push: AddEndpoint<F, ExtractCollection<S>['push'], Omit<O, 'body' | 'method'> & {
|
|
116
|
+
body: OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']> | OptionsToAdderBodyArgument<O, ExtractCollection<S>['push']>[] | FormData;
|
|
101
117
|
}>;
|
|
102
118
|
/** Create a new item (POST) and `unshift` to the beginning
|
|
103
119
|
* @see https://dataclient.io/rest/api/RestEndpoint#unshift
|
|
104
120
|
*/
|
|
105
|
-
unshift: AddEndpoint<F, ExtractCollection<S>['unshift'],
|
|
106
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
121
|
+
unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Omit<O, 'body' | 'method'> & {
|
|
122
|
+
body: OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']> | OptionsToAdderBodyArgument<O, ExtractCollection<S>['unshift']>[] | FormData;
|
|
107
123
|
}>;
|
|
108
124
|
/** Create new item(s) (POST) and `Object.assign` merge
|
|
109
125
|
* @see https://dataclient.io/rest/api/RestEndpoint#assign
|
|
110
126
|
*/
|
|
111
|
-
assign: AddEndpoint<F, ExtractCollection<S>,
|
|
112
|
-
body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
|
|
127
|
+
assign: AddEndpoint<F, ExtractCollection<S>, Omit<O, 'body' | 'method'> & {
|
|
128
|
+
body: Record<string, OptionsToAdderBodyArgument<O, ExtractCollection<S>['assign']>> | FormData;
|
|
113
129
|
}>;
|
|
114
130
|
/** Remove item(s) (PATCH) from collection
|
|
115
131
|
* @see https://dataclient.io/rest/api/RestEndpoint#remove
|
|
116
132
|
*/
|
|
117
|
-
remove: RemoveEndpoint<F, ExtractCollection<S>['remove'],
|
|
118
|
-
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
133
|
+
remove: RemoveEndpoint<F, ExtractCollection<S>['remove'], Omit<O, 'body' | 'method'> & {
|
|
134
|
+
body: Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>> | Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['remove']>>[] | FormData;
|
|
119
135
|
}>;
|
|
120
136
|
/** Move item between collections (PATCH) - removes from old, adds to new
|
|
121
137
|
* @see https://dataclient.io/rest/api/RestEndpoint#move
|
|
122
138
|
*/
|
|
123
139
|
move: MoveEndpoint<F, ExtractCollection<S>['move'], {
|
|
124
140
|
path: 'movePath' extends keyof O ? O['movePath'] & string : O['path'];
|
|
125
|
-
body: OptionsToAdderBodyArgument<O> | FormData;
|
|
141
|
+
body: Partial<OptionsToAdderBodyArgument<O, ExtractCollection<S>['move']>> | FormData;
|
|
126
142
|
}>;
|
|
127
143
|
}
|
|
128
144
|
export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
|
|
@@ -138,7 +154,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
138
154
|
O['searchParams']
|
|
139
155
|
] extends [
|
|
140
156
|
undefined
|
|
141
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, OptionsToBodyArgument<'body' extends keyof O ? O : E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
157
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, OptionsToBodyArgument<'body' extends keyof O ? O : E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
142
158
|
path: Exclude<O['path'], undefined>;
|
|
143
159
|
body: 'body' extends keyof O ? O['body'] : E['body'];
|
|
144
160
|
searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
|
|
@@ -148,7 +164,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
148
164
|
O['searchParams']
|
|
149
165
|
] extends [
|
|
150
166
|
undefined
|
|
151
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
167
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
152
168
|
path: E['path'];
|
|
153
169
|
body: O['body'];
|
|
154
170
|
searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
|
|
@@ -158,7 +174,7 @@ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstance
|
|
|
158
174
|
O['searchParams']
|
|
159
175
|
] extends [
|
|
160
176
|
undefined
|
|
161
|
-
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
|
|
177
|
+
] ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<E, 'method' extends keyof O ? O['method'] : E['method']>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : ResolveType<F>, {
|
|
162
178
|
path: E['path'];
|
|
163
179
|
body: E['body'];
|
|
164
180
|
searchParams: O['searchParams'];
|
|
@@ -179,7 +195,7 @@ export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestIn
|
|
|
179
195
|
};
|
|
180
196
|
} ? {
|
|
181
197
|
paginationField: E['getPage']['paginationField'];
|
|
182
|
-
} : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
|
|
198
|
+
} : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : 'content' extends keyof O ? Promise<ContentReturnType<O['content'] & ContentType>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
|
|
183
199
|
export interface PartialRestGenerics {
|
|
184
200
|
/** @see https://dataclient.io/rest/api/RestEndpoint#path */
|
|
185
201
|
readonly path?: string;
|
|
@@ -197,6 +213,8 @@ export interface PartialRestGenerics {
|
|
|
197
213
|
readonly paginationField?: string;
|
|
198
214
|
/** @see https://dataclient.io/rest/api/RestEndpoint#process */
|
|
199
215
|
process?(value: any, ...args: any): any;
|
|
216
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
217
|
+
readonly content?: ContentType;
|
|
200
218
|
}
|
|
201
219
|
/** Generic types when constructing a RestEndpoint
|
|
202
220
|
*
|
|
@@ -291,7 +309,7 @@ type InferRestMethodWhenOmitted<O extends RestGenerics> = O extends {
|
|
|
291
309
|
type MethodArgForBodyInference<O extends RestGenerics> = 'method' extends keyof O ? O['method'] : InferRestMethodWhenOmitted<O>;
|
|
292
310
|
type OptionsToAdderBodyArgument<O extends {
|
|
293
311
|
body?: any;
|
|
294
|
-
}> = 'body' extends keyof O ? O['body'] :
|
|
312
|
+
}, EntitySchema = any> = 'body' extends keyof O ? O['body'] : Partial<Denormalize<EntitySchema>>;
|
|
295
313
|
export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
|
|
296
314
|
/** Prepended to all urls
|
|
297
315
|
* @see https://dataclient.io/rest/api/RestEndpoint#urlPrefix
|
|
@@ -314,6 +332,8 @@ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S
|
|
|
314
332
|
* @see https://dataclient.io/rest/api/RestEndpoint#parseResponse
|
|
315
333
|
*/
|
|
316
334
|
parseResponse?(response: Response): Promise<any>;
|
|
335
|
+
/** @see https://dataclient.io/rest/api/RestEndpoint#content */
|
|
336
|
+
content?: ContentType;
|
|
317
337
|
sideEffect?: boolean | undefined;
|
|
318
338
|
name?: string;
|
|
319
339
|
signal?: AbortSignal;
|
|
@@ -326,7 +346,7 @@ export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestE
|
|
|
326
346
|
O['searchParams']
|
|
327
347
|
] extends [
|
|
328
348
|
undefined
|
|
329
|
-
] ? SoftPathArgs<O['path']> : O['searchParams'] & SoftPathArgs<O['path']> : SoftPathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, O['schema']>;
|
|
349
|
+
] ? SoftPathArgs<O['path']> : O['searchParams'] & SoftPathArgs<O['path']> : SoftPathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : any>, O['schema']>;
|
|
330
350
|
/** Simplifies endpoint definitions that follow REST patterns
|
|
331
351
|
*
|
|
332
352
|
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
@@ -335,7 +355,7 @@ export interface RestEndpoint<O extends RestGenerics = any> extends RestInstance
|
|
|
335
355
|
O['searchParams']
|
|
336
356
|
] extends [
|
|
337
357
|
undefined
|
|
338
|
-
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<MethodArgForBodyInference<O>>, 'method' extends keyof O ? O : O & {
|
|
358
|
+
] ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, MethodArgForBodyInference<O>>, O['process'] extends {} ? ReturnType<O['process']> : 'content' extends keyof O ? ContentReturnType<O['content'] & ContentType> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], boolean | undefined> : MethodToSide<MethodArgForBodyInference<O>>, 'method' extends keyof O ? O : O & {
|
|
339
359
|
method: InferRestMethodWhenOmitted<O>;
|
|
340
360
|
}> {
|
|
341
361
|
}
|
|
@@ -344,7 +364,7 @@ export interface RestEndpointConstructor {
|
|
|
344
364
|
*
|
|
345
365
|
* @see https://dataclient.io/rest/api/RestEndpoint
|
|
346
366
|
*/
|
|
347
|
-
new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O>): RestEndpoint<O>;
|
|
367
|
+
new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O> & ContentSchemaGuard<O>): RestEndpoint<O>;
|
|
348
368
|
readonly prototype: RestInstanceBase;
|
|
349
369
|
}
|
|
350
370
|
export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
|
package/ts4.1/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
|
-
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
|
|
2
|
+
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, ContentType, } from './RestEndpoint.js';
|
|
3
3
|
export type { RestEndpoint as IRestEndpoint } from './RestEndpointTypes.js';
|
|
4
4
|
export { getUrlBase, getUrlTokens } from './RestHelpers.js';
|
|
5
5
|
export { default as resource } from './resource.js';
|
|
@@ -11,6 +11,7 @@ export type { HookResource, HookableEndpointInterface, } from './hookifyResource
|
|
|
11
11
|
export { default as NetworkError } from './NetworkError.js';
|
|
12
12
|
export type { OptionsToFunction } from './OptionsToFunction.js';
|
|
13
13
|
export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
|
|
14
|
+
export type { CollectionOptions } from '@data-client/endpoint';
|
|
14
15
|
export * from '@data-client/endpoint';
|
|
15
16
|
export * from './RestEndpoint.js';
|
|
16
17
|
//# sourceMappingURL=index.d.ts.map
|