mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-02 10:08:02 +08:00
Before this fix, timeout messages are not quite useful > error: Request undefined fail, retry attempt #1: TimeoutError: Timeout awaiting 'request' for 5000ms
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
const logger = require('./logger');
|
|
const config = require('@/config').value;
|
|
const got = require('got');
|
|
|
|
const custom = got.extend({
|
|
retry: config.requestRetry,
|
|
hooks: {
|
|
beforeRetry: [
|
|
(options, err, count) => {
|
|
logger.error(`Request ${options.url} fail, retry attempt #${count}: ${err}`);
|
|
},
|
|
],
|
|
afterResponse: [
|
|
(response) => {
|
|
try {
|
|
response.data = JSON.parse(response.body);
|
|
} catch (e) {
|
|
response.data = response.body;
|
|
}
|
|
response.status = response.statusCode;
|
|
return response;
|
|
},
|
|
],
|
|
init: [
|
|
(options) => {
|
|
// compatible with axios api
|
|
if (options && options.data) {
|
|
options.body = options.body || options.data;
|
|
}
|
|
},
|
|
],
|
|
},
|
|
headers: {
|
|
'user-agent': config.ua,
|
|
},
|
|
timeout: config.requestTimeout,
|
|
});
|
|
custom.all = (list) => Promise.all(list);
|
|
|
|
module.exports = custom;
|