@data-client/rest 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/legacy/OptionsToFunction.js +1 -1
  2. package/legacy/createResource.js +1 -1
  3. package/legacy/extractCollection.js +1 -1
  4. package/legacy/extractObject.js +2 -0
  5. package/lib/OptionsToFunction.d.ts +8 -3
  6. package/lib/OptionsToFunction.d.ts.map +1 -1
  7. package/lib/OptionsToFunction.js +1 -1
  8. package/lib/RestEndpoint.d.ts +91 -31
  9. package/lib/createResource.d.ts +2 -0
  10. package/lib/createResource.d.ts.map +1 -1
  11. package/lib/createResource.js +1 -1
  12. package/lib/extractCollection.d.ts +7 -4
  13. package/lib/extractCollection.d.ts.map +1 -1
  14. package/lib/extractCollection.js +1 -1
  15. package/lib/extractObject.d.ts +6 -0
  16. package/lib/extractObject.d.ts.map +1 -0
  17. package/lib/extractObject.js +2 -0
  18. package/package.json +17 -5
  19. package/src/OptionsToFunction.ts +23 -9
  20. package/src/RestEndpoint.d.ts +91 -31
  21. package/src/__tests__/RestEndpoint.ts +10 -1
  22. package/src/__tests__/types.test.ts +6 -2
  23. package/src/createResource.ts +6 -0
  24. package/src/extractCollection.ts +16 -14
  25. package/src/extractObject.ts +7 -0
  26. package/ts4.0/OptionsToFunction.d.ts +7 -4
  27. package/ts4.0/RestEndpoint.d.ts +59 -22
  28. package/ts4.0/createResource.d.ts +2 -0
  29. package/ts4.0/deleteme.d.ts +0 -0
  30. package/ts4.0/extractCollection.d.ts +7 -4
  31. package/ts4.0/extractObject.d.ts +1 -0
  32. package/ts4.1/NetworkError.d.ts +11 -0
  33. package/ts4.1/OptionsToFunction.d.ts +12 -0
  34. package/ts4.1/RestEndpoint.d.ts +263 -0
  35. package/ts4.1/RestEndpointTypeHelp.d.ts +10 -0
  36. package/ts4.1/RestHelpers.d.ts +7 -0
  37. package/ts4.1/createResource.d.ts +113 -0
  38. package/ts4.1/deleteme.d.ts +0 -0
  39. package/ts4.1/extractCollection.d.ts +14 -0
  40. package/ts4.1/extractObject.d.ts +6 -0
  41. package/ts4.1/getArrayPath.d.ts +3 -0
  42. package/ts4.1/hookifyResource.d.ts +14 -0
  43. package/ts4.1/index.d.ts +14 -0
  44. package/ts4.1/mapCollection.d.ts +16 -0
  45. package/ts4.1/next/index.d.ts +6 -0
  46. package/ts4.1/paginatedCollections.d.ts +3 -0
  47. package/ts4.1/paginationUpdate.d.ts +10 -0
  48. package/ts4.1/paramsToString.d.ts +2 -0
  49. package/ts4.1/paramsToString.native.d.ts +2 -0
  50. package/ts4.1/pathTypes.d.ts +21 -0
  51. package/ts4.1/resourceTypes.d.ts +41 -0
  52. package/ts4.1/test-fixtures.d.ts +141 -0
  53. package/ts4.1/utiltypes.d.ts +21 -0
@@ -1,13 +1,14 @@
1
1
  /* eslint-disable @typescript-eslint/ban-types */
2
- import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, } from '@data-client/endpoint';
2
+ import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, schema, } from '@data-client/endpoint';
3
3
  import type { ExtractCollection } from './extractCollection.js';
4
- import { OptionsToFunction } from './OptionsToFunction.js';
4
+ import { OptionsToBodyArgument, OptionsToFunction, } from './OptionsToFunction.js';
5
5
  import { PathArgs } from './pathTypes.js';
6
6
  import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
7
7
  export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
8
8
  path: string;
