@alba-cars/common-modules 1.10.6 → 1.10.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,6 +3,7 @@ interface ApiRequestOptions extends RequestInit {
|
|
|
3
3
|
query?: Record<string, any>;
|
|
4
4
|
timeout?: number;
|
|
5
5
|
signal?: AbortSignal;
|
|
6
|
+
isRetry?: boolean;
|
|
6
7
|
}
|
|
7
8
|
type RequestInterceptor = (config: RequestInit & {
|
|
8
9
|
url: string;
|
|
@@ -16,10 +17,12 @@ type RequestInterceptor = (config: RequestInit & {
|
|
|
16
17
|
});
|
|
17
18
|
type ResponseInterceptor = (response: Response, requestConfig: RequestInit & {
|
|
18
19
|
url: string;
|
|
20
|
+
options: ApiRequestOptions;
|
|
19
21
|
}) => Promise<Response> | Response;
|
|
20
22
|
type ResponseErrorInterceptor = (error: any, requestConfig: RequestInit & {
|
|
21
23
|
url: string;
|
|
22
|
-
|
|
24
|
+
options: ApiRequestOptions;
|
|
25
|
+
}) => Promise<any> | any;
|
|
23
26
|
declare class InterceptorManager {
|
|
24
27
|
private requestInterceptors;
|
|
25
28
|
private responseInterceptors;
|
|
@@ -43,12 +46,15 @@ declare class InterceptorManager {
|
|
|
43
46
|
}>;
|
|
44
47
|
applyResponseInterceptors(response: Response, requestConfig: RequestInit & {
|
|
45
48
|
url: string;
|
|
49
|
+
options: ApiRequestOptions;
|
|
46
50
|
}): Promise<Response>;
|
|
47
51
|
applyResponseErrorInterceptors(error: any, requestConfig: RequestInit & {
|
|
48
52
|
url: string;
|
|
53
|
+
options: ApiRequestOptions;
|
|
49
54
|
}): Promise<any>;
|
|
50
55
|
retryRequest<T>(config: RequestInit & {
|
|
51
56
|
url: string;
|
|
57
|
+
options: ApiRequestOptions;
|
|
52
58
|
}): Promise<T>;
|
|
53
59
|
}
|
|
54
60
|
export declare const interceptors: InterceptorManager;
|
|
@@ -69,30 +69,30 @@ class InterceptorManager {
|
|
|
69
69
|
return modifiedResponse;
|
|
70
70
|
}
|
|
71
71
|
async applyResponseErrorInterceptors(error, requestConfig) {
|
|
72
|
-
|
|
72
|
+
// Create a chain of error interceptors that can potentially resolve the error
|
|
73
|
+
let promise = Promise.reject(error);
|
|
73
74
|
for (const interceptor of this.responseErrorInterceptors) {
|
|
74
|
-
|
|
75
|
-
// If an interceptor resolves the error, return the resolved value
|
|
76
|
-
const result = await interceptor(modifiedError, requestConfig);
|
|
77
|
-
if (result instanceof Response) {
|
|
78
|
-
return result;
|
|
79
|
-
}
|
|
80
|
-
modifiedError = result;
|
|
81
|
-
}
|
|
82
|
-
catch (newError) {
|
|
83
|
-
// If an interceptor rejects, continue with the new error
|
|
84
|
-
modifiedError = newError;
|
|
85
|
-
}
|
|
75
|
+
promise = promise.catch((currentError) => interceptor(currentError, requestConfig));
|
|
86
76
|
}
|
|
87
|
-
|
|
88
|
-
throw modifiedError;
|
|
77
|
+
return promise;
|
|
89
78
|
}
|
|
90
79
|
async retryRequest(config) {
|
|
80
|
+
// Mark this as a retry request to avoid infinite loops
|
|
81
|
+
const retryConfig = {
|
|
82
|
+
...config,
|
|
83
|
+
options: {
|
|
84
|
+
...config.options,
|
|
85
|
+
isRetry: true,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
91
88
|
try {
|
|
89
|
+
// Apply request interceptors before retrying
|
|
90
|
+
const interceptedConfig = await this.applyRequestInterceptors(retryConfig);
|
|
92
91
|
// Make a new request with the updated config
|
|
93
|
-
const response = await fetch(
|
|
92
|
+
const response = await fetch(interceptedConfig.url, interceptedConfig);
|
|
94
93
|
// Apply response interceptors
|
|
95
|
-
const interceptedResponse = await this.applyResponseInterceptors(response,
|
|
94
|
+
const interceptedResponse = await this.applyResponseInterceptors(response, interceptedConfig);
|
|
95
|
+
// Process the response
|
|
96
96
|
const contentType = interceptedResponse.headers.get("content-type");
|
|
97
97
|
const responseData = (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json"))
|
|
98
98
|
? await interceptedResponse.json()
|
|
@@ -105,8 +105,12 @@ class InterceptorManager {
|
|
|
105
105
|
throw apiError;
|
|
106
106
|
}
|
|
107
107
|
catch (error) {
|
|
108
|
+
// For retry requests, don't apply error interceptors again to avoid loops
|
|
109
|
+
if (config.options.isRetry) {
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
108
112
|
// Apply response error interceptors
|
|
109
|
-
return this.applyResponseErrorInterceptors(error,
|
|
113
|
+
return this.applyResponseErrorInterceptors(error, retryConfig);
|
|
110
114
|
}
|
|
111
115
|
}
|
|
112
116
|
}
|
|
@@ -146,14 +150,12 @@ async function apiRequest(endpoint, options = {}) {
|
|
|
146
150
|
headers,
|
|
147
151
|
...(signal && { signal }),
|
|
148
152
|
...options,
|
|
153
|
+
options, // Keep a reference to the original options
|
|
149
154
|
};
|
|
150
155
|
try {
|
|
151
|
-
const response = await makeRequest(endpoint, config
|
|
156
|
+
const response = await makeRequest(endpoint, config);
|
|
152
157
|
return response;
|
|
153
158
|
}
|
|
154
|
-
catch (error) {
|
|
155
|
-
return Promise.reject(error);
|
|
156
|
-
}
|
|
157
159
|
finally {
|
|
158
160
|
if (timeoutId) {
|
|
159
161
|
clearTimeout(timeoutId);
|
|
@@ -161,20 +163,19 @@ async function apiRequest(endpoint, options = {}) {
|
|
|
161
163
|
}
|
|
162
164
|
}
|
|
163
165
|
exports.apiRequest = apiRequest;
|
|
164
|
-
async function makeRequest(endpoint, config
|
|
165
|
-
const url = buildUrl(endpoint, options.query);
|
|
166
|
-
// Apply request interceptors
|
|
166
|
+
async function makeRequest(endpoint, config) {
|
|
167
|
+
const url = buildUrl(endpoint, config.options.query);
|
|
168
|
+
// Apply request interceptors with the full context including options
|
|
167
169
|
let requestConfig = await exports.interceptors.applyRequestInterceptors({
|
|
168
170
|
...config,
|
|
169
171
|
url,
|
|
170
|
-
options,
|
|
171
172
|
});
|
|
172
173
|
// Extract the URL after interceptors may have modified it
|
|
173
174
|
const interceptedUrl = requestConfig.url;
|
|
174
175
|
try {
|
|
175
176
|
// Make the actual request
|
|
176
177
|
const response = await fetch(interceptedUrl, requestConfig);
|
|
177
|
-
// Apply response interceptors
|
|
178
|
+
// Apply response interceptors with the full context
|
|
178
179
|
const interceptedResponse = await exports.interceptors.applyResponseInterceptors(response, { ...requestConfig, url: interceptedUrl });
|
|
179
180
|
const contentType = interceptedResponse.headers.get("content-type");
|
|
180
181
|
const responseData = (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json"))
|
|
@@ -188,7 +189,7 @@ async function makeRequest(endpoint, config, options = {}) {
|
|
|
188
189
|
throw apiError;
|
|
189
190
|
}
|
|
190
191
|
catch (error) {
|
|
191
|
-
// Apply response error interceptors
|
|
192
|
+
// Apply response error interceptors with the full context
|
|
192
193
|
return exports.interceptors.applyResponseErrorInterceptors(error, {
|
|
193
194
|
...requestConfig,
|
|
194
195
|
url: interceptedUrl,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { UserCreateDTO, UserGetDTO, UserUpdateDTO } from "../../../auth";
|
|
2
2
|
export declare enum Designation {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
SALES_MANAGER = "sales-manager",
|
|
4
|
+
SALES_EXECUTIVE = "sales-executive",
|
|
5
5
|
CO_FOUNDER = "co-founder",
|
|
6
6
|
CSO = "cso",
|
|
7
7
|
PURCHASE_MANAGER = "Purchase Manager"
|
|
8
8
|
}
|
|
9
9
|
export declare enum TeamCategory {
|
|
10
|
-
SALES = "
|
|
11
|
-
|
|
10
|
+
SALES = "sales",
|
|
11
|
+
MANAGEMENT = "management"
|
|
12
12
|
}
|
|
13
13
|
export declare enum Languages {
|
|
14
14
|
ARABIC = "Arabic",
|
|
@@ -15,16 +15,16 @@ const class_transformer_1 = require("class-transformer");
|
|
|
15
15
|
const auth_1 = require("../../../auth");
|
|
16
16
|
var Designation;
|
|
17
17
|
(function (Designation) {
|
|
18
|
-
Designation["
|
|
19
|
-
Designation["
|
|
18
|
+
Designation["SALES_MANAGER"] = "sales-manager";
|
|
19
|
+
Designation["SALES_EXECUTIVE"] = "sales-executive";
|
|
20
20
|
Designation["CO_FOUNDER"] = "co-founder";
|
|
21
21
|
Designation["CSO"] = "cso";
|
|
22
22
|
Designation["PURCHASE_MANAGER"] = "Purchase Manager";
|
|
23
23
|
})(Designation = exports.Designation || (exports.Designation = {}));
|
|
24
24
|
var TeamCategory;
|
|
25
25
|
(function (TeamCategory) {
|
|
26
|
-
TeamCategory["SALES"] = "
|
|
27
|
-
TeamCategory["
|
|
26
|
+
TeamCategory["SALES"] = "sales";
|
|
27
|
+
TeamCategory["MANAGEMENT"] = "management";
|
|
28
28
|
})(TeamCategory = exports.TeamCategory || (exports.TeamCategory = {}));
|
|
29
29
|
var Languages;
|
|
30
30
|
(function (Languages) {
|
package/package.json
CHANGED