@jayfong/x-request 2.83.1 → 2.85.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 +23 -10
- package/lib/index.d.ts +6 -0
- package/lib/index.js +23 -10
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -137,6 +137,9 @@ class XRequest {
|
|
|
137
137
|
if (referer && !referer.startsWith('http:') && !referer.startsWith('https:')) {
|
|
138
138
|
gotOptions.headers.referer = new URL(String(gotOptions.headers.referer), options.baseUrl || options.url).toString();
|
|
139
139
|
}
|
|
140
|
+
if (options.gotOptions) {
|
|
141
|
+
Object.assign(gotOptions, options.gotOptions);
|
|
142
|
+
}
|
|
140
143
|
return gotOptions;
|
|
141
144
|
}
|
|
142
145
|
static afterResponse(options, responseType, res) {
|
|
@@ -170,11 +173,16 @@ class XRequest {
|
|
|
170
173
|
if (responseType === 'stream') {
|
|
171
174
|
return res;
|
|
172
175
|
}
|
|
173
|
-
return res.then(res =>
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
return res.then(res => {
|
|
177
|
+
var _res$redirectUrls;
|
|
178
|
+
return this.afterResponse(options, responseType, {
|
|
179
|
+
status: res.statusCode,
|
|
180
|
+
headers: res.headers,
|
|
181
|
+
data: res.body,
|
|
182
|
+
initialUrl: res.requestUrl,
|
|
183
|
+
finalUrl: (_res$redirectUrls = res.redirectUrls) != null && _res$redirectUrls.length ? res.redirectUrls[res.redirectUrls.length - 1] : res.requestUrl
|
|
184
|
+
});
|
|
185
|
+
});
|
|
178
186
|
}
|
|
179
187
|
static post(options, responseType) {
|
|
180
188
|
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
@@ -200,11 +208,16 @@ class XRequest {
|
|
|
200
208
|
if (responseType === 'stream') {
|
|
201
209
|
return res;
|
|
202
210
|
}
|
|
203
|
-
return res.then(res =>
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
211
|
+
return res.then(res => {
|
|
212
|
+
var _res$redirectUrls2;
|
|
213
|
+
return this.afterResponse(options, responseType, {
|
|
214
|
+
status: res.statusCode,
|
|
215
|
+
headers: res.headers,
|
|
216
|
+
data: res.body,
|
|
217
|
+
initialUrl: res.requestUrl,
|
|
218
|
+
finalUrl: (_res$redirectUrls2 = res.redirectUrls) != null && _res$redirectUrls2.length ? res.redirectUrls[res.redirectUrls.length - 1] : res.requestUrl
|
|
219
|
+
});
|
|
220
|
+
});
|
|
208
221
|
}
|
|
209
222
|
static async getText(options) {
|
|
210
223
|
return this.get(options, 'text');
|
package/lib/index.d.ts
CHANGED
|
@@ -93,11 +93,17 @@ export interface XRequestOptions {
|
|
|
93
93
|
* @default false
|
|
94
94
|
*/
|
|
95
95
|
dnsCache?: Options['dnsCache'];
|
|
96
|
+
/**
|
|
97
|
+
* 透传去 Got 的选项
|
|
98
|
+
*/
|
|
99
|
+
gotOptions?: Options;
|
|
96
100
|
}
|
|
97
101
|
export interface XRequestResponse<T> {
|
|
98
102
|
status: number;
|
|
99
103
|
headers: Record<string, any>;
|
|
100
104
|
data: T;
|
|
105
|
+
initialUrl: string;
|
|
106
|
+
finalUrl: string;
|
|
101
107
|
}
|
|
102
108
|
export interface XRequestGetOptions extends XRequestOptions {
|
|
103
109
|
method?: string;
|
package/lib/index.js
CHANGED
|
@@ -128,6 +128,9 @@ export class XRequest {
|
|
|
128
128
|
if (referer && !referer.startsWith('http:') && !referer.startsWith('https:')) {
|
|
129
129
|
gotOptions.headers.referer = new URL(String(gotOptions.headers.referer), options.baseUrl || options.url).toString();
|
|
130
130
|
}
|
|
131
|
+
if (options.gotOptions) {
|
|
132
|
+
Object.assign(gotOptions, options.gotOptions);
|
|
133
|
+
}
|
|
131
134
|
return gotOptions;
|
|
132
135
|
}
|
|
133
136
|
static afterResponse(options, responseType, res) {
|
|
@@ -161,11 +164,16 @@ export class XRequest {
|
|
|
161
164
|
if (responseType === 'stream') {
|
|
162
165
|
return res;
|
|
163
166
|
}
|
|
164
|
-
return res.then(res =>
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
167
|
+
return res.then(res => {
|
|
168
|
+
var _res$redirectUrls;
|
|
169
|
+
return this.afterResponse(options, responseType, {
|
|
170
|
+
status: res.statusCode,
|
|
171
|
+
headers: res.headers,
|
|
172
|
+
data: res.body,
|
|
173
|
+
initialUrl: res.requestUrl,
|
|
174
|
+
finalUrl: (_res$redirectUrls = res.redirectUrls) != null && _res$redirectUrls.length ? res.redirectUrls[res.redirectUrls.length - 1] : res.requestUrl
|
|
175
|
+
});
|
|
176
|
+
});
|
|
169
177
|
}
|
|
170
178
|
static post(options, responseType) {
|
|
171
179
|
const gotOptions = this.getGotOptions(options, responseType || 'buffer');
|
|
@@ -191,11 +199,16 @@ export class XRequest {
|
|
|
191
199
|
if (responseType === 'stream') {
|
|
192
200
|
return res;
|
|
193
201
|
}
|
|
194
|
-
return res.then(res =>
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
202
|
+
return res.then(res => {
|
|
203
|
+
var _res$redirectUrls2;
|
|
204
|
+
return this.afterResponse(options, responseType, {
|
|
205
|
+
status: res.statusCode,
|
|
206
|
+
headers: res.headers,
|
|
207
|
+
data: res.body,
|
|
208
|
+
initialUrl: res.requestUrl,
|
|
209
|
+
finalUrl: (_res$redirectUrls2 = res.redirectUrls) != null && _res$redirectUrls2.length ? res.redirectUrls[res.redirectUrls.length - 1] : res.requestUrl
|
|
210
|
+
});
|
|
211
|
+
});
|
|
199
212
|
}
|
|
200
213
|
static async getText(options) {
|
|
201
214
|
return this.get(options, 'text');
|