fix: 修复 axios.fork() 无法访问私有方法

pull/41/head
zjx0905 2023-04-07 15:18:05 +08:00
parent 40f1f77d46
commit 2254e73cf7
2 changed files with 23 additions and 1 deletions

View File

@ -89,7 +89,14 @@ function createInstance(defaults: AxiosRequestConfig): AxiosInstance {
}
Object.assign(axios, instance);
Object.setPrototypeOf(axios, Object.getPrototypeOf(instance));
Object.setPrototypeOf(
axios,
Object.assign(Object.getPrototypeOf(instance), {
// axios.fork 内部调用了 instance 的私有方法,无法直接访问私有方法
// axios.fork 调用时 this 重新指向 instance
fork: instance.fork.bind(instance),
}),
);
return axios as AxiosInstance;
}

View File

@ -118,4 +118,19 @@ describe('src/axios.ts', () => {
});
});
});
test('应该可以获取 URI', () => {
expect(
axios.getUri({
url: 'test',
}),
).toBe('test');
});
test('应该可以派生领域', () => {
const a = axios.fork({
baseURL: 'test',
});
expect(a.defaults.baseURL).toBe('http://api.com/test');
});
});