@zerosls/clm-sdk 1.1.5 β 1.1.7
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.
- package/dist/core/api-client.d.ts +1 -0
- package/dist/core/api-client.js +68 -26
- package/dist/core/legacy-api-client.js +1 -0
- package/dist/modules/legacy/classificationtypes/classificationtypes-api.js +1 -1
- package/dist/types/sdk.d.ts +1 -0
- package/package.json +1 -1
- package/src/core/api-client.ts +84 -33
- package/src/core/legacy-api-client.ts +1 -0
- package/src/modules/legacy/classificationtypes/classificationtypes-api.ts +1 -1
- package/src/types/sdk.ts +1 -0
package/dist/core/api-client.js
CHANGED
|
@@ -6,6 +6,7 @@ export class ApiClient {
|
|
|
6
6
|
var _a, _b;
|
|
7
7
|
this.token = null;
|
|
8
8
|
this.baseUrl = config.baseUrl;
|
|
9
|
+
this.fallbackBaseUrl = config.fallbackBaseUrl || "http://216.250.117.119/ZeroServicesQA/api/v1";
|
|
9
10
|
this.organization = config.organization;
|
|
10
11
|
this.token = config.token || null;
|
|
11
12
|
this.eventEmitter = eventEmitter;
|
|
@@ -56,36 +57,34 @@ export class ApiClient {
|
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
async request(method, endpoint, data, params, options = {}) {
|
|
59
|
-
|
|
60
|
+
// β
Primer intento con baseUrl normal
|
|
61
|
+
const primaryUrl = buildUrl(this.baseUrl, endpoint, params);
|
|
60
62
|
const base = buildHeaders(this.token, {
|
|
61
63
|
"X-Organization": this.organization,
|
|
62
64
|
...(options.headers || {}),
|
|
63
65
|
});
|
|
64
66
|
const headers = new Headers(base);
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
if (this.debug) {
|
|
74
|
-
console.log("π Using legacy token for:", endpoint);
|
|
75
|
-
}
|
|
67
|
+
// β
Obtener token legacy
|
|
68
|
+
const legacyToken = window.__LEGACY_TOKEN__ ||
|
|
69
|
+
sessionStorage.getItem("legacy_token") ||
|
|
70
|
+
null;
|
|
71
|
+
if (legacyToken) {
|
|
72
|
+
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
73
|
+
if (this.debug) {
|
|
74
|
+
console.log("π Using legacy token for:", endpoint);
|
|
76
75
|
}
|
|
77
|
-
|
|
78
|
-
|
|
76
|
+
}
|
|
77
|
+
else if (this.token) {
|
|
78
|
+
if (this.debug) {
|
|
79
|
+
console.log("π Using v1 token for:", endpoint);
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
82
|
else {
|
|
82
|
-
|
|
83
|
-
headers.delete("Authorization");
|
|
84
|
-
}
|
|
83
|
+
console.warn("β οΈ No token available for endpoint:", endpoint);
|
|
85
84
|
}
|
|
86
85
|
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
87
86
|
if (useCache && method === "GET") {
|
|
88
|
-
const cacheKey = generateCacheKey(method,
|
|
87
|
+
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
89
88
|
const cachedData = this.cache.get(cacheKey);
|
|
90
89
|
if (cachedData) {
|
|
91
90
|
if (this.debug) {
|
|
@@ -95,7 +94,7 @@ export class ApiClient {
|
|
|
95
94
|
}
|
|
96
95
|
}
|
|
97
96
|
this.eventEmitter.emit("beforeRequest", {
|
|
98
|
-
url,
|
|
97
|
+
url: primaryUrl,
|
|
99
98
|
method,
|
|
100
99
|
data,
|
|
101
100
|
});
|
|
@@ -107,16 +106,58 @@ export class ApiClient {
|
|
|
107
106
|
};
|
|
108
107
|
if (data && method !== "GET") {
|
|
109
108
|
fetchOptions.body = JSON.stringify(data);
|
|
110
|
-
|
|
109
|
+
if (this.debug) {
|
|
110
|
+
console.log(`π€ ${method} Body:`, data);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (this.debug) {
|
|
114
|
+
console.log(`π ${method} ${primaryUrl}`);
|
|
115
|
+
}
|
|
116
|
+
// β
Primer intento
|
|
117
|
+
const response = await fetch(primaryUrl, fetchOptions);
|
|
118
|
+
// β
Si es 404, intentar con fallback URL
|
|
119
|
+
if (response.status === 404) {
|
|
120
|
+
console.warn(`β οΈ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
121
|
+
const fallbackUrl = buildUrl(this.fallbackBaseUrl, endpoint, params);
|
|
122
|
+
if (this.debug) {
|
|
123
|
+
console.log(`π Retry: ${method} ${fallbackUrl}`);
|
|
124
|
+
}
|
|
125
|
+
// β
Segundo intento con fallback
|
|
126
|
+
const fallbackResponse = await fetch(fallbackUrl, fetchOptions);
|
|
127
|
+
if (!fallbackResponse.ok) {
|
|
128
|
+
let errorData;
|
|
129
|
+
try {
|
|
130
|
+
errorData = await fallbackResponse.json();
|
|
131
|
+
}
|
|
132
|
+
catch (_a) {
|
|
133
|
+
errorData = { message: fallbackResponse.statusText };
|
|
134
|
+
}
|
|
135
|
+
console.error(`β Fallback tambiΓ©n fallΓ³ ${fallbackResponse.status}:`, errorData);
|
|
136
|
+
if (fallbackResponse.status === 401) {
|
|
137
|
+
this.eventEmitter.emit("authError", {
|
|
138
|
+
statusCode: 401,
|
|
139
|
+
message: errorData.message || "Authentication required",
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return errorData;
|
|
143
|
+
}
|
|
144
|
+
// β
Fallback exitoso
|
|
145
|
+
const fallbackData = await parseResponse(fallbackResponse);
|
|
146
|
+
console.log(`β
Fallback exitoso para: ${endpoint}`);
|
|
147
|
+
this.eventEmitter.emit("afterRequest", {
|
|
148
|
+
url: fallbackUrl,
|
|
149
|
+
method,
|
|
150
|
+
response: fallbackData,
|
|
151
|
+
});
|
|
152
|
+
return fallbackData;
|
|
111
153
|
}
|
|
112
|
-
|
|
113
|
-
const response = await fetch(url, fetchOptions);
|
|
154
|
+
// β
Otros errores (no 404)
|
|
114
155
|
if (!response.ok) {
|
|
115
156
|
let errorData;
|
|
116
157
|
try {
|
|
117
158
|
errorData = await response.json();
|
|
118
159
|
}
|
|
119
|
-
catch (
|
|
160
|
+
catch (_b) {
|
|
120
161
|
errorData = { message: response.statusText };
|
|
121
162
|
}
|
|
122
163
|
console.error(`β ${method} ${response.status}:`, errorData);
|
|
@@ -128,9 +169,10 @@ export class ApiClient {
|
|
|
128
169
|
}
|
|
129
170
|
return errorData;
|
|
130
171
|
}
|
|
172
|
+
// β
Respuesta exitosa del primer intento
|
|
131
173
|
const responseData = await parseResponse(response);
|
|
132
174
|
if (useCache && method === "GET") {
|
|
133
|
-
const cacheKey = generateCacheKey(method,
|
|
175
|
+
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
134
176
|
const cacheTime = options.cacheTime || undefined;
|
|
135
177
|
this.cache.set(cacheKey, responseData, cacheTime);
|
|
136
178
|
if (this.debug) {
|
|
@@ -138,7 +180,7 @@ export class ApiClient {
|
|
|
138
180
|
}
|
|
139
181
|
}
|
|
140
182
|
this.eventEmitter.emit("afterRequest", {
|
|
141
|
-
url,
|
|
183
|
+
url: primaryUrl,
|
|
142
184
|
method,
|
|
143
185
|
response: responseData,
|
|
144
186
|
});
|
|
@@ -146,7 +188,7 @@ export class ApiClient {
|
|
|
146
188
|
}
|
|
147
189
|
catch (error) {
|
|
148
190
|
this.eventEmitter.emit("requestError", {
|
|
149
|
-
url,
|
|
191
|
+
url: primaryUrl,
|
|
150
192
|
method,
|
|
151
193
|
error,
|
|
152
194
|
});
|
package/dist/types/sdk.d.ts
CHANGED
package/package.json
CHANGED
package/src/core/api-client.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// core/api-client.ts
|
|
1
2
|
import { SdkConfig, SdkEventType, SdkEvents } from "../types/sdk";
|
|
2
3
|
import { PaginatedResponse, RequestOptions } from "../types/common";
|
|
3
4
|
import { EventEmitter } from "./event-emitter";
|
|
@@ -12,6 +13,7 @@ import {
|
|
|
12
13
|
|
|
13
14
|
export class ApiClient {
|
|
14
15
|
private baseUrl: string;
|
|
16
|
+
private fallbackBaseUrl: string; // β
URL de respaldo
|
|
15
17
|
private organization: string;
|
|
16
18
|
private token: string | null = null;
|
|
17
19
|
private eventEmitter: EventEmitter;
|
|
@@ -22,6 +24,7 @@ export class ApiClient {
|
|
|
22
24
|
|
|
23
25
|
constructor(config: SdkConfig, eventEmitter: EventEmitter) {
|
|
24
26
|
this.baseUrl = config.baseUrl;
|
|
27
|
+
this.fallbackBaseUrl = config.fallbackBaseUrl || "http://216.250.117.119/ZeroServicesQA/api/v1";
|
|
25
28
|
this.organization = config.organization;
|
|
26
29
|
this.token = config.token || null;
|
|
27
30
|
this.eventEmitter = eventEmitter;
|
|
@@ -129,8 +132,9 @@ export class ApiClient {
|
|
|
129
132
|
params?: Record<string, any>,
|
|
130
133
|
options: RequestOptions = {}
|
|
131
134
|
): Promise<T> {
|
|
132
|
-
|
|
133
|
-
|
|
135
|
+
// β
Primer intento con baseUrl normal
|
|
136
|
+
const primaryUrl = buildUrl(this.baseUrl, endpoint, params);
|
|
137
|
+
|
|
134
138
|
const base: HeadersInit = buildHeaders(this.token, {
|
|
135
139
|
"X-Organization": this.organization,
|
|
136
140
|
...(options.headers || {}),
|
|
@@ -138,36 +142,28 @@ export class ApiClient {
|
|
|
138
142
|
|
|
139
143
|
const headers = new Headers(base);
|
|
140
144
|
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
(window as any).__LEGACY_TOKEN__ ||
|
|
147
|
-
sessionStorage.getItem("legacy_token") ||
|
|
148
|
-
null;
|
|
149
|
-
|
|
150
|
-
if (legacyToken) {
|
|
151
|
-
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
145
|
+
// β
Obtener token legacy
|
|
146
|
+
const legacyToken =
|
|
147
|
+
(window as any).__LEGACY_TOKEN__ ||
|
|
148
|
+
sessionStorage.getItem("legacy_token") ||
|
|
149
|
+
null;
|
|
152
150
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
console.warn(
|
|
158
|
-
"β οΈ No legacy token available for legacy endpoint:",
|
|
159
|
-
endpoint
|
|
160
|
-
);
|
|
151
|
+
if (legacyToken) {
|
|
152
|
+
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
153
|
+
if (this.debug) {
|
|
154
|
+
console.log("π Using legacy token for:", endpoint);
|
|
161
155
|
}
|
|
162
|
-
} else {
|
|
163
|
-
if (
|
|
164
|
-
|
|
156
|
+
} else if (this.token) {
|
|
157
|
+
if (this.debug) {
|
|
158
|
+
console.log("π Using v1 token for:", endpoint);
|
|
165
159
|
}
|
|
160
|
+
} else {
|
|
161
|
+
console.warn("β οΈ No token available for endpoint:", endpoint);
|
|
166
162
|
}
|
|
167
163
|
|
|
168
164
|
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
169
165
|
if (useCache && method === "GET") {
|
|
170
|
-
const cacheKey = generateCacheKey(method,
|
|
166
|
+
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
171
167
|
const cachedData = this.cache.get<T>(cacheKey);
|
|
172
168
|
if (cachedData) {
|
|
173
169
|
if (this.debug) {
|
|
@@ -178,7 +174,7 @@ export class ApiClient {
|
|
|
178
174
|
}
|
|
179
175
|
|
|
180
176
|
this.eventEmitter.emit("beforeRequest", {
|
|
181
|
-
url,
|
|
177
|
+
url: primaryUrl,
|
|
182
178
|
method,
|
|
183
179
|
data,
|
|
184
180
|
});
|
|
@@ -192,12 +188,66 @@ export class ApiClient {
|
|
|
192
188
|
|
|
193
189
|
if (data && method !== "GET") {
|
|
194
190
|
fetchOptions.body = JSON.stringify(data);
|
|
195
|
-
|
|
191
|
+
if (this.debug) {
|
|
192
|
+
console.log(`π€ ${method} Body:`, data);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (this.debug) {
|
|
197
|
+
console.log(`π ${method} ${primaryUrl}`);
|
|
196
198
|
}
|
|
197
199
|
|
|
198
|
-
|
|
199
|
-
const response = await fetch(
|
|
200
|
+
// β
Primer intento
|
|
201
|
+
const response = await fetch(primaryUrl, fetchOptions);
|
|
202
|
+
|
|
203
|
+
// β
Si es 404, intentar con fallback URL
|
|
204
|
+
if (response.status === 404) {
|
|
205
|
+
console.warn(`β οΈ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
206
|
+
|
|
207
|
+
const fallbackUrl = buildUrl(this.fallbackBaseUrl, endpoint, params);
|
|
208
|
+
|
|
209
|
+
if (this.debug) {
|
|
210
|
+
console.log(`π Retry: ${method} ${fallbackUrl}`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// β
Segundo intento con fallback
|
|
214
|
+
const fallbackResponse = await fetch(fallbackUrl, fetchOptions);
|
|
215
|
+
|
|
216
|
+
if (!fallbackResponse.ok) {
|
|
217
|
+
let errorData;
|
|
218
|
+
try {
|
|
219
|
+
errorData = await fallbackResponse.json();
|
|
220
|
+
} catch {
|
|
221
|
+
errorData = { message: fallbackResponse.statusText };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
console.error(`β Fallback tambiΓ©n fallΓ³ ${fallbackResponse.status}:`, errorData);
|
|
225
|
+
|
|
226
|
+
if (fallbackResponse.status === 401) {
|
|
227
|
+
this.eventEmitter.emit("authError", {
|
|
228
|
+
statusCode: 401,
|
|
229
|
+
message: errorData.message || "Authentication required",
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return errorData as T;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// β
Fallback exitoso
|
|
237
|
+
const fallbackData = await parseResponse<T>(fallbackResponse);
|
|
238
|
+
|
|
239
|
+
console.log(`β
Fallback exitoso para: ${endpoint}`);
|
|
240
|
+
|
|
241
|
+
this.eventEmitter.emit("afterRequest", {
|
|
242
|
+
url: fallbackUrl,
|
|
243
|
+
method,
|
|
244
|
+
response: fallbackData,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
return fallbackData;
|
|
248
|
+
}
|
|
200
249
|
|
|
250
|
+
// β
Otros errores (no 404)
|
|
201
251
|
if (!response.ok) {
|
|
202
252
|
let errorData;
|
|
203
253
|
try {
|
|
@@ -218,10 +268,11 @@ export class ApiClient {
|
|
|
218
268
|
return errorData as T;
|
|
219
269
|
}
|
|
220
270
|
|
|
271
|
+
// β
Respuesta exitosa del primer intento
|
|
221
272
|
const responseData = await parseResponse<T>(response);
|
|
222
273
|
|
|
223
274
|
if (useCache && method === "GET") {
|
|
224
|
-
const cacheKey = generateCacheKey(method,
|
|
275
|
+
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
225
276
|
const cacheTime = options.cacheTime || undefined;
|
|
226
277
|
this.cache.set(cacheKey, responseData, cacheTime);
|
|
227
278
|
|
|
@@ -231,7 +282,7 @@ export class ApiClient {
|
|
|
231
282
|
}
|
|
232
283
|
|
|
233
284
|
this.eventEmitter.emit("afterRequest", {
|
|
234
|
-
url,
|
|
285
|
+
url: primaryUrl,
|
|
235
286
|
method,
|
|
236
287
|
response: responseData,
|
|
237
288
|
});
|
|
@@ -239,7 +290,7 @@ export class ApiClient {
|
|
|
239
290
|
return responseData;
|
|
240
291
|
} catch (error) {
|
|
241
292
|
this.eventEmitter.emit("requestError", {
|
|
242
|
-
url,
|
|
293
|
+
url: primaryUrl,
|
|
243
294
|
method,
|
|
244
295
|
error,
|
|
245
296
|
});
|
|
@@ -253,4 +304,4 @@ export class ApiClient {
|
|
|
253
304
|
});
|
|
254
305
|
}
|
|
255
306
|
}
|
|
256
|
-
}
|
|
307
|
+
}
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
|
|
11
11
|
export class ClassificationTypesApi {
|
|
12
12
|
private apiClient: ApiClient;
|
|
13
|
-
private readonly basePath = "/
|
|
13
|
+
private readonly basePath = "/catalog/clasificationtype";
|
|
14
14
|
|
|
15
15
|
constructor(apiClient: ApiClient) {
|
|
16
16
|
this.apiClient = apiClient;
|