@data-client/rest 0.1.0 → 0.3.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 +17 -16
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +7 -2
- package/legacy/OptionsToFunction.js +1 -1
- package/legacy/RestEndpoint.js +5 -5
- package/legacy/RestHelpers.js +4 -2
- package/legacy/createResource.js +14 -13
- package/legacy/index.js +2 -1
- package/legacy/next/index.js +4 -4
- package/legacy/pathTypes.js +1 -1
- package/legacy/resourceTypes.js +2 -0
- package/lib/OptionsToFunction.d.ts.map +1 -1
- package/lib/OptionsToFunction.js +1 -1
- package/lib/RestEndpoint.d.ts +30 -36
- package/lib/RestEndpoint.js +5 -5
- package/lib/RestHelpers.js +4 -2
- package/lib/createResource.d.ts +65 -32
- package/lib/createResource.d.ts.map +1 -1
- package/lib/createResource.js +11 -12
- package/lib/index.d.ts +3 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/next/index.d.ts +5 -5
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +4 -4
- package/lib/pathTypes.d.ts +1 -0
- package/lib/pathTypes.d.ts.map +1 -1
- package/lib/pathTypes.js +1 -1
- package/lib/{next/resourceTypes.d.ts → resourceTypes.d.ts} +12 -2
- package/lib/resourceTypes.d.ts.map +1 -0
- package/lib/resourceTypes.js +2 -0
- package/package.json +3 -3
- package/src/OptionsToFunction.ts +0 -1
- package/src/RestEndpoint.d.ts +30 -36
- package/src/RestEndpoint.js +4 -4
- package/src/RestHelpers.ts +4 -4
- package/src/__tests__/Resource.test.ts +2 -2
- package/src/__tests__/RestEndpoint.ts +38 -9
- package/src/__tests__/__snapshots__/createResource.test.ts.snap +2 -0
- package/src/__tests__/createResource.test.ts +25 -4
- package/src/__tests__/hookifyResource.ts +7 -3
- package/src/__tests__/types.test.ts +353 -212
- package/src/createResource.ts +111 -62
- package/src/index.ts +2 -2
- package/src/next/index.ts +5 -5
- package/src/pathTypes.ts +2 -0
- package/src/{next/resourceTypes.ts → resourceTypes.ts} +12 -2
- package/ts4.0/RestEndpoint.d.ts +19 -33
- package/ts4.0/createResource.d.ts +77 -32
- package/ts4.0/index.d.ts +3 -1
- package/ts4.0/next/index.d.ts +5 -5
- package/ts4.0/pathTypes.d.ts +2 -0
- package/ts4.0/{next/resourceTypes.d.ts → resourceTypes.d.ts} +12 -2
- package/legacy/next/OptionsToFunction.js +0 -2
- package/legacy/next/RestEndpoint.js +0 -225
- package/legacy/next/createResource.js +0 -86
- package/legacy/next/resourceTypes.js +0 -2
- package/lib/next/OptionsToFunction.d.ts +0 -7
- package/lib/next/OptionsToFunction.d.ts.map +0 -1
- package/lib/next/OptionsToFunction.js +0 -2
- package/lib/next/RestEndpoint.d.ts +0 -542
- package/lib/next/RestEndpoint.js +0 -229
- package/lib/next/createResource.d.ts +0 -86
- package/lib/next/createResource.d.ts.map +0 -1
- package/lib/next/createResource.js +0 -100
- package/lib/next/resourceTypes.d.ts.map +0 -1
- package/lib/next/resourceTypes.js +0 -2
- package/src/next/OptionsToFunction.ts +0 -41
- package/src/next/RestEndpoint.d.ts +0 -542
- package/src/next/RestEndpoint.js +0 -262
- package/src/next/__tests__/RestEndpoint.ts +0 -1333
- package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +0 -18
- package/src/next/__tests__/types.test.ts +0 -429
- package/src/next/createResource.ts +0 -194
- package/ts4.0/next/OptionsToFunction.d.ts +0 -16
- package/ts4.0/next/RestEndpoint.d.ts +0 -226
- package/ts4.0/next/createResource.d.ts +0 -94
package/src/createResource.ts
CHANGED
|
@@ -1,72 +1,59 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AbortOptimistic,
|
|
3
|
-
schema,
|
|
4
3
|
SnapshotInterface,
|
|
4
|
+
schema,
|
|
5
5
|
} from '@data-client/endpoint';
|
|
6
|
-
import type {
|
|
7
|
-
Schema,
|
|
8
|
-
Denormalize,
|
|
9
|
-
EndpointExtraOptions,
|
|
10
|
-
} from '@data-client/endpoint';
|
|
6
|
+
import type { Denormalize } from '@data-client/endpoint';
|
|
11
7
|
|
|
12
|
-
import { PathArgs, ShortenPath } from './pathTypes.js';
|
|
8
|
+
import { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
|
|
9
|
+
import { ResourceGenerics, ResourceOptions } from './resourceTypes.js';
|
|
13
10
|
import RestEndpoint, {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
GetEndpoint,
|
|
12
|
+
MutateEndpoint,
|
|
13
|
+
PaginationFieldEndpoint,
|
|
17
14
|
RestTypeNoBody,
|
|
18
15
|
} from './RestEndpoint.js';
|
|
19
16
|
import { shortenPath } from './RestHelpers.js';
|
|
20
17
|
|
|
21
|
-
const {
|
|
18
|
+
const { Invalidate, Collection } = schema;
|
|
22
19
|
|
|
23
20
|
/** Creates collection of Endpoints for common operations on a given data/schema.
|
|
24
21
|
*
|
|
25
22
|
* @see https://resthooks.io/rest/api/createResource
|
|
26
23
|
*/
|
|
27
|
-
export default function createResource<
|
|
24
|
+
export default function createResource<O extends ResourceGenerics>({
|
|
28
25
|
path,
|
|
29
26
|
schema,
|
|
30
27
|
Endpoint = RestEndpoint,
|
|
31
28
|
optimistic,
|
|
29
|
+
paginationField,
|
|
32
30
|
...extraOptions
|
|
33
|
-
}: {
|
|
34
|
-
readonly path: U;
|
|
35
|
-
readonly schema: S;
|
|
36
|
-
readonly Endpoint?: typeof RestEndpoint;
|
|
37
|
-
urlPrefix?: string;
|
|
38
|
-
optimistic?: boolean;
|
|
39
|
-
} & EndpointExtraOptions): Resource<U, S> {
|
|
31
|
+
}: Readonly<O> & ResourceOptions): Resource<O> {
|
|
40
32
|
const shortenedPath = shortenPath(path);
|
|
41
33
|
const getName = (name: string) => `${(schema as any)?.name}.${name}`;
|
|
42
34
|
const extraMutateOptions = { ...extraOptions };
|
|
43
35
|
const extraPartialOptions = { ...extraOptions };
|
|
44
|
-
const get:
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
36
|
+
const get: GetEndpoint<{ path: O['path']; schema: O['schema'] }> =
|
|
37
|
+
new Endpoint({
|
|
38
|
+
...extraOptions,
|
|
39
|
+
path,
|
|
40
|
+
schema,
|
|
41
|
+
name: getName('get'),
|
|
42
|
+
}) as any;
|
|
50
43
|
if (optimistic) {
|
|
51
44
|
(extraMutateOptions as any).getOptimisticResponse = optimisticUpdate;
|
|
52
45
|
(extraPartialOptions as any).getOptimisticResponse = optimisticPartial(get);
|
|
53
46
|
}
|
|
54
47
|
const getList = new Endpoint({
|
|
55
|
-
...
|
|
48
|
+
...extraMutateOptions,
|
|
56
49
|
path: shortenedPath,
|
|
57
|
-
schema: [schema],
|
|
50
|
+
schema: new Collection([schema as any]),
|
|
58
51
|
name: getName('getList'),
|
|
59
52
|
});
|
|
60
|
-
|
|
53
|
+
const ret = {
|
|
61
54
|
get,
|
|
62
55
|
getList,
|
|
63
|
-
create:
|
|
64
|
-
...extraMutateOptions,
|
|
65
|
-
path: shortenedPath,
|
|
66
|
-
schema,
|
|
67
|
-
method: 'POST',
|
|
68
|
-
name: getName('create'),
|
|
69
|
-
}),
|
|
56
|
+
create: getList.push.extend({ name: getName('create') }),
|
|
70
57
|
update: new Endpoint({
|
|
71
58
|
...extraMutateOptions,
|
|
72
59
|
path,
|
|
@@ -84,27 +71,30 @@ export default function createResource<U extends string, S extends Schema>({
|
|
|
84
71
|
delete: new Endpoint({
|
|
85
72
|
...extraMutateOptions,
|
|
86
73
|
path,
|
|
87
|
-
schema: (schema as any).process ? new
|
|
74
|
+
schema: (schema as any).process ? new Invalidate(schema as any) : schema,
|
|
88
75
|
method: 'DELETE',
|
|
89
76
|
name: getName('delete'),
|
|
90
|
-
process(res, params) {
|
|
77
|
+
process(res: any, params: any) {
|
|
91
78
|
return res && Object.keys(res).length ? res : params;
|
|
92
79
|
},
|
|
93
80
|
getOptimisticResponse: optimistic ? (optimisticDelete as any) : undefined,
|
|
94
81
|
}),
|
|
95
82
|
} as any;
|
|
83
|
+
if (paginationField) ret.getNextPage = getList.paginated(paginationField);
|
|
84
|
+
return ret;
|
|
96
85
|
}
|
|
97
86
|
|
|
98
87
|
function optimisticUpdate(snap: SnapshotInterface, params: any, body: any) {
|
|
99
88
|
return {
|
|
100
89
|
...params,
|
|
101
|
-
// even tho we don't always have two arguments, the extra one will simply be undefined which spreads fine
|
|
102
90
|
...body,
|
|
103
91
|
};
|
|
104
92
|
}
|
|
105
|
-
function optimisticPartial(
|
|
93
|
+
function optimisticPartial(
|
|
94
|
+
getEndpoint: GetEndpoint<{ path: ResourcePath; schema: any }>,
|
|
95
|
+
) {
|
|
106
96
|
return function (snap: SnapshotInterface, params: any, body: any) {
|
|
107
|
-
const { data } = snap.getResponse(getEndpoint, params);
|
|
97
|
+
const { data } = snap.getResponse(getEndpoint, params) as { data: any };
|
|
108
98
|
if (!data) throw new AbortOptimistic();
|
|
109
99
|
return {
|
|
110
100
|
...params,
|
|
@@ -118,55 +108,114 @@ function optimisticDelete(snap: SnapshotInterface, params: any) {
|
|
|
118
108
|
return params;
|
|
119
109
|
}
|
|
120
110
|
|
|
121
|
-
export interface Resource<
|
|
111
|
+
export interface Resource<
|
|
112
|
+
O extends ResourceGenerics = { path: ResourcePath; schema: any },
|
|
113
|
+
> {
|
|
122
114
|
/** Get a singular item
|
|
123
115
|
*
|
|
124
116
|
* @see https://resthooks.io/rest/api/createResource#get
|
|
125
117
|
*/
|
|
126
|
-
get:
|
|
118
|
+
get: GetEndpoint<{ path: O['path']; schema: O['schema'] }>;
|
|
127
119
|
/** Get a list of item
|
|
128
120
|
*
|
|
129
121
|
* @see https://resthooks.io/rest/api/createResource#getlist
|
|
130
122
|
*/
|
|
131
|
-
getList:
|
|
132
|
-
{
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
123
|
+
getList: 'searchParams' extends keyof O
|
|
124
|
+
? GetEndpoint<{
|
|
125
|
+
path: ShortenPath<O['path']>;
|
|
126
|
+
schema: schema.Collection<[O['schema']]>;
|
|
127
|
+
searchParams: O['searchParams'];
|
|
128
|
+
}>
|
|
129
|
+
: GetEndpoint<{
|
|
130
|
+
path: ShortenPath<O['path']>;
|
|
131
|
+
schema: schema.Collection<[O['schema']]>;
|
|
132
|
+
searchParams: Record<string, number | string | boolean> | undefined;
|
|
133
|
+
}>;
|
|
134
|
+
/** Get a list of item
|
|
135
|
+
*
|
|
136
|
+
* @see https://dataclient.io/rest/api/createResource#getNextPage
|
|
137
|
+
*/
|
|
138
|
+
getNextPage: 'paginationField' extends keyof O
|
|
139
|
+
? PaginationFieldEndpoint<
|
|
140
|
+
'searchParams' extends keyof O
|
|
141
|
+
? GetEndpoint<{
|
|
142
|
+
path: ShortenPath<O['path']>;
|
|
143
|
+
schema: schema.Collection<[O['schema']]>;
|
|
144
|
+
searchParams: O['searchParams'];
|
|
145
|
+
}>
|
|
146
|
+
: GetEndpoint<{
|
|
147
|
+
path: ShortenPath<O['path']>;
|
|
148
|
+
schema: schema.Collection<[O['schema']]>;
|
|
149
|
+
searchParams:
|
|
150
|
+
| Record<string, number | string | boolean>
|
|
151
|
+
| undefined;
|
|
152
|
+
}>,
|
|
153
|
+
Exclude<O['paginationField'], undefined>
|
|
154
|
+
>
|
|
155
|
+
: undefined;
|
|
138
156
|
/** Create a new item (POST)
|
|
139
157
|
*
|
|
140
158
|
* @see https://resthooks.io/rest/api/createResource#create
|
|
141
159
|
*/
|
|
142
|
-
create:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
160
|
+
create: 'searchParams' extends keyof O
|
|
161
|
+
? MutateEndpoint<{
|
|
162
|
+
path: ShortenPath<O['path']>;
|
|
163
|
+
schema: schema.Collection<[O['schema']]>['push'];
|
|
164
|
+
body: 'body' extends keyof O
|
|
165
|
+
? O['body']
|
|
166
|
+
: Partial<Denormalize<O['schema']>>;
|
|
167
|
+
searchParams: O['searchParams'];
|
|
168
|
+
}>
|
|
169
|
+
: MutateEndpoint<{
|
|
170
|
+
path: ShortenPath<O['path']>;
|
|
171
|
+
schema: schema.Collection<[O['schema']]>['push'];
|
|
172
|
+
body: 'body' extends keyof O
|
|
173
|
+
? O['body']
|
|
174
|
+
: Partial<Denormalize<O['schema']>>;
|
|
175
|
+
}>;
|
|
146
176
|
/** Update an item (PUT)
|
|
147
177
|
*
|
|
148
178
|
* @see https://resthooks.io/rest/api/createResource#update
|
|
149
179
|
*/
|
|
150
|
-
update:
|
|
180
|
+
update: 'body' extends keyof O
|
|
181
|
+
? MutateEndpoint<{
|
|
182
|
+
path: O['path'];
|
|
183
|
+
body: O['body'];
|
|
184
|
+
schema: O['schema'];
|
|
185
|
+
}>
|
|
186
|
+
: MutateEndpoint<{
|
|
187
|
+
path: O['path'];
|
|
188
|
+
body: Partial<Denormalize<O['schema']>>;
|
|
189
|
+
schema: O['schema'];
|
|
190
|
+
}>;
|
|
151
191
|
/** Update an item (PATCH)
|
|
152
192
|
*
|
|
153
193
|
* @see https://resthooks.io/rest/api/createResource#partialupdate
|
|
154
194
|
*/
|
|
155
|
-
partialUpdate:
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
195
|
+
partialUpdate: 'body' extends keyof O
|
|
196
|
+
? MutateEndpoint<{
|
|
197
|
+
path: O['path'];
|
|
198
|
+
body: Partial<O['body']>;
|
|
199
|
+
schema: O['schema'];
|
|
200
|
+
}>
|
|
201
|
+
: MutateEndpoint<{
|
|
202
|
+
path: O['path'];
|
|
203
|
+
body: Partial<Denormalize<O['schema']>>;
|
|
204
|
+
schema: O['schema'];
|
|
205
|
+
}>;
|
|
159
206
|
/** Delete an item (DELETE)
|
|
160
207
|
*
|
|
161
208
|
* @see https://resthooks.io/rest/api/createResource#delete
|
|
162
209
|
*/
|
|
163
210
|
delete: RestTypeNoBody<
|
|
164
|
-
PathArgs<
|
|
165
|
-
|
|
211
|
+
PathArgs<O['path']>,
|
|
212
|
+
O['schema'] extends schema.EntityInterface & { process: any }
|
|
213
|
+
? schema.Invalidate<O['schema']>
|
|
214
|
+
: O['schema'],
|
|
166
215
|
undefined,
|
|
167
|
-
Partial<PathArgs<
|
|
216
|
+
Partial<PathArgs<O['path']>>,
|
|
168
217
|
{
|
|
169
|
-
path:
|
|
218
|
+
path: O['path'];
|
|
170
219
|
}
|
|
171
220
|
>;
|
|
172
221
|
}
|
package/src/index.ts
CHANGED
|
@@ -7,8 +7,6 @@ export { default as RestEndpoint } from './RestEndpoint.js';
|
|
|
7
7
|
export type {
|
|
8
8
|
GetEndpoint,
|
|
9
9
|
MutateEndpoint,
|
|
10
|
-
NewGetEndpoint,
|
|
11
|
-
NewMutateEndpoint,
|
|
12
10
|
Defaults,
|
|
13
11
|
RestGenerics,
|
|
14
12
|
FetchGet,
|
|
@@ -23,6 +21,7 @@ export type {
|
|
|
23
21
|
} from './RestEndpoint.js';
|
|
24
22
|
export { default as createResource } from './createResource.js';
|
|
25
23
|
export type { Resource } from './createResource.js';
|
|
24
|
+
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
26
25
|
export { default as hookifyResource } from './hookifyResource.js';
|
|
27
26
|
export type {
|
|
28
27
|
HookResource,
|
|
@@ -40,3 +39,4 @@ export type {
|
|
|
40
39
|
} from './pathTypes.js';
|
|
41
40
|
|
|
42
41
|
export * from '@data-client/endpoint';
|
|
42
|
+
export * from './RestEndpoint.js';
|
package/src/next/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export { default as RestEndpoint } from '
|
|
3
|
-
export { default as createResource } from '
|
|
4
|
-
export type { Resource } from '
|
|
5
|
-
export type { ResourceOptions, ResourceGenerics } from '
|
|
1
|
+
export * from '../RestEndpoint.js';
|
|
2
|
+
export { default as RestEndpoint } from '../RestEndpoint.js';
|
|
3
|
+
export { default as createResource } from '../createResource.js';
|
|
4
|
+
export type { Resource } from '../createResource.js';
|
|
5
|
+
export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
|
package/src/pathTypes.ts
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
import type { Schema } from '@data-client/endpoint';
|
|
2
2
|
|
|
3
|
+
import type { ResourcePath } from './pathTypes.js';
|
|
3
4
|
import RestEndpoint from './RestEndpoint.js';
|
|
4
5
|
|
|
5
6
|
export interface ResourceGenerics {
|
|
6
|
-
|
|
7
|
+
/** @see https://resthooks.io/rest/api/createResource#path */
|
|
8
|
+
readonly path: ResourcePath;
|
|
9
|
+
/** @see https://resthooks.io/rest/api/createResource#schema */
|
|
7
10
|
readonly schema: Schema;
|
|
11
|
+
/** @see https://resthooks.io/rest/api/createResource#paginationfield */
|
|
12
|
+
readonly paginationField?: string;
|
|
8
13
|
/** Only used for types */
|
|
14
|
+
/** @see https://dataclient.io/rest/api/createResource#body */
|
|
9
15
|
readonly body?: any;
|
|
10
16
|
/** Only used for types */
|
|
17
|
+
/** @see https://resthooks.io/rest/api/createResource#searchParams */
|
|
11
18
|
readonly searchParams?: any;
|
|
12
19
|
}
|
|
13
20
|
export interface ResourceOptions {
|
|
21
|
+
/** @see https://resthooks.io/rest/api/createResource#endpoint */
|
|
14
22
|
Endpoint?: typeof RestEndpoint;
|
|
23
|
+
/** @see https://resthooks.io/rest/api/createResource#optimistic */
|
|
24
|
+
optimistic?: boolean;
|
|
25
|
+
/** @see https://resthooks.io/rest/api/createResource#urlprefix */
|
|
15
26
|
urlPrefix?: string;
|
|
16
27
|
requestInit?: RequestInit;
|
|
17
28
|
getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
@@ -28,5 +39,4 @@ export interface ResourceOptions {
|
|
|
28
39
|
readonly invalidIfStale?: boolean;
|
|
29
40
|
/** Determines whether to throw or fallback to */
|
|
30
41
|
errorPolicy?(error: any): 'hard' | 'soft' | undefined;
|
|
31
|
-
optimistic?: boolean;
|
|
32
42
|
}
|
package/ts4.0/RestEndpoint.d.ts
CHANGED
|
@@ -28,9 +28,9 @@ export interface RestInstanceBase<F extends FetchFunction = FetchFunction, S ext
|
|
|
28
28
|
/* before-fetch */
|
|
29
29
|
url(...args: Parameters<F>): string;
|
|
30
30
|
/** @see https://resthooks.io/rest/api/RestEndpoint#getRequestInit */
|
|
31
|
-
getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): RequestInit;
|
|
31
|
+
getRequestInit(this: any, body?: RequestInit['body'] | Record<string, unknown>): Promise<RequestInit> | RequestInit;
|
|
32
32
|
/** @see https://resthooks.io/rest/api/RestEndpoint#getHeaders */
|
|
33
|
-
getHeaders(headers: HeadersInit): HeadersInit;
|
|
33
|
+
getHeaders(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
34
34
|
/* after-fetch */
|
|
35
35
|
/** @see https://resthooks.io/rest/api/RestEndpoint#fetchResponse */
|
|
36
36
|
fetchResponse(input: RequestInfo, init: RequestInit): Promise<Response>;
|
|
@@ -63,25 +63,6 @@ export interface RestInstance<F extends FetchFunction = FetchFunction, S extends
|
|
|
63
63
|
unshift: AddEndpoint<F, S, O>;
|
|
64
64
|
assign: AddEndpoint<F, S, O>;
|
|
65
65
|
}
|
|
66
|
-
export type ContainsCollectionArray = {
|
|
67
|
-
push: any;
|
|
68
|
-
unshift: any;
|
|
69
|
-
} | {
|
|
70
|
-
[K: string]: ContainsCollectionArray;
|
|
71
|
-
} | {
|
|
72
|
-
schema: {
|
|
73
|
-
[K: string]: ContainsCollectionArray;
|
|
74
|
-
};
|
|
75
|
-
};
|
|
76
|
-
export type ContainsCollectionValues = {
|
|
77
|
-
assign: any;
|
|
78
|
-
} | {
|
|
79
|
-
[K: string]: ContainsCollectionValues;
|
|
80
|
-
} | {
|
|
81
|
-
schema: {
|
|
82
|
-
[K: string]: ContainsCollectionValues;
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
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>>;
|
|
86
67
|
type OptionsToRestEndpoint<O extends PartialRestGenerics, E extends RestInstanceBase & {
|
|
87
68
|
body?: any;
|
|
@@ -133,7 +114,7 @@ export type AddEndpoint<F extends FetchFunction = FetchFunction, S extends Schem
|
|
|
133
114
|
searchParams?: any;
|
|
134
115
|
} = {
|
|
135
116
|
path: string;
|
|
136
|
-
}> = 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,
|
|
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'>> & {
|
|
137
118
|
method: 'POST';
|
|
138
119
|
};
|
|
139
120
|
export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O ? O['body'] : O['method'] extends 'POST' | 'PUT' | 'PATCH' ? Record<string, unknown> | FormData : undefined;
|
|
@@ -217,24 +198,29 @@ export type FetchMutate<A extends readonly any[] = [
|
|
|
217
198
|
export type FetchGet<A extends readonly any[] = [
|
|
218
199
|
any
|
|
219
200
|
], R = any> = (this: RestInstance, ...args: A) => Promise<R>;
|
|
220
|
-
export type GetEndpoint<UrlParams = any, S extends Schema | undefined = Schema | undefined> = RestTypeNoBody<UrlParams, S, undefined>;
|
|
221
|
-
export type MutateEndpoint<UrlParams = any, Body extends BodyInit | Record<string, any> = any, S extends Schema | undefined = Schema | undefined> = RestTypeWithBody<UrlParams, S, true, Body>;
|
|
222
201
|
export type Defaults<O, D> = {
|
|
223
202
|
[K in keyof O | keyof D]: K extends keyof O ? Exclude<O[K], undefined> : D[Extract<K, keyof D>];
|
|
224
203
|
};
|
|
225
|
-
export type
|
|
226
|
-
path: string;
|
|
227
|
-
|
|
204
|
+
export type GetEndpoint<O extends {
|
|
205
|
+
readonly path: string;
|
|
206
|
+
readonly schema: Schema;
|
|
207
|
+
/** Only used for types */
|
|
208
|
+
readonly searchParams?: any;
|
|
228
209
|
} = {
|
|
229
210
|
path: string;
|
|
230
|
-
|
|
211
|
+
schema: Schema;
|
|
212
|
+
}> = 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 & {
|
|
231
213
|
body: undefined;
|
|
232
214
|
}>;
|
|
233
|
-
export type
|
|
234
|
-
path: string;
|
|
235
|
-
|
|
236
|
-
|
|
215
|
+
export type MutateEndpoint<O extends {
|
|
216
|
+
readonly path: string;
|
|
217
|
+
readonly schema: Schema;
|
|
218
|
+
/** Only used for types */
|
|
219
|
+
readonly searchParams?: any;
|
|
220
|
+
/** Only used for types */
|
|
221
|
+
readonly body?: any;
|
|
237
222
|
} = {
|
|
238
223
|
path: string;
|
|
239
224
|
body: any;
|
|
240
|
-
|
|
225
|
+
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>;
|
|
@@ -1,66 +1,111 @@
|
|
|
1
1
|
import { schema } from '@data-client/endpoint';
|
|
2
|
-
import type {
|
|
3
|
-
import { PathArgs, ShortenPath } from './pathTypes.js';
|
|
4
|
-
import
|
|
2
|
+
import type { Denormalize } from '@data-client/endpoint';
|
|
3
|
+
import { PathArgs, ResourcePath, ShortenPath } from './pathTypes.js';
|
|
4
|
+
import { ResourceGenerics, ResourceOptions } from './resourceTypes.js';
|
|
5
|
+
import { GetEndpoint, MutateEndpoint, PaginationFieldEndpoint, RestTypeNoBody } from './RestEndpoint.js';
|
|
5
6
|
/** Creates collection of Endpoints for common operations on a given data/schema.
|
|
6
7
|
*
|
|
7
8
|
* @see https://resthooks.io/rest/api/createResource
|
|
8
9
|
*/
|
|
9
|
-
export default function createResource<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
optimistic?: boolean;
|
|
15
|
-
} & EndpointExtraOptions): Resource<U, S>;
|
|
16
|
-
export interface Resource<U extends string, S extends Schema> {
|
|
10
|
+
export default function createResource<O extends ResourceGenerics>({ path, schema, Endpoint, optimistic, paginationField, ...extraOptions }: Readonly<O> & ResourceOptions): Resource<O>;
|
|
11
|
+
export interface Resource<O extends ResourceGenerics = {
|
|
12
|
+
path: ResourcePath;
|
|
13
|
+
schema: any;
|
|
14
|
+
}> {
|
|
17
15
|
/** Get a singular item
|
|
18
16
|
*
|
|
19
17
|
* @see https://resthooks.io/rest/api/createResource#get
|
|
20
18
|
*/
|
|
21
|
-
get:
|
|
22
|
-
path:
|
|
23
|
-
|
|
19
|
+
get: GetEndpoint<{
|
|
20
|
+
path: O['path'];
|
|
21
|
+
schema: O['schema'];
|
|
22
|
+
}>;
|
|
24
23
|
/** Get a list of item
|
|
25
24
|
*
|
|
26
25
|
* @see https://resthooks.io/rest/api/createResource#getlist
|
|
27
26
|
*/
|
|
28
|
-
getList:
|
|
29
|
-
path: ShortenPath<
|
|
27
|
+
getList: 'searchParams' extends keyof O ? GetEndpoint<{
|
|
28
|
+
path: ShortenPath<O['path']>;
|
|
29
|
+
schema: schema.Collection<[
|
|
30
|
+
O['schema']
|
|
31
|
+
]>;
|
|
32
|
+
searchParams: O['searchParams'];
|
|
33
|
+
}> : GetEndpoint<{
|
|
34
|
+
path: ShortenPath<O['path']>;
|
|
35
|
+
schema: schema.Collection<[
|
|
36
|
+
O['schema']
|
|
37
|
+
]>;
|
|
30
38
|
searchParams: Record<string, number | string | boolean> | undefined;
|
|
31
|
-
}
|
|
39
|
+
}>;
|
|
40
|
+
/** Get a list of item
|
|
41
|
+
*
|
|
42
|
+
* @see https://dataclient.io/rest/api/createResource#getNextPage
|
|
43
|
+
*/
|
|
44
|
+
getNextPage: 'paginationField' extends keyof O ? PaginationFieldEndpoint<'searchParams' extends keyof O ? GetEndpoint<{
|
|
45
|
+
path: ShortenPath<O['path']>;
|
|
46
|
+
schema: schema.Collection<[
|
|
47
|
+
O['schema']
|
|
48
|
+
]>;
|
|
49
|
+
searchParams: O['searchParams'];
|
|
50
|
+
}> : GetEndpoint<{
|
|
51
|
+
path: ShortenPath<O['path']>;
|
|
52
|
+
schema: schema.Collection<[
|
|
53
|
+
O['schema']
|
|
54
|
+
]>;
|
|
55
|
+
searchParams: Record<string, number | string | boolean> | undefined;
|
|
56
|
+
}>, Exclude<O['paginationField'], undefined>> : undefined;
|
|
32
57
|
/** Create a new item (POST)
|
|
33
58
|
*
|
|
34
59
|
* @see https://resthooks.io/rest/api/createResource#create
|
|
35
60
|
*/
|
|
36
|
-
create:
|
|
37
|
-
path: ShortenPath<
|
|
38
|
-
|
|
39
|
-
|
|
61
|
+
create: 'searchParams' extends keyof O ? MutateEndpoint<{
|
|
62
|
+
path: ShortenPath<O['path']>;
|
|
63
|
+
schema: schema.Collection<[
|
|
64
|
+
O['schema']
|
|
65
|
+
]>['push'];
|
|
66
|
+
body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
|
|
67
|
+
searchParams: O['searchParams'];
|
|
68
|
+
}> : MutateEndpoint<{
|
|
69
|
+
path: ShortenPath<O['path']>;
|
|
70
|
+
schema: schema.Collection<[
|
|
71
|
+
O['schema']
|
|
72
|
+
]>['push'];
|
|
73
|
+
body: 'body' extends keyof O ? O['body'] : Partial<Denormalize<O['schema']>>;
|
|
74
|
+
}>;
|
|
40
75
|
/** Update an item (PUT)
|
|
41
76
|
*
|
|
42
77
|
* @see https://resthooks.io/rest/api/createResource#update
|
|
43
78
|
*/
|
|
44
|
-
update:
|
|
45
|
-
path:
|
|
46
|
-
body:
|
|
47
|
-
|
|
79
|
+
update: 'body' extends keyof O ? MutateEndpoint<{
|
|
80
|
+
path: O['path'];
|
|
81
|
+
body: O['body'];
|
|
82
|
+
schema: O['schema'];
|
|
83
|
+
}> : MutateEndpoint<{
|
|
84
|
+
path: O['path'];
|
|
85
|
+
body: Partial<Denormalize<O['schema']>>;
|
|
86
|
+
schema: O['schema'];
|
|
87
|
+
}>;
|
|
48
88
|
/** Update an item (PATCH)
|
|
49
89
|
*
|
|
50
90
|
* @see https://resthooks.io/rest/api/createResource#partialupdate
|
|
51
91
|
*/
|
|
52
|
-
partialUpdate:
|
|
53
|
-
path:
|
|
54
|
-
body: Partial<
|
|
55
|
-
|
|
92
|
+
partialUpdate: 'body' extends keyof O ? MutateEndpoint<{
|
|
93
|
+
path: O['path'];
|
|
94
|
+
body: Partial<O['body']>;
|
|
95
|
+
schema: O['schema'];
|
|
96
|
+
}> : MutateEndpoint<{
|
|
97
|
+
path: O['path'];
|
|
98
|
+
body: Partial<Denormalize<O['schema']>>;
|
|
99
|
+
schema: O['schema'];
|
|
100
|
+
}>;
|
|
56
101
|
/** Delete an item (DELETE)
|
|
57
102
|
*
|
|
58
103
|
* @see https://resthooks.io/rest/api/createResource#delete
|
|
59
104
|
*/
|
|
60
|
-
delete: RestTypeNoBody<PathArgs<
|
|
105
|
+
delete: RestTypeNoBody<PathArgs<O['path']>, O['schema'] extends schema.EntityInterface & {
|
|
61
106
|
process: any;
|
|
62
|
-
} ? schema.
|
|
63
|
-
path:
|
|
107
|
+
} ? schema.Invalidate<O['schema']> : O['schema'], undefined, Partial<PathArgs<O['path']>>, {
|
|
108
|
+
path: O['path'];
|
|
64
109
|
}>;
|
|
65
110
|
}
|
|
66
111
|
//# sourceMappingURL=createResource.d.ts.map
|
package/ts4.0/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
export { default as RestEndpoint } from './RestEndpoint.js';
|
|
2
|
-
export type { GetEndpoint, MutateEndpoint,
|
|
2
|
+
export type { GetEndpoint, MutateEndpoint, Defaults, RestGenerics, FetchGet, FetchMutate, RestFetch, RestType, RestInstance, KeyofRestEndpoint, RestEndpointConstructorOptions, AddEndpoint, PaginationFieldEndpoint, } from './RestEndpoint.js';
|
|
3
3
|
export { default as createResource } from './createResource.js';
|
|
4
4
|
export type { Resource } from './createResource.js';
|
|
5
|
+
export type { ResourceOptions, ResourceGenerics } from './resourceTypes.js';
|
|
5
6
|
export { default as hookifyResource } from './hookifyResource.js';
|
|
6
7
|
export type { HookResource, HookableEndpointInterface, } from './hookifyResource.js';
|
|
7
8
|
export { default as NetworkError } from './NetworkError.js';
|
|
@@ -9,4 +10,5 @@ export { default as paginationUpdate } from './paginationUpdate.js';
|
|
|
9
10
|
export type { OptionsToFunction } from './OptionsToFunction.js';
|
|
10
11
|
export type { ShortenPath, PathArgs, PathKeys, KeysToArgs, PathArgsAndSearch, } from './pathTypes.js';
|
|
11
12
|
export * from '@data-client/endpoint';
|
|
13
|
+
export * from './RestEndpoint.js';
|
|
12
14
|
//# sourceMappingURL=index.d.ts.map
|
package/ts4.0/next/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export { default as RestEndpoint } from '
|
|
3
|
-
export { default as createResource } from '
|
|
4
|
-
export type { Resource } from '
|
|
5
|
-
export type { ResourceOptions, ResourceGenerics } from '
|
|
1
|
+
export * from '../RestEndpoint.js';
|
|
2
|
+
export { default as RestEndpoint } from '../RestEndpoint.js';
|
|
3
|
+
export { default as createResource } from '../createResource.js';
|
|
4
|
+
export type { Resource } from '../createResource.js';
|
|
5
|
+
export type { ResourceOptions, ResourceGenerics } from '../resourceTypes.js';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
package/ts4.0/pathTypes.d.ts
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import type { Schema } from '@data-client/endpoint';
|
|
2
|
+
import type { ResourcePath } from './pathTypes.js';
|
|
2
3
|
import RestEndpoint from './RestEndpoint.js';
|
|
3
4
|
export interface ResourceGenerics {
|
|
4
|
-
|
|
5
|
+
/** @see https://resthooks.io/rest/api/createResource#path */
|
|
6
|
+
readonly path: ResourcePath;
|
|
7
|
+
/** @see https://resthooks.io/rest/api/createResource#schema */
|
|
5
8
|
readonly schema: Schema;
|
|
9
|
+
/** @see https://resthooks.io/rest/api/createResource#paginationfield */
|
|
10
|
+
readonly paginationField?: string;
|
|
6
11
|
/** Only used for types */
|
|
12
|
+
/** @see https://dataclient.io/rest/api/createResource#body */
|
|
7
13
|
readonly body?: any;
|
|
8
14
|
/** Only used for types */
|
|
15
|
+
/** @see https://resthooks.io/rest/api/createResource#searchParams */
|
|
9
16
|
readonly searchParams?: any;
|
|
10
17
|
}
|
|
11
18
|
export interface ResourceOptions {
|
|
19
|
+
/** @see https://resthooks.io/rest/api/createResource#endpoint */
|
|
12
20
|
Endpoint?: typeof RestEndpoint;
|
|
21
|
+
/** @see https://resthooks.io/rest/api/createResource#optimistic */
|
|
22
|
+
optimistic?: boolean;
|
|
23
|
+
/** @see https://resthooks.io/rest/api/createResource#urlprefix */
|
|
13
24
|
urlPrefix?: string;
|
|
14
25
|
requestInit?: RequestInit;
|
|
15
26
|
getHeaders?(headers: HeadersInit): Promise<HeadersInit> | HeadersInit;
|
|
@@ -26,6 +37,5 @@ export interface ResourceOptions {
|
|
|
26
37
|
readonly invalidIfStale?: boolean;
|
|
27
38
|
/** Determines whether to throw or fallback to */
|
|
28
39
|
errorPolicy?(error: any): 'hard' | 'soft' | undefined;
|
|
29
|
-
optimistic?: boolean;
|
|
30
40
|
}
|
|
31
41
|
//# sourceMappingURL=resourceTypes.d.ts.map
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export {};
|
|
2
|
-
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sInNvdXJjZXMiOlsiLi4vLi4vc3JjL25leHQvT3B0aW9uc1RvRnVuY3Rpb24udHMiXSwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHR5cGUgeyBGZXRjaEZ1bmN0aW9uLCBSZXNvbHZlVHlwZSB9IGZyb20gJ0BkYXRhLWNsaWVudC9lbmRwb2ludCc7XG5cbmltcG9ydCB7XG4gIFBhcnRpYWxSZXN0R2VuZXJpY3MsXG4gIFJlc3RJbnN0YW5jZUJhc2UsXG4gIFJlc3RGZXRjaCxcbn0gZnJvbSAnLi9SZXN0RW5kcG9pbnQuanMnO1xuaW1wb3J0IHsgUGF0aEFyZ3MgfSBmcm9tICcuLi9wYXRoVHlwZXMuanMnO1xuXG5leHBvcnQgdHlwZSBPcHRpb25zVG9GdW5jdGlvbjxcbiAgTyBleHRlbmRzIFBhcnRpYWxSZXN0R2VuZXJpY3MsXG4gIEUgZXh0ZW5kcyBSZXN0SW5zdGFuY2VCYXNlICYgeyBib2R5PzogYW55IH0sXG4gIEYgZXh0ZW5kcyBGZXRjaEZ1bmN0aW9uLFxuPiA9ICdwYXRoJyBleHRlbmRzIGtleW9mIE9cbiAgPyBSZXN0RmV0Y2g8XG4gICAgICAnc2VhcmNoUGFyYW1zJyBleHRlbmRzIGtleW9mIE9cbiAgICAgICAgPyBPWydzZWFyY2hQYXJhbXMnXSAmIFBhdGhBcmdzPEV4Y2x1ZGU8T1sncGF0aCddLCB1bmRlZmluZWQ+PlxuICAgICAgICA6IFBhdGhBcmdzPEV4Y2x1ZGU8T1sncGF0aCddLCB1bmRlZmluZWQ+PixcbiAgICAgICdib2R5JyBleHRlbmRzIGtleW9mIE8gPyBPWydib2R5J10gOiBFWydib2R5J10sXG4gICAgICBPWydwcm9jZXNzJ10gZXh0ZW5kcyB7fSA/IFJldHVyblR5cGU8T1sncHJvY2VzcyddPiA6IFJlc29sdmVUeXBlPEY+XG4gICAgPlxuICA6ICdib2R5JyBleHRlbmRzIGtleW9mIE9cbiAgPyBSZXN0RmV0Y2g8XG4gICAgICAnc2VhcmNoUGFyYW1zJyBleHRlbmRzIGtleW9mIE9cbiAgICAgICAgPyBPWydzZWFyY2hQYXJhbXMnXSAmIFBhdGhBcmdzPEV4Y2x1ZGU8RVsncGF0aCddLCB1bmRlZmluZWQ+PlxuICAgICAgICA6IFBhdGhBcmdzPEV4Y2x1ZGU8RVsncGF0aCddLCB1bmRlZmluZWQ+PixcbiAgICAgIE9bJ2JvZHknXSxcbiAgICAgIE9bJ3Byb2Nlc3MnXSBleHRlbmRzIHt9ID8gUmV0dXJuVHlwZTxPWydwcm9jZXNzJ10+IDogUmVzb2x2ZVR5cGU8Rj5cbiAgICA+XG4gIDogJ3NlYXJjaFBhcmFtcycgZXh0ZW5kcyBrZXlvZiBPXG4gID8gUmVzdEZldGNoPFxuICAgICAgT1snc2VhcmNoUGFyYW1zJ10gJiBQYXRoQXJnczxFeGNsdWRlPEVbJ3BhdGgnXSwgdW5kZWZpbmVkPj4sXG4gICAgICBFWydib2R5J10sXG4gICAgICBPWydwcm9jZXNzJ10gZXh0ZW5kcyB7fSA/IFJldHVyblR5cGU8T1sncHJvY2VzcyddPiA6IFJlc29sdmVUeXBlPEY+XG4gICAgPlxuICA6IChcbiAgICAgIHRoaXM6IFRoaXNQYXJhbWV0ZXJUeXBlPEY+LFxuICAgICAgLi4uYXJnczogUGFyYW1ldGVyczxGPlxuICAgICkgPT4gUHJvbWlzZTxcbiAgICAgIE9bJ3Byb2Nlc3MnXSBleHRlbmRzIHt9ID8gUmV0dXJuVHlwZTxPWydwcm9jZXNzJ10+IDogUmVzb2x2ZVR5cGU8Rj5cbiAgICA+O1xuIl0sIm1hcHBpbmdzIjoiIn0=
|