@data-client/rest 0.3.1 → 0.4.1

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 (60) hide show
  1. package/README.md +1 -1
  2. package/legacy/OptionsToFunction.js +1 -1
  3. package/legacy/createResource.js +1 -1
  4. package/legacy/extractCollection.js +1 -1
  5. package/legacy/extractObject.js +2 -0
  6. package/legacy/index.js +1 -1
  7. package/lib/OptionsToFunction.d.ts +8 -3
  8. package/lib/OptionsToFunction.d.ts.map +1 -1
  9. package/lib/OptionsToFunction.js +1 -1
  10. package/lib/RestEndpoint.d.ts +120 -43
  11. package/lib/createResource.d.ts +2 -0
  12. package/lib/createResource.d.ts.map +1 -1
  13. package/lib/createResource.js +1 -1
  14. package/lib/extractCollection.d.ts +7 -4
  15. package/lib/extractCollection.d.ts.map +1 -1
  16. package/lib/extractCollection.js +1 -1
  17. package/lib/extractObject.d.ts +6 -0
  18. package/lib/extractObject.d.ts.map +1 -0
  19. package/lib/extractObject.js +2 -0
  20. package/lib/index.d.ts +1 -1
  21. package/lib/index.d.ts.map +1 -1
  22. package/lib/index.js +1 -1
  23. package/package.json +17 -5
  24. package/src/OptionsToFunction.ts +23 -9
  25. package/src/RestEndpoint.d.ts +120 -43
  26. package/src/__tests__/RestEndpoint.ts +10 -1
  27. package/src/__tests__/types.test.ts +6 -2
  28. package/src/createResource.ts +6 -0
  29. package/src/extractCollection.ts +16 -14
  30. package/src/extractObject.ts +7 -0
  31. package/src/index.ts +1 -0
  32. package/ts4.0/OptionsToFunction.d.ts +7 -4
  33. package/ts4.0/RestEndpoint.d.ts +80 -25
  34. package/ts4.0/createResource.d.ts +2 -0
  35. package/ts4.0/deleteme.d.ts +0 -0
  36. package/ts4.0/extractCollection.d.ts +7 -4
  37. package/ts4.0/extractObject.d.ts +1 -0
  38. package/ts4.0/index.d.ts +1 -1
  39. package/ts4.1/NetworkError.d.ts +11 -0
  40. package/ts4.1/OptionsToFunction.d.ts +12 -0
  41. package/ts4.1/RestEndpoint.d.ts +281 -0
  42. package/ts4.1/RestEndpointTypeHelp.d.ts +10 -0
  43. package/ts4.1/RestHelpers.d.ts +7 -0
  44. package/ts4.1/createResource.d.ts +113 -0
  45. package/ts4.1/deleteme.d.ts +0 -0
  46. package/ts4.1/extractCollection.d.ts +14 -0
  47. package/ts4.1/extractObject.d.ts +6 -0
  48. package/ts4.1/getArrayPath.d.ts +3 -0
  49. package/ts4.1/hookifyResource.d.ts +14 -0
  50. package/ts4.1/index.d.ts +14 -0
  51. package/ts4.1/mapCollection.d.ts +16 -0
  52. package/ts4.1/next/index.d.ts +6 -0
  53. package/ts4.1/paginatedCollections.d.ts +3 -0
  54. package/ts4.1/paginationUpdate.d.ts +10 -0
  55. package/ts4.1/paramsToString.d.ts +2 -0
  56. package/ts4.1/paramsToString.native.d.ts +2 -0
  57. package/ts4.1/pathTypes.d.ts +21 -0
  58. package/ts4.1/resourceTypes.d.ts +41 -0
  59. package/ts4.1/test-fixtures.d.ts +141 -0
  60. package/ts4.1/utiltypes.d.ts +21 -0
