diff --git a/src/axios.ts b/src/axios.ts index 7c48b9c..3fabcb9 100644 --- a/src/axios.ts +++ b/src/axios.ts @@ -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; } diff --git a/test/axios.api.test.ts b/test/axios.api.test.ts index 53357c3..fba7fb6 100644 --- a/test/axios.api.test.ts +++ b/test/axios.api.test.ts @@ -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'); + }); });