@@ -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
3336export 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