@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
@@ -1,5 +1,7 @@
1
1
  import { Schema, schema } from '@data-client/endpoint';
2
2
 
3
+ import { ExtractObject } from './extractObject.js';
4
+
3
5
  export default function extractCollection<
4
6
  M extends <C extends schema.Collection>(collection: C) => any,
5
7
  S extends Schema | undefined,
@@ -18,17 +20,17 @@ export default function extractCollection<
18
20
  }
19
21
  }
20
22
 
21
- export type ExtractCollection<S extends Schema | undefined> =
22
- S extends schema.CollectionInterface
23
- ? S
24
- : S extends schema.Object<infer T>
25
- ? ExtractObject<T>
26
- : S extends Exclude<Schema, { [K: string]: any }>
27
- ? never
28
- : S extends { [K: string]: Schema }
29
- ? ExtractObject<S>
30
- : never;
31
-
32
- export type ExtractObject<S extends Record<string, any>> = {
33
- [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
34
- }[keyof S];
23
+ export type ExtractCollection<S extends Schema | undefined> = S extends {
24
+ push: any;
25
+ unshift: any;
26
+ assign: any;
27
+ schema: Schema;
28
+ }
29
+ ? S
30
+ : S extends schema.Object<infer T>
31
+ ? ExtractObject<T>
32
+ : S extends Exclude<Schema, { [K: string]: any }>
33
+ ? never
34
+ : S extends { [K: string]: Schema }
35
+ ? ExtractObject<S>
36
+ : never;
@@ -0,0 +1,7 @@
1
+ import { Schema } from '@data-client/endpoint';
2
+
3
+ import { ExtractCollection } from './extractCollection.js';
4
+
5
+ export type ExtractObject<S extends Record<string, any>> = {
6
+ [K in keyof S]: S[K] extends Schema ? ExtractCollection<S[K]> : never;
7
+ }[keyof S];
package/src/index.ts CHANGED
@@ -18,6 +18,7 @@ export type {
18
18
  RestEndpointConstructorOptions,
19
19
  AddEndpoint,
20
20
  PaginationFieldEndpoint,
21
+ PaginationEndpoint,
21
22
  } from './RestEndpoint.js';
22
23
  export { default as createResource } from './createResource.js';
23
24
  export type { Resource } from './createResource.js';
@@ -1,12 +1,10 @@
1
1
  import type { FetchFunction, ResolveType } from '@data-client/endpoint';
2
2
 
3
- import { PartialRestGenerics, RestInstance } from './RestEndpoint.js';
3
+ import { PartialRestGenerics } from './RestEndpoint.js';
4
4
 
5
5
  export type OptionsToFunction<
6
6
  O extends PartialRestGenerics,
7
- E extends RestInstance & {
8
- body?: any;
9
- },
7
+ E extends { body?: any; path?: string; method?: string },
10
8
  F extends FetchFunction,
11
9
  > = (
12
10
  this: ThisParameterType<F>,
@@ -14,3 +12,8 @@ export type OptionsToFunction<
14
12
  ) => Promise<
15
13
  O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
16
14
  >;
15
+
16
+ export type OptionsToBodyArgument<
17
+ O extends { body?: any },
18
+ Method extends string | undefined,
19
+ > = any;
@@ -1,17 +1,20 @@
1
1
  /* eslint-disable @typescript-eslint/ban-types */
2
2
  import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, } 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> {
15
+ /** @see https://resthooks.io/rest/api/RestEndpoint#body */
14
16
  readonly body?: 'body' extends keyof O ? O['body'] : any;
17
+ /** @see https://resthooks.io/rest/api/RestEndpoint#searchParams */
15
18
  readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
16
19
  /** Pattern to construct url based on Url Parameters
17
20
  * @see https://resthooks.io/rest/api/RestEndpoint#path
@@ -22,7 +25,10 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
22
25
  */
23
26
  readonly urlPrefix: string;
24
27
  readonly requestInit: RequestInit;
25
- readonly method: string;
28
+ /** @see https://resthooks.io/rest/api/RestEndpoint#method */
29
+ readonly method: (O & {
30
+ method: string;
31
+ })['method'];
26
32
  readonly signal: AbortSignal | undefined;
27
33
  /* fetch lifecycles */
28
34
  /* before-fetch */
@@ -46,42 +52,73 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
46
52
  // this is just a hack to handle when no members of PartialRestGenerics are present
47
53
  // Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
48
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
+ */
49
58
  extend<E extends RestInstanceBase, O extends PartialRestGenerics | {}>(this: E, options: Readonly<RestEndpointExtendOptions<O, E, F> & O>): RestExtendedEndpoint<O, E>;
50
- paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
51
- ...Parameters<E>
52
- ]): PaginationEndpoint<E, A>;
53
- paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
54
59
  }
55
60
  export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
56
61
  path: string;
57
62
  body?: any;
58
63
  searchParams?: any;
