@defjs/core 0.0.0-test-003 → 0.0.0-test-005
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} +250 -286
- 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
|
};
|
|
@@ -265,7 +330,11 @@ function defineRequest(method, endpoint) {
|
|
|
265
330
|
let doRequestOptions;
|
|
266
331
|
switch (args.length) {
|
|
267
332
|
case 1: {
|
|
268
|
-
|
|
333
|
+
if (requiredInput) {
|
|
334
|
+
input = args[0];
|
|
335
|
+
} else {
|
|
336
|
+
doRequestOptions = args[0];
|
|
337
|
+
}
|
|
269
338
|
break;
|
|
270
339
|
}
|
|
271
340
|
case 2: {
|
|
@@ -288,41 +357,41 @@ function defineRequest(method, endpoint) {
|
|
|
288
357
|
} else if (timeout > 0) {
|
|
289
358
|
abortSignal = AbortSignal.timeout(timeout);
|
|
290
359
|
}
|
|
291
|
-
const
|
|
292
|
-
const clientOptions = getClientConfig(
|
|
360
|
+
const client = isClient(doRequestOptions?.client) ? doRequestOptions.client : getGlobalClient();
|
|
361
|
+
const clientOptions = getClientConfig(client);
|
|
293
362
|
const req = {
|
|
294
363
|
host: clientOptions?.host,
|
|
295
364
|
method,
|
|
296
365
|
endpoint,
|
|
297
|
-
queryParams: new URLSearchParams,
|
|
298
|
-
headers: new Headers,
|
|
299
|
-
body:
|
|
366
|
+
queryParams: new URLSearchParams(),
|
|
367
|
+
headers: new Headers(),
|
|
368
|
+
body: void 0,
|
|
300
369
|
withCredentials,
|
|
301
370
|
responseType,
|
|
302
|
-
context:
|
|
371
|
+
context: context || makeHttpContext(),
|
|
303
372
|
observe,
|
|
304
373
|
uploadProgress,
|
|
305
374
|
downloadProgress,
|
|
306
375
|
timeout,
|
|
307
376
|
abort: abortSignal
|
|
308
377
|
};
|
|
309
|
-
const
|
|
310
|
-
if (!
|
|
378
|
+
const handler = doRequestOptions?.handler || clientOptions?.handler || getGlobalHttpHandler();
|
|
379
|
+
if (!handler) {
|
|
311
380
|
throw ERR_NOT_FOUND_HANDLER;
|
|
312
381
|
}
|
|
313
|
-
if (requiredInput) {
|
|
314
|
-
await __fillRequestFromField(req,
|
|
382
|
+
if (requiredInput && field) {
|
|
383
|
+
await __fillRequestFromField(req, field, input);
|
|
315
384
|
}
|
|
316
385
|
const chain = makeInterceptorChain([...interceptors]);
|
|
317
|
-
let res = await chain(req,
|
|
386
|
+
let res = await chain(req, handler);
|
|
318
387
|
if (typeof transformResponse === "function") {
|
|
319
388
|
try {
|
|
320
389
|
res = __makeResponse({
|
|
321
390
|
...res,
|
|
322
391
|
body: transformResponse(res)
|
|
323
392
|
});
|
|
324
|
-
} catch (
|
|
325
|
-
throw new HttpErrorResponse({ error
|
|
393
|
+
} catch (error) {
|
|
394
|
+
throw new HttpErrorResponse({ error });
|
|
326
395
|
}
|
|
327
396
|
}
|
|
328
397
|
switch (req.observe) {
|
|
@@ -342,7 +411,7 @@ function defineRequest(method, endpoint) {
|
|
|
342
411
|
};
|
|
343
412
|
};
|
|
344
413
|
fn.withField = (value) => {
|
|
345
|
-
|
|
414
|
+
field = value;
|
|
346
415
|
requiredInput = true;
|
|
347
416
|
return fn;
|
|
348
417
|
};
|
|
@@ -351,7 +420,7 @@ function defineRequest(method, endpoint) {
|
|
|
351
420
|
return fn;
|
|
352
421
|
};
|
|
353
422
|
fn.withContext = (value) => {
|
|
354
|
-
|
|
423
|
+
context = value;
|
|
355
424
|
return fn;
|
|
356
425
|
};
|
|
357
426
|
fn.withCredentials = (value) => {
|
|
@@ -382,15 +451,6 @@ function __fillUrl(endpoint, params) {
|
|
|
382
451
|
});
|
|
383
452
|
}
|
|
384
453
|
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
454
|
function inputIsObject(input2) {
|
|
395
455
|
return typeof input2 === "object" && input2 !== null;
|
|
396
456
|
}
|
|
@@ -441,52 +501,49 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
441
501
|
let params;
|
|
442
502
|
let queryParams;
|
|
443
503
|
let headers;
|
|
444
|
-
let body =
|
|
504
|
+
let body = void 0;
|
|
445
505
|
const validValue = getValidValue(input, fieldOrFieldGroup());
|
|
446
506
|
const meta = __getFieldMetadata(fieldOrFieldGroup);
|
|
447
|
-
|
|
448
|
-
if (err) {
|
|
449
|
-
throw err;
|
|
450
|
-
}
|
|
507
|
+
await doValid([...meta.validators, ...meta.asyncValidator], validValue);
|
|
451
508
|
for (const [type, aliasName] of meta.alias) {
|
|
452
509
|
switch (type) {
|
|
453
|
-
case
|
|
510
|
+
case FieldType.Query: {
|
|
454
511
|
if (!aliasName) {
|
|
455
512
|
throw ERR_NOT_SET_ALIAS;
|
|
456
513
|
}
|
|
457
|
-
queryParams = appendValue(new URLSearchParams, aliasName, validValue);
|
|
514
|
+
queryParams = appendValue(new URLSearchParams(), aliasName, validValue);
|
|
458
515
|
break;
|
|
459
516
|
}
|
|
460
|
-
case
|
|
517
|
+
case FieldType.Param: {
|
|
461
518
|
if (!aliasName) {
|
|
462
519
|
throw ERR_NOT_SET_ALIAS;
|
|
463
520
|
}
|
|
464
|
-
params = setValue(new Map, aliasName, validValue);
|
|
521
|
+
params = setValue(/* @__PURE__ */ new Map(), aliasName, validValue);
|
|
465
522
|
break;
|
|
466
523
|
}
|
|
467
|
-
case
|
|
524
|
+
case FieldType.Header: {
|
|
468
525
|
if (!aliasName) {
|
|
469
526
|
throw ERR_NOT_SET_ALIAS;
|
|
470
527
|
}
|
|
471
|
-
headers = appendValue(new Headers, aliasName, validValue);
|
|
528
|
+
headers = appendValue(new Headers(), aliasName, validValue);
|
|
472
529
|
break;
|
|
473
530
|
}
|
|
474
|
-
case
|
|
531
|
+
case FieldType.Form: {
|
|
475
532
|
if (!aliasName) {
|
|
476
533
|
throw ERR_NOT_SET_ALIAS;
|
|
477
534
|
}
|
|
478
|
-
body = appendValue(new FormData, aliasName, validValue);
|
|
535
|
+
body = appendValue(new FormData(), aliasName, validValue);
|
|
479
536
|
break;
|
|
480
537
|
}
|
|
481
|
-
case
|
|
538
|
+
case FieldType.UrlForm: {
|
|
482
539
|
if (!aliasName) {
|
|
483
540
|
throw ERR_NOT_SET_ALIAS;
|
|
484
541
|
}
|
|
485
|
-
body = appendValue(new URLSearchParams, aliasName, validValue);
|
|
542
|
+
body = appendValue(new URLSearchParams(), aliasName, validValue);
|
|
486
543
|
break;
|
|
487
544
|
}
|
|
488
|
-
case
|
|
489
|
-
case
|
|
545
|
+
case FieldType.Json:
|
|
546
|
+
case FieldType.Body:
|
|
490
547
|
body = validValue;
|
|
491
548
|
break;
|
|
492
549
|
}
|
|
@@ -500,55 +557,52 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
500
557
|
return;
|
|
501
558
|
}
|
|
502
559
|
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;
|
|
560
|
+
let params = /* @__PURE__ */ new Map();
|
|
561
|
+
let queryParams = new URLSearchParams();
|
|
562
|
+
let headers = new Headers();
|
|
563
|
+
let urlForm = new URLSearchParams();
|
|
564
|
+
let formData = new FormData();
|
|
508
565
|
const json = {};
|
|
509
|
-
let body =
|
|
566
|
+
let body = void 0;
|
|
510
567
|
let lastBodyField;
|
|
511
|
-
for (const [propertyKey,
|
|
512
|
-
const meta = __getFieldMetadata(
|
|
568
|
+
for (const [propertyKey, field] of Object.entries(fieldOrFieldGroup)) {
|
|
569
|
+
const meta = __getFieldMetadata(field);
|
|
513
570
|
for (const [type, aliasName] of meta.alias) {
|
|
514
571
|
const valueKey = aliasName || propertyKey;
|
|
515
|
-
const value = getValidValue(input[propertyKey],
|
|
516
|
-
|
|
517
|
-
if (err) {
|
|
518
|
-
throw err;
|
|
519
|
-
}
|
|
572
|
+
const value = getValidValue(input[propertyKey], field());
|
|
573
|
+
await doValid([...meta.validators, ...meta.asyncValidator], value);
|
|
520
574
|
switch (type) {
|
|
521
|
-
case
|
|
575
|
+
case FieldType.Json: {
|
|
522
576
|
Object.defineProperty(json, valueKey, {
|
|
523
577
|
value,
|
|
524
578
|
writable: true,
|
|
525
579
|
enumerable: true
|
|
526
580
|
});
|
|
527
|
-
lastBodyField =
|
|
581
|
+
lastBodyField = FieldType.Json;
|
|
528
582
|
break;
|
|
529
583
|
}
|
|
530
|
-
case
|
|
584
|
+
case FieldType.Query:
|
|
531
585
|
queryParams = appendValue(queryParams, valueKey, value);
|
|
532
586
|
break;
|
|
533
|
-
case
|
|
587
|
+
case FieldType.Param:
|
|
534
588
|
params = setValue(params, valueKey, value);
|
|
535
589
|
break;
|
|
536
|
-
case
|
|
590
|
+
case FieldType.Header:
|
|
537
591
|
headers = appendValue(headers, valueKey, value);
|
|
538
592
|
break;
|
|
539
|
-
case
|
|
593
|
+
case FieldType.Form: {
|
|
540
594
|
formData = appendValue(formData, valueKey, value);
|
|
541
|
-
lastBodyField =
|
|
595
|
+
lastBodyField = FieldType.Form;
|
|
542
596
|
break;
|
|
543
597
|
}
|
|
544
|
-
case
|
|
598
|
+
case FieldType.UrlForm: {
|
|
545
599
|
urlForm = appendValue(urlForm, valueKey, value);
|
|
546
|
-
lastBodyField =
|
|
600
|
+
lastBodyField = FieldType.UrlForm;
|
|
547
601
|
break;
|
|
548
602
|
}
|
|
549
|
-
case
|
|
603
|
+
case FieldType.Body: {
|
|
550
604
|
body = value;
|
|
551
|
-
lastBodyField =
|
|
605
|
+
lastBodyField = FieldType.Body;
|
|
552
606
|
break;
|
|
553
607
|
}
|
|
554
608
|
}
|
|
@@ -558,16 +612,16 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
558
612
|
request.queryParams = queryParams;
|
|
559
613
|
request.headers = headers;
|
|
560
614
|
switch (lastBodyField) {
|
|
561
|
-
case
|
|
615
|
+
case FieldType.Body:
|
|
562
616
|
request.body = body;
|
|
563
617
|
break;
|
|
564
|
-
case
|
|
618
|
+
case FieldType.Form:
|
|
565
619
|
request.body = formData;
|
|
566
620
|
break;
|
|
567
|
-
case
|
|
621
|
+
case FieldType.UrlForm:
|
|
568
622
|
request.body = urlForm;
|
|
569
623
|
break;
|
|
570
|
-
case
|
|
624
|
+
case FieldType.Json:
|
|
571
625
|
request.body = json;
|
|
572
626
|
break;
|
|
573
627
|
}
|
|
@@ -576,65 +630,14 @@ async function __fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
576
630
|
throw ERR_UNSUPPORTED_FIELD_TYPE;
|
|
577
631
|
}
|
|
578
632
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
return 0;
|
|
584
|
-
}
|
|
585
|
-
const num = Number(value);
|
|
586
|
-
if (isNaN(num)) {
|
|
587
|
-
return 0;
|
|
633
|
+
function __createRequest(request) {
|
|
634
|
+
const url = new URL(request.endpoint, request.host);
|
|
635
|
+
if (request.queryParams) {
|
|
636
|
+
url.search = request.queryParams.toString();
|
|
588
637
|
}
|
|
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;
|
|
638
|
+
const headers = request.headers ?? new Headers();
|
|
636
639
|
if (!headers.has("Content-Type")) {
|
|
637
|
-
const detectedType = __detectContentTypeHeader(
|
|
640
|
+
const detectedType = __detectContentTypeHeader(request.body);
|
|
638
641
|
if (detectedType) {
|
|
639
642
|
headers.set("Content-Type", detectedType);
|
|
640
643
|
}
|
|
@@ -642,41 +645,40 @@ function __createRequest(request2) {
|
|
|
642
645
|
if (!headers.has("Accept")) {
|
|
643
646
|
headers.set("Accept", "application/json, text/plain, */*");
|
|
644
647
|
}
|
|
645
|
-
const credentials =
|
|
646
|
-
const fetchConfig = request2.context?.get(FETCH_CONFIG_KEY) || {};
|
|
648
|
+
const credentials = request.withCredentials ? "include" : void 0;
|
|
647
649
|
return new Request(url, {
|
|
648
|
-
...fetchConfig,
|
|
649
650
|
headers,
|
|
650
|
-
method:
|
|
651
|
-
body: __serializeBody(
|
|
652
|
-
signal:
|
|
651
|
+
method: request.method,
|
|
652
|
+
body: __serializeBody(request.body),
|
|
653
|
+
signal: request.abort,
|
|
653
654
|
credentials
|
|
654
655
|
});
|
|
655
656
|
}
|
|
656
657
|
async function fetchHandler(httpRequest) {
|
|
657
658
|
const downloadProgress = httpRequest.downloadProgress;
|
|
658
|
-
const
|
|
659
|
-
|
|
659
|
+
const request = __createRequest(httpRequest);
|
|
660
|
+
const abortSignal = httpRequest.abort;
|
|
661
|
+
let response;
|
|
660
662
|
try {
|
|
661
|
-
|
|
662
|
-
} catch (
|
|
663
|
-
if (
|
|
663
|
+
response = await globalThis.fetch(request);
|
|
664
|
+
} catch (error) {
|
|
665
|
+
if (abortSignal?.aborted && abortSignal.reason instanceof Error) {
|
|
664
666
|
switch (true) {
|
|
665
|
-
case
|
|
667
|
+
case abortSignal.reason.name === "AbortError":
|
|
666
668
|
throw new HttpErrorResponse({ error: ERR_ABORTED });
|
|
667
|
-
case
|
|
669
|
+
case abortSignal.reason.name === "TimeoutError":
|
|
668
670
|
throw new HttpErrorResponse({ error: ERR_TIMEOUT });
|
|
669
671
|
}
|
|
670
672
|
}
|
|
671
|
-
throw new HttpErrorResponse({ error
|
|
673
|
+
throw new HttpErrorResponse({ error });
|
|
672
674
|
}
|
|
673
|
-
const { ok, headers, status, statusText, url } =
|
|
675
|
+
const { ok, headers, status, statusText, url } = response;
|
|
674
676
|
const contentLength = __getContentLength(headers);
|
|
675
677
|
const contentType = __getContentType(headers);
|
|
676
678
|
let body = null;
|
|
677
|
-
if (
|
|
679
|
+
if (response.body) {
|
|
678
680
|
const chunks = [];
|
|
679
|
-
const reader =
|
|
681
|
+
const reader = response.body.getReader();
|
|
680
682
|
let receivedLength = 0;
|
|
681
683
|
while (true) {
|
|
682
684
|
const { done, value } = await reader.read();
|
|
@@ -698,9 +700,9 @@ async function fetchHandler(httpRequest) {
|
|
|
698
700
|
content: chunksAll,
|
|
699
701
|
contentType
|
|
700
702
|
});
|
|
701
|
-
} catch (
|
|
703
|
+
} catch (error) {
|
|
702
704
|
throw new HttpErrorResponse({
|
|
703
|
-
error
|
|
705
|
+
error,
|
|
704
706
|
status,
|
|
705
707
|
statusText,
|
|
706
708
|
headers,
|
|
@@ -725,20 +727,22 @@ async function fetchHandler(httpRequest) {
|
|
|
725
727
|
body
|
|
726
728
|
});
|
|
727
729
|
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
+
|
|
731
|
+
const CLIENT = Symbol("Client");
|
|
732
|
+
let globalClient;
|
|
730
733
|
function isClient(value) {
|
|
731
734
|
return typeof value === "object" && value !== null && CLIENT in value;
|
|
732
735
|
}
|
|
733
|
-
function getClientConfig(
|
|
734
|
-
if (!isClient(
|
|
736
|
+
function getClientConfig(client) {
|
|
737
|
+
if (!isClient(client)) {
|
|
735
738
|
throw ERR_INVALID_CLIENT;
|
|
736
739
|
}
|
|
737
|
-
return
|
|
740
|
+
return client[CLIENT];
|
|
738
741
|
}
|
|
739
742
|
function createClient(options) {
|
|
740
743
|
const conf = {
|
|
741
744
|
host: options.host,
|
|
745
|
+
strict: options.strict ?? true,
|
|
742
746
|
interceptors: options.interceptors ?? [],
|
|
743
747
|
handler: options.handler || fetchHandler
|
|
744
748
|
};
|
|
@@ -746,8 +750,8 @@ function createClient(options) {
|
|
|
746
750
|
[CLIENT]: conf
|
|
747
751
|
};
|
|
748
752
|
}
|
|
749
|
-
function cloneClient(
|
|
750
|
-
const preConf = getClientConfig(
|
|
753
|
+
function cloneClient(client, options) {
|
|
754
|
+
const preConf = getClientConfig(client);
|
|
751
755
|
const conf = {
|
|
752
756
|
...preConf,
|
|
753
757
|
...options
|
|
@@ -762,70 +766,68 @@ function getGlobalClient() {
|
|
|
762
766
|
}
|
|
763
767
|
return globalClient;
|
|
764
768
|
}
|
|
765
|
-
function setGlobalClient(
|
|
766
|
-
globalClient =
|
|
769
|
+
function setGlobalClient(client) {
|
|
770
|
+
globalClient = client;
|
|
767
771
|
}
|
|
768
772
|
function restGlobalClient() {
|
|
769
|
-
globalClient =
|
|
773
|
+
globalClient = void 0;
|
|
770
774
|
}
|
|
771
775
|
function createGlobalClient(options) {
|
|
772
776
|
globalClient = createClient(options);
|
|
773
777
|
}
|
|
774
|
-
|
|
775
|
-
var globalClient;
|
|
776
|
-
// src/interceptor/authorization/basic_auth_interceptor.ts
|
|
778
|
+
|
|
777
779
|
function basicAuthInterceptor(fn, options) {
|
|
778
780
|
let encode = options?.encode;
|
|
779
781
|
if (!encode) {
|
|
780
|
-
const btoa =
|
|
782
|
+
const btoa = globalThis.btoa;
|
|
781
783
|
if (typeof btoa !== "function") {
|
|
782
784
|
throw new Error("BasicAuthInterceptor is not supported in this environment");
|
|
783
785
|
}
|
|
784
786
|
encode = (data) => btoa(`${data.username}:${data.password}`);
|
|
785
787
|
}
|
|
786
788
|
return (req, next) => {
|
|
787
|
-
const headers = req.headers || new Headers;
|
|
789
|
+
const headers = req.headers || new Headers();
|
|
788
790
|
headers.set("Authorization", `Basic ${encode(fn())}`);
|
|
789
791
|
req.headers = headers;
|
|
790
792
|
return next(req);
|
|
791
793
|
};
|
|
792
794
|
}
|
|
793
|
-
|
|
794
|
-
|
|
795
|
+
|
|
796
|
+
const required = (value) => {
|
|
795
797
|
if (!value) {
|
|
796
798
|
return new Error("required");
|
|
797
799
|
}
|
|
798
800
|
return null;
|
|
799
801
|
};
|
|
800
|
-
|
|
802
|
+
const requiredTrue = (value) => {
|
|
801
803
|
if (typeof value !== "boolean" || !value) {
|
|
802
804
|
return new Error("requiredTrue");
|
|
803
805
|
}
|
|
804
806
|
return null;
|
|
805
807
|
};
|
|
806
|
-
|
|
808
|
+
const minLength = (min2) => {
|
|
807
809
|
return (value) => {
|
|
808
810
|
if (typeof value !== "string") {
|
|
809
811
|
return new Error("invalid string");
|
|
810
812
|
}
|
|
811
|
-
if (value.length <
|
|
812
|
-
return new Error(`minLength ${
|
|
813
|
+
if (value.length < min2) {
|
|
814
|
+
return new Error(`minLength ${min2}`);
|
|
813
815
|
}
|
|
814
816
|
return null;
|
|
815
817
|
};
|
|
816
818
|
};
|
|
817
|
-
|
|
819
|
+
const maxLength = (max2) => {
|
|
818
820
|
return (value) => {
|
|
819
821
|
if (typeof value !== "string") {
|
|
820
822
|
return new Error("invalid string");
|
|
821
823
|
}
|
|
822
|
-
if (value.length >
|
|
823
|
-
return new Error(`maxLength ${
|
|
824
|
+
if (value.length > max2) {
|
|
825
|
+
return new Error(`maxLength ${max2}`);
|
|
824
826
|
}
|
|
825
827
|
return null;
|
|
826
828
|
};
|
|
827
829
|
};
|
|
828
|
-
|
|
830
|
+
const min = (min2) => {
|
|
829
831
|
return (value) => {
|
|
830
832
|
if (typeof value !== "number" || isNaN(value)) {
|
|
831
833
|
return new Error("invalid number");
|
|
@@ -836,7 +838,7 @@ var min = (min2) => {
|
|
|
836
838
|
return null;
|
|
837
839
|
};
|
|
838
840
|
};
|
|
839
|
-
|
|
841
|
+
const max = (max2) => {
|
|
840
842
|
return (value) => {
|
|
841
843
|
if (typeof value !== "number" || isNaN(value)) {
|
|
842
844
|
return new Error("invalid number");
|
|
@@ -847,7 +849,7 @@ var max = (max2) => {
|
|
|
847
849
|
return null;
|
|
848
850
|
};
|
|
849
851
|
};
|
|
850
|
-
|
|
852
|
+
const pattern = (pattern2) => {
|
|
851
853
|
return (value) => {
|
|
852
854
|
if (typeof value !== "string") {
|
|
853
855
|
return new Error("invalid string");
|
|
@@ -858,43 +860,5 @@ var pattern = (pattern2) => {
|
|
|
858
860
|
return null;
|
|
859
861
|
};
|
|
860
862
|
};
|
|
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
|
-
};
|
|
863
|
+
|
|
864
|
+
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 };
|