Skip to content

Allow usage of a custom axios instance #1434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions docs/axios-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,51 @@ in your `tsconfig.json` file:
}
}
```


## Using a custom Axios client

Sometime you may want to use your own Axios client created by `axios.create` for advanced configuration (e.g. Usage of the popular [axios-retry](https://github.com/softonic/axios-retry) interceptor) without having to [reimplement](./custom-request-file.md) the entire generated Axios request function.

In those cases, simply construct your own HttpRequest wrapper implementation and pass it into your API client

## Example

Create a file that looks like this, that references file from the `/core` folder of the generated client.

```typescript

import axios from 'axios';
import axiosRetry from 'axios-retry';
import { request as __request } from './request';
import { CancelablePromise } from './CancelablePromise';
import { BaseHttpRequest } from './BaseHttpRequest';
import { ApiRequestOptions } from './ApiRequestOptions';
import type { OpenAPIConfig } from './OpenAPI';


export class AxiosHttpRequestWithRetry extends BaseHttpRequest {
axiosInstance = axios.create();

constructor(config: OpenAPIConfig) {
super(config);
axiosRetry(this.axiosInstance);
}

public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
return __request(this.config, options, this.axiosInstance);
}
}

```

Then, when instantiating your generated test client, pass in your custom request wrapper class:

```typescript

import { AxiosHttpRequestWithRetry } from './AxiosRequestWithRetry';
import { GeneratedClient } from './generated/client';

const client = new GeneratedClient({ BASE: 'http://localhost:8123' }, AxiosHttpRequestWithRetry)

```
7 changes: 4 additions & 3 deletions src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{>header}}

import axios from 'axios';
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios';
import type { AxiosError, AxiosRequestConfig, AxiosResponse, AxiosInstance } from 'axios';
import FormData from 'form-data';

import { ApiError } from './ApiError';
Expand Down Expand Up @@ -66,10 +66,11 @@ import type { OpenAPIConfig } from './OpenAPI';
* Request method
* @param config The OpenAPI configuration object
* @param options The request options from the service
* @param axiosClient The axios client instance to use
* @returns CancelablePromise<T>
* @throws ApiError
*/
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
try {
const url = getUrl(config, options);
Expand All @@ -78,7 +79,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): C
const headers = await getHeaders(config, options, formData);

if (!onCancel.isCancelled) {
const response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel);
const response = await sendRequest<T>(config, options, url, body, formData, headers, onCancel, axiosClient);
const responseBody = getResponseBody(response);
const responseHeader = getResponseHeader(response, options.responseHeader);

Expand Down
5 changes: 3 additions & 2 deletions src/templates/core/axios/sendRequest.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const sendRequest = async <T>(
body: any,
formData: FormData | undefined,
headers: Record<string, string>,
onCancel: OnCancel
onCancel: OnCancel,
axiosClient: AxiosInstance
): Promise<AxiosResponse<T>> => {
const source = axios.CancelToken.source();

Expand All @@ -21,7 +22,7 @@ const sendRequest = async <T>(
onCancel(() => source.cancel('The user aborted a request.'));

try {
return await axios.request(requestConfig);
return await axiosClient.request(requestConfig);
} catch (error) {
const axiosError = error as AxiosError<T>;
if (axiosError.response) {
Expand Down