@defjs/core 0.0.0-test-003 → 0.0.0-test-004
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 +292 -226
- package/{index.es.js → index.js} +245 -285
- package/index.min.js +587 -0
- package/package.json +1 -4
- package/index.es.min.js +0 -1
package/{index.es.js → index.js}
RENAMED
|
@@ -1,48 +1,93 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var ERR_UNKNOWN = new Error("ERR_UNKNOWN");
|
|
13
|
-
|
|
1
|
+
const ERR_ABORTED = new Error("ERR_ABORTED");
|
|
2
|
+
const ERR_TIMEOUT = new Error("ERR_TIMEOUT");
|
|
3
|
+
const ERR_NETWORK = new Error("ERR_NETWORK");
|
|
4
|
+
const ERR_NOT_FOUND_HANDLER = new Error("ERR_NOT_FOUND_HANDLER");
|
|
5
|
+
const ERR_OBSERVE = new Error("ERR_OBSERVE");
|
|
6
|
+
const ERR_NOT_SET_ALIAS = new Error("ERR_NOT_SET_ALIAS");
|
|
7
|
+
const ERR_UNSUPPORTED_FIELD_TYPE = new Error("ERR_UNSUPPORTED_FIELD_TYPE");
|
|
8
|
+
const ERR_INVALID_CLIENT = new Error("ERR_INVALID_CLIENT");
|
|
9
|
+
const ERR_NOT_FOUND_GLOBAL_CLIENT = new Error("ERR_NOT_FOUND_GLOBAL_CLIENT");
|
|
10
|
+
const ERR_INVALID_HTTP_CONTEXT_TOKEN = new Error("ERR_INVALID_HTTP_CONTEXT_TOKEN");
|
|
11
|
+
const ERR_UNKNOWN = new Error("ERR_UNKNOWN");
|
|
14
12
|
class HttpErrorResponse extends Error {
|
|
15
13
|
status;
|
|
16
14
|
statusText;
|
|
17
15
|
url;
|
|
18
16
|
headers;
|
|
19
|
-
ok;
|
|
20
17
|
body;
|
|
21
18
|
constructor(init) {
|
|
22
|
-
const status = init
|
|
23
|
-
const statusText = init
|
|
19
|
+
const status = init?.status ?? 0;
|
|
20
|
+
const statusText = init?.statusText ?? "";
|
|
24
21
|
const ok = status >= 200 && status < 300;
|
|
25
|
-
const headers = init
|
|
26
|
-
const url = init
|
|
27
|
-
const body = init
|
|
22
|
+
const headers = init?.headers ?? new Headers();
|
|
23
|
+
const url = init?.url ?? "";
|
|
24
|
+
const body = init?.body ?? null;
|
|
28
25
|
let message;
|
|
29
26
|
if (ok) {
|
|
30
|
-
message = `Http failure during parsing for ${
|
|
27
|
+
message = `Http failure during parsing for ${url}`;
|
|
31
28
|
} else {
|
|
32
|
-
message = `Http failure response for ${
|
|
29
|
+
message = `Http failure response for ${url || "(unknown url)"}: ${status}`;
|
|
30
|
+
if (statusText) {
|
|
31
|
+
message += ` - ${statusText}`;
|
|
32
|
+
}
|
|
33
33
|
}
|
|
34
|
-
super(message, { cause: init
|
|
34
|
+
super(message, { cause: init?.error });
|
|
35
35
|
super.name = "HttpErrorResponse";
|
|
36
36
|
this.statusText = statusText;
|
|
37
37
|
this.url = url;
|
|
38
38
|
this.headers = headers;
|
|
39
39
|
this.status = status;
|
|
40
|
-
this.ok = ok;
|
|
41
40
|
this.body = body;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
function __getContentLength(headers) {
|
|
45
|
+
const value = headers.get("Content-Length");
|
|
46
|
+
if (!value) {
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
const num = Number(value);
|
|
50
|
+
if (isNaN(num)) {
|
|
51
|
+
return 0;
|
|
52
|
+
}
|
|
53
|
+
return num;
|
|
54
|
+
}
|
|
55
|
+
function __getContentType(headers) {
|
|
56
|
+
return headers.get("Content-Type") || "";
|
|
57
|
+
}
|
|
58
|
+
function __parseBody(params) {
|
|
59
|
+
const { request, content, contentType } = params;
|
|
60
|
+
const responseType = request.responseType;
|
|
61
|
+
switch (responseType) {
|
|
62
|
+
case "json": {
|
|
63
|
+
const text = new TextDecoder().decode(content);
|
|
64
|
+
if (text === "") {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return JSON.parse(text);
|
|
68
|
+
}
|
|
69
|
+
case "text":
|
|
70
|
+
return new TextDecoder().decode(content);
|
|
71
|
+
case "blob": {
|
|
72
|
+
return new Blob([content], { type: contentType });
|
|
73
|
+
}
|
|
74
|
+
case "arraybuffer":
|
|
75
|
+
return content.buffer;
|
|
76
|
+
default:
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function __concatChunks(chunks, totalLength) {
|
|
81
|
+
const chunksAll = new Uint8Array(totalLength);
|
|
82
|
+
let position = 0;
|
|
83
|
+
for (const chunk of chunks) {
|
|
84
|
+
chunksAll.set(chunk, position);
|
|
85
|
+
position += chunk.length;
|
|
86
|
+
}
|
|
87
|
+
return chunksAll;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const HTTP_CONTEXT_TOKEN = Symbol("HttpContextToken");
|
|
46
91
|
function makeHttpContextToken(defaultValue) {
|
|
47
92
|
Object.defineProperty(defaultValue, HTTP_CONTEXT_TOKEN, { value: defaultValue });
|
|
48
93
|
return defaultValue;
|
|
@@ -50,8 +95,28 @@ function makeHttpContextToken(defaultValue) {
|
|
|
50
95
|
function isHttpContextToken(value) {
|
|
51
96
|
return typeof value === "function" && HTTP_CONTEXT_TOKEN in value;
|
|
52
97
|
}
|
|
53
|
-
|
|
54
|
-
|
|
98
|
+
const HTTP_CONTEXT = Symbol("HttpContext");
|
|
99
|
+
function isHttpContext(value) {
|
|
100
|
+
return typeof value === "object" && value !== null && HTTP_CONTEXT in value;
|
|
101
|
+
}
|
|
102
|
+
function makeHttpContext(entries) {
|
|
103
|
+
const ctx = /* @__PURE__ */ new Map();
|
|
104
|
+
switch (true) {
|
|
105
|
+
case Array.isArray(entries): {
|
|
106
|
+
for (const [token, value] of entries) {
|
|
107
|
+
if (isHttpContextToken(token)) {
|
|
108
|
+
ctx.set(token, value);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case isHttpContext(entries): {
|
|
114
|
+
for (const [token, value] of entries[HTTP_CONTEXT]) {
|
|
115
|
+
ctx.set(token, value);
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
55
120
|
return {
|
|
56
121
|
set(token, value) {
|
|
57
122
|
if (!isHttpContextToken(token)) {
|
|
@@ -78,15 +143,30 @@ function makeHttpContext() {
|
|
|
78
143
|
},
|
|
79
144
|
keys() {
|
|
80
145
|
return ctx.keys();
|
|
81
|
-
}
|
|
146
|
+
},
|
|
147
|
+
get length() {
|
|
148
|
+
return Array.from(ctx.keys()).length;
|
|
149
|
+
},
|
|
150
|
+
[HTTP_CONTEXT]: ctx
|
|
82
151
|
};
|
|
83
152
|
}
|
|
84
|
-
var HTTP_CONTEXT_TOKEN = Symbol("HttpContextToken");
|
|
85
153
|
|
|
86
|
-
|
|
154
|
+
const FIELD = Symbol("field");
|
|
155
|
+
var FieldType = /* @__PURE__ */ ((FieldType2) => {
|
|
156
|
+
FieldType2[FieldType2["Json"] = 0] = "Json";
|
|
157
|
+
FieldType2[FieldType2["Query"] = 1] = "Query";
|
|
158
|
+
FieldType2[FieldType2["Param"] = 2] = "Param";
|
|
159
|
+
FieldType2[FieldType2["Header"] = 3] = "Header";
|
|
160
|
+
FieldType2[FieldType2["Form"] = 4] = "Form";
|
|
161
|
+
FieldType2[FieldType2["Body"] = 5] = "Body";
|
|
162
|
+
FieldType2[FieldType2["UrlForm"] = 6] = "UrlForm";
|
|
163
|
+
return FieldType2;
|
|
164
|
+
})(FieldType || {});
|
|
87
165
|
function field(defaultValue) {
|
|
88
166
|
const meta = {
|
|
89
|
-
|
|
167
|
+
// todo
|
|
168
|
+
required: typeof defaultValue !== "undefined",
|
|
169
|
+
alias: /* @__PURE__ */ new Map(),
|
|
90
170
|
validators: [],
|
|
91
171
|
asyncValidator: []
|
|
92
172
|
};
|
|
@@ -95,31 +175,31 @@ function field(defaultValue) {
|
|
|
95
175
|
value: meta
|
|
96
176
|
});
|
|
97
177
|
getter.withJson = (alias) => {
|
|
98
|
-
meta.alias.set(
|
|
178
|
+
meta.alias.set(0 /* Json */, alias);
|
|
99
179
|
return getter;
|
|
100
180
|
};
|
|
101
181
|
getter.withForm = (alias) => {
|
|
102
|
-
meta.alias.set(
|
|
182
|
+
meta.alias.set(4 /* Form */, alias);
|
|
103
183
|
return getter;
|
|
104
184
|
};
|
|
105
185
|
getter.withQuery = (alias) => {
|
|
106
|
-
meta.alias.set(
|
|
186
|
+
meta.alias.set(1 /* Query */, alias);
|
|
107
187
|
return getter;
|
|
108
188
|
};
|
|
109
189
|
getter.withParam = (alias) => {
|
|
110
|
-
meta.alias.set(
|
|
190
|
+
meta.alias.set(2 /* Param */, alias);
|
|
111
191
|
return getter;
|
|
112
192
|
};
|
|
113
193
|
getter.withHeader = (alias) => {
|
|
114
|
-
meta.alias.set(
|
|
194
|
+
meta.alias.set(3 /* Header */, alias);
|
|
115
195
|
return getter;
|
|
116
196
|
};
|
|
117
197
|
getter.withUrlForm = (alias) => {
|
|
118
|
-
meta.alias.set(
|
|
198
|
+
meta.alias.set(6 /* UrlForm */, alias);
|
|
119
199
|
return getter;
|
|
120
200
|
};
|
|
121
201
|
getter.withBody = () => {
|
|
122
|
-
meta.alias.set(
|
|
202
|
+
meta.alias.set(5 /* Body */, void 0);
|
|
123
203
|
return getter;
|
|
124
204
|
};
|
|
125
205
|
getter.withValidators = (...fn) => {
|
|
@@ -135,21 +215,8 @@ function field(defaultValue) {
|
|
|
135
215
|
function __getFieldMetadata(field2) {
|
|
136
216
|
return field2[FIELD];
|
|
137
217
|
}
|
|
138
|
-
function
|
|
139
|
-
|
|
140
|
-
const err = fn(value);
|
|
141
|
-
if (err) {
|
|
142
|
-
throw err;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
async function asyncValidatorField(asyncValidator, value) {
|
|
147
|
-
for (const fn of asyncValidator) {
|
|
148
|
-
const err = await fn(value);
|
|
149
|
-
if (err) {
|
|
150
|
-
throw err;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
218
|
+
async function doValid(validators, value) {
|
|
219
|
+
await Promise.all(validators.map((validator) => validator(value)));
|
|
153
220
|
}
|
|
154
221
|
function isField(value) {
|
|
155
222
|
return !!value && typeof value === "function" && FIELD in value;
|
|
@@ -157,26 +224,25 @@ function isField(value) {
|
|
|
157
224
|
function isFieldGroup(value) {
|
|
158
225
|
return !!value && typeof value === "object" && Object.entries(value).every(([, v]) => isField(v));
|
|
159
226
|
}
|
|
160
|
-
var FIELD = Symbol("field");
|
|
161
227
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
var getGlobalHttpHandler = () => globalHttpHandler;
|
|
228
|
+
let globalHttpHandler = void 0;
|
|
229
|
+
const getGlobalHttpHandler = () => globalHttpHandler;
|
|
165
230
|
|
|
166
|
-
// src/interceptor/interceptor.ts
|
|
167
231
|
function makeInterceptorChain(interceptors) {
|
|
168
|
-
return interceptors.reduceRight(
|
|
169
|
-
|
|
170
|
-
|
|
232
|
+
return interceptors.reduceRight(
|
|
233
|
+
(fn, interceptor) => {
|
|
234
|
+
return (initReq, finalHandlerFn) => interceptor(initReq, (req) => fn(req, finalHandlerFn));
|
|
235
|
+
},
|
|
236
|
+
(req, fn) => fn(req)
|
|
237
|
+
);
|
|
171
238
|
}
|
|
172
239
|
|
|
173
|
-
// src/response.ts
|
|
174
240
|
function __makeResponse(options) {
|
|
175
|
-
const status = options
|
|
176
|
-
const statusText = options
|
|
177
|
-
const url = options
|
|
178
|
-
const headers = options
|
|
179
|
-
const body = options
|
|
241
|
+
const status = options?.status ?? 0;
|
|
242
|
+
const statusText = options?.statusText ?? "";
|
|
243
|
+
const url = options?.url ?? "";
|
|
244
|
+
const headers = options?.headers ?? new Headers();
|
|
245
|
+
const body = options?.body ?? null;
|
|
180
246
|
return {
|
|
181
247
|
status,
|
|
182
248
|
statusText,
|
|
@@ -186,7 +252,6 @@ function __makeResponse(options) {
|
|
|
186
252
|
};
|
|
187
253
|
}
|
|
188
254
|
|
|
189
|
-
// src/request.ts
|
|
190
255
|
function __serializeBody(body) {
|
|
191
256
|
switch (true) {
|
|
192
257
|
case body instanceof FormData:
|
|
@@ -232,19 +297,19 @@ function __buildFieldDefaultValue(input) {
|
|
|
232
297
|
}
|
|
233
298
|
if (isFieldGroup(input)) {
|
|
234
299
|
const obj = {};
|
|
235
|
-
for (const [key,
|
|
236
|
-
obj[key] =
|
|
300
|
+
for (const [key, field] of Object.entries(input)) {
|
|
301
|
+
obj[key] = field();
|
|
237
302
|
}
|
|
238
303
|
return obj;
|
|
239
304
|
}
|
|
240
|
-
return;
|
|
305
|
+
return void 0;
|
|
241
306
|
}
|
|
242
307
|
function defineRequest(method, endpoint) {
|
|
243
308
|
let requiredInput = false;
|
|
244
|
-
let
|
|
309
|
+
let field;
|
|
245
310
|
let interceptors = [];
|
|
246
311
|
let responseType = "json";
|
|
247
|
-
let
|
|
312
|
+
let context;
|
|
248
313
|
let observe = "body";
|
|
249
314
|
let withCredentials = false;
|
|
250
315
|
let validators = [];
|
|
@@ -253,7 +318,7 @@ function defineRequest(method, endpoint) {
|
|
|
253
318
|
let uploadProgress;
|
|
254
319
|
let downloadProgress;
|
|
255
320
|
let abortSignal;
|
|
256
|
-
const getInitValue = () => __buildFieldDefaultValue(
|
|
321
|
+
const getInitValue = () => __buildFieldDefaultValue(field);
|
|
257
322
|
const setUploadProgress = (fn2) => {
|
|
258
323
|
uploadProgress = fn2;
|
|
259
324
|
};
|
|
@@ -288,41 +353,41 @@ function defineRequest(method, endpoint) {
|
|
|
288
353
|
} else if (timeout > 0) {
|
|
289
354
|
abortSignal = AbortSignal.timeout(timeout);
|
|
290
355
|
}
|
|
291
|
-
const
|
|
292
|
-
const clientOptions = getClientConfig(
|
|
356
|
+
const client = isClient(doRequestOptions?.client) ? doRequestOptions.client : getGlobalClient();
|
|
357
|
+
const clientOptions = getClientConfig(client);
|
|
293
358
|
const req = {
|
|
294
359
|
host: clientOptions?.host,
|
|
295
360
|
method,
|
|
296
361
|
endpoint,
|
|
297
|
-
queryParams: new URLSearchParams,
|
|
298
|
-
headers: new Headers,
|
|
299
|
-
body:
|
|
362
|
+
queryParams: new URLSearchParams(),
|
|
363
|
+
headers: new Headers(),
|
|
364
|
+
body: void 0,
|
|
300
365
|
withCredentials,
|
|
301
366
|
responseType,
|
|
302
|
-
context:
|
|
367
|
+
context: context || makeHttpContext(),
|
|
303
368
|
observe,
|
|
304
369
|
uploadProgress,
|
|
305
370
|
downloadProgress,
|
|
306
371
|
timeout,
|
|
307
372
|
abort: abortSignal
|
|
308
373
|
};
|
|
309
|
-
const
|
|
310
|
-
if (!
|
|
374
|
+
const handler = doRequestOptions?.handler || clientOptions?.handler || getGlobalHttpHandler();
|
|
375
|
+
if (!handler) {
|
|
311
376
|
throw ERR_NOT_FOUND_HANDLER;
|
|
312
377
|
}
|
|
313
|
-
if (requiredInput) {
|
|
314
|
-
await __fillRequestFromField(req,
|
|
378
|
+
if (requiredInput && field) {
|
|
379
|
+
await __fillRequestFromField(req, field, input);
|
|
315
380
|
}
|
|
316
381
|
const chain = makeInterceptorChain([...interceptors]);
|
|
317
|
-
let res = await chain(req,
|
|
382
|
+
let res = await chain(req, handler);
|
|
318
383
|
if (typeof transformResponse === "function") {
|
|
319
384
|
try {
|
|
320
385
|
res = __makeResponse({
|
|
321
386
|
...res,
|
|
322
387
|
body: transformResponse(res)
|
|
323
388
|
});
|
|
324
|
-
} catch (
|
|
325
|
-
throw new HttpErrorResponse({ error
|
|
389
|
+
} catch (error) {
|
|
390
|
+
throw new HttpErrorResponse({ error });
|
|
326
391
|
}
|
|
327
392
|
}
|
|
328
393
|
switch (req.observe) {
|
|
@@ -342,7 +407,7 @@ function defineRequest(method, endpoint) {
|
|
|
342
407
|
};
|
|
343
408
|
};
|
|
344
409
|
fn.withField = (value) => {
|
|
345
|
-
|
|
410
|
+
field = value;
|
|
346
411
|
requiredInput = true;
|
|
347
412
|
return fn;
|
|
348
413
|
};
|
|
@@ -351,7 +416,7 @@ function defineRequest(method, endpoint) {
|
|
|
351
416
|
return fn;
|
|
352
417
|
};
|
|
353
418
|
fn.withContext = (value) => {
|
|
354
|
-
|
|
419
|
+
context = value;
|
|
355
420
|
return fn;
|
|
356
421
|
};
|
|
357
422
|
fn.withCredentials = (value) => {
|
|
@@ -382,15 +447,6 @@ function __fillUrl(endpoint, params) {
|
|
|
382
447
|
});
|
|
383
448
|
}
|
|
384
449
|
async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
385
|
-
async function doValid(validators, value) {
|
|
386
|
-
for (const validator of validators) {
|
|
387
|
-
const err = await validator(value);
|
|
388
|
-
if (err) {
|
|
389
|
-
return err;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
return;
|
|
393
|
-
}
|
|
394
450
|
function inputIsObject(input2) {
|
|
395
451
|
return typeof input2 === "object" && input2 !== null;
|
|
396
452
|
}
|
|
@@ -441,52 +497,49 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
441
497
|
let params;
|
|
442
498
|
let queryParams;
|
|
443
499
|
let headers;
|
|
444
|
-
let body =
|
|
500
|
+
let body = void 0;
|
|
445
501
|
const validValue = getValidValue(input, fieldOrFieldGroup());
|
|
446
502
|
const meta = __getFieldMetadata(fieldOrFieldGroup);
|
|
447
|
-
|
|
448
|
-
if (err) {
|
|
449
|
-
throw err;
|
|
450
|
-
}
|
|
503
|
+
await doValid([...meta.validators, ...meta.asyncValidator], validValue);
|
|
451
504
|
for (const [type, aliasName] of meta.alias) {
|
|
452
505
|
switch (type) {
|
|
453
|
-
case
|
|
506
|
+
case FieldType.Query: {
|
|
454
507
|
if (!aliasName) {
|
|
455
508
|
throw ERR_NOT_SET_ALIAS;
|
|
456
509
|
}
|
|
457
|
-
queryParams = appendValue(new URLSearchParams, aliasName, validValue);
|
|
510
|
+
queryParams = appendValue(new URLSearchParams(), aliasName, validValue);
|
|
458
511
|
break;
|
|
459
512
|
}
|
|
460
|
-
case
|
|
513
|
+
case FieldType.Param: {
|
|
461
514
|
if (!aliasName) {
|
|
462
515
|
throw ERR_NOT_SET_ALIAS;
|
|
463
516
|
}
|
|
464
|
-
params = setValue(new Map, aliasName, validValue);
|
|
517
|
+
params = setValue(/* @__PURE__ */ new Map(), aliasName, validValue);
|
|
465
518
|
break;
|
|
466
519
|
}
|
|
467
|
-
case
|
|
520
|
+
case FieldType.Header: {
|
|
468
521
|
if (!aliasName) {
|
|
469
522
|
throw ERR_NOT_SET_ALIAS;
|
|
470
523
|
}
|
|
471
|
-
headers = appendValue(new Headers, aliasName, validValue);
|
|
524
|
+
headers = appendValue(new Headers(), aliasName, validValue);
|
|
472
525
|
break;
|
|
473
526
|
}
|
|
474
|
-
case
|
|
527
|
+
case FieldType.Form: {
|
|
475
528
|
if (!aliasName) {
|
|
476
529
|
throw ERR_NOT_SET_ALIAS;
|
|
477
530
|
}
|
|
478
|
-
body = appendValue(new FormData, aliasName, validValue);
|
|
531
|
+
body = appendValue(new FormData(), aliasName, validValue);
|
|
479
532
|
break;
|
|
480
533
|
}
|
|
481
|
-
case
|
|
534
|
+
case FieldType.UrlForm: {
|
|
482
535
|
if (!aliasName) {
|
|
483
536
|
throw ERR_NOT_SET_ALIAS;
|
|
484
537
|
}
|
|
485
|
-
body = appendValue(new URLSearchParams, aliasName, validValue);
|
|
538
|
+
body = appendValue(new URLSearchParams(), aliasName, validValue);
|
|
486
539
|
break;
|
|
487
540
|
}
|
|
488
|
-
case
|
|
489
|
-
case
|
|
541
|
+
case FieldType.Json:
|
|
542
|
+
case FieldType.Body:
|
|
490
543
|
body = validValue;
|
|
491
544
|
break;
|
|
492
545
|
}
|
|
@@ -500,55 +553,52 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
500
553
|
return;
|
|
501
554
|
}
|
|
502
555
|
if (isFieldGroup(fieldOrFieldGroup) && inputIsObject(input)) {
|
|
503
|
-
let params = new Map;
|
|
504
|
-
let queryParams = new URLSearchParams;
|
|
505
|
-
let headers = new Headers;
|
|
506
|
-
let urlForm = new URLSearchParams;
|
|
507
|
-
let formData = new FormData;
|
|
556
|
+
let params = /* @__PURE__ */ new Map();
|
|
557
|
+
let queryParams = new URLSearchParams();
|
|
558
|
+
let headers = new Headers();
|
|
559
|
+
let urlForm = new URLSearchParams();
|
|
560
|
+
let formData = new FormData();
|
|
508
561
|
const json = {};
|
|
509
|
-
let body =
|
|
562
|
+
let body = void 0;
|
|
510
563
|
let lastBodyField;
|
|
511
|
-
for (const [propertyKey,
|
|
512
|
-
const meta = __getFieldMetadata(
|
|
564
|
+
for (const [propertyKey, field] of Object.entries(fieldOrFieldGroup)) {
|
|
565
|
+
const meta = __getFieldMetadata(field);
|
|
513
566
|
for (const [type, aliasName] of meta.alias) {
|
|
514
567
|
const valueKey = aliasName || propertyKey;
|
|
515
|
-
const value = getValidValue(input[propertyKey],
|
|
516
|
-
|
|
517
|
-
if (err) {
|
|
518
|
-
throw err;
|
|
519
|
-
}
|
|
568
|
+
const value = getValidValue(input[propertyKey], field());
|
|
569
|
+
await doValid([...meta.validators, ...meta.asyncValidator], value);
|
|
520
570
|
switch (type) {
|
|
521
|
-
case
|
|
571
|
+
case FieldType.Json: {
|
|
522
572
|
Object.defineProperty(json, valueKey, {
|
|
523
573
|
value,
|
|
524
574
|
writable: true,
|
|
525
575
|
enumerable: true
|
|
526
576
|
});
|
|
527
|
-
lastBodyField =
|
|
577
|
+
lastBodyField = FieldType.Json;
|
|
528
578
|
break;
|
|
529
579
|
}
|
|
530
|
-
case
|
|
580
|
+
case FieldType.Query:
|
|
531
581
|
queryParams = appendValue(queryParams, valueKey, value);
|
|
532
582
|
break;
|
|
533
|
-
case
|
|
583
|
+
case FieldType.Param:
|
|
534
584
|
params = setValue(params, valueKey, value);
|
|
535
585
|
break;
|
|
536
|
-
case
|
|
586
|
+
case FieldType.Header:
|
|
537
587
|
headers = appendValue(headers, valueKey, value);
|
|
538
588
|
break;
|
|
539
|
-
case
|
|
589
|
+
case FieldType.Form: {
|
|
540
590
|
formData = appendValue(formData, valueKey, value);
|
|
541
|
-
lastBodyField =
|
|
591
|
+
lastBodyField = FieldType.Form;
|
|
542
592
|
break;
|
|
543
593
|
}
|
|
544
|
-
case
|
|
594
|
+
case FieldType.UrlForm: {
|
|
545
595
|
urlForm = appendValue(urlForm, valueKey, value);
|
|
546
|
-
lastBodyField =
|
|
596
|
+
lastBodyField = FieldType.UrlForm;
|
|
547
597
|
break;
|
|
548
598
|
}
|
|
549
|
-
case
|
|
599
|
+
case FieldType.Body: {
|
|
550
600
|
body = value;
|
|
551
|
-
lastBodyField =
|
|
601
|
+
lastBodyField = FieldType.Body;
|
|
552
602
|
break;
|
|
553
603
|
}
|
|
554
604
|
}
|
|
@@ -558,16 +608,16 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
558
608
|
request.queryParams = queryParams;
|
|
559
609
|
request.headers = headers;
|
|
560
610
|
switch (lastBodyField) {
|
|
561
|
-
case
|
|
611
|
+
case FieldType.Body:
|
|
562
612
|
request.body = body;
|
|
563
613
|
break;
|
|
564
|
-
case
|
|
614
|
+
case FieldType.Form:
|
|
565
615
|
request.body = formData;
|
|
566
616
|
break;
|
|
567
|
-
case
|
|
617
|
+
case FieldType.UrlForm:
|
|
568
618
|
request.body = urlForm;
|
|
569
619
|
break;
|
|
570
|
-
case
|
|
620
|
+
case FieldType.Json:
|
|
571
621
|
request.body = json;
|
|
572
622
|
break;
|
|
573
623
|
}
|
|
@@ -576,65 +626,14 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
576
626
|
throw ERR_UNSUPPORTED_FIELD_TYPE;
|
|
577
627
|
}
|
|
578
628
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
return 0;
|
|
584
|
-
}
|
|
585
|
-
const num = Number(value);
|
|
586
|
-
if (isNaN(num)) {
|
|
587
|
-
return 0;
|
|
629
|
+
function __createRequest(request) {
|
|
630
|
+
const url = new URL(request.endpoint, request.host);
|
|
631
|
+
if (request.queryParams) {
|
|
632
|
+
url.search = request.queryParams.toString();
|
|
588
633
|
}
|
|
589
|
-
|
|
590
|
-
}
|
|
591
|
-
function __getContentType(headers) {
|
|
592
|
-
return headers.get("Content-Type") || "";
|
|
593
|
-
}
|
|
594
|
-
function __parseBody(params) {
|
|
595
|
-
const { request, content, contentType } = params;
|
|
596
|
-
const responseType = request.responseType;
|
|
597
|
-
if (!responseType) {
|
|
598
|
-
return null;
|
|
599
|
-
}
|
|
600
|
-
switch (responseType) {
|
|
601
|
-
case "json": {
|
|
602
|
-
const text = new TextDecoder().decode(content);
|
|
603
|
-
if (text === "") {
|
|
604
|
-
return null;
|
|
605
|
-
}
|
|
606
|
-
return JSON.parse(text);
|
|
607
|
-
}
|
|
608
|
-
case "text":
|
|
609
|
-
return new TextDecoder().decode(content);
|
|
610
|
-
case "blob": {
|
|
611
|
-
return new Blob([content], { type: contentType });
|
|
612
|
-
}
|
|
613
|
-
case "arraybuffer":
|
|
614
|
-
return content.buffer;
|
|
615
|
-
default:
|
|
616
|
-
return null;
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
function __concatChunks(chunks, totalLength) {
|
|
620
|
-
const chunksAll = new Uint8Array(totalLength);
|
|
621
|
-
let position = 0;
|
|
622
|
-
for (const chunk of chunks) {
|
|
623
|
-
chunksAll.set(chunk, position);
|
|
624
|
-
position += chunk.length;
|
|
625
|
-
}
|
|
626
|
-
return chunksAll;
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
// src/handler/fetch/fetch.ts
|
|
630
|
-
function __createRequest(request2) {
|
|
631
|
-
const url = new URL(request2.endpoint, request2.host);
|
|
632
|
-
if (request2.queryParams) {
|
|
633
|
-
url.search = request2.queryParams.toString();
|
|
634
|
-
}
|
|
635
|
-
const headers = request2.headers ?? new Headers;
|
|
634
|
+
const headers = request.headers ?? new Headers();
|
|
636
635
|
if (!headers.has("Content-Type")) {
|
|
637
|
-
const detectedType = __detectContentTypeHeader(
|
|
636
|
+
const detectedType = __detectContentTypeHeader(request.body);
|
|
638
637
|
if (detectedType) {
|
|
639
638
|
headers.set("Content-Type", detectedType);
|
|
640
639
|
}
|
|
@@ -642,41 +641,40 @@ function __createRequest(request2) {
|
|
|
642
641
|
if (!headers.has("Accept")) {
|
|
643
642
|
headers.set("Accept", "application/json, text/plain, */*");
|
|
644
643
|
}
|
|
645
|
-
const credentials =
|
|
646
|
-
const fetchConfig = request2.context?.get(FETCH_CONFIG_KEY) || {};
|
|
644
|
+
const credentials = request.withCredentials ? "include" : void 0;
|
|
647
645
|
return new Request(url, {
|
|
648
|
-
...fetchConfig,
|
|
649
646
|
headers,
|
|
650
|
-
method:
|
|
651
|
-
body: __serializeBody(
|
|
652
|
-
signal:
|
|
647
|
+
method: request.method,
|
|
648
|
+
body: __serializeBody(request.body),
|
|
649
|
+
signal: request.abort,
|
|
653
650
|
credentials
|
|
654
651
|
});
|
|
655
652
|
}
|
|
656
653
|
async function fetchHandler(httpRequest) {
|
|
657
654
|
const downloadProgress = httpRequest.downloadProgress;
|
|
658
|
-
const
|
|
659
|
-
|
|
655
|
+
const request = __createRequest(httpRequest);
|
|
656
|
+
const abortSignal = httpRequest.abort;
|
|
657
|
+
let response;
|
|
660
658
|
try {
|
|
661
|
-
|
|
662
|
-
} catch (
|
|
663
|
-
if (
|
|
659
|
+
response = await globalThis.fetch(request);
|
|
660
|
+
} catch (error) {
|
|
661
|
+
if (abortSignal?.aborted && abortSignal.reason instanceof Error) {
|
|
664
662
|
switch (true) {
|
|
665
|
-
case
|
|
663
|
+
case abortSignal.reason.name === "AbortError":
|
|
666
664
|
throw new HttpErrorResponse({ error: ERR_ABORTED });
|
|
667
|
-
case
|
|
665
|
+
case abortSignal.reason.name === "TimeoutError":
|
|
668
666
|
throw new HttpErrorResponse({ error: ERR_TIMEOUT });
|
|
669
667
|
}
|
|
670
668
|
}
|
|
671
|
-
throw new HttpErrorResponse({ error
|
|
669
|
+
throw new HttpErrorResponse({ error });
|
|
672
670
|
}
|
|
673
|
-
const { ok, headers, status, statusText, url } =
|
|
671
|
+
const { ok, headers, status, statusText, url } = response;
|
|
674
672
|
const contentLength = __getContentLength(headers);
|
|
675
673
|
const contentType = __getContentType(headers);
|
|
676
674
|
let body = null;
|
|
677
|
-
if (
|
|
675
|
+
if (response.body) {
|
|
678
676
|
const chunks = [];
|
|
679
|
-
const reader =
|
|
677
|
+
const reader = response.body.getReader();
|
|
680
678
|
let receivedLength = 0;
|
|
681
679
|
while (true) {
|
|
682
680
|
const { done, value } = await reader.read();
|
|
@@ -698,9 +696,9 @@ async function fetchHandler(httpRequest) {
|
|
|
698
696
|
content: chunksAll,
|
|
699
697
|
contentType
|
|
700
698
|
});
|
|
701
|
-
} catch (
|
|
699
|
+
} catch (error) {
|
|
702
700
|
throw new HttpErrorResponse({
|
|
703
|
-
error
|
|
701
|
+
error,
|
|
704
702
|
status,
|
|
705
703
|
statusText,
|
|
706
704
|
headers,
|
|
@@ -725,20 +723,22 @@ async function fetchHandler(httpRequest) {
|
|
|
725
723
|
body
|
|
726
724
|
});
|
|
727
725
|
}
|
|
728
|
-
|
|
729
|
-
|
|
726
|
+
|
|
727
|
+
const CLIENT = Symbol("Client");
|
|
728
|
+
let globalClient;
|
|
730
729
|
function isClient(value) {
|
|
731
730
|
return typeof value === "object" && value !== null && CLIENT in value;
|
|
732
731
|
}
|
|
733
|
-
function getClientConfig(
|
|
734
|
-
if (!isClient(
|
|
732
|
+
function getClientConfig(client) {
|
|
733
|
+
if (!isClient(client)) {
|
|
735
734
|
throw ERR_INVALID_CLIENT;
|
|
736
735
|
}
|
|
737
|
-
return
|
|
736
|
+
return client[CLIENT];
|
|
738
737
|
}
|
|
739
738
|
function createClient(options) {
|
|
740
739
|
const conf = {
|
|
741
740
|
host: options.host,
|
|
741
|
+
strict: options.strict ?? true,
|
|
742
742
|
interceptors: options.interceptors ?? [],
|
|
743
743
|
handler: options.handler || fetchHandler
|
|
744
744
|
};
|
|
@@ -746,8 +746,8 @@ function createClient(options) {
|
|
|
746
746
|
[CLIENT]: conf
|
|
747
747
|
};
|
|
748
748
|
}
|
|
749
|
-
function cloneClient(
|
|
750
|
-
const preConf = getClientConfig(
|
|
749
|
+
function cloneClient(client, options) {
|
|
750
|
+
const preConf = getClientConfig(client);
|
|
751
751
|
const conf = {
|
|
752
752
|
...preConf,
|
|
753
753
|
...options
|
|
@@ -762,70 +762,68 @@ function getGlobalClient() {
|
|
|
762
762
|
}
|
|
763
763
|
return globalClient;
|
|
764
764
|
}
|
|
765
|
-
function setGlobalClient(
|
|
766
|
-
globalClient =
|
|
765
|
+
function setGlobalClient(client) {
|
|
766
|
+
globalClient = client;
|
|
767
767
|
}
|
|
768
768
|
function restGlobalClient() {
|
|
769
|
-
globalClient =
|
|
769
|
+
globalClient = void 0;
|
|
770
770
|
}
|
|
771
771
|
function createGlobalClient(options) {
|
|
772
772
|
globalClient = createClient(options);
|
|
773
773
|
}
|
|
774
|
-
|
|
775
|
-
var globalClient;
|
|
776
|
-
// src/interceptor/authorization/basic_auth_interceptor.ts
|
|
774
|
+
|
|
777
775
|
function basicAuthInterceptor(fn, options) {
|
|
778
776
|
let encode = options?.encode;
|
|
779
777
|
if (!encode) {
|
|
780
|
-
const btoa =
|
|
778
|
+
const btoa = globalThis.btoa;
|
|
781
779
|
if (typeof btoa !== "function") {
|
|
782
780
|
throw new Error("BasicAuthInterceptor is not supported in this environment");
|
|
783
781
|
}
|
|
784
782
|
encode = (data) => btoa(`${data.username}:${data.password}`);
|
|
785
783
|
}
|
|
786
784
|
return (req, next) => {
|
|
787
|
-
const headers = req.headers || new Headers;
|
|
785
|
+
const headers = req.headers || new Headers();
|
|
788
786
|
headers.set("Authorization", `Basic ${encode(fn())}`);
|
|
789
787
|
req.headers = headers;
|
|
790
788
|
return next(req);
|
|
791
789
|
};
|
|
792
790
|
}
|
|
793
|
-
|
|
794
|
-
|
|
791
|
+
|
|
792
|
+
const required = (value) => {
|
|
795
793
|
if (!value) {
|
|
796
794
|
return new Error("required");
|
|
797
795
|
}
|
|
798
796
|
return null;
|
|
799
797
|
};
|
|
800
|
-
|
|
798
|
+
const requiredTrue = (value) => {
|
|
801
799
|
if (typeof value !== "boolean" || !value) {
|
|
802
800
|
return new Error("requiredTrue");
|
|
803
801
|
}
|
|
804
802
|
return null;
|
|
805
803
|
};
|
|
806
|
-
|
|
804
|
+
const minLength = (min2) => {
|
|
807
805
|
return (value) => {
|
|
808
806
|
if (typeof value !== "string") {
|
|
809
807
|
return new Error("invalid string");
|
|
810
808
|
}
|
|
811
|
-
if (value.length <
|
|
812
|
-
return new Error(`minLength ${
|
|
809
|
+
if (value.length < min2) {
|
|
810
|
+
return new Error(`minLength ${min2}`);
|
|
813
811
|
}
|
|
814
812
|
return null;
|
|
815
813
|
};
|
|
816
814
|
};
|
|
817
|
-
|
|
815
|
+
const maxLength = (max2) => {
|
|
818
816
|
return (value) => {
|
|
819
817
|
if (typeof value !== "string") {
|
|
820
818
|
return new Error("invalid string");
|
|
821
819
|
}
|
|
822
|
-
if (value.length >
|
|
823
|
-
return new Error(`maxLength ${
|
|
820
|
+
if (value.length > max2) {
|
|
821
|
+
return new Error(`maxLength ${max2}`);
|
|
824
822
|
}
|
|
825
823
|
return null;
|
|
826
824
|
};
|
|
827
825
|
};
|
|
828
|
-
|
|
826
|
+
const min = (min2) => {
|
|
829
827
|
return (value) => {
|
|
830
828
|
if (typeof value !== "number" || isNaN(value)) {
|
|
831
829
|
return new Error("invalid number");
|
|
@@ -836,7 +834,7 @@ var min = (min2) => {
|
|
|
836
834
|
return null;
|
|
837
835
|
};
|
|
838
836
|
};
|
|
839
|
-
|
|
837
|
+
const max = (max2) => {
|
|
840
838
|
return (value) => {
|
|
841
839
|
if (typeof value !== "number" || isNaN(value)) {
|
|
842
840
|
return new Error("invalid number");
|
|
@@ -847,7 +845,7 @@ var max = (max2) => {
|
|
|
847
845
|
return null;
|
|
848
846
|
};
|
|
849
847
|
};
|
|
850
|
-
|
|
848
|
+
const pattern = (pattern2) => {
|
|
851
849
|
return (value) => {
|
|
852
850
|
if (typeof value !== "string") {
|
|
853
851
|
return new Error("invalid string");
|
|
@@ -858,43 +856,5 @@ var pattern = (pattern2) => {
|
|
|
858
856
|
return null;
|
|
859
857
|
};
|
|
860
858
|
};
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
setGlobalClient,
|
|
864
|
-
restGlobalClient,
|
|
865
|
-
requiredTrue,
|
|
866
|
-
required,
|
|
867
|
-
pattern,
|
|
868
|
-
minLength,
|
|
869
|
-
min,
|
|
870
|
-
maxLength,
|
|
871
|
-
max,
|
|
872
|
-
makeHttpContextToken,
|
|
873
|
-
makeHttpContext,
|
|
874
|
-
isHttpContextToken,
|
|
875
|
-
isFieldGroup,
|
|
876
|
-
isField,
|
|
877
|
-
isClient,
|
|
878
|
-
getGlobalClient,
|
|
879
|
-
field,
|
|
880
|
-
fetchHandler,
|
|
881
|
-
defineRequest,
|
|
882
|
-
createGlobalClient,
|
|
883
|
-
createClient,
|
|
884
|
-
cloneClient,
|
|
885
|
-
basicAuthInterceptor,
|
|
886
|
-
asyncValidatorField,
|
|
887
|
-
HttpErrorResponse,
|
|
888
|
-
FETCH_CONFIG_KEY,
|
|
889
|
-
ERR_UNSUPPORTED_FIELD_TYPE,
|
|
890
|
-
ERR_UNKNOWN,
|
|
891
|
-
ERR_TIMEOUT,
|
|
892
|
-
ERR_OBSERVE,
|
|
893
|
-
ERR_NOT_SET_ALIAS,
|
|
894
|
-
ERR_NOT_FOUND_HANDLER,
|
|
895
|
-
ERR_NOT_FOUND_GLOBAL_CLIENT,
|
|
896
|
-
ERR_NETWORK,
|
|
897
|
-
ERR_INVALID_HTTP_CONTEXT_TOKEN,
|
|
898
|
-
ERR_INVALID_CLIENT,
|
|
899
|
-
ERR_ABORTED
|
|
900
|
-
};
|
|
859
|
+
|
|
860
|
+
export { ERR_ABORTED, ERR_INVALID_CLIENT, ERR_INVALID_HTTP_CONTEXT_TOKEN, ERR_NETWORK, ERR_NOT_FOUND_GLOBAL_CLIENT, ERR_NOT_FOUND_HANDLER, ERR_NOT_SET_ALIAS, ERR_OBSERVE, ERR_TIMEOUT, ERR_UNKNOWN, ERR_UNSUPPORTED_FIELD_TYPE, HttpErrorResponse, basicAuthInterceptor, cloneClient, createClient, createGlobalClient, defineRequest, doValid, fetchHandler, field, getGlobalClient, isClient, isField, isFieldGroup, isHttpContextToken, makeHttpContext, makeHttpContextToken, max, maxLength, min, minLength, pattern, required, requiredTrue, restGlobalClient, setGlobalClient };
|