@defjs/core 0.0.0-test-0005 → 0.0.0-test-0007
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +18 -12
- package/dist/index.js +67 -44
- package/package.json +23 -23
package/dist/index.d.ts
CHANGED
|
@@ -69,11 +69,17 @@ export declare function makeResponse<R>(options: MakeResponseOptions<R>): HttpRe
|
|
|
69
69
|
export type InterceptorFn = (req: HttpRequest, next: HttpHandler) => Promise<HttpResponse<unknown>>;
|
|
70
70
|
export type HttpResponseType = "arraybuffer" | "blob" | "json" | "text";
|
|
71
71
|
export interface HttpProgressEvent {
|
|
72
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* Indicates whether the resources associated with Progress have a computable length.
|
|
74
|
+
* Otherwise, the Progress.total attribute will be a meaningless value.
|
|
75
|
+
*/
|
|
73
76
|
readonly lengthComputable: boolean;
|
|
74
|
-
/**
|
|
77
|
+
/** Indicates the workload of the underlying process. */
|
|
75
78
|
readonly loaded: number;
|
|
76
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* The total size of the data being processed or transmitted.
|
|
81
|
+
* If the Progress.lengthComputable attribute is false, this value is meaningless and should be ignored.
|
|
82
|
+
*/
|
|
77
83
|
readonly total: number;
|
|
78
84
|
}
|
|
79
85
|
export type HttpProgressFn = (value: HttpProgressEvent) => void;
|
|
@@ -84,19 +90,19 @@ export interface HttpRequest {
|
|
|
84
90
|
method: string;
|
|
85
91
|
endpoint: string;
|
|
86
92
|
body?: Blob | ArrayBuffer | FormData | object | string | number | boolean | null;
|
|
87
|
-
headers
|
|
88
|
-
queryParams
|
|
89
|
-
/**
|
|
90
|
-
responseType
|
|
91
|
-
context
|
|
92
|
-
timeout
|
|
93
|
+
headers?: Headers;
|
|
94
|
+
queryParams?: URLSearchParams;
|
|
95
|
+
/** default:json */
|
|
96
|
+
responseType?: HttpResponseType;
|
|
97
|
+
context?: HttpContext;
|
|
98
|
+
timeout?: number;
|
|
93
99
|
withCredentials?: boolean;
|
|
94
100
|
/** when use fetch handler, no upload progress */
|
|
95
101
|
uploadProgress?: HttpProgressFn;
|
|
96
102
|
downloadProgress?: HttpProgressFn;
|
|
97
|
-
/**
|
|
98
|
-
observe
|
|
99
|
-
abort
|
|
103
|
+
/** default:body */
|
|
104
|
+
observe?: "body" | "stream" | "response";
|
|
105
|
+
abort?: AbortSignal;
|
|
100
106
|
}
|
|
101
107
|
export type RequestInputValue<T> = T extends Field<infer V> ? V : T extends {
|
|
102
108
|
[K in keyof T]: Field<any>;
|
package/dist/index.js
CHANGED
|
@@ -159,10 +159,9 @@ function throwAliaNotSet() {
|
|
|
159
159
|
throw new Error("alias not set");
|
|
160
160
|
}
|
|
161
161
|
function fillUrl(endpoint, params) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
return endpoint;
|
|
162
|
+
return endpoint.replace(/:([^\/]+)/g, (_, p1) => {
|
|
163
|
+
return params.get(p1) ?? "undefined";
|
|
164
|
+
});
|
|
166
165
|
}
|
|
167
166
|
async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
168
167
|
async function doValid(validators, value) {
|
|
@@ -177,10 +176,30 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
177
176
|
function inputIsObject(input2) {
|
|
178
177
|
return typeof input2 === "object" && input2 !== null;
|
|
179
178
|
}
|
|
179
|
+
function appendValue(sp, key, value) {
|
|
180
|
+
if (Array.isArray(value)) {
|
|
181
|
+
for (const v of value) {
|
|
182
|
+
sp.append(key, String(v));
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
sp.set(key, String(value));
|
|
186
|
+
}
|
|
187
|
+
return sp;
|
|
188
|
+
}
|
|
189
|
+
function setValue(sp, key, value) {
|
|
190
|
+
if (Array.isArray(value)) {
|
|
191
|
+
for (const v of value) {
|
|
192
|
+
sp.set(key, String(v));
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
sp.set(key, String(value));
|
|
196
|
+
}
|
|
197
|
+
return sp;
|
|
198
|
+
}
|
|
180
199
|
if (isField(fieldOrFieldGroup)) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
200
|
+
let params;
|
|
201
|
+
let queryParams;
|
|
202
|
+
let headers;
|
|
184
203
|
let body = undefined;
|
|
185
204
|
const fieldValue = input ?? fieldOrFieldGroup();
|
|
186
205
|
const meta = getFieldMetadata(fieldOrFieldGroup);
|
|
@@ -197,30 +216,28 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
197
216
|
if (!aliasName) {
|
|
198
217
|
throwAliaNotSet();
|
|
199
218
|
}
|
|
200
|
-
queryParams
|
|
219
|
+
queryParams = appendValue(new URLSearchParams, aliasName, fieldValue);
|
|
201
220
|
break;
|
|
202
221
|
}
|
|
203
222
|
case 3 /* Param */: {
|
|
204
223
|
if (!aliasName) {
|
|
205
224
|
throwAliaNotSet();
|
|
206
225
|
}
|
|
207
|
-
|
|
226
|
+
params = setValue(new Map, aliasName, fieldValue);
|
|
208
227
|
break;
|
|
209
228
|
}
|
|
210
229
|
case 4 /* Header */: {
|
|
211
230
|
if (!aliasName) {
|
|
212
231
|
throwAliaNotSet();
|
|
213
232
|
}
|
|
214
|
-
headers
|
|
233
|
+
headers = appendValue(new Headers, aliasName, fieldValue);
|
|
215
234
|
break;
|
|
216
235
|
}
|
|
217
236
|
case 5 /* Form */: {
|
|
218
237
|
if (!aliasName) {
|
|
219
238
|
throwAliaNotSet();
|
|
220
239
|
}
|
|
221
|
-
|
|
222
|
-
form.append(aliasName, String(fieldValue));
|
|
223
|
-
body = form;
|
|
240
|
+
body = appendValue(new FormData, aliasName, fieldValue);
|
|
224
241
|
break;
|
|
225
242
|
}
|
|
226
243
|
case 6 /* Body */:
|
|
@@ -228,17 +245,19 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
228
245
|
break;
|
|
229
246
|
}
|
|
230
247
|
}
|
|
231
|
-
|
|
248
|
+
if (params) {
|
|
249
|
+
request.endpoint = fillUrl(request.endpoint, params);
|
|
250
|
+
}
|
|
232
251
|
request.queryParams = queryParams;
|
|
233
252
|
request.headers = headers;
|
|
234
253
|
request.body = body;
|
|
235
254
|
return;
|
|
236
255
|
}
|
|
237
256
|
if (isFieldGroup(fieldOrFieldGroup) && inputIsObject(input)) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
257
|
+
let params = new Map;
|
|
258
|
+
let queryParams = new URLSearchParams;
|
|
259
|
+
let headers = new Headers;
|
|
260
|
+
let formData = new FormData;
|
|
242
261
|
const json = {};
|
|
243
262
|
let body = undefined;
|
|
244
263
|
for (const [propertyKey, field3] of Object.entries(fieldOrFieldGroup)) {
|
|
@@ -258,32 +277,32 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
258
277
|
});
|
|
259
278
|
break;
|
|
260
279
|
case 2 /* Query */:
|
|
261
|
-
queryParams
|
|
280
|
+
queryParams = appendValue(queryParams, valueKey, value);
|
|
262
281
|
break;
|
|
263
282
|
case 3 /* Param */:
|
|
264
|
-
|
|
283
|
+
params = setValue(params, valueKey, value);
|
|
265
284
|
break;
|
|
266
285
|
case 4 /* Header */:
|
|
267
|
-
headers
|
|
286
|
+
headers = appendValue(headers, valueKey, value);
|
|
268
287
|
break;
|
|
269
288
|
case 5 /* Form */:
|
|
270
|
-
formData
|
|
289
|
+
formData = appendValue(formData, valueKey, value);
|
|
271
290
|
break;
|
|
272
291
|
case 6 /* Body */:
|
|
273
292
|
body = value;
|
|
274
293
|
break;
|
|
275
294
|
}
|
|
276
295
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
296
|
+
}
|
|
297
|
+
request.endpoint = fillUrl(request.endpoint, params);
|
|
298
|
+
request.queryParams = queryParams;
|
|
299
|
+
request.headers = headers;
|
|
300
|
+
if (body) {
|
|
301
|
+
request.body = body;
|
|
302
|
+
} else if (Array.from(formData.keys()).length > 0) {
|
|
303
|
+
request.body = formData;
|
|
304
|
+
} else if (Object.keys(json).length) {
|
|
305
|
+
request.body = JSON.stringify(json);
|
|
287
306
|
}
|
|
288
307
|
return;
|
|
289
308
|
}
|
|
@@ -325,7 +344,7 @@ async function doRequest(...args) {
|
|
|
325
344
|
}
|
|
326
345
|
const clientOptions = getClientConfig(options?.client || getGlobalClient());
|
|
327
346
|
const req = {
|
|
328
|
-
host: clientOptions?.host
|
|
347
|
+
host: clientOptions?.host,
|
|
329
348
|
method: requestOptions.method,
|
|
330
349
|
endpoint: requestOptions.endpoint,
|
|
331
350
|
queryParams: new URLSearchParams,
|
|
@@ -349,9 +368,9 @@ async function doRequest(...args) {
|
|
|
349
368
|
req.body = requestOptions.transformRequest(req);
|
|
350
369
|
}
|
|
351
370
|
const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
|
|
352
|
-
if (req.timeout > 0 && abortController) {
|
|
371
|
+
if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
|
|
353
372
|
setTimeout(() => {
|
|
354
|
-
if (!req.abort
|
|
373
|
+
if (!req.abort?.aborted) {
|
|
355
374
|
abortController.abort("timeout");
|
|
356
375
|
}
|
|
357
376
|
}, req.timeout);
|
|
@@ -406,18 +425,22 @@ function makeResponse(options) {
|
|
|
406
425
|
// src/handler/fetch/fetch.ts
|
|
407
426
|
function createRequest(request2) {
|
|
408
427
|
const url = new URL(request2.endpoint, request2.host);
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
const detectedType = detectContentTypeHeader(request2.body);
|
|
412
|
-
if (detectedType) {
|
|
413
|
-
request2.headers.set("Content-Type", detectedType);
|
|
414
|
-
}
|
|
428
|
+
if (request2.queryParams) {
|
|
429
|
+
url.search = request2.queryParams.toString();
|
|
415
430
|
}
|
|
416
|
-
if (
|
|
417
|
-
request2.headers.
|
|
431
|
+
if (request2.headers) {
|
|
432
|
+
if (!request2.headers.has("Content-Type")) {
|
|
433
|
+
const detectedType = detectContentTypeHeader(request2.body);
|
|
434
|
+
if (detectedType) {
|
|
435
|
+
request2.headers.set("Content-Type", detectedType);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (!request2.headers.has("Accept")) {
|
|
439
|
+
request2.headers.set("Accept", "application/json, text/plain, */*");
|
|
440
|
+
}
|
|
418
441
|
}
|
|
419
442
|
const credentials = request2.withCredentials ? "include" : undefined;
|
|
420
|
-
const fetchConfig = request2.context
|
|
443
|
+
const fetchConfig = request2.context?.get(FETCH_CONFIG_KEY) || {};
|
|
421
444
|
return new Request(url, {
|
|
422
445
|
...fetchConfig,
|
|
423
446
|
headers: request2.headers,
|
package/package.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
2
|
+
"name": "@defjs/core",
|
|
3
|
+
"version": "0.0.0-test-0007",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "bun run ./scripts/build.ts",
|
|
7
|
+
"test": "bun test",
|
|
8
|
+
"lint": "biome check",
|
|
9
|
+
"lint:fix": "biome check --write",
|
|
10
|
+
"pub": "npm publish"
|
|
11
|
+
},
|
|
12
|
+
"peerDependencies": {
|
|
13
|
+
"typescript": "^5.0"
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist"],
|
|
16
|
+
"module": "dist/index.js",
|
|
17
|
+
"typings": "dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
"./package.json": "./package.json",
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"default": "./index.js"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
25
|
}
|