@alba-cars/common-modules 1.4.7 → 1.4.9
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.
|
@@ -41,7 +41,7 @@ const refreshTokens = async () => {
|
|
|
41
41
|
var _a, _b, _c, _d;
|
|
42
42
|
try {
|
|
43
43
|
const tokens = (0, exports.getTokens)();
|
|
44
|
-
if (
|
|
44
|
+
if (!isServer && !(tokens === null || tokens === void 0 ? void 0 : tokens.refreshToken)) {
|
|
45
45
|
// only throw this error if we are on the client and there are no refresh token available
|
|
46
46
|
throw new Error("No refresh token available");
|
|
47
47
|
}
|
|
@@ -77,7 +77,7 @@ const buildUrl = (endpoint, query) => {
|
|
|
77
77
|
return url;
|
|
78
78
|
};
|
|
79
79
|
async function apiRequest(endpoint, options = {}) {
|
|
80
|
-
var _a
|
|
80
|
+
var _a;
|
|
81
81
|
const headers = {
|
|
82
82
|
Accept: "application/json",
|
|
83
83
|
"Content-Type": "application/json",
|
|
@@ -95,77 +95,86 @@ async function apiRequest(endpoint, options = {}) {
|
|
|
95
95
|
...options,
|
|
96
96
|
};
|
|
97
97
|
try {
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
isRefreshing = true;
|
|
98
|
+
const response = await makeRequest(endpoint, config, options);
|
|
99
|
+
return response;
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
console.error("API Request Error:", error);
|
|
103
|
+
return Promise.reject(error);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.apiRequest = apiRequest;
|
|
107
|
+
async function makeRequest(endpoint, config, options = {}) {
|
|
108
|
+
var _a;
|
|
109
|
+
const url = buildUrl(endpoint, options.query);
|
|
110
|
+
const response = await fetch(url, config);
|
|
111
|
+
const contentType = response.headers.get("content-type");
|
|
112
|
+
const responseData = (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json"))
|
|
113
|
+
? await response.json()
|
|
114
|
+
: await response.text();
|
|
115
|
+
if (response.ok) {
|
|
116
|
+
if (!isServer && !options.skipAuth && ((_a = responseData === null || responseData === void 0 ? void 0 : responseData.data) === null || _a === void 0 ? void 0 : _a.token)) {
|
|
117
|
+
(0, exports.setTokens)({
|
|
118
|
+
accessToken: responseData.data.token,
|
|
119
|
+
refreshToken: responseData.data.refreshToken,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return responseData;
|
|
123
|
+
}
|
|
124
|
+
// Handle unauthorized error and token refresh
|
|
125
|
+
if (!isServer &&
|
|
126
|
+
response.status === enums_1.HttpStatusCodes.UNAUTHORIZED &&
|
|
127
|
+
!options.skipAuth) {
|
|
128
|
+
return handleUnauthorizedError(endpoint, options);
|
|
129
|
+
}
|
|
130
|
+
// Handle other errors
|
|
131
|
+
throw createApiError(response.status, responseData);
|
|
132
|
+
}
|
|
133
|
+
async function handleUnauthorizedError(endpoint, options) {
|
|
134
|
+
if (isRefreshing) {
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
subscribeTokenRefresh(async (newToken) => {
|
|
138
137
|
try {
|
|
139
|
-
const
|
|
140
|
-
onTokenRefreshed(newToken);
|
|
141
|
-
isRefreshing = false;
|
|
142
|
-
// Retry the original request with new token
|
|
143
|
-
return apiRequest(endpoint, {
|
|
138
|
+
const response = await apiRequest(endpoint, {
|
|
144
139
|
...options,
|
|
145
140
|
headers: {
|
|
146
141
|
...options.headers,
|
|
147
142
|
Authorization: `Bearer ${newToken}`,
|
|
148
143
|
},
|
|
149
144
|
});
|
|
145
|
+
resolve(response);
|
|
150
146
|
}
|
|
151
|
-
catch (
|
|
152
|
-
|
|
153
|
-
throw refreshError;
|
|
147
|
+
catch (error) {
|
|
148
|
+
reject(error);
|
|
154
149
|
}
|
|
155
|
-
}
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
158
|
-
if (!isServer && !options.skipAuth && ((_d = responseData === null || responseData === void 0 ? void 0 : responseData.data) === null || _d === void 0 ? void 0 : _d.token)) {
|
|
159
|
-
(0, exports.setTokens)({
|
|
160
|
-
accessToken: responseData.data.token,
|
|
161
|
-
refreshToken: responseData.data.refreshToken,
|
|
162
150
|
});
|
|
163
|
-
}
|
|
164
|
-
return responseData;
|
|
151
|
+
});
|
|
165
152
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
153
|
+
isRefreshing = true;
|
|
154
|
+
try {
|
|
155
|
+
const newToken = await refreshTokens();
|
|
156
|
+
onTokenRefreshed(newToken);
|
|
157
|
+
return apiRequest(endpoint, {
|
|
158
|
+
...options,
|
|
159
|
+
headers: {
|
|
160
|
+
...options.headers,
|
|
161
|
+
Authorization: `Bearer ${newToken}`,
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
catch (refreshError) {
|
|
166
|
+
(0, exports.removeTokens)();
|
|
167
|
+
throw refreshError;
|
|
168
|
+
}
|
|
169
|
+
finally {
|
|
170
|
+
isRefreshing = false;
|
|
169
171
|
}
|
|
170
172
|
}
|
|
171
|
-
|
|
173
|
+
function createApiError(status, responseData) {
|
|
174
|
+
const error = new Error(typeof responseData === "object"
|
|
175
|
+
? responseData.message || "API Error"
|
|
176
|
+
: "API Error");
|
|
177
|
+
error.status = status;
|
|
178
|
+
error.data = responseData;
|
|
179
|
+
return error;
|
|
180
|
+
}
|
|
@@ -150,6 +150,12 @@ __decorate([
|
|
|
150
150
|
(0, class_validator_1.IsString)(),
|
|
151
151
|
__metadata("design:type", String)
|
|
152
152
|
], SalesAgentUpdateDTO.prototype, "name", void 0);
|
|
153
|
+
__decorate([
|
|
154
|
+
(0, class_validator_1.IsOptional)(),
|
|
155
|
+
(0, class_validator_1.IsArray)(),
|
|
156
|
+
(0, class_validator_1.IsUUID)("4", { each: true }),
|
|
157
|
+
__metadata("design:type", Array)
|
|
158
|
+
], SalesAgentUpdateDTO.prototype, "languageIds", void 0);
|
|
153
159
|
__decorate([
|
|
154
160
|
(0, class_validator_1.IsOptional)(),
|
|
155
161
|
(0, class_validator_1.IsEmail)(),
|
package/package.json
CHANGED