@data-client/rest 0.7.4 → 0.7.5

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,315 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/ban-types */
2
- import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, } from '@data-client/endpoint';
3
- import type { ExtractCollection } from './extractCollection.js';
4
- import { OptionsToBodyArgument, OptionsToFunction, } from './OptionsToFunction.js';
5
- import { PathArgs } from './pathTypes.js';
6
- import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
7
- export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
8
- path: string;
9
- body?: any;
10
- searchParams?: any;
11
- method?: string;
12
- } = {
13
- path: string;
14
- }> extends EndpointInstanceInterface<F, S, M> {
15
- /** @see https://resthooks.io/rest/api/RestEndpoint#body */
16
- readonly body?: 'body' extends keyof O ? O['body'] : any;
17
- /** @see https://resthooks.io/rest/api/RestEndpoint#searchParams */
18
- readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
19
- /** Pattern to construct url based on Url Parameters
20
- * @see https://resthooks.io/rest/api/RestEndpoint#path
21
- */
22
- readonly path: O['path'];
23
- /** Prepended to all urls
24
- * @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
25
- */
26
- readonly urlPrefix: string;
27
- readonly requestInit: RequestInit;
28
- /** @see https://resthooks.io/rest/api/RestEndpoint#method */
29
- readonly method: (O & {
30
- method: string;
31
- })['method'];
32
- readonly signal: AbortSignal | undefined;
33
- readonly paginationField?: string;
34
- /* fetch lifecycles */
35
- /* before-fetch */
36
- url(...args: Parameters<F>): string;
37
- /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
38
- getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
39
- /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
40
- getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
41
- /* after-fetch */
42
- /** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
43
- fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
44
- /** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
45
- parseResponse(response: Response): Promise<any>;
46
- /** @see https://resthooks.io/rest/api/RestEndpoint#process */
47
- process(value: any, ...args: Parameters<F>): ResolveType<F>;
48
- /* utilities */
49
- /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
50
- testKey(key: string): boolean;
51
- /* extenders */
52
- // TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
53
- // this is just a hack to handle when no members of PartialRestGenerics are present
54
- // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
55
- // should be used from the other (due to same problem with every member being partial)
56
- /** Creates a child endpoint that inherits from this while overriding provided `options`.
57
- * @see https://dataclient.io/rest/api/RestEndpoint#extend
58
- */
59
- extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
60
- }
61
- export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
62
- path: string;
63
- body?: any;
64
- searchParams?: any;
65
- method?: string;
66
- paginationField?: string;
67
- } = {
68
- path: string;
69
- }> extends RestInstanceBase<F, S, M, O> {
70
- /** Creates an Endpoint to append the next page extending a list for pagination
71
- * @see https://dataclient.io/rest/api/RestEndpoint#paginated
72
- */
73
- paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
74
- ...Parameters<E>
75
- ]): PaginationEndpoint<E, A>;
76
- paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
77
- /** Endpoint to append the next page extending a list for pagination
78
- * @see https://dataclient.io/rest/api/RestEndpoint#getPage
79
- */
80
- getPage: 'paginationField' extends keyof O ? O['paginationField'] extends string ? PaginationFieldEndpoint<F & {
81
- schema: S;
82
- sideEffect: M;
83
- } & O, O['paginationField']> : undefined : undefined;
84
- /** Endpoint that pushes (place at end) a newly created entity to this Collection
85
- * @see https://dataclient.io/rest/api/RestEndpoint#push
86
- */
87
- push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
88
- body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
89
- }>;
90
- /** Endpoint that unshifts (place at start) a newly created entity to this Collection
91
- * @see https://dataclient.io/rest/api/RestEndpoint#unshift
92
- */
93
- unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
94
- body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
95
- }>;
96
- /** Endpoint that assigns newly created entities to their respective keys in this Collection
97
- * @see https://dataclient.io/rest/api/RestEndpoint#assign
98
- */
99
- assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
100
- body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
101
- }>;
102
- }
103
- export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
104
- body?: any;
105
- path?: string;
106
- schema?: Schema;
107
- method?: string;
108
- }, F extends FetchFunction> = RestEndpointOptions<OptionsToFunction<O, E, F>, 'schema' extends keyof O ? Extract<O['schema'], Schema | undefined> : E['schema']> & Partial<Omit<E, KeyofRestEndpoint | keyof PartialRestGenerics | keyof RestEndpointOptions>>;
109
- type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
110
- body?: any;
111
- paginationField?: string;
112
- }, F extends FetchFunction> = 'path' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
113
- path: Exclude<O['path'], undefined>;
114
- body: 'body' extends keyof O ? O['body'] : E['body'];
115
- searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
116
- method: 'method' extends keyof O ? O['method'] : E['method'];
117
- paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField'];
118
- }> : 'body' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
119
- path: E['path'];
120
- body: O['body'];
121
- searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
122
- method: 'method' extends keyof O ? O['method'] : E['method'];
123
- paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract<E['paginationField'], string>;
124
- }> : 'searchParams' extends keyof O ? RestType<O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
125
- path: E['path'];
126
- body: E['body'];
127
- searchParams: O['searchParams'];
128
- method: 'method' extends keyof O ? O['method'] : E['method'];
129
- paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract<E['paginationField'], string>;
130
- }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
131
- path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
132
- body: 'body' extends keyof O ? O['body'] : E['body'];
133
- searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
134
- method: 'method' extends keyof O ? O['method'] : E['method'];
135
- paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField'];
136
- }>;
137
- export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
138
- getPage?: unknown;
139
- }> = OptionsToRestEndpoint<O, E & (E extends {
140
- getPage: {
141
- paginationField: string;
142
- };
143
- } ? {
144
- paginationField: E['getPage']['paginationField'];
145
- } : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
146
- export interface PartialRestGenerics {
147
- /** @see https://resthooks.io/rest/api/RestEndpoint#path */
148
- readonly path?: string;
149
- /** @see https://resthooks.io/rest/api/RestEndpoint#schema */
150
- readonly schema?: Schema | undefined;
151
- /** @see https://resthooks.io/rest/api/RestEndpoint#method */
152
- readonly method?: string;
153
- /** Only used for types */
154
- /** @see https://dataclient.io/rest/api/RestEndpoint#body */
155
- body?: any;
156
- /** Only used for types */
157
- /** @see https://dataclient.io/rest/api/RestEndpoint#searchParams */
158
- searchParams?: any;
159
- /** @see https://dataclient.io/rest/api/RestEndpoint#paginationfield */
160
- readonly paginationField?: string;
161
- /** @see https://resthooks.io/rest/api/RestEndpoint#process */
162
- process?(value: any, ...args: any): any;
163
- }
164
- export interface RestGenerics extends PartialRestGenerics {
165
- readonly path: string;
166
- }
167
- export type PaginationEndpoint<E extends FetchFunction & RestGenerics & {
168
- sideEffect?: true | undefined;
169
- }, A extends any[]> = RestInstanceBase<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
170
- searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
171
- }>;
172
- export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
173
- sideEffect?: true | undefined;
174
- }, C extends string> = RestInstanceBase<ParamFetchNoBody<{
175
- [K in C]: string | number | boolean;
176
- } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
177
- searchParams: {
178
- [K in C]: string | number | boolean;
179
- } & E['searchParams'];
180
- }> & {
181
- paginationField: C;
182
- };
183
- export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
184
- path: string;
185
- body: any;
186
- searchParams?: any;
187
- } = {
188
- path: string;
189
- body: any;
190
- }> = RestInstanceBase<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, O['body'], ResolveType<F>>, S, true, Omit<O, 'method'> & {
191
- method: 'POST';
192
- }>;
193
- type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
194
- body: any;
195
- } : O & {
196
- body: undefined;
197
- };
198
- type OptionsToAdderBodyArgument<O extends {
199
- body?: any;
200
- }> = 'body' extends keyof O ? O['body'] : any;
201
- export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
202
- urlPrefix?: string;
203
- requestInit?: RequestInit;
204
- getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
205
- getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
206
- fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
207
- parseResponse?(response: Response): Promise<any>;
208
- sideEffect?: true | undefined;
209
- name?: string;
210
- signal?: AbortSignal;
211
- fetch?: F;
212
- key?(...args: Parameters<F>): string;
213
- url?(...args: Parameters<F>): string;
214
- update?: EndpointUpdateFunction<F, S>;
215
- }
216
- export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestEndpointOptions<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : O extends {
217
- sideEffect: true;
218
- } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
219
- export interface RestEndpointConstructor {
220
- new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O>): RestInstance<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : O extends {
221
- sideEffect: true;
222
- } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], undefined | true> : MethodToSide<O['method']>, 'method' extends keyof O ? O : O & {
223
- method: O extends {
224
- sideEffect: true;
225
- } ? 'POST' : 'GET';
226
- }>;
227
- readonly prototype: RestInstanceBase;
228
- }
2
+ import type { RestEndpointConstructor } from './RestEndpointTypes.js';
3
+ export * from './RestEndpointTypes.js';
229
4
  /** Simplifies endpoint definitions that follow REST patterns
230
5
  *
231
6
  * @see https://resthooks.io/rest/api/RestEndpoint
232
7
  */
