@data-client/rest 0.4.1 → 0.6.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.
- package/dist/index.js +44 -5
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +44 -5
- package/legacy/createResource.js +39 -8
- package/legacy/index.js +1 -1
- package/legacy/next/index.js +1 -1
- package/legacy/resourceExtendable.js +2 -0
- package/legacy/resourceExtensionTypes.js +2 -0
- package/legacy/resourceTypes.js +1 -1
- package/lib/RestEndpoint.d.ts +1 -1
- package/lib/createResource.d.ts +2 -96
- package/lib/createResource.d.ts.map +1 -1
- package/lib/createResource.js +45 -6
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/next/index.d.ts +1 -1
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +1 -1
- package/lib/resourceExtendable.d.ts +25 -0
- package/lib/resourceExtendable.d.ts.map +1 -0
- package/lib/resourceExtendable.js +2 -0
- package/lib/resourceExtensionTypes.d.ts +33 -0
- package/lib/resourceExtensionTypes.d.ts.map +1 -0
- package/lib/resourceExtensionTypes.js +2 -0
- package/lib/resourceTypes.d.ts +94 -4
- package/lib/resourceTypes.d.ts.map +1 -1
- package/lib/resourceTypes.js +1 -1
- package/package.json +1 -1
- package/src/RestEndpoint.d.ts +1 -1
- package/src/__tests__/createResource.test.ts +490 -19
- package/src/createResource.ts +60 -127
- package/src/index.ts +1 -1
- package/src/next/index.ts +1 -1
- package/src/resourceExtendable.ts +95 -0
- package/src/resourceExtensionTypes.ts +109 -0
- package/src/resourceTypes.ts +119 -4
- package/ts4.0/RestEndpoint.d.ts +1 -1
- package/ts4.0/createResource.d.ts +2 -108
- package/ts4.0/index.d.ts +1 -1
- package/ts4.0/next/index.d.ts +1 -1
- package/ts4.0/resourceExtendable.d.ts +21 -0
- package/ts4.0/resourceExtensionTypes.d.ts +33 -0
- package/ts4.0/resourceTypes.d.ts +102 -4
- package/ts4.1/RestEndpoint.d.ts +1 -1
- package/ts4.1/createResource.d.ts +2 -108
- package/ts4.1/index.d.ts +1 -1
- package/ts4.1/next/index.d.ts +1 -1
- package/ts4.1/resourceExtendable.d.ts +21 -0
- package/ts4.1/resourceExtensionTypes.d.ts +33 -0
- package/ts4.1/resourceTypes.d.ts +102 -4
- package/ts4.0/deleteme.d.ts +0 -0
- package/ts4.1/deleteme.d.ts +0 -0
package/src/createResource.ts
CHANGED
|
@@ -3,15 +3,19 @@ import {
|
|
|
3
3
|
SnapshotInterface,
|
|
4
4
|
schema,
|
|
5
5
|
} from '@data-client/endpoint';
|
|
6
|
-
import type { Denormalize } from '@data-client/endpoint';
|
|
7
6
|
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { ResourcePath } from './pathTypes.js';
|
|
8
|
+
import {
|
|
9
|
+
ResourceGenerics,
|
|
10
|
+
ResourceOptions,
|
|
11
|
+
Resource,
|
|
12
|
+
ResourceInterface,
|
|
13
|
+
} from './resourceTypes.js';
|
|
10
14
|
import RestEndpoint, {
|
|
11
15
|
GetEndpoint,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
PartialRestGenerics,
|
|
17
|
+
RestEndpointOptions,
|
|
18
|
+
RestInstanceBase,
|
|
15
19
|
} from './RestEndpoint.js';
|
|
16
20
|
import { shortenPath } from './RestHelpers.js';
|
|
17
21
|
|
|
@@ -26,11 +30,15 @@ export default function createResource<O extends ResourceGenerics>({
|
|
|
26
30
|
schema,
|
|
27
31
|
Endpoint = RestEndpoint,
|
|
28
32
|
optimistic,
|
|
29
|
-
paginationField,
|
|
30
33
|
...extraOptions
|
|
31
34
|
}: Readonly<O> & ResourceOptions): Resource<O> {
|
|
32
35
|
const shortenedPath = shortenPath(path);
|
|
33
36
|
const getName = (name: string) => `${(schema as any)?.name}.${name}`;
|
|
37
|
+
// this accounts for derivative endpoints
|
|
38
|
+
function extendMember(extended: any, key: string, options: any) {
|
|
39
|
+
extended[key] = extended[key].extend(options);
|
|
40
|
+
}
|
|
41
|
+
|
|
34
42
|
const extraMutateOptions = { ...extraOptions };
|
|
35
43
|
const extraPartialOptions = { ...extraOptions };
|
|
36
44
|
const get: GetEndpoint<{ path: O['path']; schema: O['schema'] }> =
|
|
@@ -53,6 +61,7 @@ export default function createResource<O extends ResourceGenerics>({
|
|
|
53
61
|
const ret = {
|
|
54
62
|
get,
|
|
55
63
|
getList,
|
|
64
|
+
// TODO(deprecated): remove this once we remove creates
|
|
56
65
|
create: getList.push.extend({ name: getName('create') }),
|
|
57
66
|
update: new Endpoint({
|
|
58
67
|
...extraMutateOptions,
|
|
@@ -79,15 +88,52 @@ export default function createResource<O extends ResourceGenerics>({
|
|
|
79
88
|
},
|
|
80
89
|
getOptimisticResponse: optimistic ? (optimisticDelete as any) : undefined,
|
|
81
90
|
}),
|
|
91
|
+
extend(
|
|
92
|
+
...args:
|
|
93
|
+
| [string, RestEndpointOptions & PartialRestGenerics]
|
|
94
|
+
| [RestEndpointOptions & PartialRestGenerics]
|
|
95
|
+
| [
|
|
96
|
+
(
|
|
97
|
+
baseResource: ResourceInterface,
|
|
98
|
+
) => Record<string, RestInstanceBase>,
|
|
99
|
+
]
|
|
100
|
+
) {
|
|
101
|
+
if (typeof args[0] === 'string') {
|
|
102
|
+
const [key, options] = args;
|
|
103
|
+
if (key in this) {
|
|
104
|
+
const extended = { ...this };
|
|
105
|
+
extendMember(extended, key, options);
|
|
106
|
+
return extended;
|
|
107
|
+
} else {
|
|
108
|
+
return {
|
|
109
|
+
...this,
|
|
110
|
+
[key]: this.get.extend(options),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
} else if (typeof args[0] === 'function') {
|
|
114
|
+
const extended = args[0](this);
|
|
115
|
+
return {
|
|
116
|
+
...this,
|
|
117
|
+
...extended,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const overrides = args[0];
|
|
121
|
+
const extended = { ...this };
|
|
122
|
+
for (const key in overrides) {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
124
|
+
// @ts-ignore
|
|
125
|
+
extendMember(extended, key, overrides[key]);
|
|
126
|
+
}
|
|
127
|
+
return extended;
|
|
128
|
+
},
|
|
82
129
|
} as any;
|
|
83
|
-
if (paginationField) ret.getNextPage = getList.paginated(paginationField);
|
|
84
130
|
return ret;
|
|
85
131
|
}
|
|
86
132
|
|
|
87
133
|
function optimisticUpdate(snap: SnapshotInterface, params: any, body: any) {
|
|
88
134
|
return {
|
|
89
135
|
...params,
|
|
90
|
-
...body,
|
|
136
|
+
...ensureBodyPojo(body),
|
|
91
137
|
};
|
|
92
138
|
}
|
|
93
139
|
function optimisticPartial(
|
|
@@ -100,128 +146,15 @@ function optimisticPartial(
|
|
|
100
146
|
...params,
|
|
101
147
|
...data,
|
|
102
148
|
// even tho we don't always have two arguments, the extra one will simply be undefined which spreads fine
|
|
103
|
-
...body,
|
|
149
|
+
...ensureBodyPojo(body),
|
|
104
150
|
};
|
|
105
151
|
};
|
|
106
152
|
}
|
|
107
153
|
function optimisticDelete(snap: SnapshotInterface, params: any) {
|
|
108
154
|
return params;
|
|
109
155
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
/** Get a singular item
|
|
115
|
-
*
|
|
116
|
-
* @see https://resthooks.io/rest/api/createResource#get
|
|
117
|
-
*/
|
|
118
|
-
get: GetEndpoint<{ path: O['path']; schema: O['schema'] }>;
|
|
119
|
-
/** Get a list of item
|
|
120
|
-
*
|
|
121
|
-
* @see https://resthooks.io/rest/api/createResource#getlist
|
|
122
|
-
*/
|
|
123
|
-
getList: 'searchParams' extends keyof O
|
|
124
|
-
? GetEndpoint<{
|
|
125
|
-
path: ShortenPath<O['path']>;
|
|
126
|
-
schema: schema.Collection<[O['schema']]>;
|
|
127
|
-
body: 'body' extends keyof O
|
|
128
|
-
? O['body']
|
|
129
|
-
: Partial<Denormalize<O['schema']>>;
|
|
130
|
-
searchParams: O['searchParams'];
|
|
131
|
-
}>
|
|
132
|
-
: GetEndpoint<{
|
|
133
|
-
path: ShortenPath<O['path']>;
|
|
134
|
-
schema: schema.Collection<[O['schema']]>;
|
|
135
|
-
body: 'body' extends keyof O
|
|
136
|
-
? O['body']
|
|
137
|
-
: Partial<Denormalize<O['schema']>>;
|
|
138
|
-
searchParams: Record<string, number | string | boolean> | undefined;
|
|
139
|
-
}>;
|
|
140
|
-
/** Get a list of item
|
|
141
|
-
*
|
|
142
|
-
* @see https://dataclient.io/rest/api/createResource#getNextPage
|
|
143
|
-
*/
|
|
144
|
-
getNextPage: 'paginationField' extends keyof O
|
|
145
|
-
? PaginationFieldEndpoint<
|
|
146
|
-
'searchParams' extends keyof O
|
|
147
|
-
? GetEndpoint<{
|
|
148
|
-
path: ShortenPath<O['path']>;
|
|
149
|
-
schema: schema.Collection<[O['schema']]>;
|
|
150
|
-
searchParams: O['searchParams'];
|
|
151
|
-
}>
|
|
152
|
-
: GetEndpoint<{
|
|
153
|
-
path: ShortenPath<O['path']>;
|
|
154
|
-
schema: schema.Collection<[O['schema']]>;
|
|
155
|
-
searchParams:
|
|
156
|
-
| Record<string, number | string | boolean>
|
|
157
|
-
| undefined;
|
|
158
|
-
}>,
|
|
159
|
-
Exclude<O['paginationField'], undefined>
|
|
160
|
-
>
|
|
161
|
-
: undefined;
|
|
162
|
-
/** Create a new item (POST)
|
|
163
|
-
*
|
|
164
|
-
* @see https://resthooks.io/rest/api/createResource#create
|
|
165
|
-
*/
|
|
166
|
-
create: 'searchParams' extends keyof O
|
|
167
|
-
? MutateEndpoint<{
|
|
168
|
-
path: ShortenPath<O['path']>;
|
|
169
|
-
schema: schema.Collection<[O['schema']]>['push'];
|
|
170
|
-
body: 'body' extends keyof O
|
|
171
|
-
? O['body']
|
|
172
|
-
: Partial<Denormalize<O['schema']>>;
|
|
173
|
-
searchParams: O['searchParams'];
|
|
174
|
-
}>
|
|
175
|
-
: MutateEndpoint<{
|
|
176
|
-
path: ShortenPath<O['path']>;
|
|
177
|
-
schema: schema.Collection<[O['schema']]>['push'];
|
|
178
|
-
body: 'body' extends keyof O
|
|
179
|
-
? O['body']
|
|
180
|
-
: Partial<Denormalize<O['schema']>>;
|
|
181
|
-
}>;
|
|
182
|
-
/** Update an item (PUT)
|
|
183
|
-
*
|
|
184
|
-
* @see https://resthooks.io/rest/api/createResource#update
|
|
185
|
-
*/
|
|
186
|
-
update: 'body' extends keyof O
|
|
187
|
-
? MutateEndpoint<{
|
|
188
|
-
path: O['path'];
|
|
189
|
-
body: O['body'];
|
|
190
|
-
schema: O['schema'];
|
|
191
|
-
}>
|
|
192
|
-
: MutateEndpoint<{
|
|
193
|
-
path: O['path'];
|
|
194
|
-
body: Partial<Denormalize<O['schema']>>;
|
|
195
|
-
schema: O['schema'];
|
|
196
|
-
}>;
|
|
197
|
-
/** Update an item (PATCH)
|
|
198
|
-
*
|
|
199
|
-
* @see https://resthooks.io/rest/api/createResource#partialupdate
|
|
200
|
-
*/
|
|
201
|
-
partialUpdate: 'body' extends keyof O
|
|
202
|
-
? MutateEndpoint<{
|
|
203
|
-
path: O['path'];
|
|
204
|
-
body: Partial<O['body']>;
|
|
205
|
-
schema: O['schema'];
|
|
206
|
-
}>
|
|
207
|
-
: MutateEndpoint<{
|
|
208
|
-
path: O['path'];
|
|
209
|
-
body: Partial<Denormalize<O['schema']>>;
|
|
210
|
-
schema: O['schema'];
|
|
211
|
-
}>;
|
|
212
|
-
/** Delete an item (DELETE)
|
|
213
|
-
*
|
|
214
|
-
* @see https://resthooks.io/rest/api/createResource#delete
|
|
215
|
-
*/
|
|
216
|
-
delete: RestTypeNoBody<
|
|
217
|
-
PathArgs<O['path']>,
|
|
218
|
-
O['schema'] extends schema.EntityInterface & { process: any }
|
|
219
|
-
? schema.Invalidate<O['schema']>
|
|
220
|
-
: O['schema'],
|
|
221
|
-
undefined,
|
|
222
|
-
Partial<PathArgs<O['path']>>,
|
|
223
|
-
{
|
|
224
|
-
path: O['path'];
|
|
225
|
-
}
|
|
226
|
-
>;
|
|
156
|
+
function ensureBodyPojo(body: any) {
|
|
157
|
+
return body instanceof FormData
|
|
158
|
+
? Object.fromEntries((body as any).entries())
|
|
159
|
+
: body;
|
|
227
160
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,7 +21,7 @@ export type {
|
|
|
21
21
|
PaginationEndpoint,
|
|
22
22
|
} from './RestEndpoint.js';
|
|
23
23
|
export { default as createResource } from './createResource.js';
|
|
24
|
-
export type { Resource } from './
|
|
24
|
+
export type { Resource } from './resourceTypes.js';
|
|
25
25
|
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
26
26
|
export { default as hookifyResource } from './hookifyResource.js';
|
|
27
27
|
export type {
|
package/src/next/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from '../RestEndpoint.js';
|
|
2
2
|
export { default as RestEndpoint } from '../RestEndpoint.js';
|
|
3
3
|
export { default as createResource } from '../createResource.js';
|
|
4
|
-
export type { Resource } from '../
|
|
4
|
+
export type { Resource } from '../resourceTypes.js';
|
|
5
5
|
export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EndpointInterface,
|
|
3
|
+
EndpointToFunction,
|
|
4
|
+
} from '@data-client/endpoint';
|
|
5
|
+
|
|
6
|
+
import type { ResourcePath } from './pathTypes.js';
|
|
7
|
+
import type {
|
|
8
|
+
ResourceExtension,
|
|
9
|
+
ResourceEndpointExtensions,
|
|
10
|
+
CustomResource,
|
|
11
|
+
ExtendedResource,
|
|
12
|
+
} from './resourceExtensionTypes.js';
|
|
13
|
+
import type {
|
|
14
|
+
Resource,
|
|
15
|
+
ResourceGenerics,
|
|
16
|
+
ResourceInterface,
|
|
17
|
+
} from './resourceTypes.js';
|
|
18
|
+
import type {
|
|
19
|
+
PartialRestGenerics,
|
|
20
|
+
RestEndpointExtendOptions,
|
|
21
|
+
RestExtendedEndpoint,
|
|
22
|
+
RestInstanceBase,
|
|
23
|
+
} from './RestEndpoint.js';
|
|
24
|
+
|
|
25
|
+
export interface Extendable<
|
|
26
|
+
O extends ResourceGenerics = { path: ResourcePath; schema: any },
|
|
27
|
+
> {
|
|
28
|
+
/** Allows customizing individual endpoints
|
|
29
|
+
*
|
|
30
|
+
* @see https://dataclient.io/rest/api/createResource#extend
|
|
31
|
+
*/
|
|
32
|
+
extend<
|
|
33
|
+
R extends {
|
|
34
|
+
[K in ExtendKey]: RestInstanceBase;
|
|
35
|
+
},
|
|
36
|
+
const ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>,
|
|
37
|
+
// TODO: see RestEndpoint.extend TODO
|
|
38
|
+
ExtendOptions extends PartialRestGenerics | {},
|
|
39
|
+
>(
|
|
40
|
+
this: R,
|
|
41
|
+
key: ExtendKey,
|
|
42
|
+
options: Readonly<
|
|
43
|
+
RestEndpointExtendOptions<
|
|
44
|
+
ExtendOptions,
|
|
45
|
+
R[ExtendKey],
|
|
46
|
+
EndpointToFunction<R[ExtendKey]>
|
|
47
|
+
> &
|
|
48
|
+
ExtendOptions
|
|
49
|
+
>,
|
|
50
|
+
): ResourceExtension<R, ExtendKey, ExtendOptions>;
|
|
51
|
+
extend<
|
|
52
|
+
R extends { get: RestInstanceBase },
|
|
53
|
+
const ExtendKey extends string,
|
|
54
|
+
// TODO: see RestEndpoint.extend TODO
|
|
55
|
+
ExtendOptions extends PartialRestGenerics | {},
|
|
56
|
+
>(
|
|
57
|
+
this: R,
|
|
58
|
+
key: ExtendKey,
|
|
59
|
+
options: Readonly<
|
|
60
|
+
RestEndpointExtendOptions<
|
|
61
|
+
ExtendOptions,
|
|
62
|
+
R['get'],
|
|
63
|
+
EndpointToFunction<R['get']>
|
|
64
|
+
> &
|
|
65
|
+
ExtendOptions
|
|
66
|
+
>,
|
|
67
|
+
): R & {
|
|
68
|
+
[key in ExtendKey]: RestExtendedEndpoint<ExtendOptions, R['get']>;
|
|
69
|
+
};
|
|
70
|
+
extend<
|
|
71
|
+
R extends ResourceInterface,
|
|
72
|
+
Get extends PartialRestGenerics = any,
|
|
73
|
+
GetList extends PartialRestGenerics = any,
|
|
74
|
+
Update extends PartialRestGenerics = any,
|
|
75
|
+
PartialUpdate extends PartialRestGenerics = any,
|
|
76
|
+
Delete extends PartialRestGenerics = any,
|
|
77
|
+
>(
|
|
78
|
+
this: R,
|
|
79
|
+
options: ResourceEndpointExtensions<
|
|
80
|
+
R,
|
|
81
|
+
Get,
|
|
82
|
+
GetList,
|
|
83
|
+
Update,
|
|
84
|
+
PartialUpdate,
|
|
85
|
+
Delete
|
|
86
|
+
>,
|
|
87
|
+
): CustomResource<R, O, Get, GetList, Update, PartialUpdate, Delete>;
|
|
88
|
+
extend<
|
|
89
|
+
R extends ResourceInterface,
|
|
90
|
+
T extends Record<string, EndpointInterface>,
|
|
91
|
+
>(
|
|
92
|
+
this: R,
|
|
93
|
+
extender: (baseResource: R) => T,
|
|
94
|
+
): ExtendedResource<R, T>;
|
|
95
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EndpointInterface,
|
|
3
|
+
EndpointToFunction,
|
|
4
|
+
} from '@data-client/endpoint';
|
|
5
|
+
|
|
6
|
+
import { OptionsToFunction } from './OptionsToFunction.js';
|
|
7
|
+
import type { ResourcePath } from './pathTypes.js';
|
|
8
|
+
import { Extendable } from './resourceExtendable.js';
|
|
9
|
+
import { ResourceGenerics, ResourceInterface } from './resourceTypes.js';
|
|
10
|
+
import type {
|
|
11
|
+
PartialRestGenerics,
|
|
12
|
+
RestExtendedEndpoint,
|
|
13
|
+
RestInstanceBase,
|
|
14
|
+
RestEndpointOptions,
|
|
15
|
+
} from './RestEndpoint.js';
|
|
16
|
+
|
|
17
|
+
export type ResourceExtension<
|
|
18
|
+
R extends { [K in ExtendKey]: RestInstanceBase },
|
|
19
|
+
ExtendKey extends Exclude<Extract<keyof R, string>, 'extend'>,
|
|
20
|
+
O extends PartialRestGenerics | {},
|
|
21
|
+
> = {
|
|
22
|
+
[K in keyof R]: K extends ExtendKey ? RestExtendedEndpoint<O, R[K]> : R[K];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Resource with individual endpoints customized
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export interface CustomResource<
|
|
29
|
+
R extends ResourceInterface,
|
|
30
|
+
O extends ResourceGenerics = { path: ResourcePath; schema: any },
|
|
31
|
+
Get extends PartialRestGenerics | {} = any,
|
|
32
|
+
GetList extends PartialRestGenerics | {} = any,
|
|
33
|
+
Update extends PartialRestGenerics | {} = any,
|
|
34
|
+
PartialUpdate extends PartialRestGenerics | {} = any,
|
|
35
|
+
Delete extends PartialRestGenerics | {} = any,
|
|
36
|
+
> extends Extendable<O> {
|
|
37
|
+
// unknown only extends any. this allows us to match exclusively on members not set
|
|
38
|
+
get: unknown extends Get ? R['get'] : RestExtendedEndpoint<Get, R['get']>;
|
|
39
|
+
getList: unknown extends GetList
|
|
40
|
+
? R['getList']
|
|
41
|
+
: RestExtendedEndpoint<GetList, R['getList']>;
|
|
42
|
+
update: unknown extends Update
|
|
43
|
+
? R['update']
|
|
44
|
+
: RestExtendedEndpoint<Update, R['update']>;
|
|
45
|
+
partialUpdate: unknown extends PartialUpdate
|
|
46
|
+
? R['partialUpdate']
|
|
47
|
+
: RestExtendedEndpoint<PartialUpdate, R['partialUpdate']>;
|
|
48
|
+
delete: unknown extends Delete
|
|
49
|
+
? R['delete']
|
|
50
|
+
: RestExtendedEndpoint<Delete, R['delete']>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type ExtendedResource<
|
|
54
|
+
R extends ResourceInterface,
|
|
55
|
+
T extends Record<string, EndpointInterface>,
|
|
56
|
+
> = Omit<R, keyof T> & T;
|
|
57
|
+
|
|
58
|
+
export interface ResourceEndpointExtensions<
|
|
59
|
+
R extends ResourceInterface,
|
|
60
|
+
Get extends PartialRestGenerics = any,
|
|
61
|
+
GetList extends PartialRestGenerics = any,
|
|
62
|
+
Update extends PartialRestGenerics = any,
|
|
63
|
+
PartialUpdate extends PartialRestGenerics = any,
|
|
64
|
+
Delete extends PartialRestGenerics = any,
|
|
65
|
+
> {
|
|
66
|
+
readonly get?: RestEndpointOptions<
|
|
67
|
+
unknown extends Get
|
|
68
|
+
? EndpointToFunction<R['get']>
|
|
69
|
+
: OptionsToFunction<Get, R['get'], EndpointToFunction<R['get']>>,
|
|
70
|
+
R['get']['schema']
|
|
71
|
+
> &
|
|
72
|
+
Readonly<Get>;
|
|
73
|
+
readonly getList?: RestEndpointOptions<
|
|
74
|
+
unknown extends GetList
|
|
75
|
+
? EndpointToFunction<R['getList']>
|
|
76
|
+
: OptionsToFunction<
|
|
77
|
+
GetList,
|
|
78
|
+
R['getList'],
|
|
79
|
+
EndpointToFunction<R['getList']>
|
|
80
|
+
>,
|
|
81
|
+
R['getList']['schema']
|
|
82
|
+
> &
|
|
83
|
+
Readonly<GetList>;
|
|
84
|
+
readonly update?: RestEndpointOptions<
|
|
85
|
+
unknown extends Update
|
|
86
|
+
? EndpointToFunction<R['update']>
|
|
87
|
+
: OptionsToFunction<Update, R['update'], EndpointToFunction<R['update']>>,
|
|
88
|
+
R['update']['schema']
|
|
89
|
+
> &
|
|
90
|
+
Readonly<Update>;
|
|
91
|
+
readonly partialUpdate?: RestEndpointOptions<
|
|
92
|
+
unknown extends PartialUpdate
|
|
93
|
+
? EndpointToFunction<R['partialUpdate']>
|
|
94
|
+
: OptionsToFunction<
|
|
95
|
+
PartialUpdate,
|
|
96
|
+
R['partialUpdate'],
|
|
97
|
+
EndpointToFunction<R['partialUpdate']>
|
|
98
|
+
>,
|
|
99
|
+
R['partialUpdate']['schema']
|
|
100
|
+
> &
|
|
101
|
+
Readonly<PartialUpdate>;
|
|
102
|
+
readonly delete?: RestEndpointOptions<
|
|
103
|
+
unknown extends Delete
|
|
104
|
+
? EndpointToFunction<R['delete']>
|
|
105
|
+
: OptionsToFunction<Delete, R['delete'], EndpointToFunction<R['delete']>>,
|
|
106
|
+
R['delete']['schema']
|
|
107
|
+
> &
|
|
108
|
+
Readonly<Delete>;
|
|
109
|
+
}
|
package/src/resourceTypes.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
|
+
import type { schema } from '@data-client/endpoint';
|
|
1
2
|
import type { Schema } from '@data-client/endpoint';
|
|
3
|
+
import type { Denormalize } from '@data-client/endpoint';
|
|
2
4
|
|
|
3
|
-
import type { ResourcePath } from './pathTypes.js';
|
|
4
|
-
import
|
|
5
|
+
import type { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
|
|
6
|
+
import { Extendable } from './resourceExtendable.js';
|
|
7
|
+
import RestEndpoint, {
|
|
8
|
+
GetEndpoint,
|
|
9
|
+
MutateEndpoint,
|
|
10
|
+
RestInstanceBase,
|
|
11
|
+
RestTypeNoBody,
|
|
12
|
+
} from './RestEndpoint.js';
|
|
5
13
|
|
|
14
|
+
/** The typed (generic) options for a Resource */
|
|
6
15
|
export interface ResourceGenerics {
|
|
7
16
|
/** @see https://resthooks.io/rest/api/createResource#path */
|
|
8
17
|
readonly path: ResourcePath;
|
|
9
18
|
/** @see https://resthooks.io/rest/api/createResource#schema */
|
|
10
19
|
readonly schema: Schema;
|
|
11
|
-
/** @see https://resthooks.io/rest/api/createResource#paginationfield */
|
|
12
|
-
readonly paginationField?: string;
|
|
13
20
|
/** Only used for types */
|
|
14
21
|
/** @see https://dataclient.io/rest/api/createResource#body */
|
|
15
22
|
readonly body?: any;
|
|
@@ -17,6 +24,7 @@ export interface ResourceGenerics {
|
|
|
17
24
|
/** @see https://resthooks.io/rest/api/createResource#searchParams */
|
|
18
25
|
readonly searchParams?: any;
|
|
19
26
|
}
|
|
27
|
+
/** The untyped options for createResource() */
|
|
20
28
|
export interface ResourceOptions {
|
|
21
29
|
/** @see https://resthooks.io/rest/api/createResource#endpoint */
|
|
22
30
|
Endpoint?: typeof RestEndpoint;
|
|
@@ -40,3 +48,110 @@ export interface ResourceOptions {
|
|
|
40
48
|
/** Determines whether to throw or fallback to */
|
|
41
49
|
errorPolicy?(error: any): 'hard' | 'soft' | undefined;
|
|
42
50
|
}
|
|
51
|
+
|
|
52
|
+
/** Resources are a collection of methods for a given data model.
|
|
53
|
+
* @see https://resthooks.io/rest/api/createResource
|
|
54
|
+
*/
|
|
55
|
+
export interface Resource<
|
|
56
|
+
O extends ResourceGenerics = { path: ResourcePath; schema: any },
|
|
57
|
+
> extends Extendable<O> {
|
|
58
|
+
/** Get a singular item
|
|
59
|
+
*
|
|
60
|
+
* @see https://resthooks.io/rest/api/createResource#get
|
|
61
|
+
*/
|
|
62
|
+
get: GetEndpoint<{ path: O['path']; schema: O['schema'] }>;
|
|
63
|
+
/** Get a list of item
|
|
64
|
+
*
|
|
65
|
+
* @see https://resthooks.io/rest/api/createResource#getlist
|
|
66
|
+
*/
|
|
67
|
+
getList: 'searchParams' extends keyof O
|
|
68
|
+
? GetEndpoint<{
|
|
69
|
+
path: ShortenPath<O['path']>;
|
|
70
|
+
schema: schema.Collection<[O['schema']]>;
|
|
71
|
+
body: 'body' extends keyof O
|
|
72
|
+
? O['body']
|
|
73
|
+
: Partial<Denormalize<O['schema']>>;
|
|
74
|
+
searchParams: O['searchParams'];
|
|
75
|
+
}>
|
|
76
|
+
: GetEndpoint<{
|
|
77
|
+
path: ShortenPath<O['path']>;
|
|
78
|
+
schema: schema.Collection<[O['schema']]>;
|
|
79
|
+
body: 'body' extends keyof O
|
|
80
|
+
? O['body']
|
|
81
|
+
: Partial<Denormalize<O['schema']>>;
|
|
82
|
+
searchParams: Record<string, number | string | boolean> | undefined;
|
|
83
|
+
}>;
|
|
84
|
+
/** Create a new item (POST)
|
|
85
|
+
*
|
|
86
|
+
* @deprecated use Resource.getList.push instead
|
|
87
|
+
*/
|
|
88
|
+
create: 'searchParams' extends keyof O
|
|
89
|
+
? MutateEndpoint<{
|
|
90
|
+
path: ShortenPath<O['path']>;
|
|
91
|
+
schema: schema.Collection<[O['schema']]>['push'];
|
|
92
|
+
body: 'body' extends keyof O
|
|
93
|
+
? O['body']
|
|
94
|
+
: Partial<Denormalize<O['schema']>>;
|
|
95
|
+
searchParams: O['searchParams'];
|
|
96
|
+
}>
|
|
97
|
+
: MutateEndpoint<{
|
|
98
|
+
path: ShortenPath<O['path']>;
|
|
99
|
+
schema: schema.Collection<[O['schema']]>['push'];
|
|
100
|
+
body: 'body' extends keyof O
|
|
101
|
+
? O['body']
|
|
102
|
+
: Partial<Denormalize<O['schema']>>;
|
|
103
|
+
}>;
|
|
104
|
+
/** Update an item (PUT)
|
|
105
|
+
*
|
|
106
|
+
* @see https://resthooks.io/rest/api/createResource#update
|
|
107
|
+
*/
|
|
108
|
+
update: 'body' extends keyof O
|
|
109
|
+
? MutateEndpoint<{
|
|
110
|
+
path: O['path'];
|
|
111
|
+
body: O['body'];
|
|
112
|
+
schema: O['schema'];
|
|
113
|
+
}>
|
|
114
|
+
: MutateEndpoint<{
|
|
115
|
+
path: O['path'];
|
|
116
|
+
body: Partial<Denormalize<O['schema']>> | FormData;
|
|
117
|
+
schema: O['schema'];
|
|
118
|
+
}>;
|
|
119
|
+
/** Update an item (PATCH)
|
|
120
|
+
*
|
|
121
|
+
* @see https://resthooks.io/rest/api/createResource#partialupdate
|
|
122
|
+
*/
|
|
123
|
+
partialUpdate: 'body' extends keyof O
|
|
124
|
+
? MutateEndpoint<{
|
|
125
|
+
path: O['path'];
|
|
126
|
+
body: Partial<O['body']>;
|
|
127
|
+
schema: O['schema'];
|
|
128
|
+
}>
|
|
129
|
+
: MutateEndpoint<{
|
|
130
|
+
path: O['path'];
|
|
131
|
+
body: Partial<Denormalize<O['schema']>> | FormData;
|
|
132
|
+
schema: O['schema'];
|
|
133
|
+
}>;
|
|
134
|
+
/** Delete an item (DELETE)
|
|
135
|
+
*
|
|
136
|
+
* @see https://resthooks.io/rest/api/createResource#delete
|
|
137
|
+
*/
|
|
138
|
+
delete: RestTypeNoBody<
|
|
139
|
+
PathArgs<O['path']>,
|
|
140
|
+
O['schema'] extends schema.EntityInterface & { process: any }
|
|
141
|
+
? schema.Invalidate<O['schema']>
|
|
142
|
+
: O['schema'],
|
|
143
|
+
undefined,
|
|
144
|
+
Partial<PathArgs<O['path']>>,
|
|
145
|
+
{
|
|
146
|
+
path: O['path'];
|
|
147
|
+
}
|
|
148
|
+
>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface ResourceInterface {
|
|
152
|
+
get: RestInstanceBase;
|
|
153
|
+
getList: RestInstanceBase;
|
|
154
|
+
update: RestInstanceBase;
|
|
155
|
+
partialUpdate: RestInstanceBase;
|
|
156
|
+
delete: RestInstanceBase;
|
|
157
|
+
}
|
package/ts4.0/RestEndpoint.d.ts
CHANGED
|
@@ -156,7 +156,7 @@ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schem
|
|
|
156
156
|
} = {
|
|
157
157
|
path: string;
|
|
158
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 & {
|
|
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, Omit<O, 'method'> & {
|
|
160
160
|
method: 'POST';
|
|
161
161
|
}>;
|
|
162
162
|
type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
|