@data-client/rest 0.4.0 → 0.5.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/README.md +1 -1
- package/dist/index.js +39 -3
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +39 -3
- package/legacy/OptionsToFunction.js +1 -1
- package/legacy/createResource.js +34 -6
- 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/OptionsToFunction.d.ts +1 -1
- package/lib/OptionsToFunction.d.ts.map +1 -1
- package/lib/OptionsToFunction.js +1 -1
- package/lib/RestEndpoint.d.ts +33 -16
- package/lib/createResource.d.ts +2 -96
- package/lib/createResource.d.ts.map +1 -1
- package/lib/createResource.js +40 -4
- package/lib/index.d.ts +2 -2
- 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/OptionsToFunction.ts +1 -1
- package/src/RestEndpoint.d.ts +33 -16
- package/src/__tests__/createResource.test.ts +490 -19
- package/src/createResource.ts +54 -126
- package/src/index.ts +2 -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 +25 -7
- package/ts4.0/createResource.d.ts +2 -108
- package/ts4.0/index.d.ts +2 -2
- 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/OptionsToFunction.d.ts +1 -1
- package/ts4.1/RestEndpoint.d.ts +25 -7
- package/ts4.1/createResource.d.ts +2 -108
- package/ts4.1/index.d.ts +2 -2
- 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,8 +88,45 @@ 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
|
|
|
@@ -107,121 +153,3 @@ function optimisticPartial(
|
|
|
107
153
|
function optimisticDelete(snap: SnapshotInterface, params: any) {
|
|
108
154
|
return params;
|
|
109
155
|
}
|
|
110
|
-
|
|
111
|
-
export interface Resource<
|
|
112
|
-
O extends ResourceGenerics = { path: ResourcePath; schema: any },
|
|
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
|
-
>;
|
|
227
|
-
}
|
package/src/index.ts
CHANGED
|
@@ -18,9 +18,10 @@ 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
|
-
export type { Resource } from './
|
|
24
|
+
export type { Resource } from './resourceTypes.js';
|
|
24
25
|
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
25
26
|
export { default as hookifyResource } from './hookifyResource.js';
|
|
26
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']>>;
|
|
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']>>;
|
|
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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType,
|
|
2
|
+
import type { EndpointExtraOptions, EndpointInstanceInterface, Schema, FetchFunction, ResolveType, } from '@data-client/endpoint';
|
|
3
3
|
import type { ExtractCollection } from './extractCollection.js';
|
|
4
4
|
import { OptionsToBodyArgument, OptionsToFunction, } from './OptionsToFunction.js';
|
|
5
5
|
import { PathArgs } from './pathTypes.js';
|
|
@@ -12,7 +12,9 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
12
12
|
} = {
|
|
13
13
|
path: string;
|
|
14
14
|
}> extends EndpointInstanceInterface<F, S, M> {
|
|
15
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#body */
|
|
15
16
|
readonly body?: 'body' extends keyof O ? O['body'] : any;
|
|
17
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#searchParams */
|
|
16
18
|
readonly searchParams?: 'searchParams' extends keyof O ? O['searchParams'] : unknown;
|
|
17
19
|
/** Pattern to construct url based on Url Parameters
|
|
18
20
|
* @see https://resthooks.io/rest/api/RestEndpoint#path
|
|
@@ -23,6 +25,7 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
23
25
|
*/
|
|
24
26
|
readonly urlPrefix: string;
|
|
25
27
|
readonly requestInit: RequestInit;
|
|
28
|
+
/** @see https://resthooks.io/rest/api/RestEndpoint#method */
|
|
26
29
|
readonly method: (O & {
|
|
27
30
|
method: string;
|
|
28
31
|
})['method'];
|
|
@@ -49,11 +52,10 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
49
52
|
// this is just a hack to handle when no members of PartialRestGenerics are present
|
|
50
53
|
// Note: Using overloading (like paginated did) struggles because typescript does not have a clear way of distinguishing one
|
|
51
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
|
+
*/
|
|
52
58
|
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
59
|
}
|
|
58
60
|
export interface RestInstance<F extends FetchFunction = FetchFunction, S extends Schema | undefined = any, M extends true | undefined = true | undefined, O extends {
|
|
59
61
|
path: string;
|
|
@@ -63,12 +65,28 @@ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends
|
|
|
63
65
|
} = {
|
|
64
66
|
path: string;
|
|
65
67
|
}> extends RestInstanceBase<F, S, M, O> {
|
|
68
|
+
/** Endpoint to append the next page extending a list for pagination
|
|
69
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#paginated
|
|
70
|
+
*/
|
|
71
|
+
paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, A extends any[]>(this: E, removeCursor: (...args: A) => readonly [
|
|
72
|
+
...Parameters<E>
|
|
73
|
+
]): PaginationEndpoint<E, A>;
|
|
74
|
+
paginated<E extends RestInstanceBase<FetchFunction, any, undefined>, C extends string>(this: E, cursorField: C): PaginationFieldEndpoint<E, C>;
|
|
75
|
+
/** Endpoint that pushes (place at end) a newly created entity to this Collection
|
|
76
|
+
* @see https://dataclient.io/rest/api/RestEndpoint#push
|
|
77
|
+
*/
|
|
66
78
|
push: AddEndpoint<F, ExtractCollection<S>['push'], Exclude<O, 'body' | 'method'> & {
|
|
67
79
|
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
68
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
|
+
*/
|
|
69
84
|
unshift: AddEndpoint<F, ExtractCollection<S>['unshift'], Exclude<O, 'body' | 'method'> & {
|
|
70
85
|
body: OptionsToAdderBodyArgument<O> | OptionsToAdderBodyArgument<O>[] | FormData;
|
|
71
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
|
+
*/
|
|
72
90
|
assign: AddEndpoint<F, ExtractCollection<S>, Exclude<O, 'body' | 'method'> & {
|
|
73
91
|
body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
|
|
74
92
|
}>;
|
|
@@ -138,7 +156,7 @@ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schem
|
|
|
138
156
|
} = {
|
|
139
157
|
path: string;
|
|
140
158
|
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 & {
|
|
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'> & {
|
|
142
160
|
method: 'POST';
|
|
143
161
|
}>;
|
|
144
162
|
type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? O & {
|
|
@@ -259,5 +277,5 @@ export type MutateEndpoint<O extends {
|
|
|
259
277
|
schema: Schema;
|
|
260
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 & {
|
|
261
279
|
body: any;
|
|
262
|
-
method: 'POST' | 'PUT' | 'PATCH';
|
|
280
|
+
method: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
|
263
281
|
}>;
|