@jayfong/x-request 2.86.3 → 2.88.0
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/lib/_cjs/index.js +31 -7
- package/lib/index.d.ts +8 -2
- package/lib/index.js +31 -7
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -255,20 +255,39 @@ class XRequest {
|
|
|
255
255
|
static extend(options) {
|
|
256
256
|
return new XRequest(options);
|
|
257
257
|
}
|
|
258
|
-
constructor(options) {
|
|
258
|
+
constructor(options = {}) {
|
|
259
259
|
this.options = options;
|
|
260
|
+
this.extraHeaders = {};
|
|
261
|
+
}
|
|
262
|
+
setHeaders(headers) {
|
|
263
|
+
Object.assign(this.extraHeaders, headers);
|
|
264
|
+
}
|
|
265
|
+
setAuthorizationHeader(payload) {
|
|
266
|
+
this.setHeaders({
|
|
267
|
+
Authorization: payload.bearer ? `Bearer ${payload.bearer}` : payload.plain
|
|
268
|
+
});
|
|
260
269
|
}
|
|
261
270
|
get(options, responseType) {
|
|
262
|
-
|
|
271
|
+
const finalOptions = {
|
|
263
272
|
...this.options,
|
|
264
273
|
...options
|
|
265
|
-
}
|
|
274
|
+
};
|
|
275
|
+
finalOptions.headers = {
|
|
276
|
+
...finalOptions.headers,
|
|
277
|
+
...this.extraHeaders
|
|
278
|
+
};
|
|
279
|
+
return XRequest.get(finalOptions, responseType);
|
|
266
280
|
}
|
|
267
281
|
post(options, responseType) {
|
|
268
|
-
|
|
282
|
+
const finalOptions = {
|
|
269
283
|
...this.options,
|
|
270
284
|
...options
|
|
271
|
-
}
|
|
285
|
+
};
|
|
286
|
+
finalOptions.headers = {
|
|
287
|
+
...finalOptions.headers,
|
|
288
|
+
...this.extraHeaders
|
|
289
|
+
};
|
|
290
|
+
return XRequest.post(finalOptions, responseType);
|
|
272
291
|
}
|
|
273
292
|
async getText(options) {
|
|
274
293
|
return this.get(options, 'text');
|
|
@@ -304,10 +323,15 @@ class XRequest {
|
|
|
304
323
|
return XRequest.formFile(file, options);
|
|
305
324
|
}
|
|
306
325
|
extend(options) {
|
|
307
|
-
|
|
326
|
+
const finalOptions = {
|
|
308
327
|
...this.options,
|
|
309
328
|
...options
|
|
310
|
-
}
|
|
329
|
+
};
|
|
330
|
+
finalOptions.headers = {
|
|
331
|
+
...finalOptions.headers,
|
|
332
|
+
...this.extraHeaders
|
|
333
|
+
};
|
|
334
|
+
return new XRequest(finalOptions);
|
|
311
335
|
}
|
|
312
336
|
}
|
|
313
337
|
exports.xr = exports.XRequest = XRequest;
|
package/lib/index.d.ts
CHANGED
|
@@ -144,7 +144,7 @@ export declare class XRequestFormFile {
|
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
export declare class XRequest {
|
|
147
|
-
|
|
147
|
+
options: OmitStrict<XRequestOptions, 'url'>;
|
|
148
148
|
private static getGotOptions;
|
|
149
149
|
private static afterResponse;
|
|
150
150
|
static get(options: XRequestGetOptions): Promise<XRequestResponse<Buffer>>;
|
|
@@ -180,7 +180,13 @@ export declare class XRequest {
|
|
|
180
180
|
type?: string;
|
|
181
181
|
}): XRequestFormFile;
|
|
182
182
|
static extend(options: OmitStrict<XRequestOptions, 'url'>): XRequest;
|
|
183
|
-
constructor(options?: OmitStrict<XRequestOptions,
|
|
183
|
+
constructor(options?: OmitStrict<XRequestOptions, 'url'>);
|
|
184
|
+
private extraHeaders;
|
|
185
|
+
setHeaders(headers: Record<string, string>): void;
|
|
186
|
+
setAuthorizationHeader(payload: {
|
|
187
|
+
bearer?: string;
|
|
188
|
+
plain?: string;
|
|
189
|
+
}): void;
|
|
184
190
|
get(options: XRequestGetOptions): Promise<XRequestResponse<Buffer>>;
|
|
185
191
|
get(options: XRequestGetOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
186
192
|
get(options: XRequestGetOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
package/lib/index.js
CHANGED
|
@@ -246,20 +246,39 @@ export class XRequest {
|
|
|
246
246
|
static extend(options) {
|
|
247
247
|
return new XRequest(options);
|
|
248
248
|
}
|
|
249
|
-
constructor(options) {
|
|
249
|
+
constructor(options = {}) {
|
|
250
250
|
this.options = options;
|
|
251
|
+
this.extraHeaders = {};
|
|
252
|
+
}
|
|
253
|
+
setHeaders(headers) {
|
|
254
|
+
Object.assign(this.extraHeaders, headers);
|
|
255
|
+
}
|
|
256
|
+
setAuthorizationHeader(payload) {
|
|
257
|
+
this.setHeaders({
|
|
258
|
+
Authorization: payload.bearer ? `Bearer ${payload.bearer}` : payload.plain
|
|
259
|
+
});
|
|
251
260
|
}
|
|
252
261
|
get(options, responseType) {
|
|
253
|
-
|
|
262
|
+
const finalOptions = {
|
|
254
263
|
...this.options,
|
|
255
264
|
...options
|
|
256
|
-
}
|
|
265
|
+
};
|
|
266
|
+
finalOptions.headers = {
|
|
267
|
+
...finalOptions.headers,
|
|
268
|
+
...this.extraHeaders
|
|
269
|
+
};
|
|
270
|
+
return XRequest.get(finalOptions, responseType);
|
|
257
271
|
}
|
|
258
272
|
post(options, responseType) {
|
|
259
|
-
|
|
273
|
+
const finalOptions = {
|
|
260
274
|
...this.options,
|
|
261
275
|
...options
|
|
262
|
-
}
|
|
276
|
+
};
|
|
277
|
+
finalOptions.headers = {
|
|
278
|
+
...finalOptions.headers,
|
|
279
|
+
...this.extraHeaders
|
|
280
|
+
};
|
|
281
|
+
return XRequest.post(finalOptions, responseType);
|
|
263
282
|
}
|
|
264
283
|
async getText(options) {
|
|
265
284
|
return this.get(options, 'text');
|
|
@@ -295,10 +314,15 @@ export class XRequest {
|
|
|
295
314
|
return XRequest.formFile(file, options);
|
|
296
315
|
}
|
|
297
316
|
extend(options) {
|
|
298
|
-
|
|
317
|
+
const finalOptions = {
|
|
299
318
|
...this.options,
|
|
300
319
|
...options
|
|
301
|
-
}
|
|
320
|
+
};
|
|
321
|
+
finalOptions.headers = {
|
|
322
|
+
...finalOptions.headers,
|
|
323
|
+
...this.extraHeaders
|
|
324
|
+
};
|
|
325
|
+
return new XRequest(finalOptions);
|
|
302
326
|
}
|
|
303
327
|
}
|
|
304
328
|
export { XRequest as xr };
|