js
async function myFetch() {
const originalFetch = window.fetch;
window.fetch = async function (url, ...args) {
try {
let config = {};
if (args.length > 0 && typeof args[0] === 'object') {
config = args[0];
}
// 创建 Headers 对象以确保现有头部不会被覆盖
let headers = new Headers(config.headers || {});
// 添加或更新 Authorization 头
headers.set('Authorization', 'token 91fu3bgltenritfl');
// 更新配置对象中的 headers 属性
config.headers = headers;
const response = await originalFetch(url, config);
return response;
} catch (error) {
throw error;
}
};
}