Skip to content

Commit 8e9b9ae

Browse files
committed
feat(client-axios): enhance createClient to accept AxiosStatic or AxiosInstance
1 parent 073d56e commit 8e9b9ae

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

docs/openapi-ts/clients/axios.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,29 @@ console.log(url); // prints '/foo/1?bar=baz'
210210
You can implement your own `axios` instance. This is useful if you need to extend the default `axios` instance with extra functionality, or replace it altogether.
211211

212212
```js
213+
import axios from 'axios';
213214
import { client } from 'client/client.gen';
214215

216+
// Customize the default axios instance
217+
axios.defaults.baseURL = 'https://example.com';
218+
215219
client.setConfig({
216-
axios: () => {
217-
/* custom `axios` instance */
218-
},
220+
axios: axios,
221+
});
222+
```
223+
224+
or you can pass an `AxiosInstance` created with `axios.create()`:
225+
226+
```js
227+
import axios from 'axios';
228+
import { client } from 'client/client.gen';
229+
230+
const customAxiosInstance = axios.create({
231+
baseURL: 'https://example.com',
232+
});
233+
234+
client.setConfig({
235+
axios: customAxiosInstance,
219236
});
220237
```
221238

packages/openapi-ts/src/plugins/@hey-api/client-axios/__tests__/client.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from 'axios';
12
import { describe, expect, it } from 'vitest';
23

34
import { createClient } from '../bundle/client';
@@ -48,3 +49,37 @@ describe('buildUrl', () => {
4849
expect(client.buildUrl(options)).toBe(url);
4950
});
5051
});
52+
53+
describe('AxiosInstance', () => {
54+
it('should create an AxiosInstance if no axios option passed', () => {
55+
const client = createClient({ baseURL: 'test-url' });
56+
57+
expect(client.instance).toBeDefined();
58+
expect(client.instance.defaults).toBeDefined();
59+
expect(client.instance.defaults.baseURL).toBe('test-url');
60+
});
61+
62+
it('should use the provided AxiosStatic if axios option passed', () => {
63+
axios.defaults.baseURL = 'test-url';
64+
65+
const client = createClient({ axios });
66+
67+
expect(client.instance).toBeDefined();
68+
expect(client.instance.defaults).toBeDefined();
69+
expect(client.instance.defaults.baseURL).toBe('test-url');
70+
});
71+
72+
it('should use the provided AxiosInstance if axios option is passed', () => {
73+
const axiosInstance = axios.create({ baseURL: 'test-url' });
74+
75+
axiosInstance.interceptors.request.use((config) => config);
76+
77+
const client = createClient({ axios: axiosInstance });
78+
79+
expect(client.instance).toBe(axiosInstance);
80+
expect(client.instance.defaults.baseURL).toBe('test-url');
81+
expect((client.instance.interceptors.request as any).handlers.length).toBe(
82+
1,
83+
);
84+
});
85+
});

packages/openapi-ts/src/plugins/@hey-api/client-axios/bundle/client.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AxiosError, RawAxiosRequestHeaders } from 'axios';
1+
import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios';
22
import axios from 'axios';
33

44
import type { Client, Config } from './types';
@@ -13,9 +13,15 @@ import {
1313
export const createClient = (config: Config = {}): Client => {
1414
let _config = mergeConfigs(createConfig(), config);
1515

16-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17-
const { auth, ...configWithoutAuth } = _config;
18-
const instance = axios.create(configWithoutAuth);
16+
let instance: AxiosInstance;
17+
18+
if (_config.axios && !('Axios' in _config.axios)) {
19+
instance = _config.axios;
20+
} else {
21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22+
const { auth, ...configWithoutAuth } = _config;
23+
instance = axios.create(configWithoutAuth);
24+
}
1925

2026
const getConfig = (): Config => ({ ..._config });
2127

packages/openapi-ts/src/plugins/@hey-api/client-axios/bundle/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export interface Config<T extends ClientOptions = ClientOptions>
1717
extends Omit<CreateAxiosDefaults, 'auth' | 'baseURL' | 'headers' | 'method'>,
1818
CoreConfig {
1919
/**
20-
* Axios implementation. You can use this option to provide a custom
21-
* Axios instance.
20+
* Axios implementation. You can use this option to provide either an
21+
* `AxiosStatic` or an `AxiosInstance`.
2222
*
2323
* @default axios
2424
*/
25-
axios?: AxiosStatic;
25+
axios?: AxiosStatic | AxiosInstance;
2626
/**
2727
* Base URL for all requests made by this client.
2828
*/

0 commit comments

Comments
 (0)