@defjs/core 0.0.0-test-0004 → 0.0.0-test-0006
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 -9
- package/dist/index.js +35 -32
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -84,19 +84,19 @@ export interface HttpRequest {
|
|
|
84
84
|
method: string;
|
|
85
85
|
endpoint: string;
|
|
86
86
|
body?: Blob | ArrayBuffer | FormData | object | string | number | boolean | null;
|
|
87
|
-
headers
|
|
88
|
-
queryParams
|
|
89
|
-
/**
|
|
90
|
-
responseType
|
|
91
|
-
context
|
|
92
|
-
timeout
|
|
87
|
+
headers?: Headers;
|
|
88
|
+
queryParams?: URLSearchParams;
|
|
89
|
+
/** default:json */
|
|
90
|
+
responseType?: HttpResponseType;
|
|
91
|
+
context?: HttpContext;
|
|
92
|
+
timeout?: number;
|
|
93
93
|
withCredentials?: boolean;
|
|
94
94
|
/** when use fetch handler, no upload progress */
|
|
95
95
|
uploadProgress?: HttpProgressFn;
|
|
96
96
|
downloadProgress?: HttpProgressFn;
|
|
97
|
-
/**
|
|
98
|
-
observe
|
|
99
|
-
abort
|
|
97
|
+
/** default:body */
|
|
98
|
+
observe?: "body" | "stream" | "response";
|
|
99
|
+
abort?: AbortSignal;
|
|
100
100
|
}
|
|
101
101
|
export type RequestInputValue<T> = T extends Field<infer V> ? V : T extends {
|
|
102
102
|
[K in keyof T]: Field<any>;
|
|
@@ -111,6 +111,7 @@ export type RequestOptions<Output = unknown, Input extends Field<any> | Record<P
|
|
|
111
111
|
responseType?: HttpResponseType;
|
|
112
112
|
context?: HttpContext;
|
|
113
113
|
observe?: "body" | "response";
|
|
114
|
+
withCredentials?: boolean;
|
|
114
115
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
115
116
|
transformRequest?: TransformRequestFn;
|
|
116
117
|
transformResponse?: TransformResponseFn<Output>;
|
|
@@ -129,6 +130,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
|
|
|
129
130
|
responseType?: "json";
|
|
130
131
|
observe?: "body";
|
|
131
132
|
context?: HttpContext;
|
|
133
|
+
withCredentials?: boolean;
|
|
132
134
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
133
135
|
transformRequest?: TransformRequestFn;
|
|
134
136
|
transformResponse?: TransformResponseFn<Output>;
|
|
@@ -141,6 +143,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
141
143
|
responseType: "text";
|
|
142
144
|
observe?: "body";
|
|
143
145
|
context?: HttpContext;
|
|
146
|
+
withCredentials?: boolean;
|
|
144
147
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
145
148
|
transformRequest?: TransformRequestFn;
|
|
146
149
|
transformResponse?: TransformResponseFn;
|
|
@@ -153,6 +156,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
153
156
|
responseType: "blob";
|
|
154
157
|
observe?: "body";
|
|
155
158
|
context?: HttpContext;
|
|
159
|
+
withCredentials?: boolean;
|
|
156
160
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
157
161
|
transformRequest?: TransformRequestFn;
|
|
158
162
|
transformResponse?: TransformResponseFn;
|
|
@@ -165,6 +169,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
165
169
|
responseType: "arraybuffer";
|
|
166
170
|
observe?: "body";
|
|
167
171
|
context?: HttpContext;
|
|
172
|
+
withCredentials?: boolean;
|
|
168
173
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
169
174
|
transformRequest?: TransformRequestFn;
|
|
170
175
|
transformResponse?: TransformResponseFn;
|
|
@@ -178,6 +183,7 @@ export declare function defineRequest<Output = unknown, Input extends Field<any>
|
|
|
178
183
|
responseType?: "json";
|
|
179
184
|
observe: "response";
|
|
180
185
|
context?: HttpContext;
|
|
186
|
+
withCredentials?: boolean;
|
|
181
187
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
182
188
|
transformRequest?: TransformRequestFn;
|
|
183
189
|
transformResponse?: TransformResponseFn<Output>;
|
|
@@ -190,6 +196,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
190
196
|
responseType?: "text";
|
|
191
197
|
observe: "response";
|
|
192
198
|
context?: HttpContext;
|
|
199
|
+
withCredentials?: boolean;
|
|
193
200
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
194
201
|
transformRequest?: TransformRequestFn;
|
|
195
202
|
transformResponse?: TransformResponseFn;
|
|
@@ -202,6 +209,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
202
209
|
responseType: "blob";
|
|
203
210
|
observe: "response";
|
|
204
211
|
context?: HttpContext;
|
|
212
|
+
withCredentials?: boolean;
|
|
205
213
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
206
214
|
transformRequest?: TransformRequestFn;
|
|
207
215
|
transformResponse?: TransformResponseFn;
|
|
@@ -214,6 +222,7 @@ export declare function defineRequest<Input extends Field<any> | Record<Property
|
|
|
214
222
|
responseType?: "arraybuffer";
|
|
215
223
|
observe: "response";
|
|
216
224
|
context?: HttpContext;
|
|
225
|
+
withCredentials?: boolean;
|
|
217
226
|
validators?: (ValidatorFn<RequestInputValue<Input>> | AsyncValidatorFn<RequestInputValue<Input>>)[];
|
|
218
227
|
transformRequest?: TransformRequestFn;
|
|
219
228
|
transformResponse?: TransformResponseFn;
|
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) {
|
|
@@ -178,7 +177,7 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
178
177
|
return typeof input2 === "object" && input2 !== null;
|
|
179
178
|
}
|
|
180
179
|
if (isField(fieldOrFieldGroup)) {
|
|
181
|
-
const
|
|
180
|
+
const params = new Map;
|
|
182
181
|
const queryParams = new URLSearchParams;
|
|
183
182
|
const headers = new Headers;
|
|
184
183
|
let body = undefined;
|
|
@@ -204,7 +203,7 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
204
203
|
if (!aliasName) {
|
|
205
204
|
throwAliaNotSet();
|
|
206
205
|
}
|
|
207
|
-
|
|
206
|
+
params.set(aliasName, String(fieldValue));
|
|
208
207
|
break;
|
|
209
208
|
}
|
|
210
209
|
case 4 /* Header */: {
|
|
@@ -228,25 +227,24 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
228
227
|
break;
|
|
229
228
|
}
|
|
230
229
|
}
|
|
231
|
-
request.endpoint = fillUrl(request.endpoint,
|
|
230
|
+
request.endpoint = fillUrl(request.endpoint, params);
|
|
232
231
|
request.queryParams = queryParams;
|
|
233
232
|
request.headers = headers;
|
|
234
233
|
request.body = body;
|
|
235
234
|
return;
|
|
236
235
|
}
|
|
237
236
|
if (isFieldGroup(fieldOrFieldGroup) && inputIsObject(input)) {
|
|
238
|
-
const
|
|
237
|
+
const params = new Map;
|
|
239
238
|
const queryParams = new URLSearchParams;
|
|
240
239
|
const headers = new Headers;
|
|
241
240
|
const formData = new FormData;
|
|
242
241
|
const json = {};
|
|
243
242
|
let body = undefined;
|
|
244
243
|
for (const [propertyKey, field3] of Object.entries(fieldOrFieldGroup)) {
|
|
245
|
-
const defaultValue = field3();
|
|
246
244
|
const meta = getFieldMetadata(field3);
|
|
247
245
|
for (const [type, aliasName] of meta.alias) {
|
|
248
246
|
const valueKey = aliasName || propertyKey;
|
|
249
|
-
const value = input[
|
|
247
|
+
const value = input[propertyKey] || field3();
|
|
250
248
|
const err = await doValid([...meta.validators, ...meta.asyncValidator], value);
|
|
251
249
|
if (err) {
|
|
252
250
|
throw err;
|
|
@@ -262,7 +260,7 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
262
260
|
queryParams.append(valueKey, value);
|
|
263
261
|
break;
|
|
264
262
|
case 3 /* Param */:
|
|
265
|
-
|
|
263
|
+
params.set(valueKey, value);
|
|
266
264
|
break;
|
|
267
265
|
case 4 /* Header */:
|
|
268
266
|
headers.set(valueKey, value);
|
|
@@ -275,16 +273,16 @@ async function fillRequestFromField(request, fieldOrFieldGroup, input) {
|
|
|
275
273
|
break;
|
|
276
274
|
}
|
|
277
275
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
276
|
+
}
|
|
277
|
+
request.endpoint = fillUrl(request.endpoint, params);
|
|
278
|
+
request.queryParams = queryParams;
|
|
279
|
+
request.headers = headers;
|
|
280
|
+
if (body) {
|
|
281
|
+
request.body = body;
|
|
282
|
+
} else if (Array.from(formData.keys()).length > 0) {
|
|
283
|
+
request.body = formData;
|
|
284
|
+
} else if (Object.keys(json).length) {
|
|
285
|
+
request.body = JSON.stringify(json);
|
|
288
286
|
}
|
|
289
287
|
return;
|
|
290
288
|
}
|
|
@@ -332,6 +330,7 @@ async function doRequest(...args) {
|
|
|
332
330
|
queryParams: new URLSearchParams,
|
|
333
331
|
headers: new Headers,
|
|
334
332
|
body: undefined,
|
|
333
|
+
withCredentials: requestOptions.withCredentials,
|
|
335
334
|
responseType: requestOptions.responseType ?? "json",
|
|
336
335
|
context: requestOptions.context || makeHttpContext(),
|
|
337
336
|
observe: requestOptions.observe ?? "body",
|
|
@@ -349,9 +348,9 @@ async function doRequest(...args) {
|
|
|
349
348
|
req.body = requestOptions.transformRequest(req);
|
|
350
349
|
}
|
|
351
350
|
const chain = makeInterceptorChain([...clientOptions?.interceptors || [], ...requestOptions?.interceptors || []]);
|
|
352
|
-
if (req.timeout > 0 && abortController) {
|
|
351
|
+
if (typeof req.timeout === "number" && req.timeout > 0 && abortController) {
|
|
353
352
|
setTimeout(() => {
|
|
354
|
-
if (!req.abort
|
|
353
|
+
if (!req.abort?.aborted) {
|
|
355
354
|
abortController.abort("timeout");
|
|
356
355
|
}
|
|
357
356
|
}, req.timeout);
|
|
@@ -406,18 +405,22 @@ function makeResponse(options) {
|
|
|
406
405
|
// src/handler/fetch/fetch.ts
|
|
407
406
|
function createRequest(request2) {
|
|
408
407
|
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
|
-
}
|
|
408
|
+
if (request2.queryParams) {
|
|
409
|
+
url.search = request2.queryParams.toString();
|
|
415
410
|
}
|
|
416
|
-
if (
|
|
417
|
-
request2.headers.
|
|
411
|
+
if (request2.headers) {
|
|
412
|
+
if (!request2.headers.has("Content-Type")) {
|
|
413
|
+
const detectedType = detectContentTypeHeader(request2.body);
|
|
414
|
+
if (detectedType) {
|
|
415
|
+
request2.headers.set("Content-Type", detectedType);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (!request2.headers.has("Accept")) {
|
|
419
|
+
request2.headers.set("Accept", "application/json, text/plain, */*");
|
|
420
|
+
}
|
|
418
421
|
}
|
|
419
422
|
const credentials = request2.withCredentials ? "include" : undefined;
|
|
420
|
-
const fetchConfig = request2.context
|
|
423
|
+
const fetchConfig = request2.context?.get(FETCH_CONFIG_KEY) || {};
|
|
421
424
|
return new Request(url, {
|
|
422
425
|
...fetchConfig,
|
|
423
426
|
headers: request2.headers,
|