@zerosls/clm-sdk 1.1.8 โ 1.1.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.
- package/dist/core/api-client.js +54 -37
- package/package.json +1 -1
- package/src/core/api-client.ts +59 -39
package/dist/core/api-client.js
CHANGED
|
@@ -236,32 +236,41 @@ export class ApiClient {
|
|
|
236
236
|
});
|
|
237
237
|
}
|
|
238
238
|
}*/
|
|
239
|
-
// core/api-client.ts
|
|
240
239
|
async request(method, endpoint, data, params, options = {}) {
|
|
241
240
|
const primaryUrl = buildUrl(this.baseUrl, endpoint, params);
|
|
242
|
-
const
|
|
241
|
+
const baseHeaders = buildHeaders(this.token, {
|
|
243
242
|
"X-Organization": this.organization,
|
|
244
243
|
...(options.headers || {}),
|
|
245
244
|
});
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
245
|
+
// โ
Funciรณn helper para debug de headers
|
|
246
|
+
const headersToObject = (headers) => {
|
|
247
|
+
const obj = {};
|
|
248
|
+
headers.forEach((value, key) => {
|
|
249
|
+
obj[key] = value;
|
|
250
|
+
});
|
|
251
|
+
return obj;
|
|
252
|
+
};
|
|
253
|
+
const createHeaders = () => {
|
|
254
|
+
const headers = new Headers(baseHeaders);
|
|
255
|
+
const legacyToken = window.__LEGACY_TOKEN__ ||
|
|
256
|
+
sessionStorage.getItem("legacy_token") ||
|
|
257
|
+
null;
|
|
258
|
+
if (legacyToken) {
|
|
259
|
+
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
260
|
+
if (this.debug) {
|
|
261
|
+
console.log("๐ Using legacy token for:", endpoint);
|
|
262
|
+
}
|
|
255
263
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
264
|
+
else if (this.token) {
|
|
265
|
+
if (this.debug) {
|
|
266
|
+
console.log("๐ Using v1 token for:", endpoint);
|
|
267
|
+
}
|
|
260
268
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
269
|
+
else {
|
|
270
|
+
console.warn("โ ๏ธ No token available for endpoint:", endpoint);
|
|
271
|
+
}
|
|
272
|
+
return headers;
|
|
273
|
+
};
|
|
265
274
|
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
266
275
|
if (useCache && method === "GET") {
|
|
267
276
|
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
@@ -279,35 +288,47 @@ export class ApiClient {
|
|
|
279
288
|
data,
|
|
280
289
|
});
|
|
281
290
|
try {
|
|
282
|
-
|
|
283
|
-
method,
|
|
284
|
-
headers,
|
|
285
|
-
credentials: "include",
|
|
286
|
-
};
|
|
291
|
+
let body;
|
|
287
292
|
if (data && method !== "GET") {
|
|
288
|
-
|
|
293
|
+
body = JSON.stringify(data);
|
|
289
294
|
if (this.debug) {
|
|
290
295
|
console.log(`๐ค ${method} Body:`, data);
|
|
291
296
|
}
|
|
292
297
|
}
|
|
298
|
+
// โ
Primer intento
|
|
299
|
+
const headers1 = createHeaders();
|
|
300
|
+
const fetchOptions1 = {
|
|
301
|
+
method,
|
|
302
|
+
headers: headers1,
|
|
303
|
+
credentials: "include",
|
|
304
|
+
...(body && { body }),
|
|
305
|
+
};
|
|
293
306
|
if (this.debug) {
|
|
294
307
|
console.log(`๐ ${method} ${primaryUrl}`);
|
|
308
|
+
console.log("๐ Headers:", headersToObject(headers1)); // โ
CORREGIDO
|
|
295
309
|
}
|
|
296
|
-
|
|
297
|
-
const response = await fetch(primaryUrl, fetchOptions);
|
|
298
|
-
// โ
Si es 404, intentar con fallback URL
|
|
310
|
+
const response = await fetch(primaryUrl, fetchOptions1);
|
|
299
311
|
if (response.status === 404) {
|
|
300
312
|
console.warn(`โ ๏ธ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
301
313
|
const fallbackUrl = buildUrl(this.fallbackBaseUrl, endpoint, params);
|
|
302
314
|
if (this.debug) {
|
|
303
315
|
console.log(`๐ Retry: ${method} ${fallbackUrl}`);
|
|
304
316
|
}
|
|
305
|
-
// โ
Segundo intento
|
|
306
|
-
const
|
|
307
|
-
|
|
317
|
+
// โ
Segundo intento
|
|
318
|
+
const headers2 = createHeaders();
|
|
319
|
+
const fetchOptions2 = {
|
|
320
|
+
method,
|
|
321
|
+
headers: headers2,
|
|
322
|
+
credentials: "include",
|
|
323
|
+
...(body && { body }),
|
|
324
|
+
};
|
|
325
|
+
if (this.debug) {
|
|
326
|
+
console.log("๐ Fallback Headers:", headersToObject(headers2)); // โ
CORREGIDO
|
|
327
|
+
}
|
|
328
|
+
const fallbackResponse = await fetch(fallbackUrl, fetchOptions2);
|
|
308
329
|
if (fallbackResponse.status === 204) {
|
|
309
330
|
console.log(`โ
Fallback exitoso (204 No Content): ${endpoint}`);
|
|
310
|
-
return {};
|
|
331
|
+
return {};
|
|
311
332
|
}
|
|
312
333
|
if (!fallbackResponse.ok) {
|
|
313
334
|
let errorData;
|
|
@@ -326,7 +347,6 @@ export class ApiClient {
|
|
|
326
347
|
}
|
|
327
348
|
return errorData;
|
|
328
349
|
}
|
|
329
|
-
// โ
Fallback exitoso con contenido
|
|
330
350
|
const fallbackData = await parseResponse(fallbackResponse);
|
|
331
351
|
console.log(`โ
Fallback exitoso: ${endpoint}`);
|
|
332
352
|
this.eventEmitter.emit("afterRequest", {
|
|
@@ -336,12 +356,10 @@ export class ApiClient {
|
|
|
336
356
|
});
|
|
337
357
|
return fallbackData;
|
|
338
358
|
}
|
|
339
|
-
// โ
Manejar 204 No Content en primer intento
|
|
340
359
|
if (response.status === 204) {
|
|
341
360
|
console.log(`โ
Request exitoso (204 No Content): ${endpoint}`);
|
|
342
|
-
return {};
|
|
361
|
+
return {};
|
|
343
362
|
}
|
|
344
|
-
// โ
Otros errores (no 404)
|
|
345
363
|
if (!response.ok) {
|
|
346
364
|
let errorData;
|
|
347
365
|
try {
|
|
@@ -359,7 +377,6 @@ export class ApiClient {
|
|
|
359
377
|
}
|
|
360
378
|
return errorData;
|
|
361
379
|
}
|
|
362
|
-
// โ
Respuesta exitosa del primer intento
|
|
363
380
|
const responseData = await parseResponse(response);
|
|
364
381
|
if (useCache && method === "GET") {
|
|
365
382
|
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
package/package.json
CHANGED
package/src/core/api-client.ts
CHANGED
|
@@ -305,8 +305,6 @@ export class ApiClient {
|
|
|
305
305
|
});
|
|
306
306
|
}
|
|
307
307
|
}*/
|
|
308
|
-
|
|
309
|
-
// core/api-client.ts
|
|
310
308
|
private async request<T>(
|
|
311
309
|
method: string,
|
|
312
310
|
endpoint: string,
|
|
@@ -316,31 +314,43 @@ export class ApiClient {
|
|
|
316
314
|
): Promise<T> {
|
|
317
315
|
const primaryUrl = buildUrl(this.baseUrl, endpoint, params);
|
|
318
316
|
|
|
319
|
-
const
|
|
317
|
+
const baseHeaders: HeadersInit = buildHeaders(this.token, {
|
|
320
318
|
"X-Organization": this.organization,
|
|
321
319
|
...(options.headers || {}),
|
|
322
320
|
});
|
|
323
321
|
|
|
324
|
-
|
|
322
|
+
// โ
Funciรณn helper para debug de headers
|
|
323
|
+
const headersToObject = (headers: Headers): Record<string, string> => {
|
|
324
|
+
const obj: Record<string, string> = {};
|
|
325
|
+
headers.forEach((value, key) => {
|
|
326
|
+
obj[key] = value;
|
|
327
|
+
});
|
|
328
|
+
return obj;
|
|
329
|
+
};
|
|
325
330
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
(window as any).__LEGACY_TOKEN__ ||
|
|
329
|
-
sessionStorage.getItem("legacy_token") ||
|
|
330
|
-
null;
|
|
331
|
+
const createHeaders = (): Headers => {
|
|
332
|
+
const headers = new Headers(baseHeaders);
|
|
331
333
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
334
|
+
const legacyToken =
|
|
335
|
+
(window as any).__LEGACY_TOKEN__ ||
|
|
336
|
+
sessionStorage.getItem("legacy_token") ||
|
|
337
|
+
null;
|
|
338
|
+
|
|
339
|
+
if (legacyToken) {
|
|
340
|
+
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
341
|
+
if (this.debug) {
|
|
342
|
+
console.log("๐ Using legacy token for:", endpoint);
|
|
343
|
+
}
|
|
344
|
+
} else if (this.token) {
|
|
345
|
+
if (this.debug) {
|
|
346
|
+
console.log("๐ Using v1 token for:", endpoint);
|
|
347
|
+
}
|
|
348
|
+
} else {
|
|
349
|
+
console.warn("โ ๏ธ No token available for endpoint:", endpoint);
|
|
340
350
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
351
|
+
|
|
352
|
+
return headers;
|
|
353
|
+
};
|
|
344
354
|
|
|
345
355
|
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
346
356
|
if (useCache && method === "GET") {
|
|
@@ -361,27 +371,30 @@ export class ApiClient {
|
|
|
361
371
|
});
|
|
362
372
|
|
|
363
373
|
try {
|
|
364
|
-
|
|
365
|
-
method,
|
|
366
|
-
headers,
|
|
367
|
-
credentials: "include",
|
|
368
|
-
};
|
|
369
|
-
|
|
374
|
+
let body: string | undefined;
|
|
370
375
|
if (data && method !== "GET") {
|
|
371
|
-
|
|
376
|
+
body = JSON.stringify(data);
|
|
372
377
|
if (this.debug) {
|
|
373
378
|
console.log(`๐ค ${method} Body:`, data);
|
|
374
379
|
}
|
|
375
380
|
}
|
|
376
381
|
|
|
382
|
+
// โ
Primer intento
|
|
383
|
+
const headers1 = createHeaders();
|
|
384
|
+
const fetchOptions1: RequestInit = {
|
|
385
|
+
method,
|
|
386
|
+
headers: headers1,
|
|
387
|
+
credentials: "include",
|
|
388
|
+
...(body && { body }),
|
|
389
|
+
};
|
|
390
|
+
|
|
377
391
|
if (this.debug) {
|
|
378
392
|
console.log(`๐ ${method} ${primaryUrl}`);
|
|
393
|
+
console.log("๐ Headers:", headersToObject(headers1)); // โ
CORREGIDO
|
|
379
394
|
}
|
|
380
395
|
|
|
381
|
-
|
|
382
|
-
const response = await fetch(primaryUrl, fetchOptions);
|
|
396
|
+
const response = await fetch(primaryUrl, fetchOptions1);
|
|
383
397
|
|
|
384
|
-
// โ
Si es 404, intentar con fallback URL
|
|
385
398
|
if (response.status === 404) {
|
|
386
399
|
console.warn(`โ ๏ธ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
387
400
|
|
|
@@ -391,13 +404,24 @@ export class ApiClient {
|
|
|
391
404
|
console.log(`๐ Retry: ${method} ${fallbackUrl}`);
|
|
392
405
|
}
|
|
393
406
|
|
|
394
|
-
// โ
Segundo intento
|
|
395
|
-
const
|
|
407
|
+
// โ
Segundo intento
|
|
408
|
+
const headers2 = createHeaders();
|
|
409
|
+
const fetchOptions2: RequestInit = {
|
|
410
|
+
method,
|
|
411
|
+
headers: headers2,
|
|
412
|
+
credentials: "include",
|
|
413
|
+
...(body && { body }),
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
if (this.debug) {
|
|
417
|
+
console.log("๐ Fallback Headers:", headersToObject(headers2)); // โ
CORREGIDO
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const fallbackResponse = await fetch(fallbackUrl, fetchOptions2);
|
|
396
421
|
|
|
397
|
-
// โ
Manejar 204 No Content
|
|
398
422
|
if (fallbackResponse.status === 204) {
|
|
399
423
|
console.log(`โ
Fallback exitoso (204 No Content): ${endpoint}`);
|
|
400
|
-
return {} as T;
|
|
424
|
+
return {} as T;
|
|
401
425
|
}
|
|
402
426
|
|
|
403
427
|
if (!fallbackResponse.ok) {
|
|
@@ -423,7 +447,6 @@ export class ApiClient {
|
|
|
423
447
|
return errorData as T;
|
|
424
448
|
}
|
|
425
449
|
|
|
426
|
-
// โ
Fallback exitoso con contenido
|
|
427
450
|
const fallbackData = await parseResponse<T>(fallbackResponse);
|
|
428
451
|
|
|
429
452
|
console.log(`โ
Fallback exitoso: ${endpoint}`);
|
|
@@ -437,13 +460,11 @@ export class ApiClient {
|
|
|
437
460
|
return fallbackData;
|
|
438
461
|
}
|
|
439
462
|
|
|
440
|
-
// โ
Manejar 204 No Content en primer intento
|
|
441
463
|
if (response.status === 204) {
|
|
442
464
|
console.log(`โ
Request exitoso (204 No Content): ${endpoint}`);
|
|
443
|
-
return {} as T;
|
|
465
|
+
return {} as T;
|
|
444
466
|
}
|
|
445
467
|
|
|
446
|
-
// โ
Otros errores (no 404)
|
|
447
468
|
if (!response.ok) {
|
|
448
469
|
let errorData;
|
|
449
470
|
try {
|
|
@@ -464,7 +485,6 @@ export class ApiClient {
|
|
|
464
485
|
return errorData as T;
|
|
465
486
|
}
|
|
466
487
|
|
|
467
|
-
// โ
Respuesta exitosa del primer intento
|
|
468
488
|
const responseData = await parseResponse<T>(response);
|
|
469
489
|
|
|
470
490
|
if (useCache && method === "GET") {
|