Skip to content
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

Add custom axios config for request #1682

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions docs/custom-axios-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Using a custom Axios config

Sometime you may want to use your own Axios config for requests.
`onUploadProgress` or `onDownloadProgress` are two examples of this.

```typescript
FileService.uploadFile({
file,
}).axiosConfig({
onUploadProgress: (event) => {
// Do something with the event
console.log(event.loaded / event.total)
}
})
```
15 changes: 13 additions & 2 deletions src/templates/core/CancelablePromise.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{>header}}

import type { AxiosRequestConfig } from 'axios';

export class CancelError extends Error {

constructor(message: string) {
Expand Down Expand Up @@ -28,12 +30,14 @@ export class CancelablePromise<T> implements Promise<T> {
readonly #promise: Promise<T>;
#resolve?: (value: T | PromiseLike<T>) => void;
#reject?: (reason?: any) => void;
#axiosConfig?: AxiosRequestConfig = {};

constructor(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void,
onCancel: OnCancel
onCancel: OnCancel,
axiosConfig?: AxiosRequestConfig
) => void
) {
this.#isResolved = false;
Expand Down Expand Up @@ -79,7 +83,9 @@ export class CancelablePromise<T> implements Promise<T> {
get: (): boolean => this.#isCancelled,
});

return executor(onResolve, onReject, onCancel as OnCancel);
const axiosConfig: AxiosRequestConfig = this.#axiosConfig;

return executor(onResolve, onReject, onCancel as OnCancel, axiosConfig);
});
}

Expand Down Expand Up @@ -123,6 +129,11 @@ export class CancelablePromise<T> implements Promise<T> {
if (this.#reject) this.#reject(new CancelError('Request aborted'));
}

public axiosConfig(axiosConfig: AxiosRequestConfig): this {
Object.assign(this.#axiosConfig as object, axiosConfig);
return this;
}

public get isCancelled(): boolean {
return this.#isCancelled;
}
Expand Down
4 changes: 2 additions & 2 deletions src/templates/core/axios/request.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ import type { OpenAPIConfig } from './OpenAPI';
* @throws ApiError
*/
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
return new CancelablePromise(async (resolve, reject, onCancel) => {
return new CancelablePromise(async (resolve, reject, onCancel, axiosConfig) => {
try {
const url = getUrl(config, options);
const formData = getFormData(options);
const body = getRequestBody(options);
const headers = await getHeaders(config, options, formData);

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

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

Expand All @@ -17,6 +18,7 @@ export const sendRequest = async <T>(
method: options.method,
withCredentials: config.WITH_CREDENTIALS,
cancelToken: source.token,
...axiosConfig,
};

onCancel(() => source.cancel('The user aborted a request.'));
Expand Down