Skip to content

Commit 348560e

Browse files
committed
add doc
fix
1 parent ef0e828 commit 348560e

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

docs/custom-axios-config.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Using a custom Axios config
2+
3+
Sometime you may want to use your own Axios config for requests.
4+
`onUploadProgress` or `onDownloadProgress` are two examples of this.
5+
6+
```typescript
7+
FileService.uploadFile({
8+
file,
9+
}).axiosConfig({
10+
onUploadProgress: (event) => {
11+
// Do something with the event
12+
console.log(event.loaded / event.total)
13+
}
14+
})
15+
```

src/templates/core/CancelablePromise.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export class CancelablePromise<T> implements Promise<T> {
2626
#isResolved: boolean;
2727
#isRejected: boolean;
2828
#isCancelled: boolean;
29-
#axiosConfig?: AxiosRequestConfig;
3029
readonly #cancelHandlers: (() => void)[];
3130
readonly #promise: Promise<T>;
3231
#resolve?: (value: T | PromiseLike<T>) => void;
3332
#reject?: (reason?: any) => void;
33+
#axiosConfig?: AxiosRequestConfig = {};
3434

3535
constructor(
3636
executor: (
@@ -83,7 +83,7 @@ export class CancelablePromise<T> implements Promise<T> {
8383
get: (): boolean => this.#isCancelled,
8484
});
8585

86-
const axiosConfig: AxiosRequestConfig = this.#axiosConfig || {};
86+
const axiosConfig: AxiosRequestConfig = this.#axiosConfig;
8787

8888
return executor(onResolve, onReject, onCancel as OnCancel, axiosConfig);
8989
});
@@ -130,7 +130,7 @@ export class CancelablePromise<T> implements Promise<T> {
130130
}
131131

132132
public axiosConfig(axiosConfig: AxiosRequestConfig): this {
133-
this.#axiosConfig = axiosConfig;
133+
Object.assign(this.#axiosConfig as object, axiosConfig);
134134
return this;
135135
}
136136

src/templates/core/axios/request.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions, ax
7979
const headers = await getHeaders(config, options, formData);
8080

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

src/templates/core/axios/sendRequest.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const sendRequest = async <T>(
77
headers: Record<string, string>,
88
onCancel: OnCancel,
99
axiosClient: AxiosInstance,
10-
axiosConfig: AxiosRequestConfig,
10+
axiosConfig?: AxiosRequestConfig,
1111
): Promise<AxiosResponse<T>> => {
1212
const source = axios.CancelToken.source();
1313

0 commit comments

Comments
 (0)