Skip to content

Commit 60ba98b

Browse files
author
Mahmoud Zeyada
committed
feature: added custome axios action request
1 parent 24c9fe2 commit 60ba98b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

templates/base/http-clients/axios-http-client.ejs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface ApiConfig<SecurityDataType = unknown> extends Omit<AxiosRequest
2828
securityWorker?: (securityData: SecurityDataType | null) => Promise<AxiosRequestConfig | void> | AxiosRequestConfig | void;
2929
secure?: boolean;
3030
format?: ResponseType;
31+
customAction?: <T>(
32+
action: () => Promise<AxiosResponse<T>>,
33+
) => Promise<AxiosResponse<T>>;
3134
}
3235

3336
export enum ContentType {
@@ -43,12 +46,13 @@ export class HttpClient<SecurityDataType = unknown> {
4346
private securityWorker?: ApiConfig<SecurityDataType>["securityWorker"];
4447
private secure?: boolean;
4548
private format?: ResponseType;
46-
47-
constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
49+
private customAction?: ApiConfig<SecurityDataType>["customAction"];
50+
constructor({ securityWorker, secure, format, customAction, ...axiosConfig }: ApiConfig<SecurityDataType> = {}) {
4851
this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" })
4952
this.secure = secure;
5053
this.format = format;
5154
this.securityWorker = securityWorker;
55+
this.customAction = customAction;
5256
}
5357

5458
public setSecurityData = (data: SecurityDataType | null) => {
@@ -123,7 +127,7 @@ export class HttpClient<SecurityDataType = unknown> {
123127
body = JSON.stringify(body);
124128
}
125129

126-
return this.instance.request({
130+
const action = () => this.instance.request({
127131
...requestParams,
128132
headers: {
129133
...(requestParams.headers || {}),
@@ -138,5 +142,7 @@ export class HttpClient<SecurityDataType = unknown> {
138142
<% } else { %>
139143
});
140144
<% } %>
145+
146+
return this.customAction ? this.customAction(action) : action();
141147
};
142148
}

0 commit comments

Comments
 (0)