fix: 修复 axios.fork() 无法访问私有方法
parent
40f1f77d46
commit
2254e73cf7
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue