@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 ((!isServer && !(tokens === null || tokens === void 0 ? void 0 : tokens.refreshToken))) {
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, _b, _c, _d;
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 url = buildUrl(endpoint, options.query);
99
- const response = await fetch(url, config);
100
- const contentType = response.headers.get("content-type");
101
- const responseData = (contentType === null || contentType === void 0 ? void 0 : contentType.includes("application/json"))
102
- ? await response.json()
103
- : await response.text();
104
- if (!response.ok) {
105
- const error = new Error(typeof responseData === "object"
106
- ? responseData.message || "API Error"
107
- : "API Error");
108
- error.status = response.status;
109
- error.data = responseData;
110
- // Handle token expired error
111
- if (!isServer &&
112
- response.status === enums_1.HttpStatusCodes.UNAUTHORIZED &&
113
- ((_b = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _b === void 0 ? void 0 : _b.type) === "AUTHENTICATION_ERROR" &&
114
- ((_c = responseData === null || responseData === void 0 ? void 0 : responseData.error) === null || _c === void 0 ? void 0 : _c.message) === "Token expired" &&
115
- !options.skipAuth) {
116
- if (isRefreshing) {
117
- // Wait for token refresh if already in progress
118
- return new Promise((resolve, reject) => {
119
- subscribeTokenRefresh(async (newToken) => {
120
- try {
121
- // Retry the original request with new token
122
- const retriedResponse = await apiRequest(endpoint, {
123
- ...options,
124
- headers: {
125
- ...options.headers,
126
- Authorization: `Bearer ${newToken}`,
127
- },
128
- });
129
- resolve(retriedResponse);
130
- }
131
- catch (error) {
132
- reject(error);
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 newToken = await refreshTokens();
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 (refreshError) {
152
- isRefreshing = false;
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
- catch (error) {
167
- console.error("API Request Error:", error);
168
- return Promise.reject(error);
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
- exports.apiRequest = apiRequest;
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
+ }
@@ -34,6 +34,7 @@ export declare class SalesAgentUpdateDTO {
34
34
  designation?: Designation;
35
35
  agentIntro?: string;
36
36
  name?: string;
37
+ languageIds?: string[];
37
38
  email?: string;
38
39
  phone?: string;
39
40
  photo?: string;
@@ -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
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.4.7",
6
+ "version": "1.4.9",
7
7
  "description": "A package containing DTOs, validation classes and common modules and interfaces for Alba Cars",
8
8
  "main": "dist/index.js",
9
9
  "types": "dist/index.d.ts",