@jayfong/x-request 2.12.18 → 2.13.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 +46 -22
- package/lib/index.d.ts +54 -24
- package/lib/index.js +46 -22
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -116,7 +116,7 @@ class XRequest {
|
|
|
116
116
|
}
|
|
117
117
|
return gotOptions;
|
|
118
118
|
}
|
|
119
|
-
static
|
|
119
|
+
static get(options, responseType) {
|
|
120
120
|
let url = options.url;
|
|
121
121
|
if (options.data) {
|
|
122
122
|
const _url = new URL(url);
|
|
@@ -139,7 +139,7 @@ class XRequest {
|
|
|
139
139
|
data: res.body
|
|
140
140
|
}));
|
|
141
141
|
}
|
|
142
|
-
static
|
|
142
|
+
static post(options, responseType) {
|
|
143
143
|
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
144
144
|
if (responseType === 'stream' && options.data instanceof _formstream.default) {
|
|
145
145
|
gotOptions.headers = options.data.headers(gotOptions.headers);
|
|
@@ -170,32 +170,44 @@ class XRequest {
|
|
|
170
170
|
}));
|
|
171
171
|
}
|
|
172
172
|
static async getText(options) {
|
|
173
|
-
return this.
|
|
173
|
+
return this.get(options, 'text');
|
|
174
174
|
}
|
|
175
175
|
static async postText(options) {
|
|
176
|
-
return this.
|
|
176
|
+
return this.post(options, 'text');
|
|
177
177
|
}
|
|
178
|
-
static async
|
|
179
|
-
return this.
|
|
178
|
+
static async getJson(options) {
|
|
179
|
+
return this.get(options, 'json');
|
|
180
180
|
}
|
|
181
|
-
static async
|
|
182
|
-
return this.
|
|
181
|
+
static async postJson(options) {
|
|
182
|
+
return this.post(options, 'json');
|
|
183
183
|
}
|
|
184
184
|
static getStream(options) {
|
|
185
|
-
return this.
|
|
185
|
+
return this.get(options, 'stream');
|
|
186
186
|
}
|
|
187
187
|
static postStream(options) {
|
|
188
|
-
return this.
|
|
188
|
+
return this.post(options, 'stream');
|
|
189
189
|
}
|
|
190
190
|
static isTimeoutError(err) {
|
|
191
191
|
return err instanceof _got.default.TimeoutError;
|
|
192
192
|
}
|
|
193
|
+
static formData(payload) {
|
|
194
|
+
return new XRequestFormData(payload);
|
|
195
|
+
}
|
|
196
|
+
static formStream(payload) {
|
|
197
|
+
return new XRequestFormStream(payload);
|
|
198
|
+
}
|
|
199
|
+
static formUrlencoded(payload) {
|
|
200
|
+
return new XRequestFormUrlencoded(payload);
|
|
201
|
+
}
|
|
202
|
+
static formFile(file, options) {
|
|
203
|
+
return new XRequestFormFile(file, options);
|
|
204
|
+
}
|
|
193
205
|
constructor(options) {
|
|
194
206
|
this.options = options;
|
|
195
207
|
}
|
|
196
|
-
|
|
208
|
+
get(options, responseType) {
|
|
197
209
|
var _this$options;
|
|
198
|
-
return XRequest.
|
|
210
|
+
return XRequest.get({
|
|
199
211
|
...this.options,
|
|
200
212
|
...options,
|
|
201
213
|
headers: {
|
|
@@ -204,9 +216,9 @@ class XRequest {
|
|
|
204
216
|
}
|
|
205
217
|
}, responseType);
|
|
206
218
|
}
|
|
207
|
-
|
|
219
|
+
post(options, responseType) {
|
|
208
220
|
var _this$options2;
|
|
209
|
-
return XRequest.
|
|
221
|
+
return XRequest.post({
|
|
210
222
|
...this.options,
|
|
211
223
|
...options,
|
|
212
224
|
headers: {
|
|
@@ -216,25 +228,37 @@ class XRequest {
|
|
|
216
228
|
}, responseType);
|
|
217
229
|
}
|
|
218
230
|
async getText(options) {
|
|
219
|
-
return this.
|
|
231
|
+
return this.get(options, 'text');
|
|
220
232
|
}
|
|
221
233
|
async postText(options) {
|
|
222
|
-
return this.
|
|
234
|
+
return this.post(options, 'text');
|
|
223
235
|
}
|
|
224
|
-
async
|
|
225
|
-
return this.
|
|
236
|
+
async getJson(options) {
|
|
237
|
+
return this.get(options, 'json');
|
|
226
238
|
}
|
|
227
|
-
async
|
|
228
|
-
return this.
|
|
239
|
+
async postJson(options) {
|
|
240
|
+
return this.post(options, 'json');
|
|
229
241
|
}
|
|
230
242
|
getStream(options) {
|
|
231
|
-
return this.
|
|
243
|
+
return this.get(options, 'stream');
|
|
232
244
|
}
|
|
233
245
|
postStream(options) {
|
|
234
|
-
return this.
|
|
246
|
+
return this.post(options, 'stream');
|
|
235
247
|
}
|
|
236
248
|
isTimeoutError(err) {
|
|
237
249
|
return XRequest.isTimeoutError(err);
|
|
238
250
|
}
|
|
251
|
+
formData(payload) {
|
|
252
|
+
return XRequest.formData(payload);
|
|
253
|
+
}
|
|
254
|
+
formStream(payload) {
|
|
255
|
+
return XRequest.formStream(payload);
|
|
256
|
+
}
|
|
257
|
+
formUrlencoded(payload) {
|
|
258
|
+
return XRequest.formUrlencoded(payload);
|
|
259
|
+
}
|
|
260
|
+
formFile(file, options) {
|
|
261
|
+
return XRequest.formFile(file, options);
|
|
262
|
+
}
|
|
239
263
|
}
|
|
240
264
|
exports.XRequest = XRequest;
|
package/lib/index.d.ts
CHANGED
|
@@ -93,39 +93,69 @@ export declare class XRequestFormFile {
|
|
|
93
93
|
export declare class XRequest {
|
|
94
94
|
private options?;
|
|
95
95
|
private static getGotOptions;
|
|
96
|
-
static
|
|
97
|
-
static
|
|
98
|
-
static
|
|
99
|
-
static
|
|
100
|
-
static
|
|
101
|
-
static
|
|
102
|
-
static
|
|
103
|
-
static
|
|
104
|
-
static
|
|
105
|
-
static
|
|
96
|
+
static get(options: XRequestGetStreamOptions): Promise<XRequestResponse<Buffer>>;
|
|
97
|
+
static get(options: XRequestGetStreamOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
98
|
+
static get(options: XRequestGetStreamOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
|
99
|
+
static get<T>(options: XRequestGetStreamOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
|
|
100
|
+
static get(options: XRequestGetStreamOptions, responseType: 'stream'): Request;
|
|
101
|
+
static post(options: XRequestPostOptions): Promise<XRequestResponse<Buffer>>;
|
|
102
|
+
static post(options: XRequestPostOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
103
|
+
static post(options: XRequestPostOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
|
104
|
+
static post<T>(options: XRequestPostOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
|
|
105
|
+
static post(options: XRequestPostOptions, responseType: 'stream'): Request;
|
|
106
106
|
static getText(options: XRequestGetStreamOptions): Promise<XRequestResponse<string>>;
|
|
107
107
|
static postText(options: XRequestPostOptions): Promise<XRequestResponse<string>>;
|
|
108
|
-
static
|
|
109
|
-
static
|
|
108
|
+
static getJson<T>(options: XRequestGetStreamOptions): Promise<XRequestResponse<T>>;
|
|
109
|
+
static postJson<T>(options: XRequestPostOptions): Promise<XRequestResponse<T>>;
|
|
110
110
|
static getStream(options: XRequestGetOptions): Request;
|
|
111
111
|
static postStream(options: XRequestPostStreamOptions): Request;
|
|
112
112
|
static isTimeoutError(err: any): boolean;
|
|
113
|
+
static formData(payload: ConstructorParameters<typeof XRequestFormData>[0]): XRequestFormData;
|
|
114
|
+
static formStream(payload: ConstructorParameters<typeof XRequestFormStream>[0]): XRequestFormStream;
|
|
115
|
+
static formUrlencoded(payload: ConstructorParameters<typeof XRequestFormUrlencoded>[0]): XRequestFormUrlencoded;
|
|
116
|
+
static formFile(file: Buffer, options: {
|
|
117
|
+
name: string;
|
|
118
|
+
type?: string;
|
|
119
|
+
}): XRequestFormFile;
|
|
120
|
+
static formFile(file: Readable, options: {
|
|
121
|
+
name: string;
|
|
122
|
+
type?: string;
|
|
123
|
+
}): XRequestFormFile;
|
|
124
|
+
static formFile(file: string, options?: {
|
|
125
|
+
name?: string;
|
|
126
|
+
type?: string;
|
|
127
|
+
}): XRequestFormFile;
|
|
113
128
|
constructor(options?: OmitStrict<XRequestOptions, 'url'>);
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
129
|
+
get(options: XRequestGetStreamOptions): Promise<XRequestResponse<Buffer>>;
|
|
130
|
+
get(options: XRequestGetStreamOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
131
|
+
get(options: XRequestGetStreamOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
|
132
|
+
get<T>(options: XRequestGetStreamOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
|
|
133
|
+
get(options: XRequestGetStreamOptions, responseType: 'stream'): Request;
|
|
134
|
+
post(options: XRequestPostOptions): Promise<XRequestResponse<Buffer>>;
|
|
135
|
+
post(options: XRequestPostOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
136
|
+
post(options: XRequestPostOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
|
137
|
+
post<T>(options: XRequestPostOptions, responseType: 'json'): Promise<XRequestResponse<T>>;
|
|
138
|
+
post(options: XRequestPostOptions, responseType: 'stream'): Request;
|
|
124
139
|
getText(options: XRequestGetStreamOptions): Promise<XRequestResponse<string>>;
|
|
125
140
|
postText(options: XRequestPostOptions): Promise<XRequestResponse<string>>;
|
|
126
|
-
|
|
127
|
-
|
|
141
|
+
getJson<T>(options: XRequestGetStreamOptions): Promise<XRequestResponse<T>>;
|
|
142
|
+
postJson<T>(options: XRequestPostOptions): Promise<XRequestResponse<T>>;
|
|
128
143
|
getStream(options: XRequestGetOptions): Request;
|
|
129
144
|
postStream(options: XRequestPostStreamOptions): Request;
|
|
130
145
|
isTimeoutError(err: any): boolean;
|
|
146
|
+
formData(payload: ConstructorParameters<typeof XRequestFormData>[0]): XRequestFormData;
|
|
147
|
+
formStream(payload: ConstructorParameters<typeof XRequestFormStream>[0]): XRequestFormStream;
|
|
148
|
+
formUrlencoded(payload: ConstructorParameters<typeof XRequestFormUrlencoded>[0]): XRequestFormUrlencoded;
|
|
149
|
+
formFile(file: Buffer, options: {
|
|
150
|
+
name: string;
|
|
151
|
+
type?: string;
|
|
152
|
+
}): XRequestFormFile;
|
|
153
|
+
formFile(file: Readable, options: {
|
|
154
|
+
name: string;
|
|
155
|
+
type?: string;
|
|
156
|
+
}): XRequestFormFile;
|
|
157
|
+
formFile(file: string, options?: {
|
|
158
|
+
name?: string;
|
|
159
|
+
type?: string;
|
|
160
|
+
}): XRequestFormFile;
|
|
131
161
|
}
|
package/lib/index.js
CHANGED
|
@@ -107,7 +107,7 @@ export class XRequest {
|
|
|
107
107
|
}
|
|
108
108
|
return gotOptions;
|
|
109
109
|
}
|
|
110
|
-
static
|
|
110
|
+
static get(options, responseType) {
|
|
111
111
|
let url = options.url;
|
|
112
112
|
if (options.data) {
|
|
113
113
|
const _url = new URL(url);
|
|
@@ -130,7 +130,7 @@ export class XRequest {
|
|
|
130
130
|
data: res.body
|
|
131
131
|
}));
|
|
132
132
|
}
|
|
133
|
-
static
|
|
133
|
+
static post(options, responseType) {
|
|
134
134
|
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
135
135
|
if (responseType === 'stream' && options.data instanceof FormStream) {
|
|
136
136
|
gotOptions.headers = options.data.headers(gotOptions.headers);
|
|
@@ -161,32 +161,44 @@ export class XRequest {
|
|
|
161
161
|
}));
|
|
162
162
|
}
|
|
163
163
|
static async getText(options) {
|
|
164
|
-
return this.
|
|
164
|
+
return this.get(options, 'text');
|
|
165
165
|
}
|
|
166
166
|
static async postText(options) {
|
|
167
|
-
return this.
|
|
167
|
+
return this.post(options, 'text');
|
|
168
168
|
}
|
|
169
|
-
static async
|
|
170
|
-
return this.
|
|
169
|
+
static async getJson(options) {
|
|
170
|
+
return this.get(options, 'json');
|
|
171
171
|
}
|
|
172
|
-
static async
|
|
173
|
-
return this.
|
|
172
|
+
static async postJson(options) {
|
|
173
|
+
return this.post(options, 'json');
|
|
174
174
|
}
|
|
175
175
|
static getStream(options) {
|
|
176
|
-
return this.
|
|
176
|
+
return this.get(options, 'stream');
|
|
177
177
|
}
|
|
178
178
|
static postStream(options) {
|
|
179
|
-
return this.
|
|
179
|
+
return this.post(options, 'stream');
|
|
180
180
|
}
|
|
181
181
|
static isTimeoutError(err) {
|
|
182
182
|
return err instanceof got.TimeoutError;
|
|
183
183
|
}
|
|
184
|
+
static formData(payload) {
|
|
185
|
+
return new XRequestFormData(payload);
|
|
186
|
+
}
|
|
187
|
+
static formStream(payload) {
|
|
188
|
+
return new XRequestFormStream(payload);
|
|
189
|
+
}
|
|
190
|
+
static formUrlencoded(payload) {
|
|
191
|
+
return new XRequestFormUrlencoded(payload);
|
|
192
|
+
}
|
|
193
|
+
static formFile(file, options) {
|
|
194
|
+
return new XRequestFormFile(file, options);
|
|
195
|
+
}
|
|
184
196
|
constructor(options) {
|
|
185
197
|
this.options = options;
|
|
186
198
|
}
|
|
187
|
-
|
|
199
|
+
get(options, responseType) {
|
|
188
200
|
var _this$options;
|
|
189
|
-
return XRequest.
|
|
201
|
+
return XRequest.get({
|
|
190
202
|
...this.options,
|
|
191
203
|
...options,
|
|
192
204
|
headers: {
|
|
@@ -195,9 +207,9 @@ export class XRequest {
|
|
|
195
207
|
}
|
|
196
208
|
}, responseType);
|
|
197
209
|
}
|
|
198
|
-
|
|
210
|
+
post(options, responseType) {
|
|
199
211
|
var _this$options2;
|
|
200
|
-
return XRequest.
|
|
212
|
+
return XRequest.post({
|
|
201
213
|
...this.options,
|
|
202
214
|
...options,
|
|
203
215
|
headers: {
|
|
@@ -207,24 +219,36 @@ export class XRequest {
|
|
|
207
219
|
}, responseType);
|
|
208
220
|
}
|
|
209
221
|
async getText(options) {
|
|
210
|
-
return this.
|
|
222
|
+
return this.get(options, 'text');
|
|
211
223
|
}
|
|
212
224
|
async postText(options) {
|
|
213
|
-
return this.
|
|
225
|
+
return this.post(options, 'text');
|
|
214
226
|
}
|
|
215
|
-
async
|
|
216
|
-
return this.
|
|
227
|
+
async getJson(options) {
|
|
228
|
+
return this.get(options, 'json');
|
|
217
229
|
}
|
|
218
|
-
async
|
|
219
|
-
return this.
|
|
230
|
+
async postJson(options) {
|
|
231
|
+
return this.post(options, 'json');
|
|
220
232
|
}
|
|
221
233
|
getStream(options) {
|
|
222
|
-
return this.
|
|
234
|
+
return this.get(options, 'stream');
|
|
223
235
|
}
|
|
224
236
|
postStream(options) {
|
|
225
|
-
return this.
|
|
237
|
+
return this.post(options, 'stream');
|
|
226
238
|
}
|
|
227
239
|
isTimeoutError(err) {
|
|
228
240
|
return XRequest.isTimeoutError(err);
|
|
229
241
|
}
|
|
242
|
+
formData(payload) {
|
|
243
|
+
return XRequest.formData(payload);
|
|
244
|
+
}
|
|
245
|
+
formStream(payload) {
|
|
246
|
+
return XRequest.formStream(payload);
|
|
247
|
+
}
|
|
248
|
+
formUrlencoded(payload) {
|
|
249
|
+
return XRequest.formUrlencoded(payload);
|
|
250
|
+
}
|
|
251
|
+
formFile(file, options) {
|
|
252
|
+
return XRequest.formFile(file, options);
|
|
253
|
+
}
|
|
230
254
|
}
|