9
9
  body?: any;
10
10
  searchParams?: any;
11
+ method?: string;
11
12
  } = {
12
13
  path: string;
13
14
  }> extends EndpointInstanceInterface<F, S, M> {
@@ -22,7 +23,9 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
22
23
  */
23
24
  readonly urlPrefix: string;
24
25
  readonly requestInit: RequestInit;
25
- readonly method: string;
26
+ readonly method: (O & {
27
+ method: string;
28
+ })['method'];
26
29
  readonly signal: AbortSignal | undefined;
27
30
  /* fetch lifecycles */
28
31
  /* before-fetch */
@@ -56,32 +59,48 @@ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends
56
59
  path: string;
57
60
  body?: any;
58
61
  searchParams?: any;
62
+ method?: string;
59
63
  } = {
60
64
  path: string;
61
65
  }> extends RestInstanceBase<F, S, M, O> {
62
- push: AddEndpoint<F, S, O>;
63
- unshift: AddEndpoint<F, S, O>;
64
- assign: AddEndpoint<F, S, O>;
66
+ push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
67
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
68
+ }>;
69
+ unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
70
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
71
+ }>;
72
+ assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
73
+ body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
74
+ }>;
65
75
  }
66
- export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends RestInstanceBase, 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>>;
76
+ export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
77
+ body?: any;
78
+ path?: string;
79
+ schema?: Schema;
80
+ method?: string;
81
+ }, 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>>;
67
82
  type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
68
83
  body?: any;
69
- }, 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>>, 'body' extends keyof O ? O['body'] : E['body'], '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>, {
84
+ }, 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>, {
70
85
  path: Exclude<O['path'], undefined>;
71
86
  body: 'body' extends keyof O ? O['body'] : E['body'];
72
87
  searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
73
- }> : '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>>, O['body'], '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>, {
88
+ method: 'method' extends keyof O ? O['method'] : E['method'];
89
+ }> : '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>, {
74
90
  path: E['path'];
75
91
  body: O['body'];
76
92
  searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
77
- }> : 'searchParams' extends keyof O ? RestType<O['searchParams'] extends undefined ? PathArgs<Exclude<O['path'], undefined>> : O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, E['body'], '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>, {
93
+ method: 'method' extends keyof O ? O['method'] : E['method'];
94
+ }> : '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>, {
78
95
  path: E['path'];
79
96
  body: E['body'];
80
97
  searchParams: O['searchParams'];
98
+ method: 'method' extends keyof O ? O['method'] : E['method'];
81
99
  }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
82
100
  path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
83
101
  body: 'body' extends keyof O ? O['body'] : E['body'];
84
102
  searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
103
+ method: 'method' extends keyof O ? O['method'] : E['method'];
85
104
  }>;
86
105
  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>;
87
106
  export interface PartialRestGenerics {
@@ -89,19 +108,23 @@ export interface PartialRestGenerics {
89
108
  readonly schema?: Schema | undefined;
90
109
  readonly method?: string;
91
110
  /** Only used for types */
92
- readonly body?: any;
111
+ body?: any;
93
112
  /** Only used for types */
94
- readonly searchParams?: any;
113
+ searchParams?: any;
95
114
  /** @see https://resthooks.io/rest/api/RestEndpoint#process */
96
115
  process?(value: any, ...args: any): any;
97
116
  }
98
117
  export interface RestGenerics extends PartialRestGenerics {
99
118
  readonly path: string;
100
119
  }
101
- export type PaginationEndpoint<E extends RestInstanceBase, A extends any[]> = RestInstance<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
120
+ export type PaginationEndpoint<E extends FetchFunction & RestGenerics & {
121
+ sideEffect?: true | undefined;
122
+ }, A extends any[]> = RestInstance<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
102
123
  searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
103
124
  }>;
104
- export type PaginationFieldEndpoint<E extends RestInstanceBase, C extends string> = RestInstance<ParamFetchNoBody<{
125
+ export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
126
+ sideEffect?: true | undefined;
127
+ }, C extends string> = RestInstance<ParamFetchNoBody<{
105
128
  [K in C]: string | number | boolean;
106
129
  } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
