* 执行 HTTPS 请求 * @param {Object} options - 请求选项 * @param {string} data - 请求数据 * @returns {Promise }
(options, data)
| 267 | * @returns {Promise<Object>} |
| 268 | */ |
| 269 | static async request(options, data) { |
| 270 | return new Promise((resolve, reject) => { |
| 271 | const req = https.request(options, (res) => { |
| 272 | let responseData = ''; |
| 273 | |
| 274 | res.on('data', (chunk) => { |
| 275 | responseData += chunk; |
| 276 | }); |
| 277 | |
| 278 | res.on('end', () => { |
| 279 | resolve({ |
| 280 | statusCode: res.statusCode, |
| 281 | headers: res.headers, |
| 282 | data: responseData |
| 283 | }); |
| 284 | }); |
| 285 | }); |
| 286 | |
| 287 | req.on('error', (error) => { |
| 288 | reject(error); |
| 289 | }); |
| 290 | |
| 291 | req.on('timeout', () => { |
| 292 | req.destroy(); |
| 293 | reject(new Error('Request timeout')); |
| 294 | }); |
| 295 | |
| 296 | if (data) { |
| 297 | req.write(data); |
| 298 | } |
| 299 | |
| 300 | req.end(); |
| 301 | }); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
no test coverage detected