@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.
@@ -3,6 +3,7 @@ import { RequestOptions } from "../types/common";
3
3
  import { EventEmitter } from "./event-emitter";
4
4
  export declare class ApiClient {
5
5
  private baseUrl;
6
+ private fallbackBaseUrl;
6
7
  private organization;
7
8
  private token;
8
9
  private eventEmitter;
@@ -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
- const url = buildUrl(this.baseUrl, endpoint, params);
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
- const legacyPattern = /(^|\/)legacy(\/|$)/i;
66
- const isLegacyEndpoint = legacyPattern.test(endpoint);
67
- if (isLegacyEndpoint) {
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);
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
- else {
78
- console.warn("⚠️ No legacy token available for legacy endpoint:", endpoint);
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
- if (!this.token) {
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, url, data);
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
- console.log(`πŸ“€ ${method} Body:`, data);
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
- console.log(`🌐 ${method} ${url}`, fetchOptions);
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 (_a) {
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, url, data);
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
  });
@@ -8,6 +8,7 @@ export class LegacyApiClient {
8
8
  const response = await fetch(url, {
9
9
  method: 'POST',
10
10
  headers: { 'Content-Type': 'application/json' },
11
+ credentials: 'include',
11
12
  body: JSON.stringify({ userName, password }),
12
13
  });
13
14
  if (!response.ok) {
@@ -1,6 +1,6 @@
1
1
  export class ClassificationTypesApi {
2
2
  constructor(apiClient) {
3
- this.basePath = "/legacy/catalog/clasificationtype";
3
+ this.basePath = "/catalog/clasificationtype";
4
4
  this.apiClient = apiClient;
5
5
  }
6
6
  async getClassificationTypes(options) {
@@ -1,5 +1,6 @@
1
1
  export interface SdkConfig {
2
2
  baseUrl: string;
3
+ fallbackBaseUrl?: string;
3
4
  organization: string;
4
5
  token?: string | null;
5
6
  cache?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerosls/clm-sdk",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "SDK for ZeroCLM API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -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
- const url = buildUrl(this.baseUrl, endpoint, params);
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
- const legacyPattern = /(^|\/)legacy(\/|$)/i;
142
- const isLegacyEndpoint = legacyPattern.test(endpoint);
143
-
144
- if (isLegacyEndpoint) {
145
- const legacyToken =
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
- if (this.debug) {
154
- console.log("πŸ” Using legacy token for:", endpoint);
155
- }
156
- } else {
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 (!this.token) {
164
- headers.delete("Authorization");
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, url, data);
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
- console.log(`πŸ“€ ${method} Body:`, data);
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
- console.log(`🌐 ${method} ${url}`, fetchOptions);
199
- const response = await fetch(url, fetchOptions);
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, url, data);
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
+ }
@@ -33,6 +33,7 @@ export class LegacyApiClient {
33
33
  const response = await fetch(url, {
34
34
  method: 'POST',
35
35
  headers: { 'Content-Type': 'application/json' },
36
+ credentials: 'include',
36
37
  body: JSON.stringify({ userName, password }),
37
38
  });
38
39
 
@@ -10,7 +10,7 @@ import {
10
10
 
11
11
  export class ClassificationTypesApi {
12
12
  private apiClient: ApiClient;
13
- private readonly basePath = "/legacy/catalog/clasificationtype";
13
+ private readonly basePath = "/catalog/clasificationtype";
14
14
 
15
15
  constructor(apiClient: ApiClient) {
16
16
  this.apiClient = apiClient;
package/src/types/sdk.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export interface SdkConfig {
2
2
  baseUrl: string;
3
+ fallbackBaseUrl?: string;
3
4
  organization: string;
4
5
  token?: string | null;
5
6
  cache?: {