From 60ba98b28b3814c7a9d44273123b5f1d978e5264 Mon Sep 17 00:00:00 2001 From: Mahmoud Zeyada Date: Wed, 13 Nov 2024 15:50:26 +0200 Subject: [PATCH] feature: added custome axios action request --- templates/base/http-clients/axios-http-client.ejs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/templates/base/http-clients/axios-http-client.ejs b/templates/base/http-clients/axios-http-client.ejs index 0b0a9ecb..bdd1364e 100644 --- a/templates/base/http-clients/axios-http-client.ejs +++ b/templates/base/http-clients/axios-http-client.ejs @@ -28,6 +28,9 @@ export interface ApiConfig extends Omit Promise | AxiosRequestConfig | void; secure?: boolean; format?: ResponseType; + customAction?: ( + action: () => Promise>, + ) => Promise>; } export enum ContentType { @@ -43,12 +46,13 @@ export class HttpClient { private securityWorker?: ApiConfig["securityWorker"]; private secure?: boolean; private format?: ResponseType; - - constructor({ securityWorker, secure, format, ...axiosConfig }: ApiConfig = {}) { + private customAction?: ApiConfig["customAction"]; + constructor({ securityWorker, secure, format, customAction, ...axiosConfig }: ApiConfig = {}) { this.instance = axios.create({ ...axiosConfig, baseURL: axiosConfig.baseURL || "<%~ apiConfig.baseUrl %>" }) this.secure = secure; this.format = format; this.securityWorker = securityWorker; + this.customAction = customAction; } public setSecurityData = (data: SecurityDataType | null) => { @@ -123,7 +127,7 @@ export class HttpClient { body = JSON.stringify(body); } - return this.instance.request({ + const action = () => this.instance.request({ ...requestParams, headers: { ...(requestParams.headers || {}), @@ -138,5 +142,7 @@ export class HttpClient { <% } else { %> }); <% } %> + + return this.customAction ? this.customAction(action) : action(); }; }