@data-client/rest 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/legacy/OptionsToFunction.js +1 -1
- package/legacy/createResource.js +1 -1
- package/legacy/extractCollection.js +1 -1
- package/legacy/extractObject.js +2 -0
- package/lib/OptionsToFunction.d.ts +8 -3
- package/lib/OptionsToFunction.d.ts.map +1 -1
- package/lib/OptionsToFunction.js +1 -1
- package/lib/RestEndpoint.d.ts +91 -31
- package/lib/createResource.d.ts +2 -0
- package/lib/createResource.d.ts.map +1 -1
- package/lib/createResource.js +1 -1
- package/lib/extractCollection.d.ts +7 -4
- package/lib/extractCollection.d.ts.map +1 -1
- package/lib/extractCollection.js +1 -1
- package/lib/extractObject.d.ts +6 -0
- package/lib/extractObject.d.ts.map +1 -0
- package/lib/extractObject.js +2 -0
- package/package.json +19 -5
- package/src/OptionsToFunction.ts +23 -9
- package/src/RestEndpoint.d.ts +91 -31
- package/src/__tests__/RestEndpoint.ts +10 -1
- package/src/__tests__/types.test.ts +6 -2
- package/src/createResource.ts +6 -0
- package/src/extractCollection.ts +16 -14
- package/src/extractObject.ts +7 -0
- package/ts4.0/OptionsToFunction.d.ts +7 -4
- package/ts4.0/RestEndpoint.d.ts +59 -22
- package/ts4.0/createResource.d.ts +2 -0
- package/ts4.0/deleteme.d.ts +0 -0
- package/ts4.0/extractCollection.d.ts +7 -4
- package/ts4.0/extractObject.d.ts +1 -0
- package/ts4.1/NetworkError.d.ts +11 -0
- package/ts4.1/OptionsToFunction.d.ts +12 -0
- package/ts4.1/RestEndpoint.d.ts +263 -0
- package/ts4.1/RestEndpointTypeHelp.d.ts +10 -0
- package/ts4.1/RestHelpers.d.ts +7 -0
- package/ts4.1/createResource.d.ts +113 -0
- package/ts4.1/deleteme.d.ts +0 -0
- package/ts4.1/extractCollection.d.ts +14 -0
- package/ts4.1/extractObject.d.ts +6 -0
- package/ts4.1/getArrayPath.d.ts +3 -0
- package/ts4.1/hookifyResource.d.ts +14 -0
- package/ts4.1/index.d.ts +14 -0
- package/ts4.1/mapCollection.d.ts +16 -0
- package/ts4.1/next/index.d.ts +6 -0
- package/ts4.1/paginatedCollections.d.ts +3 -0
- package/ts4.1/paginationUpdate.d.ts +10 -0
- package/ts4.1/paramsToString.d.ts +2 -0
- package/ts4.1/paramsToString.native.d.ts +2 -0
- package/ts4.1/pathTypes.d.ts +21 -0
- package/ts4.1/resourceTypes.d.ts +41 -0
- package/ts4.1/test-fixtures.d.ts +141 -0
- package/ts4.1/utiltypes.d.ts +21 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-client/rest",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Endpoints for REST APIs",
|
|
5
5
|
"homepage": "https://dataclient.io/rest",
|
|
6
6
|
"repository": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"model",
|
|
22
22
|
"resource",
|
|
23
23
|
"typescript",
|
|
24
|
+
"HTTP",
|
|
24
25
|
"path-to-regex",
|
|
25
26
|
"path template",
|
|
26
27
|
"data fetching",
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
"normalized cache",
|
|
30
31
|
"subject pattern",
|
|
31
32
|
"endpoint",
|
|
33
|
+
"mutation",
|
|
32
34
|
"react-native",
|
|
33
35
|
"ios",
|
|
34
36
|
"android",
|
|
@@ -42,7 +44,7 @@
|
|
|
42
44
|
"unpkg": "dist/index.umd.min.js",
|
|
43
45
|
"types": "lib/index.d.ts",
|
|
44
46
|
"typesVersions": {
|
|
45
|
-
">=
|
|
47
|
+
">=5.0": {
|
|
46
48
|
"": [
|
|
47
49
|
"lib/index.d.ts"
|
|
48
50
|
],
|
|
@@ -53,6 +55,17 @@
|
|
|
53
55
|
"lib/index.d.ts"
|
|
54
56
|
]
|
|
55
57
|
},
|
|
58
|
+
">=4.1": {
|
|
59
|
+
"": [
|
|
60
|
+
"ts4.1/index.d.ts"
|
|
61
|
+
],
|
|
62
|
+
"next": [
|
|
63
|
+
"ts4.1/next/index.d.ts"
|
|
64
|
+
],
|
|
65
|
+
"*": [
|
|
66
|
+
"ts4.1/index.d.ts"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
56
69
|
">=4.0": {
|
|
57
70
|
"": [
|
|
58
71
|
"ts4.0/index.d.ts"
|
|
@@ -90,6 +103,7 @@
|
|
|
90
103
|
"node.mjs",
|
|
91
104
|
"legacy",
|
|
92
105
|
"ts4.0",
|
|
106
|
+
"ts4.1",
|
|
93
107
|
"LICENSE",
|
|
94
108
|
"README.md"
|
|
95
109
|
],
|
|
@@ -99,8 +113,8 @@
|
|
|
99
113
|
"build:js:node": "BROWSERSLIST_ENV=node12 yarn g:rollup",
|
|
100
114
|
"build:js:browser": "BROWSERSLIST_ENV=legacy yarn g:rollup",
|
|
101
115
|
"build:bundle": "yarn g:runs build:js:\\* && echo '{\"type\":\"commonjs\"}' > dist/package.json",
|
|
102
|
-
"build:clean": "yarn g:clean ts4.0 index.d.ts next.d.ts",
|
|
103
|
-
"build:legacy-types": "yarn g:downtypes lib ts4.0 --to=4.0 && yarn g:copy --up 1 ./src-4.0-types/**/*.d.ts ./ts4.0",
|
|
116
|
+
"build:clean": "yarn g:clean ts4.0 ts4.1 index.d.ts next.d.ts",
|
|
117
|
+
"build:legacy-types": "yarn g:downtypes lib ts4.0 --to=4.0 && yarn g:downtypes lib ts4.1 --to=4.1 && yarn g:copy --up 1 ./src-4.1-types/**/*.d.ts ./ts4.0/ && yarn g:copy --up 1 ./src-4.1-types/**/*.d.ts ./ts4.1 && yarn g:copy --up 1 ./src-4.0-types/**/*.d.ts ./ts4.0",
|
|
104
118
|
"build": "run build:lib && run build:legacy:lib && run build:bundle",
|
|
105
119
|
"dev": "run build:lib -w",
|
|
106
120
|
"prepare": "run build:lib",
|
|
@@ -110,7 +124,7 @@
|
|
|
110
124
|
"license": "Apache-2.0",
|
|
111
125
|
"dependencies": {
|
|
112
126
|
"@babel/runtime": "^7.17.0",
|
|
113
|
-
"@data-client/endpoint": "^0.2.
|
|
127
|
+
"@data-client/endpoint": "^0.2.3",
|
|
114
128
|
"path-to-regexp": "^6.2.1"
|
|
115
129
|
},
|
|
116
130
|
"devDependencies": {
|
package/src/OptionsToFunction.ts
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
import type { FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
2
|
|
|
3
3
|
import { PathArgs } from './pathTypes.js';
|
|
4
|
-
import {
|
|
5
|
-
PartialRestGenerics,
|
|
6
|
-
RestInstanceBase,
|
|
7
|
-
RestFetch,
|
|
8
|
-
} from './RestEndpoint.js';
|
|
4
|
+
import { PartialRestGenerics, RestFetch } from './RestEndpoint.js';
|
|
9
5
|
|
|
10
6
|
export type OptionsToFunction<
|
|
11
7
|
O extends PartialRestGenerics,
|
|
12
|
-
E extends
|
|
8
|
+
E extends { body?: any; path?: string; method?: string },
|
|
13
9
|
F extends FetchFunction,
|
|
14
10
|
> = 'path' extends keyof O
|
|
15
11
|
? RestFetch<
|
|
16
12
|
'searchParams' extends keyof O
|
|
17
13
|
? O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
18
14
|
: PathArgs<Exclude<O['path'], undefined>>,
|
|
19
|
-
|
|
15
|
+
OptionsToBodyArgument<
|
|
16
|
+
'body' extends keyof O ? O : E,
|
|
17
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
18
|
+
>,
|
|
20
19
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
21
20
|
>
|
|
22
21
|
: 'body' extends keyof O
|
|
@@ -24,13 +23,19 @@ export type OptionsToFunction<
|
|
|
24
23
|
'searchParams' extends keyof O
|
|
25
24
|
? O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
|
|
26
25
|
: PathArgs<Exclude<E['path'], undefined>>,
|
|
27
|
-
|
|
26
|
+
OptionsToBodyArgument<
|
|
27
|
+
O,
|
|
28
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
29
|
+
>,
|
|
28
30
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
29
31
|
>
|
|
30
32
|
: 'searchParams' extends keyof O
|
|
31
33
|
? RestFetch<
|
|
32
34
|
O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
|
|
33
|
-
|
|
35
|
+
OptionsToBodyArgument<
|
|
36
|
+
E,
|
|
37
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
38
|
+
>,
|
|
34
39
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
35
40
|
>
|
|
36
41
|
: (
|
|
@@ -39,3 +44,12 @@ export type OptionsToFunction<
|
|
|
39
44
|
) => Promise<
|
|
40
45
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>
|
|
41
46
|
>;
|
|
47
|
+
|
|
48
|
+
export type OptionsToBodyArgument<
|
|
49
|
+
O extends { body?: any },
|
|
50
|
+
Method extends string | undefined,
|
|
51
|
+
> = Method extends 'POST' | 'PUT' | 'PATCH'
|
|
52
|
+
? 'body' extends keyof O
|
|
53
|
+
? O['body']
|
|
54
|
+
: any
|
|
55
|
+
: undefined;
|
package/src/RestEndpoint.d.ts
CHANGED
|
@@ -5,10 +5,14 @@ import type {
|
|
|
5
5
|
Schema,
|
|
6
6
|
FetchFunction,
|
|
7
7
|
ResolveType,
|
|
8
|
+
schema,
|
|
8
9
|
} from '@data-client/endpoint';
|
|
9
10
|
|
|
10
11
|
import type { ExtractCollection } from './extractCollection.js';
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
OptionsToBodyArgument,
|
|
14
|
+
OptionsToFunction,
|
|
15
|
+
} from './OptionsToFunction.js';
|
|
12
16
|
import { PathArgs } from './pathTypes.js';
|
|
13
17
|
import { EndpointUpdateFunction } from './RestEndpointTypeHelp.js';
|
|
14
18
|
|
|
@@ -20,6 +24,7 @@ export interface RestInstanceBase<
|
|
|
20
24
|
path: string;
|
|
21
25
|
body?: any;
|
|
22
26
|
searchParams?: any;
|
|
27
|
+
method?: string;
|
|
23
28
|
} = { path: string },
|
|
24
29
|
> extends EndpointInstanceInterface<F, S, M> {
|
|
25
30
|
readonly body?: 'body' extends keyof O ? O['body'] : any;
|
|
@@ -37,7 +42,7 @@ export interface RestInstanceBase<
|
|
|
37
42
|
*/
|
|
38
43
|
readonly urlPrefix: string;
|
|
39
44
|
readonly requestInit: RequestInit;
|
|
40
|
-
readonly method: string;
|
|
45
|
+
readonly method: (O & { method: string })['method'];
|
|
41
46
|
readonly signal: AbortSignal | undefined;
|
|
42
47
|
|
|
43
48
|
/* fetch lifecycles */
|
|
@@ -95,16 +100,41 @@ export interface RestInstance<
|
|
|
95
100
|
path: string;
|
|
96
101
|
body?: any;
|
|
97
102
|
searchParams?: any;
|
|
103
|
+
method?: string;
|
|
98
104
|
} = { path: string },
|
|
99
105
|
> extends RestInstanceBase<F, S, M, O> {
|
|
100
|
-
push: AddEndpoint<
|
|
101
|
-
|
|
102
|
-
|
|
106
|
+
push: AddEndpoint<
|
|
107
|
+
F,
|
|
108
|
+
ExtractCollection<S>['push'],
|
|
109
|
+
Exclude<O, 'body' | 'method'> & {
|
|
110
|
+
body:
|
|
111
|
+
| OptionsToAdderBodyArgument<O>
|
|
112
|
+
| OptionsToAdderBodyArgument<O>[]
|
|
113
|
+
| FormData;
|
|
114
|
+
}
|
|
115
|
+
>;
|
|
116
|
+
unshift: AddEndpoint<
|
|
117
|
+
F,
|
|
118
|
+
ExtractCollection<S>['unshift'],
|
|
119
|
+
Exclude<O, 'body' | 'method'> & {
|
|
120
|
+
body:
|
|
121
|
+
| OptionsToAdderBodyArgument<O>
|
|
122
|
+
| OptionsToAdderBodyArgument<O>[]
|
|
123
|
+
| FormData;
|
|
124
|
+
}
|
|
125
|
+
>;
|
|
126
|
+
assign: AddEndpoint<
|
|
127
|
+
F,
|
|
128
|
+
ExtractCollection<S>,
|
|
129
|
+
Exclude<O, 'body' | 'method'> & {
|
|
130
|
+
body: Record<string, OptionsToAdderBodyArgument<O>> | FormData;
|
|
131
|
+
}
|
|
132
|
+
>;
|
|
103
133
|
}
|
|
104
134
|
|
|
105
135
|
export type RestEndpointExtendOptions<
|
|
106
136
|
O extends PartialRestGenerics,
|
|
107
|
-
E extends
|
|
137
|
+
E extends { body?: any; path?: string; schema?: Schema; method?: string },
|
|
108
138
|
F extends FetchFunction,
|
|
109
139
|
> = RestEndpointOptions<
|
|
110
140
|
OptionsToFunction<O, E, F>,
|
|
@@ -125,7 +155,10 @@ type OptionsToRestEndpoint<
|
|
|
125
155
|
? PathArgs<Exclude<O['path'], undefined>>
|
|
126
156
|
: O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
127
157
|
: PathArgs<Exclude<O['path'], undefined>>,
|
|
128
|
-
|
|
158
|
+
OptionsToBodyArgument<
|
|
159
|
+
'body' extends keyof O ? O : E,
|
|
160
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
161
|
+
>,
|
|
129
162
|
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
130
163
|
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
131
164
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
@@ -135,6 +168,7 @@ type OptionsToRestEndpoint<
|
|
|
135
168
|
searchParams: 'searchParams' extends keyof O
|
|
136
169
|
? O['searchParams']
|
|
137
170
|
: E['searchParams'];
|
|
171
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
138
172
|
}
|
|
139
173
|
>
|
|
140
174
|
: 'body' extends keyof O
|
|
@@ -144,7 +178,10 @@ type OptionsToRestEndpoint<
|
|
|
144
178
|
? PathArgs<Exclude<O['path'], undefined>>
|
|
145
179
|
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>
|
|
146
180
|
: PathArgs<Exclude<E['path'], undefined>>,
|
|
147
|
-
|
|
181
|
+
OptionsToBodyArgument<
|
|
182
|
+
O,
|
|
183
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
184
|
+
>,
|
|
148
185
|
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
149
186
|
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
150
187
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
@@ -154,6 +191,7 @@ type OptionsToRestEndpoint<
|
|
|
154
191
|
searchParams: 'searchParams' extends keyof O
|
|
155
192
|
? O['searchParams']
|
|
156
193
|
: E['searchParams'];
|
|
194
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
157
195
|
}
|
|
158
196
|
>
|
|
159
197
|
: 'searchParams' extends keyof O
|
|
@@ -161,7 +199,10 @@ type OptionsToRestEndpoint<
|
|
|
161
199
|
O['searchParams'] extends undefined
|
|
162
200
|
? PathArgs<Exclude<O['path'], undefined>>
|
|
163
201
|
: O['searchParams'] & PathArgs<Exclude<E['path'], undefined>>,
|
|
164
|
-
|
|
202
|
+
OptionsToBodyArgument<
|
|
203
|
+
E,
|
|
204
|
+
'method' extends keyof O ? O['method'] : E['method']
|
|
205
|
+
>,
|
|
165
206
|
'schema' extends keyof O ? O['schema'] : E['schema'],
|
|
166
207
|
'method' extends keyof O ? MethodToSide<O['method']> : E['sideEffect'],
|
|
167
208
|
O['process'] extends {} ? ReturnType<O['process']> : ResolveType<F>,
|
|
@@ -169,6 +210,7 @@ type OptionsToRestEndpoint<
|
|
|
169
210
|
path: E['path'];
|
|
170
211
|
body: E['body'];
|
|
171
212
|
searchParams: O['searchParams'];
|
|
213
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
172
214
|
}
|
|
173
215
|
>
|
|
174
216
|
: RestInstance<
|
|
@@ -183,6 +225,7 @@ type OptionsToRestEndpoint<
|
|
|
183
225
|
searchParams: 'searchParams' extends keyof O
|
|
184
226
|
? O['searchParams']
|
|
185
227
|
: E['searchParams'];
|
|
228
|
+
method: 'method' extends keyof O ? O['method'] : E['method'];
|
|
186
229
|
}
|
|
187
230
|
>;
|
|
188
231
|
|
|
@@ -210,9 +253,9 @@ export interface PartialRestGenerics {
|
|
|
210
253
|
readonly schema?: Schema | undefined;
|
|
211
254
|
readonly method?: string;
|
|
212
255
|
/** Only used for types */
|
|
213
|
-
|
|
256
|
+
body?: any;
|
|
214
257
|
/** Only used for types */
|
|
215
|
-
|
|
258
|
+
searchParams?: any;
|
|
216
259
|
/** @see https://resthooks.io/rest/api/RestEndpoint#process */
|
|
217
260
|
process?(value: any, ...args: any): any;
|
|
218
261
|
}
|
|
@@ -221,7 +264,7 @@ export interface RestGenerics extends PartialRestGenerics {
|
|
|
221
264
|
}
|
|
222
265
|
|
|
223
266
|
export type PaginationEndpoint<
|
|
224
|
-
E extends
|
|
267
|
+
E extends FetchFunction & RestGenerics & { sideEffect?: true | undefined },
|
|
225
268
|
A extends any[],
|
|
226
269
|
> = RestInstance<
|
|
227
270
|
ParamFetchNoBody<A[0], ResolveType<E>>,
|
|
@@ -232,7 +275,7 @@ export type PaginationEndpoint<
|
|
|
232
275
|
}
|
|
233
276
|
>;
|
|
234
277
|
export type PaginationFieldEndpoint<
|
|
235
|
-
E extends
|
|
278
|
+
E extends FetchFunction & RestGenerics & { sideEffect?: true | undefined },
|
|
236
279
|
C extends string,
|
|
237
280
|
> = RestInstance<
|
|
238
281
|
ParamFetchNoBody<
|
|
@@ -251,9 +294,9 @@ export type AddEndpoint<
|
|
|
251
294
|
S extends Schema | undefined = any,
|
|
252
295
|
O extends {
|
|
253
296
|
path: string;
|
|
254
|
-
body
|
|
297
|
+
body: any;
|
|
255
298
|
searchParams?: any;
|
|
256
|
-
} = { path: string },
|
|
299
|
+
} = { path: string; body: any },
|
|
257
300
|
> = RestInstanceBase<
|
|
258
301
|
RestFetch<
|
|
259
302
|
'searchParams' extends keyof O
|
|
@@ -261,19 +304,13 @@ export type AddEndpoint<
|
|
|
261
304
|
? PathArgs<Exclude<O['path'], undefined>>
|
|
262
305
|
: O['searchParams'] & PathArgs<Exclude<O['path'], undefined>>
|
|
263
306
|
: PathArgs<Exclude<O['path'], undefined>>,
|
|
264
|
-
|
|
307
|
+
O['body'],
|
|
265
308
|
ResolveType<F>
|
|
266
309
|
>,
|
|
267
|
-
|
|
310
|
+
S,
|
|
268
311
|
true,
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
export type BodyDefault<O extends RestGenerics> = 'body' extends keyof O
|
|
273
|
-
? O['body']
|
|
274
|
-
: O['method'] extends 'POST' | 'PUT' | 'PATCH'
|
|
275
|
-
? Record<string, unknown> | FormData
|
|
276
|
-
: undefined;
|
|
312
|
+
O & { method: 'POST' }
|
|
313
|
+
>;
|
|
277
314
|
|
|
278
315
|
type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O
|
|
279
316
|
? O
|
|
@@ -281,6 +318,9 @@ type OptionsBodyDefault<O extends RestGenerics> = 'body' extends keyof O
|
|
|
281
318
|
? O & { body: any }
|
|
282
319
|
: O & { body: undefined };
|
|
283
320
|
|
|
321
|
+
type OptionsToAdderBodyArgument<O extends { body?: any }> =
|
|
322
|
+
'body' extends keyof O ? O['body'] : any;
|
|
323
|
+
|
|
284
324
|
export interface RestEndpointOptions<
|
|
285
325
|
F extends FetchFunction = FetchFunction,
|
|
286
326
|
S extends Schema | undefined = undefined,
|
|
@@ -309,7 +349,14 @@ export type RestEndpointConstructorOptions<O extends RestGenerics = any> =
|
|
|
309
349
|
? PathArgs<O['path']>
|
|
310
350
|
: O['searchParams'] & PathArgs<O['path']>
|
|
311
351
|
: PathArgs<O['path']>,
|
|
312
|
-
|
|
352
|
+
OptionsToBodyArgument<
|
|
353
|
+
O,
|
|
354
|
+
'method' extends keyof O
|
|
355
|
+
? O['method']
|
|
356
|
+
: O extends { sideEffect: true }
|
|
357
|
+
? 'POST'
|
|
358
|
+
: 'GET'
|
|
359
|
+
>,
|
|
313
360
|
O['process'] extends {}
|
|
314
361
|
? ReturnType<O['process']>
|
|
315
362
|
: any /*Denormalize<O['schema']>*/
|
|
@@ -330,14 +377,27 @@ export interface RestEndpointConstructor {
|
|
|
330
377
|
? PathArgs<O['path']>
|
|
331
378
|
: O['searchParams'] & PathArgs<O['path']>
|
|
332
379
|
: PathArgs<O['path']>,
|
|
333
|
-
|
|
380
|
+
OptionsToBodyArgument<
|
|
381
|
+
O,
|
|
382
|
+
'method' extends keyof O
|
|
383
|
+
? O['method']
|
|
384
|
+
: O extends { sideEffect: true }
|
|
385
|
+
? 'POST'
|
|
386
|
+
: 'GET'
|
|
387
|
+
>,
|
|
334
388
|
O['process'] extends {}
|
|
335
389
|
? ReturnType<O['process']>
|
|
336
390
|
: any /*Denormalize<O['schema']>*/
|
|
337
391
|
>,
|
|
338
392
|
'schema' extends keyof O ? O['schema'] : undefined,
|
|
339
|
-
|
|
340
|
-
|
|
393
|
+
'sideEffect' extends keyof O
|
|
394
|
+
? Extract<O['sideEffect'], undefined | true>
|
|
395
|
+
: MethodToSide<O['method']>,
|
|
396
|
+
'method' extends keyof O
|
|
397
|
+
? O
|
|
398
|
+
: O & {
|
|
399
|
+
method: O extends { sideEffect: true } ? 'POST' : 'GET';
|
|
400
|
+
}
|
|
341
401
|
>;
|
|
342
402
|
readonly prototype: RestInstanceBase;
|
|
343
403
|
}
|
|
@@ -512,7 +572,7 @@ export type GetEndpoint<
|
|
|
512
572
|
O['schema'],
|
|
513
573
|
undefined,
|
|
514
574
|
any,
|
|
515
|
-
O & {
|
|
575
|
+
O & { method: 'GET' }
|
|
516
576
|
>;
|
|
517
577
|
|
|
518
578
|
export type MutateEndpoint<
|
|
@@ -538,5 +598,5 @@ export type MutateEndpoint<
|
|
|
538
598
|
true,
|
|
539
599
|
O['body'],
|
|
540
600
|
any,
|
|
541
|
-
O
|
|
601
|
+
O & { body: any; method: 'POST' | 'PUT' | 'PATCH' }
|
|
542
602
|
>;
|
|
@@ -164,6 +164,7 @@ describe('RestEndpoint', () => {
|
|
|
164
164
|
const epbody = new RestEndpoint({
|
|
165
165
|
path: '/users/:id?/:group?',
|
|
166
166
|
body: { title: '' },
|
|
167
|
+
method: 'POST',
|
|
167
168
|
});
|
|
168
169
|
() => ep();
|
|
169
170
|
() => ep({ id: 5 });
|
|
@@ -483,6 +484,11 @@ describe('RestEndpoint', () => {
|
|
|
483
484
|
body: 5,
|
|
484
485
|
path: 'http\\://test.com/charmer/:charm',
|
|
485
486
|
});
|
|
487
|
+
() => {
|
|
488
|
+
// test type widening
|
|
489
|
+
const second = updateUser.extend({ body: { body: '' } });
|
|
490
|
+
second({ charm: 5 }, { body: 'hi' });
|
|
491
|
+
};
|
|
486
492
|
const response = await updateUser(
|
|
487
493
|
{ charm: 5 },
|
|
488
494
|
// @ts-expect-error
|
|
@@ -736,6 +742,7 @@ describe('RestEndpoint', () => {
|
|
|
736
742
|
.extend({
|
|
737
743
|
body: {} as { title: string },
|
|
738
744
|
dataExpiryLength: 0,
|
|
745
|
+
method: 'POST',
|
|
739
746
|
})
|
|
740
747
|
.extend({ dataExpiryLength: 5 });
|
|
741
748
|
() => newBody({ group: 'hi', id: 'what' }, { title: 'cool' });
|
|
@@ -864,10 +871,12 @@ describe('RestEndpoint', () => {
|
|
|
864
871
|
const endpoint = new RestEndpoint({
|
|
865
872
|
sideEffect: true,
|
|
866
873
|
path: 'http\\://test.com/article-cooler',
|
|
867
|
-
body: 0 as any,
|
|
868
874
|
schema: CoolerArticle,
|
|
869
875
|
}).extend({ name: 'createarticle' });
|
|
876
|
+
const a: true = endpoint.sideEffect;
|
|
877
|
+
const b: 'POST' = endpoint.method;
|
|
870
878
|
expect(endpoint.method).toBe('POST');
|
|
879
|
+
expect(endpoint.sideEffect).toBe(true);
|
|
871
880
|
const article = await endpoint(payload);
|
|
872
881
|
expect(article).toMatchInlineSnapshot(`
|
|
873
882
|
{
|
|
@@ -436,15 +436,19 @@ it('should handle more open ended type definitions', () => {
|
|
|
436
436
|
});
|
|
437
437
|
|
|
438
438
|
unknownParams({ hi: 5 });
|
|
439
|
+
unknownParams();
|
|
440
|
+
// @ts-expect-error
|
|
441
|
+
unknownParams(5);
|
|
439
442
|
|
|
440
443
|
const explicit: GetEndpoint<{
|
|
441
444
|
path: `${string}:${string}`;
|
|
442
|
-
schema: typeof User
|
|
445
|
+
schema: schema.Collection<[typeof User]>;
|
|
443
446
|
}> = new RestEndpoint({
|
|
444
447
|
path: '' as `${string}:${string}`,
|
|
445
|
-
schema: User,
|
|
448
|
+
schema: new schema.Collection([User]),
|
|
446
449
|
});
|
|
447
450
|
explicit({ hi: 5 });
|
|
448
451
|
explicit.push.process({} as any, { hi: 5 });
|
|
452
|
+
explicit.push();
|
|
449
453
|
};
|
|
450
454
|
});
|
package/src/createResource.ts
CHANGED
|
@@ -124,11 +124,17 @@ export interface Resource<
|
|
|
124
124
|
? GetEndpoint<{
|
|
125
125
|
path: ShortenPath<O['path']>;
|
|
126
126
|
schema: schema.Collection<[O['schema']]>;
|
|
127
|
+
body: 'body' extends keyof O
|
|
128
|
+
? O['body']
|
|
129
|
+
: Partial<Denormalize<O['schema']>>;
|
|
127
130
|
searchParams: O['searchParams'];
|
|
128
131
|
}>
|
|
129
132
|
: GetEndpoint<{
|
|
130
133
|
path: ShortenPath<O['path']>;
|
|
131
134
|
schema: schema.Collection<[O['schema']]>;
|
|
135
|
+
body: 'body' extends keyof O
|
|
136
|
+
? O['body']
|
|
137
|
+
: Partial<Denormalize<O['schema']>>;
|
|
132
138
|
searchParams: Record<string, number | string | boolean> | undefined;
|
|
133
139
|
}>;
|
|
134
140
|
/** Get a list of item
|
package/src/extractCollection.ts
CHANGED
|
@@ -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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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;
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import type { FetchFunction, ResolveType } from '@data-client/endpoint';
|
|
2
2
|
|
|
3
|
-
import { PartialRestGenerics
|
|
3
|
+
import { PartialRestGenerics } from './RestEndpoint.js';
|
|
4
4
|
|
|
5
5
|
export type OptionsToFunction<
|
|
6
6
|
O extends PartialRestGenerics,
|
|
7
|
-
E extends
|
|
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;
|