Skip to content

Commit ef0e828

Browse files
committed
add axios config
1 parent 9b796e0 commit ef0e828

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/templates/core/CancelablePromise.hbs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{{>header}}
22

3+
import type { AxiosRequestConfig } from 'axios';
4+
35
export class CancelError extends Error {
46

57
constructor(message: string) {
@@ -24,6 +26,7 @@ export class CancelablePromise<T> implements Promise<T> {
2426
#isResolved: boolean;
2527
#isRejected: boolean;
2628
#isCancelled: boolean;
29+
#axiosConfig?: AxiosRequestConfig;
2730
readonly #cancelHandlers: (() => void)[];
2831
readonly #promise: Promise<T>;
2932
#resolve?: (value: T | PromiseLike<T>) => void;
@@ -33,7 +36,8 @@ export class CancelablePromise<T> implements Promise<T> {
3336
executor: (
3437
resolve: (value: T | PromiseLike<T>) => void,
3538
reject: (reason?: any) => void,
36-
onCancel: OnCancel
39+
onCancel: OnCancel,
40+
axiosConfig?: AxiosRequestConfig
3741
) => void
3842
) {
3943
this.#isResolved = false;
@@ -79,7 +83,9 @@ export class CancelablePromise<T> implements Promise<T> {
7983
get: (): boolean => this.#isCancelled,
8084
});
8185

82-
return executor(onResolve, onReject, onCancel as OnCancel);
86+
const axiosConfig: AxiosRequestConfig = this.#axiosConfig || {};
87+
88+
return executor(onResolve, onReject, onCancel as OnCancel, axiosConfig);
8389
});
8490
}
8591

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

132+
public axiosConfig(axiosConfig: AxiosRequestConfig): this {
133+
this.#axiosConfig = axiosConfig;
134+
return this;
135+
}
136+
126137
public get isCancelled(): boolean {
127138
return this.#isCancelled;
128139
}

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import type { OpenAPIConfig } from './OpenAPI';
7171
* @throws ApiError
7272
*/
7373
export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, axiosClient: AxiosInstance = axios): CancelablePromise<T> => {
74-
return new CancelablePromise(async (resolve, reject, onCancel) => {
74+
return new CancelablePromise(async (resolve, reject, onCancel, axiosConfig) => {
7575
try {
7676
const url = getUrl(config, options);
7777
const formData = getFormData(options);

src/templates/core/axios/sendRequest.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export const sendRequest = async <T>(
66
formData: FormData | undefined,
77
headers: Record<string, string>,
88
onCancel: OnCancel,
9-
axiosClient: AxiosInstance
9+
axiosClient: AxiosInstance,
10+
axiosConfig: AxiosRequestConfig,
1011
): Promise<AxiosResponse<T>> => {
1112
const source = axios.CancelToken.source();
1213

@@ -17,6 +18,7 @@ export const sendRequest = async <T>(
1718
method: options.method,
1819
withCredentials: config.WITH_CREDENTIALS,
1920
cancelToken: source.token,
21+
...axiosConfig,
2022
};
2123

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

0 commit comments

Comments
 (0)