@jayfong/x-request 2.66.0 → 2.67.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 +16 -2
- package/lib/index.d.ts +7 -0
- package/lib/index.js +16 -2
- package/package.json +1 -1
package/lib/_cjs/index.js
CHANGED
|
@@ -127,6 +127,20 @@ class XRequest {
|
|
|
127
127
|
}
|
|
128
128
|
return gotOptions;
|
|
129
129
|
}
|
|
130
|
+
static afterResponse(options, responseType, res) {
|
|
131
|
+
if (responseType === 'text' && options.autoTransformEncoding) {
|
|
132
|
+
const contentType = String(res.headers['content-type']);
|
|
133
|
+
if (contentType.includes('charset=')) {
|
|
134
|
+
const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
|
|
135
|
+
if (charset !== 'utf-8' && charset !== 'utf8') {
|
|
136
|
+
try {
|
|
137
|
+
res.data = new TextDecoder(charset).decode(res.data);
|
|
138
|
+
} catch {}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return res;
|
|
143
|
+
}
|
|
130
144
|
static get(options, responseType) {
|
|
131
145
|
const res = (0, _got.default)({
|
|
132
146
|
method: options.method || 'GET',
|
|
@@ -135,7 +149,7 @@ class XRequest {
|
|
|
135
149
|
if (responseType === 'stream') {
|
|
136
150
|
return res;
|
|
137
151
|
}
|
|
138
|
-
return res.then(res => ({
|
|
152
|
+
return res.then(res => this.afterResponse(options, responseType, {
|
|
139
153
|
status: res.statusCode,
|
|
140
154
|
headers: res.headers,
|
|
141
155
|
data: res.body
|
|
@@ -165,7 +179,7 @@ class XRequest {
|
|
|
165
179
|
if (responseType === 'stream') {
|
|
166
180
|
return res;
|
|
167
181
|
}
|
|
168
|
-
return res.then(res => ({
|
|
182
|
+
return res.then(res => this.afterResponse(options, responseType, {
|
|
169
183
|
status: res.statusCode,
|
|
170
184
|
headers: res.headers,
|
|
171
185
|
data: res.body
|
package/lib/index.d.ts
CHANGED
|
@@ -74,6 +74,12 @@ export interface XRequestOptions {
|
|
|
74
74
|
* @default false
|
|
75
75
|
*/
|
|
76
76
|
noThrowHttpErrors?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* 是否自动转换编码
|
|
79
|
+
*
|
|
80
|
+
* @default false
|
|
81
|
+
*/
|
|
82
|
+
autoTransformEncoding?: boolean;
|
|
77
83
|
}
|
|
78
84
|
export interface XRequestResponse<T> {
|
|
79
85
|
status: number;
|
|
@@ -121,6 +127,7 @@ export declare class XRequestFormFile {
|
|
|
121
127
|
export declare class XRequest {
|
|
122
128
|
private options?;
|
|
123
129
|
private static getGotOptions;
|
|
130
|
+
private static afterResponse;
|
|
124
131
|
static get(options: XRequestGetOptions): Promise<XRequestResponse<Buffer>>;
|
|
125
132
|
static get(options: XRequestGetOptions, responseType: 'buffer'): Promise<XRequestResponse<Buffer>>;
|
|
126
133
|
static get(options: XRequestGetOptions, responseType: 'text'): Promise<XRequestResponse<string>>;
|
package/lib/index.js
CHANGED
|
@@ -118,6 +118,20 @@ export class XRequest {
|
|
|
118
118
|
}
|
|
119
119
|
return gotOptions;
|
|
120
120
|
}
|
|
121
|
+
static afterResponse(options, responseType, res) {
|
|
122
|
+
if (responseType === 'text' && options.autoTransformEncoding) {
|
|
123
|
+
const contentType = String(res.headers['content-type']);
|
|
124
|
+
if (contentType.includes('charset=')) {
|
|
125
|
+
const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
|
|
126
|
+
if (charset !== 'utf-8' && charset !== 'utf8') {
|
|
127
|
+
try {
|
|
128
|
+
res.data = new TextDecoder(charset).decode(res.data);
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return res;
|
|
134
|
+
}
|
|
121
135
|
static get(options, responseType) {
|
|
122
136
|
const res = got({
|
|
123
137
|
method: options.method || 'GET',
|
|
@@ -126,7 +140,7 @@ export class XRequest {
|
|
|
126
140
|
if (responseType === 'stream') {
|
|
127
141
|
return res;
|
|
128
142
|
}
|
|
129
|
-
return res.then(res => ({
|
|
143
|
+
return res.then(res => this.afterResponse(options, responseType, {
|
|
130
144
|
status: res.statusCode,
|
|
131
145
|
headers: res.headers,
|
|
132
146
|
data: res.body
|
|
@@ -156,7 +170,7 @@ export class XRequest {
|
|
|
156
170
|
if (responseType === 'stream') {
|
|
157
171
|
return res;
|
|
158
172
|
}
|
|
159
|
-
return res.then(res => ({
|
|
173
|
+
return res.then(res => this.afterResponse(options, responseType, {
|
|
160
174
|
status: res.statusCode,
|
|
161
175
|
headers: res.headers,
|
|
162
176
|
data: res.body
|