@defjs/core 0.3.0 → 0.3.1
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/index.d.ts +12 -12
- package/index.js +139 -204
- package/index.min.js +1 -1
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -68,7 +68,6 @@ export declare const ERR_TIMEOUT: Error;
|
|
|
68
68
|
export declare const ERR_NETWORK: Error;
|
|
69
69
|
export declare const ERR_NOT_FOUND_HANDLER: Error;
|
|
70
70
|
export declare const ERR_OBSERVE: Error;
|
|
71
|
-
export declare const ERR_NOT_SET_ALIAS: Error;
|
|
72
71
|
export declare const ERR_UNSUPPORTED_FIELD_TYPE: Error;
|
|
73
72
|
export declare const ERR_INVALID_CLIENT: Error;
|
|
74
73
|
export declare const ERR_NOT_FOUND_GLOBAL_CLIENT: Error;
|
|
@@ -116,6 +115,10 @@ export interface HttpProgressEvent {
|
|
|
116
115
|
readonly total: number;
|
|
117
116
|
}
|
|
118
117
|
export type HttpProgressFn = (event: HttpProgressEvent) => void;
|
|
118
|
+
/**
|
|
119
|
+
* No suitable scenario was found.
|
|
120
|
+
* export type TransformRequestFn<Input = unknown> = (input: Input, request: HttpRequest) => HttpRequest['body']
|
|
121
|
+
*/
|
|
119
122
|
export type TransformResponseFn<Output = unknown> = (response: HttpResponse<unknown>) => Output;
|
|
120
123
|
export interface HttpRequest {
|
|
121
124
|
host?: string;
|
|
@@ -134,10 +137,10 @@ export interface HttpRequest {
|
|
|
134
137
|
downloadProgress?: HttpProgressFn;
|
|
135
138
|
abort?: AbortSignal;
|
|
136
139
|
}
|
|
137
|
-
export type RequestInputValue<T> = T extends
|
|
140
|
+
export type RequestInputValue<T> = T extends {
|
|
138
141
|
[K in keyof T]: Field<any>;
|
|
139
142
|
} ? {
|
|
140
|
-
[K in keyof T]:
|
|
143
|
+
[K in keyof T]: T[K] extends Field<infer V> ? V : never;
|
|
141
144
|
} : never;
|
|
142
145
|
export type UseRequestOptions = {
|
|
143
146
|
abort?: AbortSignal;
|
|
@@ -145,24 +148,21 @@ export type UseRequestOptions = {
|
|
|
145
148
|
client?: Client;
|
|
146
149
|
timeout?: number;
|
|
147
150
|
};
|
|
151
|
+
export type IsOptionalObject<T> = Partial<T> extends T ? true : false;
|
|
148
152
|
export type UseRequestFn<Input, Output> = {
|
|
149
|
-
doRequest:
|
|
153
|
+
doRequest: IsOptionalObject<Input> extends true ? (input?: RequestInputValue<Input>) => Promise<HttpResponse<Output>> : (input: RequestInputValue<Input>) => Promise<HttpResponse<Output>>;
|
|
150
154
|
cancel: () => void;
|
|
151
|
-
getInitValue: () =>
|
|
152
|
-
[K in keyof Input]: Field<any>;
|
|
153
|
-
} ? {
|
|
154
|
-
[K in keyof Input]: RequestInputValue<Input[K]>;
|
|
155
|
-
} : undefined;
|
|
155
|
+
getInitValue: () => RequestInputValue<Input>;
|
|
156
156
|
setUploadProgress: (fn: HttpProgressFn) => void;
|
|
157
157
|
setDownloadProgress: (fn: HttpProgressFn) => void;
|
|
158
158
|
};
|
|
159
|
-
export interface DefineRequest<Input extends
|
|
159
|
+
export interface DefineRequest<Input extends Record<PropertyKey, Field<any>> | undefined = undefined, Output = unknown> {
|
|
160
160
|
(options?: UseRequestOptions): UseRequestFn<Input, Output>;
|
|
161
|
-
withField<I extends
|
|
161
|
+
withField<I extends Record<PropertyKey, Field<any>>>(value: I): DefineRequest<I, Output>;
|
|
162
162
|
withInterceptors(value: InterceptorFn[]): DefineRequest<Input, Output>;
|
|
163
163
|
withContext(value: HttpContext): DefineRequest<Input, Output>;
|
|
164
164
|
withCredentials(value: boolean): DefineRequest<Input, Output>;
|
|
165
|
-
withValidators(
|
|
165
|
+
withValidators(...fn: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[]): DefineRequest<Input, Output>;
|
|
166
166
|
withTransformResponse<O>(fn: TransformResponseFn<O>): DefineRequest<Input, O>;
|
|
167
167
|
withResponseType(value: "json"): DefineRequest<Input, Output>;
|
|
168
168
|
withResponseType(value: "text"): DefineRequest<Input, string>;
|
package/index.js
CHANGED
|
@@ -1,51 +1,14 @@
|
|
|
1
|
-
// src/handler/util.ts
|
|
2
|
-
function __getContentLength(headers) {
|
|
3
|
-
const value = headers.get("Content-Length");
|
|
4
|
-
if (!value) {
|
|
5
|
-
return 0;
|
|
6
|
-
}
|
|
7
|
-
const num = Number(value);
|
|
8
|
-
if (isNaN(num)) {
|
|
9
|
-
return 0;
|
|
10
|
-
}
|
|
11
|
-
return num;
|
|
12
|
-
}
|
|
13
|
-
function __getContentType(headers) {
|
|
14
|
-
return headers.get("Content-Type") || "";
|
|
15
|
-
}
|
|
16
|
-
function __parseBody(params) {
|
|
17
|
-
const { request, content, contentType } = params;
|
|
18
|
-
const responseType = request.responseType;
|
|
19
|
-
switch (responseType) {
|
|
20
|
-
case "json": {
|
|
21
|
-
const text = new TextDecoder().decode(content);
|
|
22
|
-
if (text === "") {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
return JSON.parse(text);
|
|
26
|
-
}
|
|
27
|
-
case "text":
|
|
28
|
-
return new TextDecoder().decode(content);
|
|
29
|
-
case "blob": {
|
|
30
|
-
return new Blob([content], { type: contentType });
|
|
31
|
-
}
|
|
32
|
-
case "arraybuffer":
|
|
33
|
-
return content.buffer;
|
|
34
|
-
default:
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function __concatChunks(chunks, totalLength) {
|
|
39
|
-
const chunksAll = new Uint8Array(totalLength);
|
|
40
|
-
let position = 0;
|
|
41
|
-
for (const chunk of chunks) {
|
|
42
|
-
chunksAll.set(chunk, position);
|
|
43
|
-
position += chunk.length;
|
|
44
|
-
}
|
|
45
|
-
return chunksAll;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
1
|
// src/response.ts
|
|
2
|
+
var ERR_ABORTED = new Error("ERR_ABORTED");
|
|
3
|
+
var ERR_TIMEOUT = new Error("ERR_TIMEOUT");
|
|
4
|
+
var ERR_NETWORK = new Error("ERR_NETWORK");
|
|
5
|
+
var ERR_NOT_FOUND_HANDLER = new Error("ERR_NOT_FOUND_HANDLER");
|
|
6
|
+
var ERR_OBSERVE = new Error("ERR_OBSERVE");
|
|
7
|
+
var ERR_UNSUPPORTED_FIELD_TYPE = new Error("ERR_UNSUPPORTED_FIELD_TYPE");
|
|
8
|
+
var ERR_INVALID_CLIENT = new Error("ERR_INVALID_CLIENT");
|
|
9
|
+
var ERR_NOT_FOUND_GLOBAL_CLIENT = new Error("ERR_NOT_FOUND_GLOBAL_CLIENT");
|
|
10
|
+
var ERR_INVALID_HTTP_CONTEXT_TOKEN = new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN");
|
|
11
|
+
var ERR_UNKNOWN = new Error("ERR_UNKNOWN");
|
|
49
12
|
function __makeResponse(options) {
|
|
50
13
|
const status = options?.status ?? 0;
|
|
51
14
|
const ok = status >= 200 && status < 300;
|
|
@@ -70,19 +33,9 @@ function __makeResponse(options) {
|
|
|
70
33
|
error
|
|
71
34
|
};
|
|
72
35
|
}
|
|
73
|
-
var ERR_ABORTED = new Error("ERR_ABORTED");
|
|
74
|
-
var ERR_TIMEOUT = new Error("ERR_TIMEOUT");
|
|
75
|
-
var ERR_NETWORK = new Error("ERR_NETWORK");
|
|
76
|
-
var ERR_NOT_FOUND_HANDLER = new Error("ERR_NOT_FOUND_HANDLER");
|
|
77
|
-
var ERR_OBSERVE = new Error("ERR_OBSERVE");
|
|
78
|
-
var ERR_NOT_SET_ALIAS = new Error("ERR_NOT_SET_ALIAS");
|
|
79
|
-
var ERR_UNSUPPORTED_FIELD_TYPE = new Error("ERR_UNSUPPORTED_FIELD_TYPE");
|
|
80
|
-
var ERR_INVALID_CLIENT = new Error("ERR_INVALID_CLIENT");
|
|
81
|
-
var ERR_NOT_FOUND_GLOBAL_CLIENT = new Error("ERR_NOT_FOUND_GLOBAL_CLIENT");
|
|
82
|
-
var ERR_INVALID_HTTP_CONTEXT_TOKEN = new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN");
|
|
83
|
-
var ERR_UNKNOWN = new Error("ERR_UNKNOWN");
|
|
84
36
|
|
|
85
37
|
// src/context.ts
|
|
38
|
+
var HTTP_CONTEXT_TOKEN = Symbol("HttpContextToken");
|
|
86
39
|
function makeHttpContextToken(defaultValue) {
|
|
87
40
|
Object.defineProperty(defaultValue, HTTP_CONTEXT_TOKEN, { value: defaultValue });
|
|
88
41
|
return defaultValue;
|
|
@@ -90,6 +43,7 @@ function makeHttpContextToken(defaultValue) {
|
|
|
90
43
|
function isHttpContextToken(value) {
|
|
91
44
|
return typeof value === "function" && HTTP_CONTEXT_TOKEN in value;
|
|
92
45
|
}
|
|
46
|
+
var HTTP_CONTEXT = Symbol("HttpContext");
|
|
93
47
|
function isHttpContext(value) {
|
|
94
48
|
return typeof value === "object" && value !== null && HTTP_CONTEXT in value;
|
|
95
49
|
}
|
|
@@ -144,10 +98,9 @@ function makeHttpContext(entries) {
|
|
|
144
98
|
[HTTP_CONTEXT]: ctx
|
|
145
99
|
};
|
|
146
100
|
}
|
|
147
|
-
var HTTP_CONTEXT_TOKEN = Symbol("HttpContextToken");
|
|
148
|
-
var HTTP_CONTEXT = Symbol("HttpContext");
|
|
149
101
|
|
|
150
102
|
// src/field.ts
|
|
103
|
+
var FIELD = Symbol("field");
|
|
151
104
|
function field(defaultValue) {
|
|
152
105
|
const meta = {
|
|
153
106
|
required: typeof defaultValue !== "undefined",
|
|
@@ -209,7 +162,6 @@ function isField(value) {
|
|
|
209
162
|
function isFieldGroup(value) {
|
|
210
163
|
return !!value && typeof value === "object" && Object.entries(value).every(([, v]) => isField(v));
|
|
211
164
|
}
|
|
212
|
-
var FIELD = Symbol("field");
|
|
213
165
|
|
|
214
166
|
// src/handler/handler.ts
|
|
215
167
|
var globalHttpHandler = undefined;
|
|
@@ -263,13 +215,10 @@ function __detectContentTypeHeader(body) {
|
|
|
263
215
|
}
|
|
264
216
|
}
|
|
265
217
|
function __buildFieldDefaultValue(input) {
|
|
266
|
-
if (isField(input)) {
|
|
267
|
-
return input();
|
|
268
|
-
}
|
|
269
218
|
if (isFieldGroup(input)) {
|
|
270
219
|
const obj = {};
|
|
271
|
-
for (const [key,
|
|
272
|
-
obj[key] =
|
|
220
|
+
for (const [key, field2] of Object.entries(input)) {
|
|
221
|
+
obj[key] = field2();
|
|
273
222
|
}
|
|
274
223
|
return obj;
|
|
275
224
|
}
|
|
@@ -278,11 +227,10 @@ function __buildFieldDefaultValue(input) {
|
|
|
278
227
|
function defineRequest(...args) {
|
|
279
228
|
let method;
|
|
280
229
|
let endpoint;
|
|
281
|
-
let
|
|
282
|
-
let field3;
|
|
230
|
+
let field2;
|
|
283
231
|
let interceptors = [];
|
|
284
232
|
let responseType = "json";
|
|
285
|
-
let
|
|
233
|
+
let context;
|
|
286
234
|
let withCredentials = false;
|
|
287
235
|
let validators = [];
|
|
288
236
|
let transformResponse;
|
|
@@ -302,10 +250,10 @@ function defineRequest(...args) {
|
|
|
302
250
|
}
|
|
303
251
|
const fn = (options) => {
|
|
304
252
|
const timeout = options?.timeout || 0;
|
|
305
|
-
const
|
|
306
|
-
const clientOptions = getClientConfig(
|
|
307
|
-
const
|
|
308
|
-
if (!
|
|
253
|
+
const client = options?.client || getGlobalClient();
|
|
254
|
+
const clientOptions = getClientConfig(client);
|
|
255
|
+
const handler = options?.handler || clientOptions?.handler || getGlobalHttpHandler();
|
|
256
|
+
if (!handler) {
|
|
309
257
|
throw ERR_NOT_FOUND_HANDLER;
|
|
310
258
|
}
|
|
311
259
|
let uploadProgress;
|
|
@@ -324,7 +272,7 @@ function defineRequest(...args) {
|
|
|
324
272
|
const cancel = () => {
|
|
325
273
|
abortController?.abort();
|
|
326
274
|
};
|
|
327
|
-
const getInitValue = () => __buildFieldDefaultValue(
|
|
275
|
+
const getInitValue = () => __buildFieldDefaultValue(field2);
|
|
328
276
|
const setUploadProgress = (fn2) => {
|
|
329
277
|
uploadProgress = fn2;
|
|
330
278
|
};
|
|
@@ -332,39 +280,36 @@ function defineRequest(...args) {
|
|
|
332
280
|
downloadProgress = fn2;
|
|
333
281
|
};
|
|
334
282
|
const doRequest = async (...args2) => {
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
343
|
-
if (validators.length > 0) {
|
|
344
|
-
for (const fn2 of validators) {
|
|
345
|
-
fn2(input);
|
|
346
|
-
}
|
|
283
|
+
let input;
|
|
284
|
+
if (args2.length === 1) {
|
|
285
|
+
input = args2[0];
|
|
286
|
+
}
|
|
287
|
+
if (validators.length > 0) {
|
|
288
|
+
for (const fn2 of validators) {
|
|
289
|
+
fn2(input);
|
|
347
290
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
291
|
+
}
|
|
292
|
+
const req = {
|
|
293
|
+
host: clientOptions?.host,
|
|
294
|
+
method,
|
|
295
|
+
endpoint,
|
|
296
|
+
queryParams: new URLSearchParams,
|
|
297
|
+
headers: new Headers,
|
|
298
|
+
body: undefined,
|
|
299
|
+
withCredentials,
|
|
300
|
+
responseType,
|
|
301
|
+
context: context || makeHttpContext(),
|
|
302
|
+
uploadProgress,
|
|
303
|
+
downloadProgress,
|
|
304
|
+
timeout,
|
|
305
|
+
abort: AbortSignal.any(abortSignal)
|
|
306
|
+
};
|
|
307
|
+
try {
|
|
308
|
+
if (input && field2) {
|
|
309
|
+
await __fillRequestFromField(req, field2, input);
|
|
365
310
|
}
|
|
366
311
|
const chain = makeInterceptorChain([...clientOptions.interceptors, ...interceptors]);
|
|
367
|
-
let res = await chain(req,
|
|
312
|
+
let res = await chain(req, handler);
|
|
368
313
|
if (typeof transformResponse === "function") {
|
|
369
314
|
res = __makeResponse({
|
|
370
315
|
...res,
|
|
@@ -385,8 +330,7 @@ function defineRequest(...args) {
|
|
|
385
330
|
};
|
|
386
331
|
};
|
|
387
332
|
fn.withField = (value) => {
|
|
388
|
-
|
|
389
|
-
requiredInput = true;
|
|
333
|
+
field2 = value;
|
|
390
334
|
return fn;
|
|
391
335
|
};
|
|
392
336
|
fn.withInterceptors = (value) => {
|
|
@@ -394,15 +338,15 @@ function defineRequest(...args) {
|
|
|
394
338
|
return fn;
|
|
395
339
|
};
|
|
396
340
|
fn.withContext = (value) => {
|
|
397
|
-
|
|
341
|
+
context = value;
|
|
398
342
|
return fn;
|
|
399
343
|
};
|
|
400
344
|
fn.withCredentials = (value) => {
|
|
401
345
|
withCredentials = value;
|
|
402
346
|
return fn;
|
|
403
347
|
};
|
|
404
|
-
fn.withValidators = (
|
|
405
|
-
validators =
|
|
348
|
+
fn.withValidators = (...fns) => {
|
|
349
|
+
validators = fns;
|
|
406
350
|
return fn;
|
|
407
351
|
};
|
|
408
352
|
fn.withTransformResponse = (value) => {
|
|
@@ -420,7 +364,7 @@ function __fillUrl(endpoint, params) {
|
|
|
420
364
|
return params.get(p1) ?? "undefined";
|
|
421
365
|
});
|
|
422
366
|
}
|
|
423
|
-
async function __fillRequestFromField(request,
|
|
367
|
+
async function __fillRequestFromField(request, fieldGroup, input) {
|
|
424
368
|
function inputIsObject(input2) {
|
|
425
369
|
return typeof input2 === "object" && input2 !== null;
|
|
426
370
|
}
|
|
@@ -433,9 +377,10 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
433
377
|
case typeof value === "boolean":
|
|
434
378
|
case typeof value === "number":
|
|
435
379
|
case typeof value === "string":
|
|
380
|
+
case typeof value === "undefined":
|
|
436
381
|
return String(value);
|
|
437
382
|
default:
|
|
438
|
-
throw new Error(
|
|
383
|
+
throw new Error(`Unsupported value type: ${typeof value}`);
|
|
439
384
|
}
|
|
440
385
|
}
|
|
441
386
|
function appendValue(sp, key, value) {
|
|
@@ -443,7 +388,9 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
443
388
|
for (const v of value) {
|
|
444
389
|
sp.append(key, serializeToString(v));
|
|
445
390
|
}
|
|
446
|
-
|
|
391
|
+
return sp;
|
|
392
|
+
}
|
|
393
|
+
if (typeof value !== "undefined") {
|
|
447
394
|
sp.set(key, serializeToString(value));
|
|
448
395
|
}
|
|
449
396
|
return sp;
|
|
@@ -453,8 +400,11 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
453
400
|
for (const v of value) {
|
|
454
401
|
setValue(sp, key, serializeToString(v));
|
|
455
402
|
}
|
|
403
|
+
return sp;
|
|
404
|
+
}
|
|
405
|
+
if (typeof value !== "undefined") {
|
|
406
|
+
sp.set(key, serializeToString(value));
|
|
456
407
|
}
|
|
457
|
-
sp.set(key, serializeToString(value));
|
|
458
408
|
return sp;
|
|
459
409
|
}
|
|
460
410
|
function getValidValue(value, defaultValue) {
|
|
@@ -467,66 +417,7 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
467
417
|
return value;
|
|
468
418
|
}
|
|
469
419
|
}
|
|
470
|
-
if (
|
|
471
|
-
let params;
|
|
472
|
-
let queryParams;
|
|
473
|
-
let headers;
|
|
474
|
-
let body = undefined;
|
|
475
|
-
const validValue = getValidValue(input, fieldOrFieldGroup());
|
|
476
|
-
const meta = __getFieldMetadata(fieldOrFieldGroup);
|
|
477
|
-
await doValid([...meta.validators, ...meta.asyncValidator], validValue);
|
|
478
|
-
for (const [type, aliasName] of meta.alias) {
|
|
479
|
-
switch (type) {
|
|
480
|
-
case 1 /* Query */: {
|
|
481
|
-
if (!aliasName) {
|
|
482
|
-
throw ERR_NOT_SET_ALIAS;
|
|
483
|
-
}
|
|
484
|
-
queryParams = appendValue(new URLSearchParams, aliasName, validValue);
|
|
485
|
-
break;
|
|
486
|
-
}
|
|
487
|
-
case 2 /* Param */: {
|
|
488
|
-
if (!aliasName) {
|
|
489
|
-
throw ERR_NOT_SET_ALIAS;
|
|
490
|
-
}
|
|
491
|
-
params = setValue(new Map, aliasName, validValue);
|
|
492
|
-
break;
|
|
493
|
-
}
|
|
494
|
-
case 3 /* Header */: {
|
|
495
|
-
if (!aliasName) {
|
|
496
|
-
throw ERR_NOT_SET_ALIAS;
|
|
497
|
-
}
|
|
498
|
-
headers = appendValue(new Headers, aliasName, validValue);
|
|
499
|
-
break;
|
|
500
|
-
}
|
|
501
|
-
case 4 /* Form */: {
|
|
502
|
-
if (!aliasName) {
|
|
503
|
-
throw ERR_NOT_SET_ALIAS;
|
|
504
|
-
}
|
|
505
|
-
body = appendValue(new FormData, aliasName, validValue);
|
|
506
|
-
break;
|
|
507
|
-
}
|
|
508
|
-
case 6 /* UrlForm */: {
|
|
509
|
-
if (!aliasName) {
|
|
510
|
-
throw ERR_NOT_SET_ALIAS;
|
|
511
|
-
}
|
|
512
|
-
body = appendValue(new URLSearchParams, aliasName, validValue);
|
|
513
|
-
break;
|
|
514
|
-
}
|
|
515
|
-
case 0 /* Json */:
|
|
516
|
-
case 5 /* Body */:
|
|
517
|
-
body = validValue;
|
|
518
|
-
break;
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
if (params) {
|
|
522
|
-
request.endpoint = __fillUrl(request.endpoint, params);
|
|
523
|
-
}
|
|
524
|
-
request.queryParams = queryParams;
|
|
525
|
-
request.headers = headers;
|
|
526
|
-
request.body = body;
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
if (isFieldGroup(fieldOrFieldGroup) && inputIsObject(input)) {
|
|
420
|
+
if (isFieldGroup(fieldGroup) && inputIsObject(input)) {
|
|
530
421
|
let params = new Map;
|
|
531
422
|
let queryParams = new URLSearchParams;
|
|
532
423
|
let headers = new Headers;
|
|
@@ -535,11 +426,11 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
535
426
|
const json = {};
|
|
536
427
|
let body = undefined;
|
|
537
428
|
let lastBodyField;
|
|
538
|
-
for (const [propertyKey,
|
|
539
|
-
const meta = __getFieldMetadata(
|
|
429
|
+
for (const [propertyKey, field2] of Object.entries(fieldGroup)) {
|
|
430
|
+
const meta = __getFieldMetadata(field2);
|
|
540
431
|
for (const [type, aliasName] of meta.alias) {
|
|
541
432
|
const valueKey = aliasName || propertyKey;
|
|
542
|
-
const value = getValidValue(input[propertyKey],
|
|
433
|
+
const value = getValidValue(input[propertyKey], field2());
|
|
543
434
|
await doValid([...meta.validators, ...meta.asyncValidator], value);
|
|
544
435
|
switch (type) {
|
|
545
436
|
case 0 /* Json */: {
|
|
@@ -595,20 +486,65 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
595
486
|
request.body = json;
|
|
596
487
|
break;
|
|
597
488
|
}
|
|
598
|
-
return;
|
|
599
489
|
}
|
|
600
|
-
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// src/handler/util.ts
|
|
493
|
+
function __getContentLength(headers) {
|
|
494
|
+
const value = headers.get("Content-Length");
|
|
495
|
+
if (!value) {
|
|
496
|
+
return 0;
|
|
497
|
+
}
|
|
498
|
+
const num = Number(value);
|
|
499
|
+
if (isNaN(num)) {
|
|
500
|
+
return 0;
|
|
501
|
+
}
|
|
502
|
+
return num;
|
|
503
|
+
}
|
|
504
|
+
function __getContentType(headers) {
|
|
505
|
+
return headers.get("Content-Type") || "";
|
|
506
|
+
}
|
|
507
|
+
function __parseBody(params) {
|
|
508
|
+
const { request, content, contentType } = params;
|
|
509
|
+
const responseType = request.responseType;
|
|
510
|
+
switch (responseType) {
|
|
511
|
+
case "json": {
|
|
512
|
+
const text = new TextDecoder().decode(content);
|
|
513
|
+
if (text === "") {
|
|
514
|
+
return null;
|
|
515
|
+
}
|
|
516
|
+
return JSON.parse(text);
|
|
517
|
+
}
|
|
518
|
+
case "text":
|
|
519
|
+
return new TextDecoder().decode(content);
|
|
520
|
+
case "blob": {
|
|
521
|
+
return new Blob([content], { type: contentType });
|
|
522
|
+
}
|
|
523
|
+
case "arraybuffer":
|
|
524
|
+
return content.buffer;
|
|
525
|
+
default:
|
|
526
|
+
return null;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
function __concatChunks(chunks, totalLength) {
|
|
530
|
+
const chunksAll = new Uint8Array(totalLength);
|
|
531
|
+
let position = 0;
|
|
532
|
+
for (const chunk of chunks) {
|
|
533
|
+
chunksAll.set(chunk, position);
|
|
534
|
+
position += chunk.length;
|
|
535
|
+
}
|
|
536
|
+
return chunksAll;
|
|
601
537
|
}
|
|
602
538
|
|
|
603
539
|
// src/handler/fetch/fetch.ts
|
|
604
|
-
function __createRequest(
|
|
605
|
-
const url = new URL(
|
|
606
|
-
if (
|
|
607
|
-
url.search =
|
|
540
|
+
function __createRequest(request) {
|
|
541
|
+
const url = new URL(request.endpoint, request.host);
|
|
542
|
+
if (request.queryParams) {
|
|
543
|
+
url.search = request.queryParams.toString();
|
|
608
544
|
}
|
|
609
|
-
const headers =
|
|
545
|
+
const headers = request.headers ?? new Headers;
|
|
610
546
|
if (!headers.has("Content-Type")) {
|
|
611
|
-
const detectedType = __detectContentTypeHeader(
|
|
547
|
+
const detectedType = __detectContentTypeHeader(request.body);
|
|
612
548
|
if (detectedType) {
|
|
613
549
|
headers.set("Content-Type", detectedType);
|
|
614
550
|
}
|
|
@@ -616,22 +552,22 @@ function __createRequest(request2) {
|
|
|
616
552
|
if (!headers.has("Accept")) {
|
|
617
553
|
headers.set("Accept", "application/json, text/plain, */*");
|
|
618
554
|
}
|
|
619
|
-
const credentials =
|
|
555
|
+
const credentials = request.withCredentials ? "include" : undefined;
|
|
620
556
|
return new Request(url, {
|
|
621
557
|
headers,
|
|
622
|
-
method:
|
|
623
|
-
body: __serializeBody(
|
|
624
|
-
signal:
|
|
558
|
+
method: request.method,
|
|
559
|
+
body: __serializeBody(request.body),
|
|
560
|
+
signal: request.abort,
|
|
625
561
|
credentials
|
|
626
562
|
});
|
|
627
563
|
}
|
|
628
564
|
async function fetchHandler(httpRequest) {
|
|
629
565
|
const downloadProgress = httpRequest.downloadProgress;
|
|
630
|
-
const
|
|
566
|
+
const request = __createRequest(httpRequest);
|
|
631
567
|
const abortSignal = httpRequest.abort;
|
|
632
|
-
let
|
|
568
|
+
let response;
|
|
633
569
|
try {
|
|
634
|
-
|
|
570
|
+
response = await globalThis.fetch(request);
|
|
635
571
|
} catch (error) {
|
|
636
572
|
if (abortSignal?.aborted && abortSignal.reason instanceof Error) {
|
|
637
573
|
switch (true) {
|
|
@@ -643,13 +579,13 @@ async function fetchHandler(httpRequest) {
|
|
|
643
579
|
}
|
|
644
580
|
return __makeResponse({ error });
|
|
645
581
|
}
|
|
646
|
-
const { headers, status, statusText, url } =
|
|
582
|
+
const { headers, status, statusText, url } = response;
|
|
647
583
|
const contentLength = __getContentLength(headers);
|
|
648
584
|
const contentType = __getContentType(headers);
|
|
649
585
|
let body = null;
|
|
650
|
-
if (
|
|
586
|
+
if (response.body) {
|
|
651
587
|
const chunks = [];
|
|
652
|
-
const reader =
|
|
588
|
+
const reader = response.body.getReader();
|
|
653
589
|
let receivedLength = 0;
|
|
654
590
|
while (true) {
|
|
655
591
|
const { done, value } = await reader.read();
|
|
@@ -690,14 +626,16 @@ async function fetchHandler(httpRequest) {
|
|
|
690
626
|
});
|
|
691
627
|
}
|
|
692
628
|
// src/client.ts
|
|
629
|
+
var CLIENT = Symbol("Client");
|
|
630
|
+
var globalClient;
|
|
693
631
|
function isClient(value) {
|
|
694
632
|
return typeof value === "object" && value !== null && CLIENT in value;
|
|
695
633
|
}
|
|
696
|
-
function getClientConfig(
|
|
697
|
-
if (!isClient(
|
|
634
|
+
function getClientConfig(client) {
|
|
635
|
+
if (!isClient(client)) {
|
|
698
636
|
throw ERR_INVALID_CLIENT;
|
|
699
637
|
}
|
|
700
|
-
return
|
|
638
|
+
return client[CLIENT];
|
|
701
639
|
}
|
|
702
640
|
function createClient(options) {
|
|
703
641
|
const conf = {
|
|
@@ -709,8 +647,8 @@ function createClient(options) {
|
|
|
709
647
|
[CLIENT]: conf
|
|
710
648
|
};
|
|
711
649
|
}
|
|
712
|
-
function cloneClient(
|
|
713
|
-
const preConf = getClientConfig(
|
|
650
|
+
function cloneClient(client, options) {
|
|
651
|
+
const preConf = getClientConfig(client);
|
|
714
652
|
const conf = {
|
|
715
653
|
...preConf,
|
|
716
654
|
...options
|
|
@@ -725,8 +663,8 @@ function getGlobalClient() {
|
|
|
725
663
|
}
|
|
726
664
|
return globalClient;
|
|
727
665
|
}
|
|
728
|
-
function setGlobalClient(
|
|
729
|
-
globalClient =
|
|
666
|
+
function setGlobalClient(client) {
|
|
667
|
+
globalClient = client;
|
|
730
668
|
}
|
|
731
669
|
function restGlobalClient() {
|
|
732
670
|
globalClient = undefined;
|
|
@@ -734,8 +672,6 @@ function restGlobalClient() {
|
|
|
734
672
|
function createGlobalClient(options) {
|
|
735
673
|
globalClient = createClient(options);
|
|
736
674
|
}
|
|
737
|
-
var CLIENT = Symbol("Client");
|
|
738
|
-
var globalClient;
|
|
739
675
|
// src/interceptor/authorization/basic_auth_interceptor.ts
|
|
740
676
|
function basicAuthInterceptor(fn, options) {
|
|
741
677
|
let encode = options?.encode;
|
|
@@ -850,7 +786,6 @@ export {
|
|
|
850
786
|
ERR_UNKNOWN,
|
|
851
787
|
ERR_TIMEOUT,
|
|
852
788
|
ERR_OBSERVE,
|
|
853
|
-
ERR_NOT_SET_ALIAS,
|
|
854
789
|
ERR_NOT_FOUND_HANDLER,
|
|
855
790
|
ERR_NOT_FOUND_GLOBAL_CLIENT,
|
|
856
791
|
ERR_NETWORK,
|
package/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function d(t){const n=t.get("Content-Length");if(!n)return 0;const r=Number(n);if(isNaN(r))return 0;return r}function a(t){return t.get("Content-Type")||""}function q(t){const{request:n,content:r,contentType:o}=t;switch(n.responseType){case"json":{const H=new TextDecoder().decode(r);if(H==="")return null;return JSON.parse(H)}case"text":return new TextDecoder().decode(r);case"blob":return new Blob([r],{type:o});case"arraybuffer":return r.buffer;default:return null}}function l(t,n){const r=new Uint8Array(n);let o=0;for(let T of t)r.set(T,o),o+=T.length;return r}function N(t){const n=t?.status??0,r=n>=200&&n<300,o=t?.statusText??"",T=t?.url??"",H=t?.headers??new Headers,f=t?.body??null;let R=t?.error;if(!R&&!r){let e=`Http failure response for ${T||"(unknown url)"}: ${n}`;if(o)e+=` - ${o}`;R=new Error(e)}return{status:n,statusText:o,url:T,headers:H,body:f,error:R}}var y=new Error("ERR_ABORTED"),K=new Error("ERR_TIMEOUT"),Ht=new Error("ERR_NETWORK"),X=new Error("ERR_NOT_FOUND_HANDLER"),Et=new Error("ERR_OBSERVE"),A=new Error("ERR_NOT_SET_ALIAS"),W=new Error("ERR_UNSUPPORTED_FIELD_TYPE"),J=new Error("ERR_INVALID_CLIENT"),j=new Error("ERR_NOT_FOUND_GLOBAL_CLIENT"),P=new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN"),pt=new Error("ERR_UNKNOWN");function wt(t){return Object.defineProperty(t,v,{value:t}),t}function V(t){return typeof t==="function"&&v in t}function Rt(t){return typeof t==="object"&&t!==null&&Q in t}function Y(t){const n=new Map;switch(!0){case Array.isArray(t):{for(let[r,o]of t)if(V(r))n.set(r,o);break}case Rt(t):{for(let[r,o]of t[Q])n.set(r,o);break}}return{set(r,o){if(!V(r))throw P;return n.set(r,o),this},get(r){if(!V(r))throw P;if(!n.has(r))n.set(r,r());return n.get(r)},del(r){return n.delete(r),this},has(r){return n.has(r)},keys(){return n.keys()},get length(){return Array.from(n.keys()).length},[Q]:n}}var v=Symbol("HttpContextToken"),Q=Symbol("HttpContext");function it(t){const n={required:typeof t!=="undefined",alias:new Map,validators:[],asyncValidator:[]},r=()=>t;return Object.defineProperty(r,Z,{value:n}),r.withJson=(o)=>{return n.alias.set(0,o),r},r.withForm=(o)=>{return n.alias.set(4,o),r},r.withQuery=(o)=>{return n.alias.set(1,o),r},r.withParam=(o)=>{return n.alias.set(2,o),r},r.withHeader=(o)=>{return n.alias.set(3,o),r},r.withUrlForm=(o)=>{return n.alias.set(6,o),r},r.withBody=()=>{return n.alias.set(5,void 0),r},r.withValidators=(...o)=>{return n.validators=o,r},r.withAsyncValidators=(...o)=>{return n.asyncValidator=o,r},r}function G(t){return t[Z]}async function M(t,n){await Promise.all(t.map((r)=>r(n)))}function D(t){return!!t&&typeof t==="function"&&Z in t}function $(t){return!!t&&typeof t==="object"&&Object.entries(t).every(([,n])=>D(n))}var Z=Symbol("field");var ft=void 0;var tt=()=>ft;function nt(t){return t.reduceRight((n,r)=>{return(o,T)=>r(o,(H)=>n(H,T))},(n,r)=>r(n))}function ot(t){switch(!0){case t instanceof FormData:case t instanceof Blob:case t instanceof ArrayBuffer:case t instanceof URLSearchParams:case typeof t==="string":return t;case typeof t==="object":case typeof t==="boolean":case typeof t==="number":case Array.isArray(t):return JSON.stringify(t);default:return null}}function et(t){switch(!0){case t instanceof FormData:return"multipart/form-data";case t instanceof ArrayBuffer:return"application/octet-stream";case t instanceof Blob:return t.type||"application/octet-stream";case t instanceof URLSearchParams:return"application/x-www-form-urlencoded;charset=UTF-8";case typeof t==="string":return"text/plain";case(typeof t==="object"&&t!==null):case typeof t==="number":case typeof t==="boolean":return"application/json";default:return null}}function kt(t){if(D(t))return t();if($(t)){const n={};for(let[r,o]of Object.entries(t))n[r]=o();return n}return}function Ct(...t){let n,r,o=!1,T,H=[],f="json",R,e=!1,p=[],w;switch(t.length){case 1:{n="GET",r=t[0];break}case 2:{n=t[0],r=t[1];break}default:throw new Error("Invalid arguments")}const c=(x)=>{const i=x?.timeout||0,k=x?.client||S(),E=z(k),C=x?.handler||E?.handler||tt();if(!C)throw X;let m,I,B;const _=[];if(x?.abort)_.push(x.abort);else B=new AbortController,_.push(B.signal);if(i>0)_.push(AbortSignal.timeout(i));return{doRequest:async(...F)=>{try{let U;if(o&&F.length===1)U=F[0];if(o&&!U)throw new Error("Because the request has input, the argument must be the input value");if(p.length>0)for(let ct of p)ct(U);const b={host:E?.host,method:n,endpoint:r,queryParams:new URLSearchParams,headers:new Headers,body:void 0,withCredentials:e,responseType:f,context:R||Y(),uploadProgress:m,downloadProgress:I,timeout:i,abort:AbortSignal.any(_)};if(o&&T)await st(b,T,U);let h=await nt([...E.interceptors,...H])(b,C);if(typeof w==="function")h=N({...h,body:w(h)});return h}catch(U){return N({error:U})}},cancel:()=>{B?.abort()},getInitValue:()=>kt(T),setUploadProgress:(F)=>{m=F},setDownloadProgress:(F)=>{I=F}}};return c.withField=(x)=>{return T=x,o=!0,c},c.withInterceptors=(x)=>{return H=x,c},c.withContext=(x)=>{return R=x,c},c.withCredentials=(x)=>{return e=x,c},c.withValidators=(x)=>{return p=x,c},c.withTransformResponse=(x)=>{return w=x,c},c.withResponseType=(x)=>{return f=x,c},c}function rt(t,n){return t.replace(/:([^\/]+)/g,(r,o)=>{return n.get(o)??"undefined"})}async function st(t,n,r){function o(e){return typeof e==="object"&&e!==null}function T(e){switch(!0){case Array.isArray(e):case(typeof e==="object"&&e!==null):return JSON.stringify(e);case(typeof e==="object"&&e===null):case typeof e==="boolean":case typeof e==="number":case typeof e==="string":return String(e);default:throw new Error("Unsupported value type")}}function H(e,p,w){if(Array.isArray(w))for(let c of w)e.append(p,T(c));else e.set(p,T(w));return e}function f(e,p,w){if(Array.isArray(w))for(let c of w)f(e,p,T(c));return e.set(p,T(w)),e}function R(e,p){switch(!0){case typeof e==="undefined":return p;case(typeof e==="object"&&e===null):return e;default:return e}}if(D(n)){let e,p,w,c=void 0;const x=R(r,n()),i=G(n);await M([...i.validators,...i.asyncValidator],x);for(let[k,E]of i.alias)switch(k){case 1:{if(!E)throw A;p=H(new URLSearchParams,E,x);break}case 2:{if(!E)throw A;e=f(new Map,E,x);break}case 3:{if(!E)throw A;w=H(new Headers,E,x);break}case 4:{if(!E)throw A;c=H(new FormData,E,x);break}case 6:{if(!E)throw A;c=H(new URLSearchParams,E,x);break}case 0:case 5:c=x;break}if(e)t.endpoint=rt(t.endpoint,e);t.queryParams=p,t.headers=w,t.body=c;return}if($(n)&&o(r)){let e=new Map,p=new URLSearchParams,w=new Headers,c=new URLSearchParams,x=new FormData;const i={};let k=void 0,E;for(let[C,m]of Object.entries(n)){const I=G(m);for(let[B,_]of I.alias){const O=_||C,s=R(r[C],m());switch(await M([...I.validators,...I.asyncValidator],s),B){case 0:{Object.defineProperty(i,O,{value:s,writable:!0,enumerable:!0}),E=0;break}case 1:p=H(p,O,s);break;case 2:e=f(e,O,s);break;case 3:w=H(w,O,s);break;case 4:{x=H(x,O,s),E=4;break}case 6:{c=H(c,O,s),E=6;break}case 5:{k=s,E=5;break}}}}switch(t.endpoint=rt(t.endpoint,e),t.queryParams=p,t.headers=w,E){case 5:t.body=k;break;case 4:t.body=x;break;case 6:t.body=c;break;case 0:t.body=i;break}return}throw W}function Nt(t){const n=new URL(t.endpoint,t.host);if(t.queryParams)n.search=t.queryParams.toString();const r=t.headers??new Headers;if(!r.has("Content-Type")){const T=et(t.body);if(T)r.set("Content-Type",T)}if(!r.has("Accept"))r.set("Accept","application/json, text/plain, */*");const o=t.withCredentials?"include":void 0;return new Request(n,{headers:r,method:t.method,body:ot(t.body),signal:t.abort,credentials:o})}async function u(t){const n=t.downloadProgress,r=Nt(t),o=t.abort;let T;try{T=await globalThis.fetch(r)}catch(x){if(o?.aborted&&o.reason instanceof Error)switch(!0){case o.reason.name==="AbortError":return N({error:y});case o.reason.name==="TimeoutError":return N({error:K})}return N({error:x})}const{headers:H,status:f,statusText:R,url:e}=T,p=d(H),w=a(H);let c=null;if(T.body){const x=[],i=T.body.getReader();let k=0;while(!0){const{done:C,value:m}=await i.read();if(C)break;x.push(m),k+=m.length,n?.({lengthComputable:p>0,loaded:k,total:p})}const E=l(x,k);try{c=q({request:t,content:E,contentType:w})}catch(C){return N({error:C,status:f,statusText:R,headers:H,url:e})}}return N({status:f,statusText:R,headers:H,url:e,body:c})}function xt(t){return typeof t==="object"&&t!==null&&g in t}function z(t){if(!xt(t))throw J;return t[g]}function Tt(t){const n={host:t.host,interceptors:t.interceptors??[],handler:t.handler||u};return{[g]:n}}function mt(t,n){const o={...z(t),...n};return{[g]:o}}function S(){if(!L)throw j;return L}function Ot(t){L=t}function At(){L=void 0}function It(t){L=Tt(t)}var g=Symbol("Client"),L;function _t(t,n){let r=n?.encode;if(!r){const o=globalThis.btoa;if(typeof o!=="function")throw new Error("BasicAuthInterceptor is not supported in this environment");r=(T)=>o(`${T.username}:${T.password}`)}return(o,T)=>{const H=o.headers||new Headers;return H.set("Authorization",`Basic ${r(t())}`),o.headers=H,T(o)}}var Ft=(t)=>{if(!t)return new Error("required");return null},Ut=(t)=>{if(typeof t!=="boolean"||!t)return new Error("requiredTrue");return null},Bt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length<t)return new Error(`minLength ${t}`);return null}},Dt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length>t)return new Error(`maxLength ${t}`);return null}},Lt=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n<t)return new Error(`minLength ${t}`);return null}},ht=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n>t)return new Error(`maxLength ${t}`);return null}},Pt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(!t.test(n))return new Error("pattern");return null}};export{Ot as setGlobalClient,At as restGlobalClient,Ut as requiredTrue,Ft as required,Pt as pattern,Bt as minLength,Lt as min,Dt as maxLength,ht as max,wt as makeHttpContextToken,Y as makeHttpContext,V as isHttpContextToken,$ as isFieldGroup,D as isField,xt as isClient,S as getGlobalClient,it as field,u as fetchHandler,M as doValid,Ct as defineRequest,It as createGlobalClient,Tt as createClient,mt as cloneClient,_t as basicAuthInterceptor,W as ERR_UNSUPPORTED_FIELD_TYPE,pt as ERR_UNKNOWN,K as ERR_TIMEOUT,Et as ERR_OBSERVE,A as ERR_NOT_SET_ALIAS,X as ERR_NOT_FOUND_HANDLER,j as ERR_NOT_FOUND_GLOBAL_CLIENT,Ht as ERR_NETWORK,P as ERR_INVALID_HTTP_CONTEXT_TOKEN,J as ERR_INVALID_CLIENT,y as ERR_ABORTED};
|
|
1
|
+
var L=new Error("ERR_ABORTED"),h=new Error("ERR_TIMEOUT"),Tt=new Error("ERR_NETWORK"),B=new Error("ERR_NOT_FOUND_HANDLER"),it=new Error("ERR_OBSERVE"),xt=new Error("ERR_UNSUPPORTED_FIELD_TYPE"),D=new Error("ERR_INVALID_CLIENT"),M=new Error("ERR_NOT_FOUND_GLOBAL_CLIENT"),U=new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN"),pt=new Error("ERR_UNKNOWN");function k(t){let n=t?.status??0,r=n>=200&&n<300,o=t?.statusText??"",x=t?.url??"",p=t?.headers??new Headers,w=t?.body??null,H=t?.error;if(!H&&!r){let e=`Http failure response for ${x||"(unknown url)"}: ${n}`;if(o)e+=` - ${o}`;H=new Error(e)}return{status:n,statusText:o,url:x,headers:p,body:w,error:H}}var G=Symbol("HttpContextToken");function ct(t){return Object.defineProperty(t,G,{value:t}),t}function y(t){return typeof t==="function"&&G in t}var g=Symbol("HttpContext");function Et(t){return typeof t==="object"&&t!==null&&g in t}function $(t){let n=new Map;switch(!0){case Array.isArray(t):{for(let[r,o]of t)if(y(r))n.set(r,o);break}case Et(t):{for(let[r,o]of t[g])n.set(r,o);break}}return{set(r,o){if(!y(r))throw U;return n.set(r,o),this},get(r){if(!y(r))throw U;if(!n.has(r))n.set(r,r());return n.get(r)},del(r){return n.delete(r),this},has(r){return n.has(r)},keys(){return n.keys()},get length(){return Array.from(n.keys()).length},[g]:n}}var K=Symbol("field");function Ht(t){let n={required:typeof t!=="undefined",alias:new Map,validators:[],asyncValidator:[]},r=()=>t;return Object.defineProperty(r,K,{value:n}),r.withJson=(o)=>{return n.alias.set(0,o),r},r.withForm=(o)=>{return n.alias.set(4,o),r},r.withQuery=(o)=>{return n.alias.set(1,o),r},r.withParam=(o)=>{return n.alias.set(2,o),r},r.withHeader=(o)=>{return n.alias.set(3,o),r},r.withUrlForm=(o)=>{return n.alias.set(6,o),r},r.withBody=()=>{return n.alias.set(5,void 0),r},r.withValidators=(...o)=>{return n.validators=o,r},r.withAsyncValidators=(...o)=>{return n.asyncValidator=o,r},r}function Z(t){return t[K]}async function X(t,n){await Promise.all(t.map((r)=>r(n)))}function S(t){return!!t&&typeof t==="function"&&K in t}function u(t){return!!t&&typeof t==="object"&&Object.entries(t).every(([,n])=>S(n))}var wt=void 0;var z=()=>wt;function b(t){return t.reduceRight((n,r)=>{return(o,x)=>r(o,(p)=>n(p,x))},(n,r)=>r(n))}function l(t){switch(!0){case t instanceof FormData:case t instanceof Blob:case t instanceof ArrayBuffer:case t instanceof URLSearchParams:case typeof t==="string":return t;case typeof t==="object":case typeof t==="boolean":case typeof t==="number":case Array.isArray(t):return JSON.stringify(t);default:return null}}function q(t){switch(!0){case t instanceof FormData:return"multipart/form-data";case t instanceof ArrayBuffer:return"application/octet-stream";case t instanceof Blob:return t.type||"application/octet-stream";case t instanceof URLSearchParams:return"application/x-www-form-urlencoded;charset=UTF-8";case typeof t==="string":return"text/plain";case(typeof t==="object"&&t!==null):case typeof t==="number":case typeof t==="boolean":return"application/json";default:return null}}function Rt(t){if(u(t)){let n={};for(let[r,o]of Object.entries(t))n[r]=o();return n}return}function ft(...t){let n,r,o,x=[],p="json",w,H=!1,e=[],c;switch(t.length){case 1:{n="GET",r=t[0];break}case 2:{n=t[0],r=t[1];break}default:throw new Error("Invalid arguments")}let T=(i)=>{let E=i?.timeout||0,F=i?.client||J(),R=W(F),f=i?.handler||R?.handler||z();if(!f)throw B;let s,N,C,_=[];if(i?.abort)_.push(i.abort);else C=new AbortController,_.push(C.signal);if(E>0)_.push(AbortSignal.timeout(E));return{doRequest:async(...a)=>{let d;if(a.length===1)d=a[0];if(e.length>0)for(let I of e)I(d);let Y={host:R?.host,method:n,endpoint:r,queryParams:new URLSearchParams,headers:new Headers,body:void 0,withCredentials:H,responseType:p,context:w||$(),uploadProgress:s,downloadProgress:N,timeout:E,abort:AbortSignal.any(_)};try{if(d&&o)await mt(Y,o,d);let V=await b([...R.interceptors,...x])(Y,f);if(typeof c==="function")V=k({...V,body:c(V)});return V}catch(I){return k({error:I})}},cancel:()=>{C?.abort()},getInitValue:()=>Rt(o),setUploadProgress:(a)=>{s=a},setDownloadProgress:(a)=>{N=a}}};return T.withField=(i)=>{return o=i,T},T.withInterceptors=(i)=>{return x=i,T},T.withContext=(i)=>{return w=i,T},T.withCredentials=(i)=>{return H=i,T},T.withValidators=(...i)=>{return e=i,T},T.withTransformResponse=(i)=>{return c=i,T},T.withResponseType=(i)=>{return p=i,T},T}function st(t,n){return t.replace(/:([^\/]+)/g,(r,o)=>{return n.get(o)??"undefined"})}async function mt(t,n,r){function o(e){return typeof e==="object"&&e!==null}function x(e){switch(!0){case Array.isArray(e):case(typeof e==="object"&&e!==null):return JSON.stringify(e);case(typeof e==="object"&&e===null):case typeof e==="boolean":case typeof e==="number":case typeof e==="string":case typeof e==="undefined":return String(e);default:throw new Error(`Unsupported value type: ${typeof e}`)}}function p(e,c,T){if(Array.isArray(T)){for(let i of T)e.append(c,x(i));return e}if(typeof T!=="undefined")e.set(c,x(T));return e}function w(e,c,T){if(Array.isArray(T)){for(let i of T)w(e,c,x(i));return e}if(typeof T!=="undefined")e.set(c,x(T));return e}function H(e,c){switch(!0){case typeof e==="undefined":return c;case(typeof e==="object"&&e===null):return e;default:return e}}if(u(n)&&o(r)){let e=new Map,c=new URLSearchParams,T=new Headers,i=new URLSearchParams,E=new FormData,F={},R=void 0,f;for(let[s,N]of Object.entries(n)){let C=Z(N);for(let[_,Q]of C.alias){let O=Q||s,m=H(r[s],N());switch(await X([...C.validators,...C.asyncValidator],m),_){case 0:{Object.defineProperty(F,O,{value:m,writable:!0,enumerable:!0}),f=0;break}case 1:c=p(c,O,m);break;case 2:e=w(e,O,m);break;case 3:T=p(T,O,m);break;case 4:{E=p(E,O,m),f=4;break}case 6:{i=p(i,O,m),f=6;break}case 5:{R=m,f=5;break}}}}switch(t.endpoint=st(t.endpoint,e),t.queryParams=c,t.headers=T,f){case 5:t.body=R;break;case 4:t.body=E;break;case 6:t.body=i;break;case 0:t.body=F;break}}}function v(t){let n=t.get("Content-Length");if(!n)return 0;let r=Number(n);if(isNaN(r))return 0;return r}function tt(t){return t.get("Content-Type")||""}function nt(t){let{request:n,content:r,contentType:o}=t;switch(n.responseType){case"json":{let p=new TextDecoder().decode(r);if(p==="")return null;return JSON.parse(p)}case"text":return new TextDecoder().decode(r);case"blob":return new Blob([r],{type:o});case"arraybuffer":return r.buffer;default:return null}}function rt(t,n){let r=new Uint8Array(n),o=0;for(let x of t)r.set(x,o),o+=x.length;return r}function kt(t){let n=new URL(t.endpoint,t.host);if(t.queryParams)n.search=t.queryParams.toString();let r=t.headers??new Headers;if(!r.has("Content-Type")){let x=q(t.body);if(x)r.set("Content-Type",x)}if(!r.has("Accept"))r.set("Accept","application/json, text/plain, */*");let o=t.withCredentials?"include":void 0;return new Request(n,{headers:r,method:t.method,body:l(t.body),signal:t.abort,credentials:o})}async function j(t){let n=t.downloadProgress,r=kt(t),o=t.abort,x;try{x=await globalThis.fetch(r)}catch(E){if(o?.aborted&&o.reason instanceof Error)switch(!0){case o.reason.name==="AbortError":return k({error:L});case o.reason.name==="TimeoutError":return k({error:h})}return k({error:E})}let{headers:p,status:w,statusText:H,url:e}=x,c=v(p),T=tt(p),i=null;if(x.body){let E=[],F=x.body.getReader(),R=0;while(!0){let{done:s,value:N}=await F.read();if(s)break;E.push(N),R+=N.length,n?.({lengthComputable:c>0,loaded:R,total:c})}let f=rt(E,R);try{i=nt({request:t,content:f,contentType:T})}catch(s){return k({error:s,status:w,statusText:H,headers:p,url:e})}}return k({status:w,statusText:H,headers:p,url:e,body:i})}var P=Symbol("Client"),A;function ot(t){return typeof t==="object"&&t!==null&&P in t}function W(t){if(!ot(t))throw D;return t[P]}function et(t){let n={host:t.host,interceptors:t.interceptors??[],handler:t.handler||j};return{[P]:n}}function Nt(t,n){let o={...W(t),...n};return{[P]:o}}function J(){if(!A)throw M;return A}function Ct(t){A=t}function Ot(){A=void 0}function Ft(t){A=et(t)}function _t(t,n){let r=n?.encode;if(!r){let o=globalThis.btoa;if(typeof o!=="function")throw new Error("BasicAuthInterceptor is not supported in this environment");r=(x)=>o(`${x.username}:${x.password}`)}return(o,x)=>{let p=o.headers||new Headers;return p.set("Authorization",`Basic ${r(t())}`),o.headers=p,x(o)}}var at=(t)=>{if(!t)return new Error("required");return null},At=(t)=>{if(typeof t!=="boolean"||!t)return new Error("requiredTrue");return null},dt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length<t)return new Error(`minLength ${t}`);return null}},It=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(n.length>t)return new Error(`maxLength ${t}`);return null}},Vt=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n<t)return new Error(`minLength ${t}`);return null}},Ut=(t)=>{return(n)=>{if(typeof n!=="number"||isNaN(n))return new Error("invalid number");if(n>t)return new Error(`maxLength ${t}`);return null}},yt=(t)=>{return(n)=>{if(typeof n!=="string")return new Error("invalid string");if(!t.test(n))return new Error("pattern");return null}};export{Ct as setGlobalClient,Ot as restGlobalClient,At as requiredTrue,at as required,yt as pattern,dt as minLength,Vt as min,It as maxLength,Ut as max,ct as makeHttpContextToken,$ as makeHttpContext,y as isHttpContextToken,u as isFieldGroup,S as isField,ot as isClient,J as getGlobalClient,Ht as field,j as fetchHandler,X as doValid,ft as defineRequest,Ft as createGlobalClient,et as createClient,Nt as cloneClient,_t as basicAuthInterceptor,xt as ERR_UNSUPPORTED_FIELD_TYPE,pt as ERR_UNKNOWN,h as ERR_TIMEOUT,it as ERR_OBSERVE,B as ERR_NOT_FOUND_HANDLER,M as ERR_NOT_FOUND_GLOBAL_CLIENT,Tt as ERR_NETWORK,U as ERR_INVALID_HTTP_CONTEXT_TOKEN,D as ERR_INVALID_CLIENT,L as ERR_ABORTED};
|