64
+ method?: string;
59
65
  } = {
60
66
  path: string;
61
67
  }> extends RestInstanceBase<F, S, M, O> {
62
- push: AddEndpoint<F, S, O>;
63
- unshift: AddEndpoint<F, S, O>;
64
- assign: AddEndpoint<F, S, 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
+ }>;
65
93
  }
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>>;
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>>;
67
100
  type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
68
101
  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>, {
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>, {
70
103
  path: Exclude<O['path'], undefined>;
71
104
  body: 'body' extends keyof O ? O['body'] : E['body'];
72
105
  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>, {
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>, {
74
108
  path: E['path'];
75
109
  body: O['body'];
76
110
  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>, {
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>, {
78
113
  path: E['path'];
79
114
  body: E['body'];
80
115
  searchParams: O['searchParams'];
116
+ method: 'method' extends keyof O ? O['method'] : E['method'];
81
117
  }> : RestInstance<F, 'schema' extends keyof O ? O['schema'] : E['schema'], 'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'], {
82
118
  path: 'path' extends keyof O ? Exclude<O['path'], undefined> : E['path'];
83
119
  body: 'body' extends keyof O ? O['body'] : E['body'];
84
120
  searchParams: 'searchParams' extends keyof O ? O['searchParams'] : E['searchParams'];
121
+ method: 'method' extends keyof O ? O['method'] : E['method'];
85
122
  }>;
86
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>;
87
124
  export interface PartialRestGenerics {
@@ -89,19 +126,23 @@ export interface PartialRestGenerics {
89
126
  readonly schema?: Schema | undefined;
90
127
  readonly method?: string;
91
128
  /** Only used for types */
92
- readonly body?: any;
129
+ body?: any;
93
130
  /** Only used for types */
94
- readonly searchParams?: any;
131
+ searchParams?: any;
95
132
  /** @see https://resthooks.io/rest/api/RestEndpoint#process */
96
133
  process?(value: any, ...args: any): any;
97
134
  }
98
135
  export interface RestGenerics extends PartialRestGenerics {
99
136
  readonly path: string;
100
137
  }
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'> & {
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'> & {
102
141
  searchParams: Omit<A[0], keyof PathArgs<E['path']>>;
103
142
  }>;
104
- export type PaginationFieldEndpoint<E extends RestInstanceBase, C extends string> = RestInstance<ParamFetchNoBody<{
143
+ export type PaginationFieldEndpoint<E extends FetchFunction & RestGenerics & {
144
+ sideEffect?: true | undefined;
145
+ }, C extends string> = RestInstance<ParamFetchNoBody<{
105
146
  [K in C]: string | number | boolean;
106
147
  } & E['searchParams'] & PathArgs<Exclude<E['path'], undefined>>, ResolveType<E>>, E['schema'], E['sideEffect'], Pick<E, 'path' | 'searchParams' | 'body'> & {
107
148
  searchParams: {
@@ -110,19 +151,22 @@ export type PaginationFieldEndpoint<E extends RestInstanceBase, C extends string
110
151
  }>;
111
152
  export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, O extends {
112
153
  path: string;
113
- body?: any;
154
+ body: any;
114
155
  searchParams?: any;
115
156
  } = {
116
157
  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'>> & {
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 & {
118
160
  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;
161
+ }>;
121
162
  type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
122
163
  body: any;
123
164
  } : O & {
124
165
  body: undefined;
125
166
  };
167
+ type OptionsToAdderBodyArgument<O extends {
168
+ body?: any;
169
+ }> = 'body' extends keyof O ? O['body'] : any;
126
170
  export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S extends Schema | undefined = undefined> extends EndpointExtraOptions<F> {
127
171
  urlPrefix?: string;
128
172
  requestInit?: RequestInit;
@@ -138,9 +182,17 @@ export interface RestEndpointOptions<F extends FetchFunction = FetchFunction, S
138
182
  url?(...args: Parameters<F>): string;
139
183
  update?: EndpointUpdateFunction<F, S>;
140
184
  }
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']>;
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']>;
142
188
  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>>;
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
+ }>;
144
196
  readonly prototype: RestInstanceBase;
145
197
  }
146
198
  /** Simplifies endpoint definitions that follow REST patterns
@@ -210,7 +262,7 @@ export type GetEndpoint<O extends {
210
262
  path: string;
211
263
  schema: Schema;
212
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 & {
213
- body: undefined;
265
+ method: 'GET';
214
266
  }>;
215
267
  export type MutateEndpoint<O extends {
216
268
  readonly path: string;
@@ -223,4 +275,7 @@ export type MutateEndpoint<O extends {
223
275
  path: string;
224
276
  body: any;
225
277
  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>;
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
+ }>;
@@ -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;
package/ts4.0/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { default as RestEndpoint } from './RestEndpoint.js';
2
- export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, } from './RestEndpoint.js';
2
+ export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, PaginationEndpoint, } from './RestEndpoint.js';
3
3
  export { default as createResource } from './createResource.js';
4
4
  export type { Resource } from './createResource.js';
5
5
  export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
@@ -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' | 'DELETE' ? 'body' extends keyof O ? O['body'] : any : undefined;
12
+ //# sourceMappingURL=OptionsToFunction.d.ts.map