Skip to content

Commit 5eac799

Browse files
authored
Support custom Axios instances in setupOpenApiClient (#187)
* Support custom Axios instances in `setupOpenApiClient` and add corresponding tests * Clarify default and custom axios options in `typedApiClient.setupOpenApiClient` usage docs * Formatting
1 parent 47ef6ff commit 5eac799

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

vue/src/network/__tests__/typedApiClient.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ describe('network/typedApiClient', () => {
3131
expect(setConfig).toHaveBeenCalledExactlyOnceWith({ axios: axiosInstance })
3232
})
3333

34+
it('setupOpenApiClient should attach custom axios instance when provided', () => {
35+
const setConfig = vi.fn()
36+
const client = { setConfig }
37+
const customAxios = { custom: true } as any
38+
typedApiClient.setupOpenApiClient(client, customAxios)
39+
expect(setConfig).toHaveBeenCalledTimes(1)
40+
expect(setConfig).toHaveBeenCalledExactlyOnceWith({ axios: customAxios })
41+
})
42+
3443
describe('query()', () => {
3544
it('configures useQuery with callable name and options ref; enabled guard; unwraps data', async () => {
3645
// Define a named callable to check queryKey uses callable.name

vue/src/network/network.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The lib provide utils and helpers to handle communications with a backend (ReST
4040
1. Attach axios to the "client"
4141
2. Ensure TanStack wrappers are up and running
4242
4. Create your OpenAPI-based API Client, if applicable
43-
1. Attach axios to the "client"
43+
1. Attach axios to the "client" (defaults to the axios singleton)
4444
2. Ensure TanStack wrappers are up and running
4545
5. Initialize the backendStore
4646
1. Attach the reactive interceptors on the axios Instance, for authentication
@@ -100,11 +100,18 @@ If your backend exposes an OpenAPI schema, you can leverage it by auto-generatin
100100

101101
- From your OpenAPI specs, generate a typed SDK : `npx @hey-api/openapi-ts --input ./path/to/openapi.yaml --output ./src/openapi --client @hey-api/client-axios`
102102
- Link the typedApiClient to the SDK, in main.ts :
103+
103104
```ts
104105
import { client } from '@/openapi/client.gen'
105106
import { typedApiClient } from '@nside/wefa/network'
107+
108+
// By default, it will use the axios singleton from @nside/wefa/network
106109
typedApiClient.setupOpenApiClient(client)
110+
111+
// Or you can provide a custom axios instance
112+
typedApiClient.setupOpenApiClient(client, myCustomAxiosInstance)
107113
```
114+
108115
- Use the wrappers anywhere :
109116

110117
```ts

vue/src/network/typedApiClient.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {
1010
type UseMutationReturnType,
1111
type UseMutationOptions,
1212
} from '@tanstack/vue-query'
13-
import type { AxiosError } from 'axios'
13+
import type { AxiosError, AxiosInstance } from 'axios'
1414
import type { Ref } from 'vue'
1515

1616
/**
17-
* Attaches the axios singleton to an OpenAPI Client/SDK,
17+
* Attaches an axios instance to an OpenAPI Client/SDK,
1818
* autogenerated by @hey-api/openapi-ts.
1919
* You need to call this during application startup to have a working OpenAPI Client
2020
* E.g., if you're running: npx @hey-api/openapi-ts --input .path/to/openapi.yaml --output ./src/openapi
@@ -23,11 +23,12 @@ import type { Ref } from 'vue'
2323
* import { typedApiClient } from '@nside/wefa/network'
2424
* typedApiClient.setupOpenApiClient(client)
2525
* @param client Auto-generated OpenAPI client
26+
* @param axios Optional axios instance. If not provided, it defaults to the axios singleton.
2627
*/
2728
// We can't really type the client arg because the type itself is codegen so it might not exist
28-
function setupOpenApiClient(client: any) {
29+
function setupOpenApiClient(client: any, axios?: AxiosInstance) {
2930
client.setConfig({
30-
axios: axiosInstance,
31+
axios: axios ?? axiosInstance,
3132
})
3233
}
3334

0 commit comments

Comments
 (0)