233
8
  export declare let RestEndpoint: RestEndpointConstructor;
234
9
  export default RestEndpoint;
235
- export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
236
- /** RestEndpoint types simplified */
237
- export type RestType<UrlParams = any, Body = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
238
- path: string;
239
- body?: any;
240
- searchParams?: any;
241
- paginationField?: string;
242
- } = {
243
- path: string;
244
- paginationField: string;
245
- }> = IfTypeScriptLooseNull<RestInstance<keyof UrlParams extends never ? (this: EndpointInstanceInterface, body?: Body) => Promise<R> : {} extends UrlParams ? ((this: EndpointInstanceInterface, body?: Body) => Promise<R>) | ((this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>) : (this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>, S, M, O>, Body extends {} ? RestTypeWithBody<UrlParams, S, M, Body, R, O> : RestTypeNoBody<UrlParams, S, M, R, O>>;
246
- export type RestTypeWithBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, Body = any, R = any /*Denormalize<S>*/, O extends {
247
- path: string;
248
- body?: any;
249
- searchParams?: any;
250
- } = {
251
- path: string;
252
- body: any;
253
- }> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
254
- export type RestTypeNoBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any /*Denormalize<S>*/, O extends {
255
- path: string;
256
- body?: undefined;
257
- searchParams?: any;
258
- } = {
259
- path: string;
260
- body: undefined;
261
- }> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
262
- /** Simple parameters, and body fetch functions */
263
- export type RestFetch<UrlParams,
264
- // eslint-disable-next-line @typescript-eslint/ban-types
265
- Body = {}, Resolve = any> = IfTypeScriptLooseNull<ParamFetchNoBody<UrlParams, Resolve> | ParamFetchWithBody<UrlParams, Body, Resolve>, Body extends {} ? ParamFetchWithBody<UrlParams, Body, Resolve> : ParamFetchNoBody<UrlParams, Resolve>>;
266
- export type ParamFetchWithBody<P, B = {}, R = any> =
267
- // we must always allow undefined in a union and give it a type without params
268
- P extends undefined ? (this: EndpointInstanceInterface, body: B) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface, body: B) => Promise<R> : ((this: EndpointInstanceInterface, params: P, body: B) => Promise<R>) | ((this: EndpointInstanceInterface, body: B) => Promise<R>) : (this: EndpointInstanceInterface, params: P, body: B) => Promise<R>;
269
- export type ParamFetchNoBody<P, R = any> =
270
- // we must always allow undefined in a union and give it a type without params
271
- P extends undefined ? (this: EndpointInstanceInterface) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface) => Promise<R> : ((this: EndpointInstanceInterface, params: P) => Promise<R>) | ((this: EndpointInstanceInterface) => Promise<R>) : (this: EndpointInstanceInterface, params: P) => Promise<R>;
272
- type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
273
- export type KeyofRestEndpoint = keyof RestInstance;
274
- export type FromFallBack<K extends keyof E, O, E> = K extends keyof O ? O[K] : E[K];
275
- export type FetchMutate<A extends readonly any[] = [
276
- any,
277
- {}
278
- ]
279
- // eslint-disable-next-line @typescript-eslint/ban-types
280
- | [
281
- {}
282
- ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
283
- export type FetchGet<A extends readonly any[] = [
284
- any
285
- ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
286
- export type Defaults<O, D> = {
287
- [K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
288
- };
289
- export type GetEndpoint<O extends {
290
- readonly path: string;
291
- readonly schema: Schema;
292
- /** Only used for types */
293
- readonly searchParams?: any;
294
- readonly paginationField?: string;
295
- } = {
296
- path: string;
297
- schema: Schema;
298
- }> = RestTypeNoBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], undefined, any, O & {
299
- method: 'GET';
300
- }>;
301
- export type MutateEndpoint<O extends {
302
- readonly path: string;
303
- readonly schema: Schema;
304
- /** Only used for types */
305
- readonly searchParams?: any;
306
- /** Only used for types */
307
- readonly body?: any;
308
- } = {
309
- path: string;
310
- body: any;
311
- schema: Schema;
312
- }> = RestTypeWithBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], true, O['body'], any, O & {
313
- body: any;
314
- method: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
315
- }>;
@@ -0,0 +1,288 @@
1
+ import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType } from '@data-client/endpoint';
2
+ import type { ExtractCollection } from './extractCollection.js';
3
+ import { OptionsToBodyArgument, OptionsToFunction } from './OptionsToFunction.js';
4
+ import { PathArgs } from './pathTypes.js';
5
+ import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
6
+ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
7
+ path: string;
8
+ body?: any;
9
+ searchParams?: any;
10
+ method?: string;
11
+ } = {
12
+ path: string;
13
+ }> extends EndpointInstanceInterface<F, S, M> {
14
+ /** @see https://resthooks.io/rest/api/RestEndpoint#body */
15
+ readonly body?: 'body' extends keyof O ? O['body'] : any;
16
+ /** @see https://resthooks.io/rest/api/RestEndpoint#searchParams */
17
+ readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
18
+ /** Pattern to construct url based on Url Parameters
19
+ * @see https://resthooks.io/rest/api/RestEndpoint#path
20
+ */
21
+ readonly path: O['path'];
22
+ /** Prepended to all urls
23
+ * @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
24
+ */
25
+ readonly urlPrefix: string;
26
+ readonly requestInit: RequestInit;
27
+ /** @see https://resthooks.io/rest/api/RestEndpoint#method */
28
+ readonly method: (O & {
29
+ method: string;
30
+ })['method'];
31
+ readonly signal: AbortSignal | undefined;
32
+ readonly paginationField?: string;
33
+ url(...args: Parameters<F>): string;
34
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
35
+ getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
36
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
37
+ getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
38
+ /** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
39
+ fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
40
+ /** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
41
+ parseResponse(response: Response): Promise<any>;
42
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
43
+ process(value: any, ...args: Parameters<F>): ResolveType<F>;
44
+ /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
45
+ testKey(key: string): boolean;
46
+ /** Creates a child endpoint that inherits from this while overriding provided `options`.
47
+ * @see https://dataclient.io/rest/api/RestEndpoint#extend
48
+ */
49
+ extend<E extends RestInstanceBase, ExtendOptions extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<ExtendOptions, E, F> & ExtendOptions>): RestExtendedEndpoint<ExtendOptions, E>;
50
+ }
51
+ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
52
+ path: string;
53
+ body?: any;
54
+ searchParams?: any;
55
+ method?: string;
56
+ paginationField?: string;
57
+ } = {
58
+ path: string;
59
+ }> extends RestInstanceBase<F, S, M, O> {
60
+ /** Creates an Endpoint to append the next page extending a list for pagination
61
+ * @see https://dataclient.io/rest/api/RestEndpoint#paginated
62
+ */
63
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
64
+ ...Parameters<E>
65
+ ]): PaginationEndpoint<E, A>;
66
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
67
+ /** Endpoint to append the next page extending a list for pagination
68
+ * @see https://dataclient.io/rest/api/RestEndpoint#getPage
69
+ */
70
+ getPage: 'paginationField' extends keyof O ? O['paginationField'] extends string ? PaginationFieldEndpoint<F & {
71
+ schema: S;
72
+ sideEffect: M;
73
+ } & O, O['paginationField']> : undefined : undefined;
74
+ /** Endpoint that pushes (place at end) a newly created entity to this Collection
75
+ * @see https://dataclient.io/rest/api/RestEndpoint#push
76
+ */
77
+ push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
78
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
79
+ }>;
80
+ /** Endpoint that unshifts (place at start) a newly created entity to this Collection
81
+ * @see https://dataclient.io/rest/api/RestEndpoint#unshift
82
+ */
83
+ unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
84
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
85
+ }>;
86
+ /** Endpoint that assigns newly created entities to their respective keys in this Collection
87
+ * @see https://dataclient.io/rest/api/RestEndpoint#assign
88
+ */
89
+ assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
90
+ body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
91
+ }>;
92
+ }
93
+ export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
94
+ body?: any;
95
+ path?: string;
96
+ schema?: Schema;
97
+ method?: string;
98
+ }, F extends FetchFunction> = RestEndpointOptions<OptionsToFunction<O, E, F>, 'schema' extends keyof O ? Extract<O['schema'], Schema | undefined> : E['schema']> & Partial<Omit<E, KeyofRestEndpoint | keyof PartialRestGenerics | keyof RestEndpointOptions>>;
99
+ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
100
+ body?: any;
101
+ paginationField?: string;
102
+ }, F extends FetchFunction> = 'path' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
103
+ path: Exclude<O['path'], undefined>;
104
+ body: 'body' extends keyof O ? O['body'] : E['body'];
105
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
106
+ method: 'method' extends keyof O ? O['method'] : E['method'];
107
+ paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField'];
108
+ }> : 'body' extends keyof O ? RestType<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
109
+ path: E['path'];
110
+ body: O['body'];
111
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
112
+ method: 'method' extends keyof O ? O['method'] : E['method'];
113
+ paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract<E['paginationField'], string>;
114
+ }> : 'searchParams' extends keyof O ? RestType<O['searchParams'] extends undefined ? 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'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>, {
115
+ path: E['path'];
116
+ body: E['body'];
117
+ searchParams: O['searchParams'];
118
+ method: 'method' extends keyof O ? O['method'] : E['method'];
119
+ paginationField: 'paginationField' extends keyof O ? O['paginationField'] : Extract<E['paginationField'], string>;
120
+ }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
121
+ path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
122
+ body: 'body' extends keyof O ? O['body'] : E['body'];
123
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
124
+ method: 'method' extends keyof O ? O['method'] : E['method'];
125
+ paginationField: 'paginationField' extends keyof O ? O['paginationField'] : E['paginationField'];
126
+ }>;
127
+ export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
128
+ getPage?: unknown;
129
+ }> = OptionsToRestEndpoint<O, E & (E extends {
130
+ getPage: {
131
+ paginationField: string;
132
+ };
133
+ } ? {
134
+ paginationField: E['getPage']['paginationField'];
135
+ } : unknown), RestInstance<(...args: Parameters<E>) => O['process'] extends {} ? Promise<ReturnType<O['process']>> : ReturnType<E>, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect']>> & Omit<O, KeyofRestEndpoint> & Omit<E, KeyofRestEndpoint | keyof O>;
136
+ export interface PartialRestGenerics {
137
+ /** @see https://resthooks.io/rest/api/RestEndpoint#path */
138
+ readonly path?: string;
139
+ /** @see https://resthooks.io/rest/api/RestEndpoint#schema */
140
+ readonly schema?: Schema | undefined;
141
+ /** @see https://resthooks.io/rest/api/RestEndpoint#method */
142
+ readonly method?: string;
143
+ /** Only used for types */
144
+ /** @see https://dataclient.io/rest/api/RestEndpoint#body */
145
+ body?: any;
146
+ /** Only used for types */
147
+ /** @see https://dataclient.io/rest/api/RestEndpoint#searchParams */
148
+ searchParams?: any;
149
+ /** @see https://dataclient.io/rest/api/RestEndpoint#paginationfield */
150
+ readonly paginationField?: string;
151
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
152
+ process?(value: any, ...args: any): any;
153
+ }
154
+ export interface RestGenerics extends PartialRestGenerics {
155
+ readonly path: string;
156
+ }
157
+ export type PaginationEndpoint<E extends FetchFunction & RestGenerics & {
158
+ sideEffect?: true | undefined;
159
+ }, A extends any[]> = RestInstanceBase<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
160
+ searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
161
+ }>;
162
+ export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
163
+ sideEffect?: true | undefined;
164
+ }, C extends string> = RestInstanceBase<ParamFetchNoBody<{
165
+ [K in C]: string | number | boolean;
166
+ } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
167
+ searchParams: {
168
+ [K in C]: string | number | boolean;
169
+ } & E['searchParams'];
170
+ }> & {
171
+ paginationField: C;
172
+ };
173
+ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
174
+ path: string;
175
+ body: any;
176
+ searchParams?: any;
177
+ } = {
178
+ path: string;
179
+ body: any;
180
+ }> = RestInstanceBase<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<O['path'], undefined>> : PathArgs<Exclude<O['path'], undefined>>, O['body'], ResolveType<F>>, S, true, Omit<O, 'method'> & {
181
+ method: 'POST';
182
+ }>;
183
+ type OptionsToAdderBodyArgument<O extends {
184
+ body?: any;
185
+ }> = 'body' extends keyof O ? O['body'] : any;
186
+ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
187
+ urlPrefix?: string;
188
+ requestInit?: RequestInit;
189
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
190
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
191
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
192
+ parseResponse?(response: Response): Promise<any>;
193
+ sideEffect?: true | undefined;
194
+ name?: string;
195
+ signal?: AbortSignal;
196
+ fetch?: F;
197
+ key?(...args: Parameters<F>): string;
198
+ url?(...args: Parameters<F>): string;
199
+ update?: EndpointUpdateFunction<F, S>;
200
+ }
201
+ export type RestEndpointConstructorOptions<O extends RestGenerics = any> = RestEndpointOptions<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : O extends {
202
+ sideEffect: true;
203
+ } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any>, O['schema']>;
204
+ export interface RestEndpointConstructor {
205
+ new <O extends RestGenerics = any>({ method, sideEffect, name, ...options }: RestEndpointConstructorOptions<O> & Readonly<O>): RestInstance<RestFetch<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : O extends {
206
+ sideEffect: true;
207
+ } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any>, 'schema' extends keyof O ? O['schema'] : undefined, 'sideEffect' extends keyof O ? Extract<O['sideEffect'], undefined | true> : MethodToSide<O['method']>, 'method' extends keyof O ? O : O & {
208
+ method: O extends {
209
+ sideEffect: true;
210
+ } ? 'POST' : 'GET';
211
+ }>;
212
+ readonly prototype: RestInstanceBase;
213
+ }
214
+ export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
215
+ /** RestEndpoint types simplified */
216
+ export type RestType<UrlParams = any, Body = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
217
+ path: string;
218
+ body?: any;
219
+ searchParams?: any;
220
+ paginationField?: string;
221
+ } = {
222
+ path: string;
223
+ paginationField: string;
224
+ }> = IfTypeScriptLooseNull<RestInstance<keyof UrlParams extends never ? (this: EndpointInstanceInterface, body?: Body) => Promise<R> : {} extends UrlParams ? ((this: EndpointInstanceInterface, body?: Body) => Promise<R>) | ((this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>) : (this: EndpointInstanceInterface, params: UrlParams, body?: Body) => Promise<R>, S, M, O>, Body extends {} ? RestTypeWithBody<UrlParams, S, M, Body, R, O> : RestTypeNoBody<UrlParams, S, M, R, O>>;
225
+ export type RestTypeWithBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, Body = any, R = any, O extends {
226
+ path: string;
227
+ body?: any;
228
+ searchParams?: any;
229
+ } = {
230
+ path: string;
231
+ body: any;
232
+ }> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
233
+ export type RestTypeNoBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
234
+ path: string;
235
+ body?: undefined;
236
+ searchParams?: any;
237
+ } = {
238
+ path: string;
239
+ body: undefined;
240
+ }> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
241
+ /** Simple parameters, and body fetch functions */
242
+ export type RestFetch<UrlParams, Body = {}, Resolve = any> = IfTypeScriptLooseNull<ParamFetchNoBody<UrlParams, Resolve> | ParamFetchWithBody<UrlParams, Body, Resolve>, Body extends {} ? ParamFetchWithBody<UrlParams, Body, Resolve> : ParamFetchNoBody<UrlParams, Resolve>>;
243
+ export type ParamFetchWithBody<P, B = {}, R = any> = P extends undefined ? (this: EndpointInstanceInterface, body: B) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface, body: B) => Promise<R> : ((this: EndpointInstanceInterface, params: P, body: B) => Promise<R>) | ((this: EndpointInstanceInterface, body: B) => Promise<R>) : (this: EndpointInstanceInterface, params: P, body: B) => Promise<R>;
244
+ export type ParamFetchNoBody<P, R = any> = P extends undefined ? (this: EndpointInstanceInterface) => Promise<R> : {} extends P ? keyof P extends never ? (this: EndpointInstanceInterface) => Promise<R> : ((this: EndpointInstanceInterface, params: P) => Promise<R>) | ((this: EndpointInstanceInterface) => Promise<R>) : (this: EndpointInstanceInterface, params: P) => Promise<R>;
245
+ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
246
+ export type KeyofRestEndpoint = keyof RestInstance;
247
+ export type FromFallBack<K extends keyof E, O, E> = K extends keyof O ? O[K] : E[K];
248
+ export type FetchMutate<A extends readonly any[] = [
249
+ any,
250
+ {}
251
+ ] | [
252
+ {}
253
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
254
+ export type FetchGet<A extends readonly any[] = [
255
+ any
256
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
257
+ export type Defaults<O, D> = {
258
+ [K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
259
+ };
260
+ export type GetEndpoint<O extends {
261
+ readonly path: string;
262
+ readonly schema: Schema;
263
+ /** Only used for types */
264
+ readonly searchParams?: any;
265
+ readonly paginationField?: string;
266
+ } = {
267
+ path: string;
268
+ schema: Schema;
269
+ }> = RestTypeNoBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], undefined, any, O & {
270
+ method: 'GET';
271
+ }>;
272
+ export type MutateEndpoint<O extends {
273
+ readonly path: string;
274
+ readonly schema: Schema;
275
+ /** Only used for types */
276
+ readonly searchParams?: any;
277
+ /** Only used for types */
278
+ readonly body?: any;
279
+ } = {
280
+ path: string;
281
+ body: any;
282
+ schema: Schema;
283
+ }> = RestTypeWithBody<'searchParams' extends keyof O ? O['searchParams'] extends undefined ? PathArgs<O['path']> : O['searchParams'] & PathArgs<O['path']> : PathArgs<O['path']>, O['schema'], true, O['body'], any, O & {
284
+ body: any;
285
+ method: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
286
+ }>;
287
+ export {};
288
+ //# sourceMappingURL=RestEndpointTypes.d.ts.map