107
130
  searchParams: {
@@ -110,19 +133,22 @@ export type PaginationFieldEndpoint<E extends RestInstanceBase, C extends string
110
133
  }>;
111
134
  export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
112
135
  path: string;
113
- body?: any;
136
+ body: any;
114
137
  searchParams?: any;
115
138
  } = {
116
139
  path: string;
117
- }> = 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>>, any, ResolveType<F>>, ExtractCollection<S>, true, Omit<O, 'body'>> & {
140
+ body: any;
141
+ }> = 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 & {
118
142
  method: 'POST';
119
- };
120
- export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O['body'] : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? Record<string, unknown> | FormData : undefined;
143
+ }>;
121
144
  type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
122
145
  body: any;
123
146
  } : O & {
124
147
  body: undefined;
125
148
  };
149
+ type OptionsToAdderBodyArgument<O extends {
150
+ body?: any;
151
+ }> = 'body' extends keyof O ? O['body'] : any;
126
152
  export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
127
153
  urlPrefix?: string;
128
154
  requestInit?: RequestInit;
@@ -138,9 +164,17 @@ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S
138
164
  url?(...args: Parameters<F>): string;
139
165
  update?: EndpointUpdateFunction<F, S>;
140
166
  }
141
- 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']>, BodyDefault<O>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
167
+ 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 {
168
+ sideEffect: true;
169
+ } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
142
170
  export interface RestEndpointConstructor {
143
- 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']>, BodyDefault<O>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, 'schema' extends keyof O ? O['schema'] : undefined, MethodToSide<O['method']>, OptionsBodyDefault<O>>;
171
+ 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 {
172
+ sideEffect: true;
173
+ } ? '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 & {
174
+ method: O extends {
175
+ sideEffect: true;
176
+ } ? 'POST' : 'GET';
177
+ }>;
144
178
  readonly prototype: RestInstanceBase;
145
179
  }
