@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.
@@ -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 base = buildHeaders(this.token, {
241
+ const baseHeaders = buildHeaders(this.token, {
243
242
  "X-Organization": this.organization,
244
243
  ...(options.headers || {}),
245
244
  });
246
- const headers = new Headers(base);
247
- // โœ… Obtener token legacy
248
- const legacyToken = window.__LEGACY_TOKEN__ ||
249
- sessionStorage.getItem("legacy_token") ||
250
- null;
251
- if (legacyToken) {
252
- headers.set("Authorization", `Bearer ${legacyToken}`);
253
- if (this.debug) {
254
- console.log("๐Ÿ” Using legacy token for:", endpoint);
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
- else if (this.token) {
258
- if (this.debug) {
259
- console.log("๐Ÿ” Using v1 token for:", endpoint);
264
+ else if (this.token) {
265
+ if (this.debug) {
266
+ console.log("๐Ÿ” Using v1 token for:", endpoint);
267
+ }
260
268
  }
261
- }
262
- else {
263
- console.warn("โš ๏ธ No token available for endpoint:", endpoint);
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
- const fetchOptions = {
283
- method,
284
- headers,
285
- credentials: "include",
286
- };
291
+ let body;
287
292
  if (data && method !== "GET") {
288
- fetchOptions.body = JSON.stringify(data);
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
- // โœ… Primer intento
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 con fallback
306
- const fallbackResponse = await fetch(fallbackUrl, fetchOptions);
307
- // โœ… Manejar 204 No Content
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 {}; // Retornar objeto vacรญo
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 {}; // Retornar objeto vacรญo
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerosls/clm-sdk",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "SDK for ZeroCLM API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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 base: HeadersInit = buildHeaders(this.token, {
317
+ const baseHeaders: HeadersInit = buildHeaders(this.token, {
320
318
  "X-Organization": this.organization,
321
319
  ...(options.headers || {}),
322
320
  });
323
321
 
324
- const headers = new Headers(base);
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
- // โœ… Obtener token legacy
327
- const legacyToken =
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
- if (legacyToken) {
333
- headers.set("Authorization", `Bearer ${legacyToken}`);
334
- if (this.debug) {
335
- console.log("๐Ÿ” Using legacy token for:", endpoint);
336
- }
337
- } else if (this.token) {
338
- if (this.debug) {
339
- console.log("๐Ÿ” Using v1 token for:", endpoint);
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
- } else {
342
- console.warn("โš ๏ธ No token available for endpoint:", endpoint);
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
- const fetchOptions: RequestInit = {
365
- method,
366
- headers,
367
- credentials: "include",
368
- };
369
-
374
+ let body: string | undefined;
370
375
  if (data && method !== "GET") {
371
- fetchOptions.body = JSON.stringify(data);
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
- // โœ… Primer intento
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 con fallback
395
- const fallbackResponse = await fetch(fallbackUrl, fetchOptions);
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; // Retornar objeto vacรญo
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; // Retornar objeto vacรญo
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") {