Allow usage of a custom axios client

This commit is contained in:
Or Zarchi 2023-02-05 18:43:01 +02:00
parent c8cf2ec254
commit f27366c9a9
3 changed files with 55 additions and 5 deletions

View File

@ -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)
```

View File

@ -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';
@ -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);
@ -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);

View File

@ -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();
@ -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) {