@@ -0,0 +1,281 @@
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
+ /* fetch lifecycles */
34
+ /* before-fetch */
35
+ url(...args: Parameters<F>): string;
36
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
37
+ getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
38
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
39
+ getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
40
+ /* after-fetch */
41
+ /** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
42
+ fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
43
+ /** @see https://resthooks.io/rest/api/RestEndpoint#parseResponse */
44
+ parseResponse(response: Response): Promise<any>;
45
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
46
+ process(value: any, ...args: Parameters<F>): ResolveType<F>;
47
+ /* utilities */
48
+ /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
49
+ testKey(key: string): boolean;
50
+ /* extenders */
51
+ // TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
52
+ // this is just a hack to handle when no members of PartialRestGenerics are present
53
+ // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
54
+ // should be used from the other (due to same problem with every member being partial)
55
+ /** Creates a child endpoint that inherits from this while overriding provided `options`.
56
+ * @see https://dataclient.io/rest/api/RestEndpoint#extend
57
+ */
58
+ extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<O, E, F> & O>): RestExtendedEndpoint<O, E>;
59
+ }
60
+ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
61
+ path: string;
62
+ body?: any;
63
+ searchParams?: any;
64
+ method?: string;
65
+ } = {
66
+ path: string;
67
+ }> extends RestInstanceBase<F, S, M, O> {
68
+ /** Endpoint to append the next page extending a list for pagination
69
+ * @see https://dataclient.io/rest/api/RestEndpoint#paginated
70
+ */
71
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
72
+ ...Parameters<E>
73
+ ]): PaginationEndpoint<E, A>;
74
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
75
+ /** Endpoint that pushes (place at end) a newly created entity to this Collection
76
+ * @see https://dataclient.io/rest/api/RestEndpoint#push
77
+ */
78
+ push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
79
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
80
+ }>;
81
+ /** Endpoint that unshifts (place at start) a newly created entity to this Collection
82
+ * @see https://dataclient.io/rest/api/RestEndpoint#unshift
83
+ */
84
+ unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
85
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
86
+ }>;
87
+ /** Endpoint that assigns newly created entities to their respective keys in this Collection
88
+ * @see https://dataclient.io/rest/api/RestEndpoint#assign
89
+ */
90
+ assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
91
+ body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
92
+ }>;
93
+ }
94
+ export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
95
+ body?: any;
96
+ path?: string;
97
+ schema?: Schema;
98
+ method?: string;
99
+ }, 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>>;
100
+ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
101
+ body?: any;
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
+ }> : '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>, {
108
+ path: E['path'];
109
+ body: O['body'];
110
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
111
+ method: 'method' extends keyof O ? O['method'] : E['method'];
112
+ }> : '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>, {
113
+ path: E['path'];
114
+ body: E['body'];
115
+ searchParams: O['searchParams'];
116
+ method: 'method' extends keyof O ? O['method'] : E['method'];
117
+ }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
118
+ path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
119
+ body: 'body' extends keyof O ? O['body'] : E['body'];
120
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
121
+ method: 'method' extends keyof O ? O['method'] : E['method'];
122
+ }>;
123
+ export type RestExtendedEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase> = OptionsToRestEndpoint<O, E, 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>;
124
+ export interface PartialRestGenerics {
125
+ readonly path?: string;
126
+ readonly schema?: Schema | undefined;
127
+ readonly method?: string;
128
+ /** Only used for types */
129
+ body?: any;
130
+ /** Only used for types */
131
+ searchParams?: any;
132
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
133
+ process?(value: any, ...args: any): any;
134
+ }
135
+ export interface RestGenerics extends PartialRestGenerics {
136
+ readonly path: string;
137
+ }
138
+ export type PaginationEndpoint<E extends FetchFunction & RestGenerics & {
139
+ sideEffect?: true | undefined;
140
+ }, A extends any[]> = RestInstance<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
141
+ searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
142
+ }>;
143
+ export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
144
+ sideEffect?: true | undefined;
145
+ }, C extends string> = RestInstance<ParamFetchNoBody<{
146
+ [K in C]: string | number | boolean;
147
+ } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
148
+ searchParams: {
149
+ [K in C]: string | number | boolean;
150
+ } & E['searchParams'];
151
+ }>;
152
+ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
153
+ path: string;
154
+ body: any;
155
+ searchParams?: any;
156
+ } = {
157
+ path: string;
158
+ body: any;
159
+ }> = 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, O & {
160
+ method: 'POST';
161
+ }>;
162
+ type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
163
+ body: any;
164
+ } : O & {
165
+ body: undefined;
166
+ };
167
+ type OptionsToAdderBodyArgument<O extends {
168
+ body?: any;
169
+ }> = 'body' extends keyof O ? O['body'] : any;
170
+ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
171
+ urlPrefix?: string;
172
+ requestInit?: RequestInit;
173
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
174
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
175
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
176
+ parseResponse?(response: Response): Promise<any>;
177
+ sideEffect?: true | undefined;
178
+ name?: string;
179
+ signal?: AbortSignal;
180
+ fetch?: F;
181
+ key?(...args: Parameters<F>): string;
182
+ url?(...args: Parameters<F>): string;
183
+ update?: EndpointUpdateFunction<F, S>;
184
+ }
185
+ 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 {
186
+ sideEffect: true;
187
+ } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
188
+ export interface RestEndpointConstructor {
189
+ 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 {
190
+ sideEffect: true;
191
+ } ? '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 & {
192
+ method: O extends {
193
+ sideEffect: true;
194
+ } ? 'POST' : 'GET';
195
+ }>;
196
+ readonly prototype: RestInstanceBase;
197
+ }
198
+ /** Simplifies endpoint definitions that follow REST patterns
199
+ *
200
+ * @see https://resthooks.io/rest/api/RestEndpoint
201
+ */
202
+ export declare let RestEndpoint: RestEndpointConstructor;
203
+ export default RestEndpoint;
204
+ export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
205
+ /** RestEndpoint types simplified */
206
+ export type RestType<UrlParams = any, Body = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
207
+ path: string;
208
+ body?: any;
209
+ searchParams?: any;
210
+ } = {
211
+ path: string;
212
+ }> = 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>>;
213
+ 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 {
214
+ path: string;
215
+ body?: any;
216
+ searchParams?: any;
217
+ } = {
218
+ path: string;
219
+ body: any;
220
+ }> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
221
+ export type RestTypeNoBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any /*Denormalize<S>*/, O extends {
222
+ path: string;
223
+ body?: undefined;
224
+ searchParams?: any;
225
+ } = {
226
+ path: string;
227
+ body: undefined;
228
+ }> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
229
+ /** Simple parameters, and body fetch functions */
230
+ export type RestFetch<UrlParams,
231
+ // eslint-disable-next-line @typescript-eslint/ban-types
232
+ Body = {}, Resolve = any> = IfTypeScriptLooseNull<ParamFetchNoBody<UrlParams, Resolve> | ParamFetchWithBody<UrlParams, Body, Resolve>, Body extends {} ? ParamFetchWithBody<UrlParams, Body, Resolve> : ParamFetchNoBody<UrlParams, Resolve>>;
233
+ export type ParamFetchWithBody<P, B = {}, R = any> =
234
+ // we must always allow undefined in a union and give it a type without params
235
+ 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>;
236
+ export type ParamFetchNoBody<P, R = any> =
237
+ // we must always allow undefined in a union and give it a type without params
238
+ 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>;
239
+ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
240
+ export type KeyofRestEndpoint = keyof RestInstance;
241
+ export type FromFallBack<K extends keyof E, O, E> = K extends keyof O ? O[K] : E[K];
242
+ export type FetchMutate<A extends readonly any[] = [
243
+ any,
244
+ {}
245
+ ]
246
+ // eslint-disable-next-line @typescript-eslint/ban-types
247
+ | [
248
+ {}
249
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
250
+ export type FetchGet<A extends readonly any[] = [
251
+ any
252
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
253
+ export type Defaults<O, D> = {
254
+ [K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
255
+ };
256
+ export type GetEndpoint<O extends {
257
+ readonly path: string;
258
+ readonly schema: Schema;
259
+ /** Only used for types */
260
+ readonly searchParams?: any;
261
+ } = {
262
+ path: string;
263
+ schema: Schema;
264
+ }> = 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 & {
265
+ method: 'GET';
266
+ }>;
267
+ export type MutateEndpoint<O extends {
268
+ readonly path: string;
269
+ readonly schema: Schema;
270
+ /** Only used for types */
271
+ readonly searchParams?: any;
272
+ /** Only used for types */
273
+ readonly body?: any;
274
+ } = {
275
+ path: string;
276
+ body: any;
277
+ schema: Schema;
278
+ }> = 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 & {
279
+ body: any;
280
+ method: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
281
+ }>;
@@ -0,0 +1,10 @@
1
+ import type { FetchFunction, ResolveType, Normalize } from '@data-client/endpoint';
2
+ export type EndpointUpdateFunction<Source extends FetchFunction, Schema, Updaters extends Record<string, any> = Record<string, any>> = (source: ResultEntry<Source & {
3
+ schema: Schema;
4
+ }>, ...args: Parameters<Source>) => {
5
+ [K in keyof Updaters]: (result: Updaters[K]) => Updaters[K];
6
+ };
7
+ export type ResultEntry<E extends FetchFunction & {
8
+ schema: any;
9
+ }> = E['schema'] extends undefined | null ? ResolveType<E> : Normalize<E['schema']>;
10
+ //# sourceMappingURL=RestEndpointTypeHelp.d.ts.map
@@ -0,0 +1,7 @@
1
+ import { PathFunction } from 'path-to-regexp';
2
+ import { ShortenPath } from './pathTypes.js';
3
+ export declare function getUrlBase(path: string): PathFunction;
4
+ export declare function getUrlTokens(path: string): Set<string>;
5
+ export declare function isPojo(obj: unknown): obj is Record<string, any>;
6
+ export declare function shortenPath<S extends string>(path: S): ShortenPath<S>;
7
+ //# sourceMappingURL=RestHelpers.d.ts.map
@@ -0,0 +1,113 @@
1
+ import { schema } from '@data-client/endpoint';
2
+ import type { Denormalize } from '@data-client/endpoint';
3
+ import { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
4
+ import { ResourceGenerics, ResourceOptions } from './resourceTypes.js';
5
+ import { GetEndpoint, MutateEndpoint, PaginationFieldEndpoint, RestTypeNoBody } from './RestEndpoint.js';
6
+ /** Creates collection of Endpoints for common operations on a given data/schema.
7
+ *
8
+ * @see https://resthooks.io/rest/api/createResource
9
+ */
10
+ export default function createResource<O extends ResourceGenerics>({ path, schema, Endpoint, optimistic, paginationField, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
11
+ export interface Resource<O extends ResourceGenerics = {
12
+ path: ResourcePath;
13
+ schema: any;
14
+ }> {
15
+ /** Get a singular item
16
+ *
17
+ * @see https://resthooks.io/rest/api/createResource#get
18
+ */
19
+ get: GetEndpoint<{
20
+ path: O['path'];
21
+ schema: O['schema'];
22
+ }>;
23
+ /** Get a list of item
24
+ *
25
+ * @see https://resthooks.io/rest/api/createResource#getlist
26
+ */
27
+ getList: 'searchParams' extends keyof O ? GetEndpoint<{
28
+ path: ShortenPath<O['path']>;
29
+ schema: schema.Collection<[
30
+ O['schema']
31
+ ]>;
32
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
33
+ searchParams: O['searchParams'];
34
+ }> : GetEndpoint<{
35
+ path: ShortenPath<O['path']>;
36
+ schema: schema.Collection<[
37
+ O['schema']
38
+ ]>;
39
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
40
+ searchParams: Record<string, number | string | boolean> | undefined;
41
+ }>;
42
+ /** Get a list of item
43
+ *
44
+ * @see https://dataclient.io/rest/api/createResource#getNextPage
45
+ */
46
+ getNextPage: 'paginationField' extends keyof O ? PaginationFieldEndpoint<'searchParams' extends keyof O ? GetEndpoint<{
47
+ path: ShortenPath<O['path']>;
48
+ schema: schema.Collection<[
49
+ O['schema']
50
+ ]>;
51
+ searchParams: O['searchParams'];
52
+ }> : GetEndpoint<{
53
+ path: ShortenPath<O['path']>;
54
+ schema: schema.Collection<[
55
+ O['schema']
56
+ ]>;
57
+ searchParams: Record<string, number | string | boolean> | undefined;
58
+ }>, Exclude<O['paginationField'], undefined>> : undefined;
59
+ /** Create a new item (POST)
60
+ *
61
+ * @see https://resthooks.io/rest/api/createResource#create
62
+ */
63
+ create: 'searchParams' extends keyof O ? MutateEndpoint<{
64
+ path: ShortenPath<O['path']>;
65
+ schema: schema.Collection<[
66
+ O['schema']
67
+ ]>['push'];
68
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
69
+ searchParams: O['searchParams'];
70
+ }> : MutateEndpoint<{
71
+ path: ShortenPath<O['path']>;
72
+ schema: schema.Collection<[
73
+ O['schema']
74
+ ]>['push'];
75
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
76
+ }>;
77
+ /** Update an item (PUT)
78
+ *
79
+ * @see https://resthooks.io/rest/api/createResource#update
80
+ */
81
+ update: 'body' extends keyof O ? MutateEndpoint<{
82
+ path: O['path'];
83
+ body: O['body'];
84
+ schema: O['schema'];
85
+ }> : MutateEndpoint<{
86
+ path: O['path'];
87
+ body: Partial<Denormalize<O['schema']>>;
88
+ schema: O['schema'];
89
+ }>;
90
+ /** Update an item (PATCH)
91
+ *
92
+ * @see https://resthooks.io/rest/api/createResource#partialupdate
93
+ */
94
+ partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
95
+ path: O['path'];
96
+ body: Partial<O['body']>;
97
+ schema: O['schema'];
98
+ }> : MutateEndpoint<{
99
+ path: O['path'];
100
+ body: Partial<Denormalize<O['schema']>>;
101
+ schema: O['schema'];
102
+ }>;
103
+ /** Delete an item (DELETE)
104
+ *
105
+ * @see https://resthooks.io/rest/api/createResource#delete
106
+ */
107
+ delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
108
+ process: any;
109
+ } ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
110
+ path: O['path'];
111
+ }>;
112
+ }
113
+ //# sourceMappingURL=createResource.d.ts.map
File without changes
@@ -0,0 +1,14 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+ import { ExtractObject } from './extractObject.js';
3
+ export default function extractCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): ExtractCollection<S> | undefined;
4
+ export type ExtractCollection<S extends Schema | undefined> = S extends {
5
+ push: any;
6
+ unshift: any;
7
+ assign: any;
8
+ schema: Schema;
9
+ } ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
10
+ [K: string]: any;
11
+ }> ? never : S extends {
12
+ [K: string]: Schema;
13
+ } ? ExtractObject<S> : never;
14
+ //# sourceMappingURL=extractCollection.d.ts.map
@@ -0,0 +1,6 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ import { ExtractCollection } from './extractCollection.js';
3
+ export type ExtractObject<S extends Record<string, any>> = {
4
+ [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
5
+ }[keyof S];
6
+ //# sourceMappingURL=extractObject.d.ts.map
@@ -0,0 +1,3 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ export default function getArrayPath(s: Schema | undefined): string[] | false;
3
+ //# sourceMappingURL=getArrayPath.d.ts.map
@@ -0,0 +1,14 @@
1
+ import { EndpointInterface } from '@data-client/endpoint';
2
+ export interface HookableEndpointInterface extends EndpointInterface {
3
+ extend(...args: any): HookableEndpointInterface;
4
+ }
5
+ /** Turns a collection of Endpoints (Resource) into a collection of hooks.
6
+ * This is useful for Endpoints that need hooks to prepare their fetch requests.
7
+ *
8
+ * @see https://resthooks.io/rest/api/hookifyResource
9
+ */
10
+ export default function hookifyResource<R extends {}>(resource: R, useRequestInit: () => RequestInit): HookResource<R>;
11
+ export type HookResource<R extends {}> = {
12
+ [K in Extract<keyof R, string> as `use${Capitalize<K>}`]: () => R[K];
13
+ };
14
+ //# sourceMappingURL=hookifyResource.d.ts.map
@@ -0,0 +1,14 @@
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';
3
+ export { default as createResource } from './createResource.js';
4
+ export type { Resource } from './createResource.js';
5
+ export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
6
+ export { default as hookifyResource } from './hookifyResource.js';
7
+ export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
8
+ export { default as NetworkError } from './NetworkError.js';
9
+ export { default as paginationUpdate } from './paginationUpdate.js';
10
+ export type { OptionsToFunction } from './OptionsToFunction.js';
11
+ export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
12
+ export * from '@data-client/endpoint';
13
+ export * from './RestEndpoint.js';
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,16 @@
1
+ import { Schema, schema } from '@data-client/endpoint';
2
+ export default function mapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined>(s: S, mapper: M): S extends schema.Collection ? ReturnType<typeof mapper<S>> : S extends schema.Object<infer T> ? {
3
+ [K in keyof T]: T[K] extends Schema ? typeof mapCollection<M, T[K]> : T[K];
4
+ } : S extends {
5
+ [K: string]: any;
6
+ } ? {
7
+ [K in keyof S]: S[K] extends Schema ? typeof mapCollection<M, S[K]> : S[K];
8
+ } : S;
9
+ type MapCollection<M extends <C extends schema.Collection>(collection: C) => any, S extends Schema | undefined> = S extends schema.Collection ? ReturnType<M> : S extends schema.Object<infer T> ? MapCollection<M, T> : S extends {
10
+ [K: string]: any;
11
+ } ? MapObject<M, S> : never;
12
+ export type MapObject<M extends (collection: schema.Collection) => any, S extends Record<string, any>> = {
13
+ [K in keyof S]: S[K] extends Schema ? MapCollection<M, S[K]> : S[K];
14
+ };
15
+ export {};
16
+ //# sourceMappingURL=mapCollection.d.ts.map
@@ -0,0 +1,6 @@
1
+ export * from '../RestEndpoint.js';
2
+ export { default as RestEndpoint } from '../RestEndpoint.js';
3
+ export { default as createResource } from '../createResource.js';
4
+ export type { Resource } from '../createResource.js';
5
+ export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,3 @@
1
+ export declare function paginatedMerge(existing: any[], incoming: any[]): any[];
2
+ export declare const paginatedFilter: <C extends (...args: readonly any[]) => readonly any[]>(removeCursor: C) => (...args: Parameters<C>) => (collectionKey: Record<string, string>) => boolean;
3
+ //# sourceMappingURL=paginatedCollections.d.ts.map
@@ -0,0 +1,10 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+ export default function paginationUpdate<E extends {
3
+ schema: Schema;
4
+ key: any;
5
+ } & ((...args: any) => Promise<any>), A extends any[]>(endpoint: E, removeCursor: (...args: A) => readonly [
6
+ ...Parameters<typeof endpoint>
7
+ ]): (newPage: any, ...args: A) => {
8
+ [x: number]: (existing: any) => any;
9
+ };
10
+ //# sourceMappingURL=paginationUpdate.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default function paramsToString(searchParams: Readonly<Record<string, string | number | boolean>>): string;
2
+ //# sourceMappingURL=paramsToString.d.ts.map
@@ -0,0 +1,2 @@
1
+ export default function paramsToString(searchParams: Readonly<Record<string, string | number>>): string;
2
+ //# sourceMappingURL=paramsToString.native.d.ts.map
@@ -0,0 +1,21 @@
1
+ type OnlyOptional<S extends string> = S extends `${infer K}?` ? K : never;
2
+ type OnlyRequired<S extends string> = S extends `${string}?` ? never : S;
3
+ /** Computes the union of keys for a path string */
4
+ export type PathKeys<S extends string> = string extends S ? string : S extends `${infer A}\\:${infer B}` ? PathKeys<A> | PathKeys<B> : S extends `${infer A}\\?${infer B}` ? PathKeys<A> | PathKeys<B> : PathSplits<S>;
5
+ type PathSplits<S extends string> = S extends `${string}:${infer K}${'/' | ',' | '%' | '&'}${infer R}` ? PathSplits<`:${K}`> | PathSplits<R> : S extends `${string}:${infer K}:${infer R}` ? PathSplits<`:${K}`> | PathSplits<`:${R}`> : S extends `${string}:${infer K}` ? K : never;
6
+ /** Parameters for a given path */
7
+ export type PathArgs<S extends string> = PathKeys<S> extends never ? unknown : KeysToArgs<PathKeys<S>>;
8
+ export type KeysToArgs<Key extends string> = {
9
+ [K in Key as OnlyOptional<K>]?: string | number;
10
+ } & (OnlyRequired<Key> extends never ? unknown : {
11
+ [K in Key as OnlyRequired<K>]: string | number;
12
+ });
13
+ export type PathArgsAndSearch<S extends string> = OnlyRequired<PathKeys<S>> extends never ? Record<string, number | string | boolean> | undefined : {
14
+ [K in PathKeys<S> as OnlyRequired<K>]: string | number;
15
+ } & Record<string, number | string>;
16
+ /** Removes the last :token */
17
+ export type ShortenPath<S extends string> = string extends S ? string : S extends `${infer B}:${infer R}` ? TrimColon<`${B}:${ShortenPath<R>}`> : '';
18
+ type TrimColon<S extends string> = string extends S ? string : S extends `${infer R}:` ? R : S;
19
+ export type ResourcePath = string;
20
+ export {};
21
+ //# sourceMappingURL=pathTypes.d.ts.map
@@ -0,0 +1,41 @@
1
+ import type { Schema } from '@data-client/endpoint';
2
+ import type { ResourcePath } from './pathTypes.js';
3
+ import RestEndpoint from './RestEndpoint.js';
4
+ export interface ResourceGenerics {
5
+ /** @see https://resthooks.io/rest/api/createResource#path */
6
+ readonly path: ResourcePath;
7
+ /** @see https://resthooks.io/rest/api/createResource#schema */
8
+ readonly schema: Schema;
9
+ /** @see https://resthooks.io/rest/api/createResource#paginationfield */
10
+ readonly paginationField?: string;
11
+ /** Only used for types */
12
+ /** @see https://dataclient.io/rest/api/createResource#body */
13
+ readonly body?: any;
14
+ /** Only used for types */
15
+ /** @see https://resthooks.io/rest/api/createResource#searchParams */
16
+ readonly searchParams?: any;
17
+ }
18
+ export interface ResourceOptions {
19
+ /** @see https://resthooks.io/rest/api/createResource#endpoint */
20
+ Endpoint?: typeof RestEndpoint;
21
+ /** @see https://resthooks.io/rest/api/createResource#optimistic */
22
+ optimistic?: boolean;
23
+ /** @see https://resthooks.io/rest/api/createResource#urlprefix */
24
+ urlPrefix?: string;
25
+ requestInit?: RequestInit;
26
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
27
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
28
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
29
+ parseResponse?(response: Response): Promise<any>;
30
+ /** Default data expiry length, will fall back to NetworkManager default if not defined */
31
+ readonly dataExpiryLength?: number;
32
+ /** Default error expiry length, will fall back to NetworkManager default if not defined */
33
+ readonly errorExpiryLength?: number;
34
+ /** Poll with at least this frequency in miliseconds */
35
+ readonly pollFrequency?: number;
36
+ /** Marks cached resources as invalid if they are stale */
37
+ readonly invalidIfStale?: boolean;
38
+ /** Determines whether to throw or fallback to */
39
+ errorPolicy?(error: any): 'hard' | 'soft' | undefined;
40
+ }
41
+ //# sourceMappingURL=resourceTypes.d.ts.map