@zerosls/clm-sdk 1.1.9 → 2.0.0
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/config/config.js +5 -7
- package/dist/core/api-client.d.ts +0 -1
- package/dist/core/api-client.js +28 -283
- package/dist/index.d.ts +0 -4
- package/dist/index.js +0 -9
- package/dist/modules/legacy/classificationtypes/classificationtypes-api.js +1 -1
- package/dist/modules/v1/auth/auth-api.d.ts +3 -5
- package/dist/modules/v1/auth/auth-api.js +46 -35
- package/dist/types/sdk.d.ts +0 -1
- package/package.json +1 -1
- package/src/config/config.ts +5 -5
- package/src/core/api-client.ts +27 -301
- package/src/index.ts +0 -14
- package/src/modules/legacy/classificationtypes/classificationtypes-api.ts +1 -1
- package/src/modules/v1/auth/auth-api.ts +66 -42
- package/src/types/sdk.ts +0 -1
- package/dist/core/legacy-api-client.d.ts +0 -26
- package/dist/core/legacy-api-client.js +0 -35
- package/src/core/legacy-api-client.ts +0 -66
package/dist/config/config.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
function normalizeUrl(url) {
|
|
2
2
|
return url.trim().replace(/\/+$/, "");
|
|
3
3
|
}
|
|
4
|
-
function resolveDefaultBaseUrl()
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return normalizeUrl(String(fromWindow));
|
|
9
|
-
}
|
|
4
|
+
//function resolveDefaultBaseUrl(){
|
|
5
|
+
// const fromWindow = (globalThis as any)?.SDK_CONFIG?.API_BASE_URL;
|
|
6
|
+
// if (fromWindow) return normalizeUrl(String(fromWindow));
|
|
7
|
+
//}
|
|
10
8
|
export const DEFAULT_CONFIG = {
|
|
11
|
-
baseUrl: resolveDefaultBaseUrl(),
|
|
9
|
+
baseUrl: "https://zero-api-qa.onrender.com/api/v1", //resolveDefaultBaseUrl(),
|
|
12
10
|
organization: "default-org",
|
|
13
11
|
cache: { enabled: true, ttl: 60000 },
|
|
14
12
|
debug: true,
|
package/dist/core/api-client.js
CHANGED
|
@@ -6,8 +6,6 @@ export class ApiClient {
|
|
|
6
6
|
var _a, _b;
|
|
7
7
|
this.token = null;
|
|
8
8
|
this.baseUrl = config.baseUrl;
|
|
9
|
-
this.fallbackBaseUrl =
|
|
10
|
-
config.fallbackBaseUrl || "http://216.250.117.119/ZeroServicesQA/api/v1";
|
|
11
9
|
this.organization = config.organization;
|
|
12
10
|
this.token = config.token || null;
|
|
13
11
|
this.eventEmitter = eventEmitter;
|
|
@@ -57,223 +55,31 @@ export class ApiClient {
|
|
|
57
55
|
}
|
|
58
56
|
}
|
|
59
57
|
}
|
|
60
|
-
/*private async request<T>(
|
|
61
|
-
method: string,
|
|
62
|
-
endpoint: string,
|
|
63
|
-
data?: any,
|
|
64
|
-
params?: Record<string, any>,
|
|
65
|
-
options: RequestOptions = {}
|
|
66
|
-
): Promise<T> {
|
|
67
|
-
// ✅ Primer intento con baseUrl normal
|
|
68
|
-
const primaryUrl = buildUrl(this.baseUrl, endpoint, params);
|
|
69
|
-
|
|
70
|
-
const base: HeadersInit = buildHeaders(this.token, {
|
|
71
|
-
"X-Organization": this.organization,
|
|
72
|
-
...(options.headers || {}),
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const headers = new Headers(base);
|
|
76
|
-
|
|
77
|
-
// ✅ Obtener token legacy
|
|
78
|
-
const legacyToken =
|
|
79
|
-
(window as any).__LEGACY_TOKEN__ ||
|
|
80
|
-
sessionStorage.getItem("legacy_token") ||
|
|
81
|
-
null;
|
|
82
|
-
|
|
83
|
-
if (legacyToken) {
|
|
84
|
-
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
85
|
-
if (this.debug) {
|
|
86
|
-
console.log("🔐 Using legacy token for:", endpoint);
|
|
87
|
-
}
|
|
88
|
-
} else if (this.token) {
|
|
89
|
-
if (this.debug) {
|
|
90
|
-
console.log("🔐 Using v1 token for:", endpoint);
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
console.warn("⚠️ No token available for endpoint:", endpoint);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
97
|
-
if (useCache && method === "GET") {
|
|
98
|
-
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
99
|
-
const cachedData = this.cache.get<T>(cacheKey);
|
|
100
|
-
if (cachedData) {
|
|
101
|
-
if (this.debug) {
|
|
102
|
-
console.log(`[SDK-Cache] Hit: ${cacheKey}`);
|
|
103
|
-
}
|
|
104
|
-
return cachedData;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
this.eventEmitter.emit("beforeRequest", {
|
|
109
|
-
url: primaryUrl,
|
|
110
|
-
method,
|
|
111
|
-
data,
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
const fetchOptions: RequestInit = {
|
|
116
|
-
method,
|
|
117
|
-
headers,
|
|
118
|
-
credentials: "include",
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
if (data && method !== "GET") {
|
|
122
|
-
fetchOptions.body = JSON.stringify(data);
|
|
123
|
-
if (this.debug) {
|
|
124
|
-
console.log(`📤 ${method} Body:`, data);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (this.debug) {
|
|
129
|
-
console.log(`🌐 ${method} ${primaryUrl}`);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// ✅ Primer intento
|
|
133
|
-
const response = await fetch(primaryUrl, fetchOptions);
|
|
134
|
-
|
|
135
|
-
// ✅ Si es 404, intentar con fallback URL
|
|
136
|
-
if (response.status === 404) {
|
|
137
|
-
console.warn(`⚠️ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
138
|
-
|
|
139
|
-
const fallbackUrl = buildUrl(this.fallbackBaseUrl, endpoint, params);
|
|
140
|
-
|
|
141
|
-
if (this.debug) {
|
|
142
|
-
console.log(`🔄 Retry: ${method} ${fallbackUrl}`);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// ✅ Segundo intento con fallback
|
|
146
|
-
const fallbackResponse = await fetch(fallbackUrl, fetchOptions);
|
|
147
|
-
|
|
148
|
-
if (!fallbackResponse.ok) {
|
|
149
|
-
let errorData;
|
|
150
|
-
try {
|
|
151
|
-
errorData = await fallbackResponse.json();
|
|
152
|
-
} catch {
|
|
153
|
-
errorData = { message: fallbackResponse.statusText };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
console.error(`❌ Fallback también falló ${fallbackResponse.status}:`, errorData);
|
|
157
|
-
|
|
158
|
-
if (fallbackResponse.status === 401) {
|
|
159
|
-
this.eventEmitter.emit("authError", {
|
|
160
|
-
statusCode: 401,
|
|
161
|
-
message: errorData.message || "Authentication required",
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return errorData as T;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// ✅ Fallback exitoso
|
|
169
|
-
const fallbackData = await parseResponse<T>(fallbackResponse);
|
|
170
|
-
|
|
171
|
-
console.log(`✅ Fallback exitoso para: ${endpoint}`);
|
|
172
|
-
|
|
173
|
-
this.eventEmitter.emit("afterRequest", {
|
|
174
|
-
url: fallbackUrl,
|
|
175
|
-
method,
|
|
176
|
-
response: fallbackData,
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
return fallbackData;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// ✅ Otros errores (no 404)
|
|
183
|
-
if (!response.ok) {
|
|
184
|
-
let errorData;
|
|
185
|
-
try {
|
|
186
|
-
errorData = await response.json();
|
|
187
|
-
} catch {
|
|
188
|
-
errorData = { message: response.statusText };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
console.error(`❌ ${method} ${response.status}:`, errorData);
|
|
192
|
-
|
|
193
|
-
if (response.status === 401) {
|
|
194
|
-
this.eventEmitter.emit("authError", {
|
|
195
|
-
statusCode: 401,
|
|
196
|
-
message: errorData.message || "Authentication required",
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
return errorData as T;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// ✅ Respuesta exitosa del primer intento
|
|
204
|
-
const responseData = await parseResponse<T>(response);
|
|
205
|
-
|
|
206
|
-
if (useCache && method === "GET") {
|
|
207
|
-
const cacheKey = generateCacheKey(method, primaryUrl, data);
|
|
208
|
-
const cacheTime = options.cacheTime || undefined;
|
|
209
|
-
this.cache.set(cacheKey, responseData, cacheTime);
|
|
210
|
-
|
|
211
|
-
if (this.debug) {
|
|
212
|
-
console.log(`[SDK-Cache] Set: ${cacheKey}`);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
this.eventEmitter.emit("afterRequest", {
|
|
217
|
-
url: primaryUrl,
|
|
218
|
-
method,
|
|
219
|
-
response: responseData,
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
return responseData;
|
|
223
|
-
} catch (error) {
|
|
224
|
-
this.eventEmitter.emit("requestError", {
|
|
225
|
-
url: primaryUrl,
|
|
226
|
-
method,
|
|
227
|
-
error,
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
if (error instanceof ApiError) {
|
|
231
|
-
throw error;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
throw new ApiError((error as Error).message || "Network error", 0, {
|
|
235
|
-
originalError: error,
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
}*/
|
|
239
58
|
async request(method, endpoint, data, params, options = {}) {
|
|
240
|
-
const
|
|
241
|
-
const
|
|
59
|
+
const url = buildUrl(this.baseUrl, endpoint, params);
|
|
60
|
+
const base = buildHeaders(this.token, {
|
|
242
61
|
"X-Organization": this.organization,
|
|
243
62
|
...(options.headers || {}),
|
|
244
63
|
});
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const createHeaders = () => {
|
|
254
|
-
const headers = new Headers(baseHeaders);
|
|
255
|
-
const legacyToken = window.__LEGACY_TOKEN__ ||
|
|
256
|
-
sessionStorage.getItem("legacy_token") ||
|
|
64
|
+
const headers = new Headers(base);
|
|
65
|
+
const legacyPattern = /(^|\/)legacy(\/|$)/i;
|
|
66
|
+
const isLegacyEndpoint = legacyPattern.test(endpoint);
|
|
67
|
+
const isLegacyLogin = /(^|\/)legacy\/login$/i.test(endpoint);
|
|
68
|
+
if (isLegacyEndpoint && !isLegacyLogin) {
|
|
69
|
+
const legacyToken = (typeof window !== "undefined" && window.__LEGACY_TOKEN__) ||
|
|
70
|
+
(typeof sessionStorage !== "undefined" &&
|
|
71
|
+
sessionStorage.getItem("legacy_token")) ||
|
|
257
72
|
null;
|
|
258
73
|
if (legacyToken) {
|
|
259
74
|
headers.set("Authorization", `Bearer ${legacyToken}`);
|
|
260
|
-
if (this.debug) {
|
|
261
|
-
console.log("🔐 Using legacy token for:", endpoint);
|
|
262
|
-
}
|
|
263
75
|
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
console.warn("⚠️ No token available for endpoint:", endpoint);
|
|
271
|
-
}
|
|
272
|
-
return headers;
|
|
273
|
-
};
|
|
76
|
+
}
|
|
77
|
+
if (!isLegacyEndpoint && !this.token) {
|
|
78
|
+
headers.delete("Authorization");
|
|
79
|
+
}
|
|
274
80
|
const useCache = this.cacheEnabled && options.useCache !== false;
|
|
275
81
|
if (useCache && method === "GET") {
|
|
276
|
-
const cacheKey = generateCacheKey(method,
|
|
82
|
+
const cacheKey = generateCacheKey(method, url, data);
|
|
277
83
|
const cachedData = this.cache.get(cacheKey);
|
|
278
84
|
if (cachedData) {
|
|
279
85
|
if (this.debug) {
|
|
@@ -283,89 +89,28 @@ export class ApiClient {
|
|
|
283
89
|
}
|
|
284
90
|
}
|
|
285
91
|
this.eventEmitter.emit("beforeRequest", {
|
|
286
|
-
url
|
|
92
|
+
url,
|
|
287
93
|
method,
|
|
288
94
|
data,
|
|
289
95
|
});
|
|
290
96
|
try {
|
|
291
|
-
|
|
292
|
-
if (data && method !== "GET") {
|
|
293
|
-
body = JSON.stringify(data);
|
|
294
|
-
if (this.debug) {
|
|
295
|
-
console.log(`📤 ${method} Body:`, data);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
// ✅ Primer intento
|
|
299
|
-
const headers1 = createHeaders();
|
|
300
|
-
const fetchOptions1 = {
|
|
97
|
+
const fetchOptions = {
|
|
301
98
|
method,
|
|
302
|
-
headers
|
|
303
|
-
credentials:
|
|
304
|
-
...(body && { body }),
|
|
99
|
+
headers,
|
|
100
|
+
credentials: this.credentials,
|
|
305
101
|
};
|
|
306
|
-
if (
|
|
307
|
-
|
|
308
|
-
console.log(
|
|
309
|
-
}
|
|
310
|
-
const response = await fetch(primaryUrl, fetchOptions1);
|
|
311
|
-
if (response.status === 404) {
|
|
312
|
-
console.warn(`⚠️ 404 en ${primaryUrl}, intentando con fallback...`);
|
|
313
|
-
const fallbackUrl = buildUrl(this.fallbackBaseUrl, endpoint, params);
|
|
314
|
-
if (this.debug) {
|
|
315
|
-
console.log(`🔄 Retry: ${method} ${fallbackUrl}`);
|
|
316
|
-
}
|
|
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);
|
|
329
|
-
if (fallbackResponse.status === 204) {
|
|
330
|
-
console.log(`✅ Fallback exitoso (204 No Content): ${endpoint}`);
|
|
331
|
-
return {};
|
|
332
|
-
}
|
|
333
|
-
if (!fallbackResponse.ok) {
|
|
334
|
-
let errorData;
|
|
335
|
-
try {
|
|
336
|
-
errorData = await fallbackResponse.json();
|
|
337
|
-
}
|
|
338
|
-
catch (_a) {
|
|
339
|
-
errorData = { message: fallbackResponse.statusText };
|
|
340
|
-
}
|
|
341
|
-
console.error(`❌ Fallback falló ${fallbackResponse.status}:`, errorData);
|
|
342
|
-
if (fallbackResponse.status === 401) {
|
|
343
|
-
this.eventEmitter.emit("authError", {
|
|
344
|
-
statusCode: 401,
|
|
345
|
-
message: errorData.message || "Authentication required",
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
return errorData;
|
|
349
|
-
}
|
|
350
|
-
const fallbackData = await parseResponse(fallbackResponse);
|
|
351
|
-
console.log(`✅ Fallback exitoso: ${endpoint}`);
|
|
352
|
-
this.eventEmitter.emit("afterRequest", {
|
|
353
|
-
url: fallbackUrl,
|
|
354
|
-
method,
|
|
355
|
-
response: fallbackData,
|
|
356
|
-
});
|
|
357
|
-
return fallbackData;
|
|
358
|
-
}
|
|
359
|
-
if (response.status === 204) {
|
|
360
|
-
console.log(`✅ Request exitoso (204 No Content): ${endpoint}`);
|
|
361
|
-
return {};
|
|
102
|
+
if (data && method !== "GET") {
|
|
103
|
+
fetchOptions.body = JSON.stringify(data);
|
|
104
|
+
console.log(`📤 ${method} Body:`, data);
|
|
362
105
|
}
|
|
106
|
+
console.log(`🌐 ${method} ${url}`, fetchOptions);
|
|
107
|
+
const response = await fetch(url, fetchOptions);
|
|
363
108
|
if (!response.ok) {
|
|
364
109
|
let errorData;
|
|
365
110
|
try {
|
|
366
111
|
errorData = await response.json();
|
|
367
112
|
}
|
|
368
|
-
catch (
|
|
113
|
+
catch (_a) {
|
|
369
114
|
errorData = { message: response.statusText };
|
|
370
115
|
}
|
|
371
116
|
console.error(`❌ ${method} ${response.status}:`, errorData);
|
|
@@ -379,7 +124,7 @@ export class ApiClient {
|
|
|
379
124
|
}
|
|
380
125
|
const responseData = await parseResponse(response);
|
|
381
126
|
if (useCache && method === "GET") {
|
|
382
|
-
const cacheKey = generateCacheKey(method,
|
|
127
|
+
const cacheKey = generateCacheKey(method, url, data);
|
|
383
128
|
const cacheTime = options.cacheTime || undefined;
|
|
384
129
|
this.cache.set(cacheKey, responseData, cacheTime);
|
|
385
130
|
if (this.debug) {
|
|
@@ -387,7 +132,7 @@ export class ApiClient {
|
|
|
387
132
|
}
|
|
388
133
|
}
|
|
389
134
|
this.eventEmitter.emit("afterRequest", {
|
|
390
|
-
url
|
|
135
|
+
url,
|
|
391
136
|
method,
|
|
392
137
|
response: responseData,
|
|
393
138
|
});
|
|
@@ -395,7 +140,7 @@ export class ApiClient {
|
|
|
395
140
|
}
|
|
396
141
|
catch (error) {
|
|
397
142
|
this.eventEmitter.emit("requestError", {
|
|
398
|
-
url
|
|
143
|
+
url,
|
|
399
144
|
method,
|
|
400
145
|
error,
|
|
401
146
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { ClassificationTypesApi } from "./modules/legacy/classificationtypes/cla
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class ClmSdk {
|
|
14
14
|
private apiClient;
|
|
15
|
-
private legacyClient;
|
|
16
15
|
private eventEmitter;
|
|
17
16
|
private cacheInstance;
|
|
18
17
|
auth: AuthApi;
|
|
@@ -23,8 +22,6 @@ export declare class ClmSdk {
|
|
|
23
22
|
areas: AreasApi;
|
|
24
23
|
classificationTypes: ClassificationTypesApi;
|
|
25
24
|
constructor(config: Partial<SdkConfig>);
|
|
26
|
-
legacyLogin(userName: string, password: string): Promise<import("./core/legacy-api-client").LegacyLoginResponse>;
|
|
27
|
-
legacyLogout(): void;
|
|
28
25
|
/**
|
|
29
26
|
* Access to events system
|
|
30
27
|
*/
|
|
@@ -43,6 +40,5 @@ export * from "./modules/v1/auth/types";
|
|
|
43
40
|
export * from "./modules/v1/users/types";
|
|
44
41
|
export * from "./modules/v1/notifications/types";
|
|
45
42
|
export * from "./modules/v1/_logs/types";
|
|
46
|
-
export * from "./core/legacy-api-client";
|
|
47
43
|
export * from "./modules/legacy/areas/types";
|
|
48
44
|
export * from "./modules/legacy/classificationtypes/types";
|
package/dist/index.js
CHANGED
|
@@ -11,7 +11,6 @@ import { LogsApi } from "./modules/v1/_logs/logs-api";
|
|
|
11
11
|
// Legacy
|
|
12
12
|
import { AreasApi } from "./modules/legacy/areas/areas-api";
|
|
13
13
|
import { ClassificationTypesApi } from "./modules/legacy/classificationtypes/classificationtypes-api";
|
|
14
|
-
import { LegacyApiClient } from "./core/legacy-api-client";
|
|
15
14
|
/**
|
|
16
15
|
* Main SDK for consuming CLM API
|
|
17
16
|
*/
|
|
@@ -31,7 +30,6 @@ export class ClmSdk {
|
|
|
31
30
|
// Initialize core utilities
|
|
32
31
|
this.cacheInstance = new Cache((_a = fullConfig.cache) === null || _a === void 0 ? void 0 : _a.ttl);
|
|
33
32
|
this.apiClient = new ApiClient(fullConfig, this.eventEmitter);
|
|
34
|
-
this.legacyClient = new LegacyApiClient("http://216.250.117.119/ZeroServicesQA/api/v1");
|
|
35
33
|
// Initialize modules v1
|
|
36
34
|
this.auth = new AuthApi(this.apiClient);
|
|
37
35
|
this.users = new UsersApi(this.apiClient);
|
|
@@ -42,12 +40,6 @@ export class ClmSdk {
|
|
|
42
40
|
this.areas = new AreasApi(this.apiClient);
|
|
43
41
|
this.classificationTypes = new ClassificationTypesApi(this.apiClient);
|
|
44
42
|
}
|
|
45
|
-
async legacyLogin(userName, password) {
|
|
46
|
-
return await this.legacyClient.login(userName, password);
|
|
47
|
-
}
|
|
48
|
-
legacyLogout() {
|
|
49
|
-
this.legacyClient.logout();
|
|
50
|
-
}
|
|
51
43
|
/**
|
|
52
44
|
* Access to events system
|
|
53
45
|
*/
|
|
@@ -63,6 +55,5 @@ export * from "./modules/v1/users/types";
|
|
|
63
55
|
export * from "./modules/v1/notifications/types";
|
|
64
56
|
export * from "./modules/v1/_logs/types";
|
|
65
57
|
// Export legacy types
|
|
66
|
-
export * from "./core/legacy-api-client";
|
|
67
58
|
export * from "./modules/legacy/areas/types";
|
|
68
59
|
export * from "./modules/legacy/classificationtypes/types";
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { ApiClient } from "../../../core/api-client";
|
|
2
2
|
import { LoginCredentials, LoginResponse, RefreshTokenRequest } from "./types";
|
|
3
|
+
type LoginMode = "new" | "legacy";
|
|
3
4
|
export declare class AuthApi {
|
|
4
5
|
private apiClient;
|
|
5
6
|
constructor(apiClient: ApiClient);
|
|
6
|
-
|
|
7
|
-
* Login con POST JSON (evita el modal del navegador).
|
|
8
|
-
* El servidor setea cookie HttpOnly (zero_token).
|
|
9
|
-
*/
|
|
10
|
-
login(credentials: LoginCredentials): Promise<{
|
|
7
|
+
login(credentials: LoginCredentials, mode?: LoginMode): Promise<{
|
|
11
8
|
ok: boolean;
|
|
12
9
|
}>;
|
|
13
10
|
refreshToken(request?: RefreshTokenRequest): Promise<LoginResponse>;
|
|
@@ -15,3 +12,4 @@ export declare class AuthApi {
|
|
|
15
12
|
isAuthenticated(): boolean;
|
|
16
13
|
setToken(token: string | null): void;
|
|
17
14
|
}
|
|
15
|
+
export {};
|
|
@@ -2,40 +2,42 @@ export class AuthApi {
|
|
|
2
2
|
constructor(apiClient) {
|
|
3
3
|
this.apiClient = apiClient;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
5
|
+
async login(credentials, mode = "new") {
|
|
6
|
+
var _a;
|
|
7
|
+
const path = mode === "legacy" ? "/legacy/login" : "/auth/login";
|
|
8
|
+
const response = await fetch(`${this.apiClient["baseUrl"]}${path}`, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {
|
|
11
|
+
"Content-Type": "application/json",
|
|
12
|
+
"X-Organization": this.apiClient["organization"] || "default-org",
|
|
13
|
+
},
|
|
14
|
+
credentials: "include",
|
|
15
|
+
body: JSON.stringify({
|
|
16
|
+
email: credentials.email,
|
|
17
|
+
password: credentials.password,
|
|
18
|
+
}),
|
|
19
|
+
});
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
if (response.status === 401)
|
|
22
|
+
throw new Error("Invalid credentials");
|
|
23
|
+
throw new Error(`Login failed: ${response.status}`);
|
|
24
|
+
}
|
|
25
|
+
const data = await response.json().catch(() => null);
|
|
26
|
+
// ✅ Si es legacy, guarda Bearer token para siguientes llamadas legacy
|
|
27
|
+
if (mode === "legacy") {
|
|
28
|
+
const legacyToken = ((_a = data === null || data === void 0 ? void 0 : data.dataResult) === null || _a === void 0 ? void 0 : _a.token) || (data === null || data === void 0 ? void 0 : data.token);
|
|
29
|
+
if (legacyToken) {
|
|
30
|
+
if (typeof sessionStorage !== "undefined") {
|
|
31
|
+
sessionStorage.setItem("legacy_token", legacyToken);
|
|
32
|
+
}
|
|
33
|
+
if (typeof window !== "undefined") {
|
|
34
|
+
window.__LEGACY_TOKEN__ = legacyToken;
|
|
26
35
|
}
|
|
27
|
-
throw new Error(`Login failed: ${response.status}`);
|
|
28
36
|
}
|
|
29
|
-
const data = await response.json();
|
|
30
|
-
// No guardar token en memoria, solo usar cookie
|
|
31
|
-
this.apiClient.setToken(null);
|
|
32
|
-
console.log('✅ Login exitoso, cookie establecida');
|
|
33
|
-
return { ok: true };
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
console.error('❌ SDK Login error:', error);
|
|
37
|
-
throw error;
|
|
38
37
|
}
|
|
38
|
+
// ✅ Nuevo: cookie HttpOnly, no guardes token en memoria del SDK
|
|
39
|
+
this.apiClient.setToken(null);
|
|
40
|
+
return { ok: true };
|
|
39
41
|
}
|
|
40
42
|
async refreshToken(request) {
|
|
41
43
|
const response = await this.apiClient.post("/auth/refresh", request);
|
|
@@ -47,15 +49,24 @@ export class AuthApi {
|
|
|
47
49
|
try {
|
|
48
50
|
await this.apiClient.post("/auth/logout");
|
|
49
51
|
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
console.error("Error during logout:", error);
|
|
52
|
-
}
|
|
53
52
|
finally {
|
|
53
|
+
// limpia token nuevo
|
|
54
54
|
this.apiClient.setToken(null);
|
|
55
|
+
// limpia legacy token
|
|
56
|
+
if (typeof sessionStorage !== "undefined") {
|
|
57
|
+
sessionStorage.removeItem("legacy_token");
|
|
58
|
+
}
|
|
59
|
+
if (typeof window !== "undefined") {
|
|
60
|
+
delete window.__LEGACY_TOKEN__;
|
|
61
|
+
}
|
|
55
62
|
}
|
|
56
63
|
}
|
|
57
64
|
isAuthenticated() {
|
|
58
|
-
|
|
65
|
+
const hasToken = this.apiClient.getToken() !== null;
|
|
66
|
+
const hasLegacy = (typeof sessionStorage !== "undefined" &&
|
|
67
|
+
!!sessionStorage.getItem("legacy_token")) ||
|
|
68
|
+
(typeof window !== "undefined" && !!window.__LEGACY_TOKEN__);
|
|
69
|
+
return hasToken || hasLegacy;
|
|
59
70
|
}
|
|
60
71
|
setToken(token) {
|
|
61
72
|
this.apiClient.setToken(token);
|
package/dist/types/sdk.d.ts
CHANGED
package/package.json
CHANGED
package/src/config/config.ts
CHANGED
|
@@ -4,13 +4,13 @@ function normalizeUrl(url: string) {
|
|
|
4
4
|
return url.trim().replace(/\/+$/, "");
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
function resolveDefaultBaseUrl(){
|
|
8
|
-
const fromWindow = (globalThis as any)?.SDK_CONFIG?.API_BASE_URL;
|
|
9
|
-
if (fromWindow) return normalizeUrl(String(fromWindow));
|
|
10
|
-
}
|
|
7
|
+
//function resolveDefaultBaseUrl(){
|
|
8
|
+
// const fromWindow = (globalThis as any)?.SDK_CONFIG?.API_BASE_URL;
|
|
9
|
+
// if (fromWindow) return normalizeUrl(String(fromWindow));
|
|
10
|
+
//}
|
|
11
11
|
|
|
12
12
|
export const DEFAULT_CONFIG: Partial<SdkConfig> = {
|
|
13
|
-
baseUrl: resolveDefaultBaseUrl(),
|
|
13
|
+
baseUrl: "https://zero-api-qa.onrender.com/api/v1", //resolveDefaultBaseUrl(),
|
|
14
14
|
organization: "default-org",
|
|
15
15
|
cache: { enabled: true, ttl: 60000 },
|
|
16
16
|
debug: true,
|