146
180
  /** Simplifies endpoint definitions that follow REST patterns
@@ -210,7 +244,7 @@ export type GetEndpoint<O extends {
210
244
  path: string;
211
245
  schema: Schema;
212
246
  }> = 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 & {
213
- body: undefined;
247
+ method: 'GET';
214
248
  }>;
215
249
  export type MutateEndpoint<O extends {
216
250
  readonly path: string;
@@ -223,4 +257,7 @@ export type MutateEndpoint<O extends {
223
257
  path: string;
224
258
  body: any;
225
259
  schema: Schema;
226
- }> = 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>;
260
+ }> = 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 & {
261
+ body: any;
262
+ method: 'POST' | 'PUT' | 'PATCH';
263
+ }>;
@@ -29,12 +29,14 @@ export interface Resource<O extends ResourceGenerics = {
29
29
  schema: schema.Collection<[
30
30
  O['schema']
31
31
  ]>;
32
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
32
33
  searchParams: O['searchParams'];
33
34
  }> : GetEndpoint<{
34
35
  path: ShortenPath<O['path']>;
35
36
  schema: schema.Collection<[
36
37
  O['schema']
37
38
  ]>;
39
+ body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
38
40
  searchParams: Record<string, number | string | boolean> | undefined;
39
41
  }>;
40
42
  /** Get a list of item
File without changes
@@ -1,11 +1,14 @@
1
1
  import { Schema, schema } from '@data-client/endpoint';
2
+ import { ExtractObject } from './extractObject.js';
2
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;
3
- export type ExtractCollection<S extends Schema | undefined> = S extends schema.CollectionInterface ? S : S extends schema.Object<infer T> ? ExtractObject<T> : S extends Exclude<Schema, {
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, {
4
10
  [K: string]: any;
5
11
  }> ? never : S extends {
6
12
  [K: string]: Schema;
7
13
  } ? ExtractObject<S> : never;
8
- export type ExtractObject<S extends Record<string, any>> = {
9
- [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
10
- }[keyof S];
11
14
  //# sourceMappingURL=extractCollection.d.ts.map
@@ -0,0 +1 @@
1
+ export type ExtractObject<S extends Record<string, any>> = any;
@@ -0,0 +1,11 @@
1
+ /** An error with a Rest Endpoint fetch
2
+ *
3
+ * @see https://resthooks.io/rest/api/NetworkError
4
+ */
5
+ export default class NetworkError extends Error {
6
+ status: number;
7
+ response: Response;
8
+ name: string;
9
+ constructor(response: Response);
10
+ }
11
+ //# sourceMappingURL=NetworkError.d.ts.map
@@ -0,0 +1,12 @@
1
+ import type { FetchFunction, ResolveType } from '@data-client/endpoint';
2
+ import { PathArgs } from './pathTypes.js';
3
+ import { PartialRestGenerics, RestFetch } from './RestEndpoint.js';
4
+ export type OptionsToFunction<O extends PartialRestGenerics, E extends {
5
+ body?: any;
6
+ path?: string;
7
+ method?: string;
8
+ }, F extends FetchFunction> = 'path' extends keyof O ? RestFetch<'searchParams' extends keyof O ? 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']>, O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : 'body' extends keyof O ? RestFetch<'searchParams' extends keyof O ? O['searchParams'] & PathArgs<Exclude<E['path'], undefined>> : PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<O, 'method' extends keyof O ? O['method'] : E['method']>, O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : 'searchParams' extends keyof O ? RestFetch<O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, OptionsToBodyArgument<E, 'method' extends keyof O ? O['method'] : E['method']>, O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>> : (this: ThisParameterType<F>, ...args: Parameters<F>) => Promise<O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>>;
9
+ export type OptionsToBodyArgument<O extends {
10
+ body?: any;
11
+ }, Method extends string | undefined> = Method extends 'POST' | 'PUT' | 'PATCH' ? 'body' extends keyof O ? O['body'] : any : undefined;
12
+ //# sourceMappingURL=OptionsToFunction.d.ts.map
@@ -0,0 +1,263 @@
1
+ /* eslint-disable @typescript-eslint/ban-types */
2
+ import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, schema, } 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
+ readonly body?: 'body' extends keyof O ? O['body'] : any;
16
+ readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
17
+ /** Pattern to construct url based on Url Parameters
18
+ * @see https://resthooks.io/rest/api/RestEndpoint#path
19
+ */
20
+ readonly path: O['path'];
21
+ /** Prepended to all urls
22
+ * @see https://resthooks.io/rest/api/RestEndpoint#urlPrefix
23
+ */
24
+ readonly urlPrefix: string;
25
+ readonly requestInit: RequestInit;
26
+ readonly method: (O & {
27
+ method: string;
28
+ })['method'];
29
+ readonly signal: AbortSignal | undefined;
30
+ /* fetch lifecycles */
31
+ /* before-fetch */
32
+ url(...args: Parameters<F>): string;
33
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
34
+ getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
35
+ /** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
36
+ getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
37
+ /* after-fetch */
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
+ /* utilities */
45
+ /** @see https://resthooks.io/rest/api/RestEndpoint#testKey */
46
+ testKey(key: string): boolean;
47
+ /* extenders */
48
+ // TODO: figure out better way than wrapping whole options in Readonly<> + making O extend from {}
49
+ // this is just a hack to handle when no members of PartialRestGenerics are present
50
+ // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
51
+ // should be used from the other (due to same problem with every member being partial)
52
+ extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<O, E, F> & O>): RestExtendedEndpoint<O, E>;
53
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
54
+ ...Parameters<E>
55
+ ]): PaginationEndpoint<E, A>;
56
+ paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
57
+ }
58
+ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
59
+ path: string;
60
+ body?: any;
61
+ searchParams?: any;
62
+ method?: string;
63
+ } = {
64
+ path: string;
65
+ }> extends RestInstanceBase<F, S, M, O> {
66
+ push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
67
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
68
+ }>;
69
+ unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
70
+ body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
71
+ }>;
72
+ assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
73
+ body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
74
+ }>;
75
+ }
76
+ export type RestEndpointExtendOptions<O extends PartialRestGenerics, E extends {
77
+ body?: any;
78
+ path?: string;
79
+ schema?: Schema;
80
+ method?: string;
81
+ }, 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>>;
82
+ type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
83
+ body?: any;
84
+ }, 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>, {
85
+ path: Exclude<O['path'], undefined>;
86
+ body: 'body' extends keyof O ? O['body'] : E['body'];
87
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
88
+ method: 'method' extends keyof O ? O['method'] : E['method'];
89
+ }> : '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>, {
90
+ path: E['path'];
91
+ body: O['body'];
92
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
93
+ method: 'method' extends keyof O ? O['method'] : E['method'];
94
+ }> : '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>, {
95
+ path: E['path'];
96
+ body: E['body'];
97
+ searchParams: O['searchParams'];
98
+ method: 'method' extends keyof O ? O['method'] : E['method'];
99
+ }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
100
+ path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
101
+ body: 'body' extends keyof O ? O['body'] : E['body'];
102
+ searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
103
+ method: 'method' extends keyof O ? O['method'] : E['method'];
104
+ }>;
105
+ 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>;
106
+ export interface PartialRestGenerics {
107
+ readonly path?: string;
108
+ readonly schema?: Schema | undefined;
109
+ readonly method?: string;
110
+ /** Only used for types */
111
+ body?: any;
112
+ /** Only used for types */
113
+ searchParams?: any;
114
+ /** @see https://resthooks.io/rest/api/RestEndpoint#process */
115
+ process?(value: any, ...args: any): any;
116
+ }
117
+ export interface RestGenerics extends PartialRestGenerics {
118
+ readonly path: string;
119
+ }
120
+ export type PaginationEndpoint<E extends FetchFunction & RestGenerics & {
121
+ sideEffect?: true | undefined;
122
+ }, A extends any[]> = RestInstance<ParamFetchNoBody<A[0], ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
123
+ searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
124
+ }>;
125
+ export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
126
+ sideEffect?: true | undefined;
127
+ }, C extends string> = RestInstance<ParamFetchNoBody<{
128
+ [K in C]: string | number | boolean;
129
+ } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
130
+ searchParams: {
131
+ [K in C]: string | number | boolean;
132
+ } & E['searchParams'];
133
+ }>;
134
+ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
135
+ path: string;
136
+ body: any;
137
+ searchParams?: any;
138
+ } = {
139
+ path: string;
140
+ body: any;
141
+ }> = 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 & {
142
+ method: 'POST';
143
+ }>;
144
+ type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
145
+ body: any;
146
+ } : O & {
147
+ body: undefined;
148
+ };
149
+ type OptionsToAdderBodyArgument<O extends {
150
+ body?: any;
151
+ }> = 'body' extends keyof O ? O['body'] : any;
152
+ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
153
+ urlPrefix?: string;
154
+ requestInit?: RequestInit;
155
+ getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
156
+ getRequestInit?(body: any): Promise<RequestInit> | RequestInit;
157
+ fetchResponse?(input: RequestInfo, init: RequestInit): Promise<any>;
158
+ parseResponse?(response: Response): Promise<any>;
159
+ sideEffect?: true | undefined;
160
+ name?: string;
161
+ signal?: AbortSignal;
162
+ fetch?: F;
163
+ key?(...args: Parameters<F>): string;
164
+ url?(...args: Parameters<F>): string;
165
+ update?: EndpointUpdateFunction<F, S>;
166
+ }
167
+ 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 {
168
+ sideEffect: true;
169
+ } ? 'POST' : 'GET'>, O['process'] extends {} ? ReturnType<O['process']> : any /*Denormalize<O['schema']>*/>, O['schema']>;
170
+ export interface RestEndpointConstructor {
171
+ 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 {
172
+ sideEffect: true;
173
+ } ? '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 & {
174
+ method: O extends {
175
+ sideEffect: true;
176
+ } ? 'POST' : 'GET';
177
+ }>;
178
+ readonly prototype: RestInstanceBase;
179
+ }
180
+ /** Simplifies endpoint definitions that follow REST patterns
181
+ *
182
+ * @see https://resthooks.io/rest/api/RestEndpoint
183
+ */
184
+ export declare let RestEndpoint: RestEndpointConstructor;
185
+ export default RestEndpoint;
186
+ export type MethodToSide<M> = M extends string ? M extends 'GET' ? undefined : true : undefined;
187
+ /** RestEndpoint types simplified */
188
+ export type RestType<UrlParams = any, Body = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any, O extends {
189
+ path: string;
190
+ body?: any;
191
+ searchParams?: any;
192
+ } = {
193
+ path: string;
194
+ }> = 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>>;
195
+ 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 {
196
+ path: string;
197
+ body?: any;
198
+ searchParams?: any;
199
+ } = {
200
+ path: string;
201
+ body: any;
202
+ }> = RestInstance<ParamFetchWithBody<UrlParams, Body, R>, S, M, O>;
203
+ export type RestTypeNoBody<UrlParams = any, S extends Schema | undefined = Schema | undefined, M extends true | undefined = true | undefined, R = any /*Denormalize<S>*/, O extends {
204
+ path: string;
205
+ body?: undefined;
206
+ searchParams?: any;
207
+ } = {
208
+ path: string;
209
+ body: undefined;
210
+ }> = RestInstance<ParamFetchNoBody<UrlParams, R>, S, M, O>;
211
+ /** Simple parameters, and body fetch functions */
212
+ export type RestFetch<UrlParams,
213
+ // eslint-disable-next-line @typescript-eslint/ban-types
214
+ Body = {}, Resolve = any> = IfTypeScriptLooseNull<ParamFetchNoBody<UrlParams, Resolve> | ParamFetchWithBody<UrlParams, Body, Resolve>, Body extends {} ? ParamFetchWithBody<UrlParams, Body, Resolve> : ParamFetchNoBody<UrlParams, Resolve>>;
215
+ export type ParamFetchWithBody<P, B = {}, R = any> =
216
+ // we must always allow undefined in a union and give it a type without params
217
+ 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>;
218
+ export type ParamFetchNoBody<P, R = any> =
219
+ // we must always allow undefined in a union and give it a type without params
220
+ 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>;
221
+ type IfTypeScriptLooseNull<Y, N> = 1 | undefined extends 1 ? Y : N;
222
+ export type KeyofRestEndpoint = keyof RestInstance;
223
+ export type FromFallBack<K extends keyof E, O, E> = K extends keyof O ? O[K] : E[K];
224
+ export type FetchMutate<A extends readonly any[] = [
225
+ any,
226
+ {}
227
+ ]
228
+ // eslint-disable-next-line @typescript-eslint/ban-types
229
+ | [
230
+ {}
231
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
232
+ export type FetchGet<A extends readonly any[] = [
233
+ any
234
+ ], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
235
+ export type Defaults<O, D> = {
236
+ [K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
237
+ };
238
+ export type GetEndpoint<O extends {
239
+ readonly path: string;
240
+ readonly schema: Schema;
241
+ /** Only used for types */
242
+ readonly searchParams?: any;
243
+ } = {
244
+ path: string;
245
+ schema: Schema;
246
+ }> = 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 & {
247
+ method: 'GET';
248
+ }>;
249
+ export type MutateEndpoint<O extends {
250
+ readonly path: string;
251
+ readonly schema: Schema;
252
+ /** Only used for types */
253
+ readonly searchParams?: any;
254
+ /** Only used for types */
255
+ readonly body?: any;
256
+ } = {
257
+ path: string;
258
+ body: any;
259
+ schema: Schema;
260
+ }> = 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 & {
261
+ body: any;
262
+ method: 'POST' | 'PUT' | 'PATCH';
263
+ }>;
@@ -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