Files
RSSHub/lib/utils/ofetch.ts
T
bugyaluwang ff1ff8d940 feat(dev): visualize network requests in the development environment on Chrome Devtools's network tab (#17308)
* chore: add node-network-devtools to enhance development experience

* feat(dev): visualize network requests in the development environment on Chrome Devtools's network tab

* chore(deps): update node-network-devtools

* fix: lock error when ci

* chore: update node-network-devtools to v1.0.18

* chore: update node-network-devtools

* chore: use node-network-devtools v1.0.20

* feat: use hook to add custom headers for fetch

* fix: complex method 16

* test: add test for fetch

* fix: test type error

* fix: hard code

* chore(cr): rename the fetch.spec.ts to fetch.test.ts and use exact version

* chore: fix lock error

---------
2024-11-06 04:50:43 +08:00

40 lines
1.3 KiB
TypeScript

import { createFetch } from 'ofetch';
import { config } from '@/config';
import logger from '@/utils/logger';
import { register } from 'node-network-devtools';
process.env.NODE_ENV === 'dev' && register();
const rofetch = createFetch().create({
retryStatusCodes: [400, 408, 409, 425, 429, 500, 502, 503, 504],
retry: config.requestRetry,
retryDelay: 1000,
// timeout: config.requestTimeout,
onResponseError({ request, response, options }) {
if (options.retry) {
logger.warn(`Request ${request} with error ${response.status} remaining retry attempts: ${options.retry}`);
if (!options.headers) {
options.headers = {};
}
if (options.headers instanceof Headers) {
options.headers.set('x-prefer-proxy', '1');
} else {
options.headers['x-prefer-proxy'] = '1';
}
}
},
onRequestError({ request, error }) {
logger.error(`Request ${request} fail: ${error}`);
},
headers: {
'user-agent': config.ua,
},
onResponse({ request, response }) {
if (response.redirected) {
logger.http(`Redirecting to ${response.url} for ${request}`);
}
},
});
export default rofetch;