@jayfong/x-request 2.66.0 → 2.68.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 CHANGED
@@ -5,6 +5,7 @@ exports.__esModule = true;
5
5
  exports.xr = exports.XRequestFormUrlencoded = exports.XRequestFormStream = exports.XRequestFormFile = exports.XRequestFormData = exports.XRequest = void 0;
6
6
  var _assert = _interopRequireDefault(require("assert"));
7
7
  var _fs = _interopRequireDefault(require("fs"));
8
+ var _nodeUtil = require("node:util");
8
9
  var _stream = require("stream");
9
10
  var _formData = _interopRequireDefault(require("form-data"));
10
11
  var _formstream = _interopRequireDefault(require("formstream"));
@@ -89,7 +90,7 @@ class XRequest {
89
90
  }
90
91
  const gotOptions = {
91
92
  url: options.url,
92
- responseType: responseType === 'stream' ? undefined : responseType,
93
+ responseType: responseType === 'stream' ? undefined : responseType === 'text' && options.autoTransformEncoding ? 'buffer' : responseType,
93
94
  https: {
94
95
  rejectUnauthorized: false
95
96
  },
@@ -127,6 +128,29 @@ class XRequest {
127
128
  }
128
129
  return gotOptions;
129
130
  }
131
+ static afterResponse(options, responseType, res) {
132
+ if (responseType === 'text' && options.autoTransformEncoding) {
133
+ if (options.autoTransformEncoding === true) {
134
+ const contentType = String(res.headers['content-type']);
135
+ if (contentType.includes('charset=')) {
136
+ const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
137
+ if (charset !== 'utf-8' && charset !== 'utf8') {
138
+ try {
139
+ res.data = new _nodeUtil.TextDecoder(charset).decode(res.data);
140
+ } catch {}
141
+ }
142
+ }
143
+ } else {
144
+ try {
145
+ res.data = new _nodeUtil.TextDecoder(options.autoTransformEncoding).decode(res.data);
146
+ } catch {}
147
+ }
148
+ if (Buffer.isBuffer(res.data)) {
149
+ res.data = res.data.toString('utf-8');
150
+ }
151
+ }
152
+ return res;
153
+ }
130
154
  static get(options, responseType) {
131
155
  const res = (0, _got.default)({
132
156
  method: options.method || 'GET',
@@ -135,7 +159,7 @@ class XRequest {
135
159
  if (responseType === 'stream') {
136
160
  return res;
137
161
  }
138
- return res.then(res => ({
162
+ return res.then(res => this.afterResponse(options, responseType, {
139
163
  status: res.statusCode,
140
164
  headers: res.headers,
141
165
  data: res.body
@@ -165,7 +189,7 @@ class XRequest {
165
189
  if (responseType === 'stream') {
166
190
  return res;
167
191
  }
168
- return res.then(res => ({
192
+ return res.then(res => this.afterResponse(options, responseType, {
169
193
  status: res.statusCode,
170
194
  headers: res.headers,
171
195
  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 | string;
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
@@ -1,5 +1,6 @@
1
1
  import assert from 'assert';
2
2
  import fs from 'fs';
3
+ import { TextDecoder } from 'node:util';
3
4
  import { Readable } from 'stream';
4
5
  import FormData from 'form-data';
5
6
  import FormStream from 'formstream';
@@ -80,7 +81,7 @@ export class XRequest {
80
81
  }
81
82
  const gotOptions = {
82
83
  url: options.url,
83
- responseType: responseType === 'stream' ? undefined : responseType,
84
+ responseType: responseType === 'stream' ? undefined : responseType === 'text' && options.autoTransformEncoding ? 'buffer' : responseType,
84
85
  https: {
85
86
  rejectUnauthorized: false
86
87
  },
@@ -118,6 +119,29 @@ export class XRequest {
118
119
  }
119
120
  return gotOptions;
120
121
  }
122
+ static afterResponse(options, responseType, res) {
123
+ if (responseType === 'text' && options.autoTransformEncoding) {
124
+ if (options.autoTransformEncoding === true) {
125
+ const contentType = String(res.headers['content-type']);
126
+ if (contentType.includes('charset=')) {
127
+ const charset = contentType.match(/charset=([^;]+)/)[1].toLowerCase();
128
+ if (charset !== 'utf-8' && charset !== 'utf8') {
129
+ try {
130
+ res.data = new TextDecoder(charset).decode(res.data);
131
+ } catch {}
132
+ }
133
+ }
134
+ } else {
135
+ try {
136
+ res.data = new TextDecoder(options.autoTransformEncoding).decode(res.data);
137
+ } catch {}
138
+ }
139
+ if (Buffer.isBuffer(res.data)) {
140
+ res.data = res.data.toString('utf-8');
141
+ }
142
+ }
143
+ return res;
144
+ }
121
145
  static get(options, responseType) {
122
146
  const res = got({
123
147
  method: options.method || 'GET',
@@ -126,7 +150,7 @@ export class XRequest {
126
150
  if (responseType === 'stream') {
127
151
  return res;
128
152
  }
129
- return res.then(res => ({
153
+ return res.then(res => this.afterResponse(options, responseType, {
130
154
  status: res.statusCode,
131
155
  headers: res.headers,
132
156
  data: res.body
@@ -156,7 +180,7 @@ export class XRequest {
156
180
  if (responseType === 'stream') {
157
181
  return res;
158
182
  }
159
- return res.then(res => ({
183
+ return res.then(res => this.afterResponse(options, responseType, {
160
184
  status: res.statusCode,
161
185
  headers: res.headers,
162
186
  data: res.body
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-request",
3
- "version": "2.66.0",
3
+ "version": "2.68.0",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",