@defjs/core 0.0.0-test-0007 → 0.0.0-test-0009
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.d.ts +26 -28
- package/dist/index.js +98 -107
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -82,7 +82,7 @@ export interface HttpProgressEvent {
|
|
|
82
82
|
*/
|
|
83
83
|
readonly total: number;
|
|
84
84
|
}
|
|
85
|
-
export type HttpProgressFn = (
|
|
85
|
+
export type HttpProgressFn = (event: HttpProgressEvent) => void;
|
|
86
86
|
export type TransformRequestFn = (request: HttpRequest) => HttpRequest["body"];
|
|
87
87
|
export type TransformResponseFn<Output = unknown> = (response: HttpResponse<unknown>) => Output;
|
|
88
88
|
export interface HttpRequest {
|
|
@@ -122,11 +122,23 @@ export type RequestOptions<Output = unknown, Input extends Field<any> | Record<P
|
|
|
122
122
|
transformRequest?: TransformRequestFn;
|
|
123
123
|
transformResponse?: TransformResponseFn<Output>;
|
|
124
124
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
export type DoRequestOptions = {
|
|
126
|
+
abort?: AbortSignal;
|
|
127
|
+
handler?: HttpHandler;
|
|
128
|
+
client?: ClientOptions;
|
|
129
|
+
timeout?: number;
|
|
130
|
+
};
|
|
131
|
+
export type DoRequestFn<Input = unknown, Output = unknown> = Input extends undefined ? (options?: DoRequestOptions) => Output : (input: RequestInputValue<Input>, options?: DoRequestOptions) => Output;
|
|
132
|
+
export type UseRequestFn<Output = unknown, Input = unknown> = () => {
|
|
133
|
+
doRequest: DoRequestFn<Input, Output>;
|
|
134
|
+
inputValue: Input extends Field<infer V> ? V : Input extends {
|
|
135
|
+
[K in keyof Input]: Field<any>;
|
|
136
|
+
} ? {
|
|
137
|
+
[K in keyof Input]: RequestInputValue<Input[K]>;
|
|
138
|
+
} : undefined;
|
|
139
|
+
setUploadProgress: (fn: HttpProgressFn) => void;
|
|
140
|
+
setDownloadProgress: (fn: HttpProgressFn) => void;
|
|
128
141
|
};
|
|
129
|
-
export type MakeRequestFn<Output = undefined, Input = undefined> = () => RequestRef<Output, Input>;
|
|
130
142
|
/** Observe: Body */
|
|
131
143
|
export declare function defineRequest<Output = unknown, Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
132
144
|
method: string;
|
|
@@ -140,7 +152,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
|
|
|
140
152
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
141
153
|
transformRequest?: TransformRequestFn;
|
|
142
154
|
transformResponse?: TransformResponseFn<Output>;
|
|
143
|
-
}):
|
|
155
|
+
}): UseRequestFn<Output, Input>;
|
|
144
156
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
145
157
|
method: string;
|
|
146
158
|
endpoint: string;
|
|
@@ -153,7 +165,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
153
165
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
154
166
|
transformRequest?: TransformRequestFn;
|
|
155
167
|
transformResponse?: TransformResponseFn;
|
|
156
|
-
}):
|
|
168
|
+
}): UseRequestFn<string, Input>;
|
|
157
169
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
158
170
|
method: string;
|
|
159
171
|
endpoint: string;
|
|
@@ -166,7 +178,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
166
178
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
167
179
|
transformRequest?: TransformRequestFn;
|
|
168
180
|
transformResponse?: TransformResponseFn;
|
|
169
|
-
}):
|
|
181
|
+
}): UseRequestFn<Blob, Input>;
|
|
170
182
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
171
183
|
method: string;
|
|
172
184
|
endpoint: string;
|
|
@@ -179,7 +191,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
179
191
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
180
192
|
transformRequest?: TransformRequestFn;
|
|
181
193
|
transformResponse?: TransformResponseFn;
|
|
182
|
-
}):
|
|
194
|
+
}): UseRequestFn<ArrayBuffer, Input>;
|
|
183
195
|
/** Observe: Response */
|
|
184
196
|
export declare function defineRequest<Output = unknown, Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
185
197
|
method: string;
|
|
@@ -193,7 +205,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
|
|
|
193
205
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
194
206
|
transformRequest?: TransformRequestFn;
|
|
195
207
|
transformResponse?: TransformResponseFn<Output>;
|
|
196
|
-
}):
|
|
208
|
+
}): UseRequestFn<HttpResponse<Output>, Input>;
|
|
197
209
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
198
210
|
method: string;
|
|
199
211
|
endpoint: string;
|
|
@@ -206,7 +218,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
206
218
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
207
219
|
transformRequest?: TransformRequestFn;
|
|
208
220
|
transformResponse?: TransformResponseFn;
|
|
209
|
-
}):
|
|
221
|
+
}): UseRequestFn<HttpResponse<string>, Input>;
|
|
210
222
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
211
223
|
method: string;
|
|
212
224
|
endpoint: string;
|
|
@@ -219,7 +231,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
219
231
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
220
232
|
transformRequest?: TransformRequestFn;
|
|
221
233
|
transformResponse?: TransformResponseFn;
|
|
222
|
-
}):
|
|
234
|
+
}): UseRequestFn<HttpResponse<Blob>, Input>;
|
|
223
235
|
export declare function defineRequest<Input extends Field<any> | Record<PropertyKey, Field<any>> | undefined = undefined>(options: {
|
|
224
236
|
method: string;
|
|
225
237
|
endpoint: string;
|
|
@@ -232,23 +244,9 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
232
244
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
233
245
|
transformRequest?: TransformRequestFn;
|
|
234
246
|
transformResponse?: TransformResponseFn;
|
|
235
|
-
}):
|
|
236
|
-
export declare function isRequestRef(value: unknown): value is RequestRef;
|
|
237
|
-
export type DoRequestOptions = {
|
|
238
|
-
uploadProgress?: HttpProgressFn;
|
|
239
|
-
downloadProgress?: HttpProgressFn;
|
|
240
|
-
abort?: AbortSignal;
|
|
241
|
-
handler?: HttpHandler;
|
|
242
|
-
client?: ClientOptions;
|
|
243
|
-
timeout?: number;
|
|
244
|
-
};
|
|
245
|
-
export type RequestRefOutput<T> = T extends RequestRef<infer O> ? O : undefined;
|
|
246
|
-
export type RequestRefInput<T> = T extends RequestRef<unknown, infer I> ? RequestInputValue<I> : never;
|
|
247
|
-
export type RequestRefWithInput<T> = T extends RequestRef<unknown, infer Input> ? (Input extends undefined ? false : true) : false;
|
|
248
|
-
export declare function doRequest<Ref extends RequestRef>(ref: RequestRefWithInput<Ref> extends true ? never : Ref, options?: DoRequestOptions): Promise<RequestRefOutput<Ref>>;
|
|
249
|
-
export declare function doRequest<Ref extends RequestRef>(ref: RequestRefWithInput<Ref> extends true ? Ref : never, input: RequestRefInput<Ref>, options?: DoRequestOptions): Promise<RequestRefOutput<Ref>>;
|
|
247
|
+
}): UseRequestFn<HttpResponse<ArrayBuffer>, Input>;
|
|
250
248
|
export type HttpHandler = (req: HttpRequest) => Promise<HttpResponse<unknown>>;
|
|
251
|
-
export declare
|
|
249
|
+
export declare function fetchHandler(httpRequest: HttpRequest): Promise<HttpResponse<unknown>>;
|
|
252
250
|
export interface ClientOptions {
|
|
253
251
|
host: string;
|
|
254
252
|
interceptors?: InterceptorFn[];
|
package/dist/index.js
CHANGED
|
@@ -134,27 +134,103 @@ function detectContentTypeHeader(body) {
|
|
|
134
134
|
return null;
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
|
+
function buildFieldValue(input) {
|
|
138
|
+
if (isField(input)) {
|
|
139
|
+
return input();
|
|
140
|
+
}
|
|
141
|
+
if (isFieldGroup(input)) {
|
|
142
|
+
const obj = {};
|
|
143
|
+
for (const [key, field3] of Object.entries(input)) {
|
|
144
|
+
obj[key] = field3();
|
|
145
|
+
}
|
|
146
|
+
return obj;
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
137
150
|
function defineRequest(options) {
|
|
151
|
+
const requestOptions = {
|
|
152
|
+
...options
|
|
153
|
+
};
|
|
154
|
+
const requiredInput = !!requestOptions.input;
|
|
138
155
|
return () => {
|
|
139
|
-
|
|
140
|
-
|
|
156
|
+
let uploadProgress;
|
|
157
|
+
let downloadProgress;
|
|
158
|
+
let abortController;
|
|
159
|
+
let abortSignal;
|
|
160
|
+
const setUploadProgress = (fn) => {
|
|
161
|
+
uploadProgress = fn;
|
|
162
|
+
};
|
|
163
|
+
const setDownloadProgress = (fn) => {
|
|
164
|
+
downloadProgress = fn;
|
|
165
|
+
};
|
|
166
|
+
const doRequest = async (input, options2) => {
|
|
167
|
+
if (requiredInput && !input) {
|
|
168
|
+
throw new Error("input is required");
|
|
169
|
+
}
|
|
170
|
+
if (options2?.abort) {
|
|
171
|
+
abortSignal = options2.abort;
|
|
172
|
+
} else {
|
|
173
|
+
abortController = new AbortController;
|
|
174
|
+
abortSignal = abortController.signal;
|
|
175
|
+
}
|
|
176
|
+
const clientOptions = getClientConfig(options2?.client || getGlobalClient());
|
|
177
|
+
const req = {
|
|
178
|
+
host: clientOptions?.host,
|
|
179
|
+
method: requestOptions.method,
|
|
180
|
+
endpoint: requestOptions.endpoint,
|
|
181
|
+
queryParams: new URLSearchParams,
|
|
182
|
+
headers: new Headers,
|
|
183
|
+
body: undefined,
|
|
184
|
+
withCredentials: requestOptions.withCredentials,
|
|
185
|
+
responseType: requestOptions.responseType ?? "json",
|
|
186
|
+
context: requestOptions.context || makeHttpContext(),
|
|
187
|
+
observe: requestOptions.observe ?? "body",
|
|
188
|
+
uploadProgress,
|
|
189
|
+
downloadProgress,
|
|
190
|
+
timeout: options2?.timeout || 0,
|
|
191
|
+
abort: abortSignal
|
|
192
|
+
};
|
|
193
|
+
const handler2 = options2?.handler || clientOptions?.handler || getGlobalHttpHandler();
|
|
194
|
+
if (!handler2) {
|
|
195
|
+
throw new Error("no handler");
|
|
196
|
+
}
|
|
197
|
+
if (requiredInput) {
|
|
198
|
+
await fillRequestFromField(req, requestOptions.input, input);
|
|
199
|
+
}
|
|
200
|
+
if (typeof requestOptions.transformRequest === "function") {
|
|
201
|
+
req.body = requestOptions.transformRequest(req);
|
|
202
|
+
}
|
|
203
|
+
const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
|
|
204
|
+
if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
|
|
205
|
+
setTimeout(() => {
|
|
206
|
+
if (!req.abort?.aborted) {
|
|
207
|
+
abortController?.abort("timeout");
|
|
208
|
+
}
|
|
209
|
+
}, req.timeout);
|
|
210
|
+
}
|
|
211
|
+
const res = await chain(req, handler2);
|
|
212
|
+
if (typeof requestOptions.transformResponse === "function") {
|
|
213
|
+
res.body = requestOptions.transformResponse(res);
|
|
214
|
+
}
|
|
215
|
+
switch (req.observe) {
|
|
216
|
+
case "body":
|
|
217
|
+
return res.body;
|
|
218
|
+
case "stream":
|
|
219
|
+
throw new Error("streaming not implemented");
|
|
220
|
+
case "response":
|
|
221
|
+
return res;
|
|
222
|
+
default:
|
|
223
|
+
throw new Error("invalid observe");
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
doRequest,
|
|
228
|
+
inputValue: buildFieldValue(requestOptions.input),
|
|
229
|
+
setUploadProgress,
|
|
230
|
+
setDownloadProgress
|
|
141
231
|
};
|
|
142
|
-
const ref = {};
|
|
143
|
-
Object.defineProperty(ref, REQUEST_REF, {
|
|
144
|
-
value: opts
|
|
145
|
-
});
|
|
146
|
-
return ref;
|
|
147
232
|
};
|
|
148
233
|
}
|
|
149
|
-
function isRequestRef(value) {
|
|
150
|
-
return !!value && typeof value === "object" && REQUEST_REF in value;
|
|
151
|
-
}
|
|
152
|
-
function getRequestRefOptions(value) {
|
|
153
|
-
if (!isRequestRef(value)) {
|
|
154
|
-
throw new Error("no request ref");
|
|
155
|
-
}
|
|
156
|
-
return value[REQUEST_REF];
|
|
157
|
-
}
|
|
158
234
|
function throwAliaNotSet() {
|
|
159
235
|
throw new Error("alias not set");
|
|
160
236
|
}
|
|
@@ -273,7 +349,8 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
273
349
|
case 1 /* Json */:
|
|
274
350
|
Object.defineProperty(json, valueKey, {
|
|
275
351
|
value,
|
|
276
|
-
writable: true
|
|
352
|
+
writable: true,
|
|
353
|
+
enumerable: true
|
|
277
354
|
});
|
|
278
355
|
break;
|
|
279
356
|
case 2 /* Query */:
|
|
@@ -308,89 +385,6 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
308
385
|
}
|
|
309
386
|
throw new Error("unsupported field type");
|
|
310
387
|
}
|
|
311
|
-
async function doRequest(...args) {
|
|
312
|
-
const argsLength = args.length;
|
|
313
|
-
let ref;
|
|
314
|
-
let input;
|
|
315
|
-
let options;
|
|
316
|
-
switch (argsLength) {
|
|
317
|
-
case 1: {
|
|
318
|
-
ref = args[0];
|
|
319
|
-
options = args[1];
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
case 2: {
|
|
323
|
-
ref = args[0];
|
|
324
|
-
input = args[1];
|
|
325
|
-
options = args[2];
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
default: {
|
|
329
|
-
throw new Error("invalid arguments");
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
const requestOptions = getRequestRefOptions(ref);
|
|
333
|
-
const requiredInput = !!requestOptions.input;
|
|
334
|
-
if (requiredInput && !input) {
|
|
335
|
-
throw new Error("input is required");
|
|
336
|
-
}
|
|
337
|
-
let abortController;
|
|
338
|
-
let abortSignal;
|
|
339
|
-
if (options?.abort) {
|
|
340
|
-
abortSignal = options.abort;
|
|
341
|
-
} else {
|
|
342
|
-
abortController = new AbortController;
|
|
343
|
-
abortSignal = abortController.signal;
|
|
344
|
-
}
|
|
345
|
-
const clientOptions = getClientConfig(options?.client || getGlobalClient());
|
|
346
|
-
const req = {
|
|
347
|
-
host: clientOptions?.host,
|
|
348
|
-
method: requestOptions.method,
|
|
349
|
-
endpoint: requestOptions.endpoint,
|
|
350
|
-
queryParams: new URLSearchParams,
|
|
351
|
-
headers: new Headers,
|
|
352
|
-
body: undefined,
|
|
353
|
-
withCredentials: requestOptions.withCredentials,
|
|
354
|
-
responseType: requestOptions.responseType ?? "json",
|
|
355
|
-
context: requestOptions.context || makeHttpContext(),
|
|
356
|
-
observe: requestOptions.observe ?? "body",
|
|
357
|
-
uploadProgress: options?.uploadProgress,
|
|
358
|
-
downloadProgress: options?.downloadProgress,
|
|
359
|
-
timeout: options?.timeout || 0,
|
|
360
|
-
abort: abortSignal
|
|
361
|
-
};
|
|
362
|
-
const handler2 = options?.handler || clientOptions?.handler || getGlobalHttpHandler();
|
|
363
|
-
if (!handler2) {
|
|
364
|
-
throw new Error("no handler");
|
|
365
|
-
}
|
|
366
|
-
await fillRequestFromField(req, requestOptions.input, input);
|
|
367
|
-
if (typeof requestOptions.transformRequest === "function") {
|
|
368
|
-
req.body = requestOptions.transformRequest(req);
|
|
369
|
-
}
|
|
370
|
-
const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
|
|
371
|
-
if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
|
|
372
|
-
setTimeout(() => {
|
|
373
|
-
if (!req.abort?.aborted) {
|
|
374
|
-
abortController.abort("timeout");
|
|
375
|
-
}
|
|
376
|
-
}, req.timeout);
|
|
377
|
-
}
|
|
378
|
-
const res = await chain(req, handler2);
|
|
379
|
-
if (typeof requestOptions.transformResponse === "function") {
|
|
380
|
-
res.body = requestOptions.transformResponse(res);
|
|
381
|
-
}
|
|
382
|
-
switch (req.observe) {
|
|
383
|
-
case "body":
|
|
384
|
-
return res.body;
|
|
385
|
-
case "stream":
|
|
386
|
-
throw new Error("streaming not implemented");
|
|
387
|
-
case "response":
|
|
388
|
-
return res;
|
|
389
|
-
default:
|
|
390
|
-
throw new Error("invalid observe");
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
var REQUEST_REF = Symbol("RequestRef");
|
|
394
388
|
|
|
395
389
|
// src/response.ts
|
|
396
390
|
function makeResponse(options) {
|
|
@@ -495,15 +489,15 @@ function getContentLength(headers) {
|
|
|
495
489
|
}
|
|
496
490
|
return num;
|
|
497
491
|
}
|
|
498
|
-
async function
|
|
492
|
+
async function fetchHandler(httpRequest) {
|
|
493
|
+
const { downloadProgress } = httpRequest;
|
|
499
494
|
const request2 = createRequest(httpRequest);
|
|
500
|
-
const downloadProgress = httpRequest.downloadProgress;
|
|
501
495
|
let response2;
|
|
502
496
|
try {
|
|
503
497
|
response2 = await (globalThis || window).fetch(request2);
|
|
504
498
|
} catch (error) {
|
|
505
499
|
throw makeResponse({
|
|
506
|
-
error: error instanceof Error ? error : new Error("
|
|
500
|
+
error: error instanceof Error ? error : new Error("Network Error"),
|
|
507
501
|
url: request2.url
|
|
508
502
|
});
|
|
509
503
|
}
|
|
@@ -531,7 +525,7 @@ async function _request(httpRequest) {
|
|
|
531
525
|
body = parseBody(httpRequest, response2, chunksAll);
|
|
532
526
|
} catch (error) {
|
|
533
527
|
throw makeResponse({
|
|
534
|
-
error: error instanceof Error ? error : new Error("
|
|
528
|
+
error: error instanceof Error ? error : new Error("Parse response body error"),
|
|
535
529
|
url,
|
|
536
530
|
status,
|
|
537
531
|
statusText,
|
|
@@ -549,7 +543,6 @@ async function _request(httpRequest) {
|
|
|
549
543
|
});
|
|
550
544
|
}
|
|
551
545
|
var FETCH_CONFIG_KEY = makeHttpContextToken(() => ({}));
|
|
552
|
-
var fetchHandler = _request;
|
|
553
546
|
// src/client.ts
|
|
554
547
|
function isClient(value) {
|
|
555
548
|
return typeof value === "object" && value !== null && CLIENT in value;
|
|
@@ -593,14 +586,12 @@ export {
|
|
|
593
586
|
makeResponse,
|
|
594
587
|
makeHttpContextToken,
|
|
595
588
|
makeHttpContext,
|
|
596
|
-
isRequestRef,
|
|
597
589
|
isFieldGroup,
|
|
598
590
|
isField,
|
|
599
591
|
isClient,
|
|
600
592
|
getGlobalClient,
|
|
601
593
|
field,
|
|
602
594
|
fetchHandler,
|
|
603
|
-
doRequest,
|
|
604
595
|
defineRequest,
|
|
605
596
|
createGlobalClient,
|
|
606
597
|
createClient
|