@enfyra/sdk-nuxt 0.3.17 → 0.3.19

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.
Files changed (62) hide show
  1. package/dist/composables/useEnfyraApi.d.ts +8 -0
  2. package/dist/composables/useEnfyraApi.d.ts.map +1 -0
  3. package/dist/composables/useEnfyraApi.js +363 -0
  4. package/dist/composables/useEnfyraApi.mjs +345 -0
  5. package/dist/composables/useEnfyraAuth.d.ts +89 -0
  6. package/dist/composables/useEnfyraAuth.d.ts.map +1 -0
  7. package/dist/composables/useEnfyraAuth.js +77 -0
  8. package/dist/composables/useEnfyraAuth.mjs +81 -0
  9. package/dist/constants/auth.d.ts +0 -0
  10. package/dist/constants/auth.mjs +3 -0
  11. package/dist/constants/config.d.ts +2 -0
  12. package/dist/constants/config.d.ts.map +1 -0
  13. package/dist/constants/config.js +1 -0
  14. package/dist/constants/config.mjs +1 -0
  15. package/dist/module.cjs +82 -0
  16. package/dist/module.d.cts +10 -0
  17. package/dist/module.d.mts +10 -0
  18. package/dist/module.d.ts +10 -0
  19. package/dist/module.json +9 -0
  20. package/dist/module.mjs +79 -0
  21. package/dist/runtime/plugin/config-error.client.d.ts +0 -0
  22. package/dist/runtime/plugin/config-error.client.js +107 -0
  23. package/dist/runtime/plugin/config-error.client.mjs +107 -0
  24. package/dist/runtime/server/api/all.d.ts +0 -0
  25. package/dist/runtime/server/api/all.js +5 -0
  26. package/dist/runtime/server/api/all.mjs +5 -0
  27. package/dist/runtime/server/api/login.post.d.ts +0 -0
  28. package/dist/runtime/server/api/login.post.js +62 -0
  29. package/dist/runtime/server/api/login.post.mjs +62 -0
  30. package/dist/runtime/server/api/logout.post.d.ts +0 -0
  31. package/dist/runtime/server/api/logout.post.js +35 -0
  32. package/dist/runtime/server/api/logout.post.mjs +35 -0
  33. package/dist/runtime/server/middleware/auth.d.ts +0 -0
  34. package/dist/runtime/server/middleware/auth.js +36 -0
  35. package/dist/runtime/server/middleware/auth.mjs +36 -0
  36. package/dist/types/auth.d.ts +43 -0
  37. package/dist/types/auth.d.ts.map +1 -0
  38. package/dist/types/auth.js +1 -0
  39. package/dist/types/index.d.ts +141 -0
  40. package/dist/types/index.d.ts.map +1 -0
  41. package/dist/types/index.js +1 -0
  42. package/dist/types.d.mts +7 -0
  43. package/dist/types.d.ts +7 -0
  44. package/dist/utils/config.d.ts +0 -0
  45. package/dist/utils/config.mjs +16 -0
  46. package/dist/utils/http.d.ts +8 -0
  47. package/dist/utils/http.d.ts.map +1 -0
  48. package/dist/utils/http.js +57 -0
  49. package/dist/utils/http.mjs +61 -0
  50. package/dist/utils/server/proxy.d.ts +0 -0
  51. package/dist/utils/server/proxy.mjs +14 -0
  52. package/dist/utils/server/refreshToken.d.ts +0 -0
  53. package/dist/utils/server/refreshToken.mjs +69 -0
  54. package/dist/utils/url.d.ts +12 -0
  55. package/dist/utils/url.d.ts.map +1 -0
  56. package/dist/utils/url.js +77 -0
  57. package/dist/utils/url.mjs +56 -0
  58. package/index.ts +1 -1
  59. package/module.d.ts +2 -2
  60. package/package.json +25 -22
  61. package/src/composables/useEnfyraAuth.ts +1 -1
  62. package/{module.ts → src/module.ts} +9 -10
@@ -0,0 +1,8 @@
1
+ import type { ApiOptions, UseEnfyraApiSSRReturn, UseEnfyraApiClientReturn } from "../types";
2
+ export declare function useEnfyraApi<T = any>(path: (() => string) | string, opts: ApiOptions<T> & {
3
+ ssr: true;
4
+ }): UseEnfyraApiSSRReturn<T>;
5
+ export declare function useEnfyraApi<T = any>(path: (() => string) | string, opts?: ApiOptions<T> & {
6
+ ssr?: false | undefined;
7
+ }): UseEnfyraApiClientReturn<T>;
8
+ //# sourceMappingURL=useEnfyraApi.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useEnfyraApi.d.ts","sourceRoot":"","sources":["../../src/composables/useEnfyraApi.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EAGV,qBAAqB,EACrB,wBAAwB,EAEzB,MAAM,UAAU,CAAC;AA4BlB,wBAAgB,YAAY,CAAC,CAAC,GAAG,GAAG,EAClC,IAAI,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,MAAM,EAC7B,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG;IAAE,GAAG,EAAE,IAAI,CAAA;CAAE,GAClC,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAE5B,wBAAgB,YAAY,CAAC,CAAC,GAAG,GAAG,EAClC,IAAI,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,MAAM,EAC7B,IAAI,CAAC,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG;IAAE,GAAG,CAAC,EAAE,KAAK,GAAG,SAAS,CAAA;CAAE,GACjD,wBAAwB,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,363 @@
1
+ import { ref, unref, toRaw } from "vue";
2
+ import { $fetch } from "../utils/http";
3
+ import { getAppUrl, normalizeUrl } from "../utils/url";
4
+ import { ENFYRA_API_PREFIX } from "../constants/config";
5
+ import { useRuntimeConfig, useFetch, useRequestHeaders, useNuxtApp } from "#imports";
6
+ function handleError(error, context, customHandler) {
7
+ const apiError = {
8
+ message: error?.message || error?.data?.message || "Request failed",
9
+ status: error?.status || error?.response?.status,
10
+ data: error?.data || error?.response?.data,
11
+ response: error?.response || error,
12
+ };
13
+ if (customHandler) {
14
+ customHandler(apiError, context);
15
+ }
16
+ else {
17
+ console.error(`[Enfyra API Error]`, { error: apiError, context });
18
+ }
19
+ return apiError;
20
+ }
21
+ export function useEnfyraApi(path, opts = {}) {
22
+ const { method = "get", body, query, errorContext, onError, ssr, key } = opts;
23
+ const batchOptions = opts;
24
+ const { batchSize, concurrent, onProgress } = batchOptions;
25
+ if (ssr) {
26
+ const config = useRuntimeConfig().public.enfyraSDK;
27
+ const basePath = (typeof path === "function" ? path() : path)
28
+ .replace(/^\/?api\/?/, "")
29
+ .replace(/^\/+/, "");
30
+ const appUrl = getAppUrl();
31
+ const finalUrl = normalizeUrl(appUrl, config?.apiPrefix || ENFYRA_API_PREFIX, basePath);
32
+ const clientHeaders = process.client
33
+ ? {}
34
+ : useRequestHeaders([
35
+ "authorization",
36
+ "cookie",
37
+ "user-agent",
38
+ "accept",
39
+ "accept-language",
40
+ "referer",
41
+ ]);
42
+ const serverHeaders = { ...clientHeaders };
43
+ delete serverHeaders.connection;
44
+ delete serverHeaders["keep-alive"];
45
+ delete serverHeaders.host;
46
+ delete serverHeaders["content-length"];
47
+ const nuxtApp = useNuxtApp();
48
+ const fetchOptions = {
49
+ method: method,
50
+ body: body,
51
+ query: query,
52
+ headers: {
53
+ ...serverHeaders,
54
+ ...opts.headers,
55
+ },
56
+ };
57
+ // Pass all useFetch-specific options
58
+ if (key !== undefined) {
59
+ fetchOptions.key = key;
60
+ }
61
+ if (opts.default !== undefined) {
62
+ fetchOptions.default = opts.default;
63
+ }
64
+ if (opts.server !== undefined) {
65
+ fetchOptions.server = opts.server;
66
+ }
67
+ if (opts.lazy !== undefined) {
68
+ fetchOptions.lazy = opts.lazy;
69
+ }
70
+ if (opts.immediate !== undefined) {
71
+ fetchOptions.immediate = opts.immediate;
72
+ }
73
+ if (opts.transform !== undefined) {
74
+ fetchOptions.transform = opts.transform;
75
+ }
76
+ if (opts.pick !== undefined) {
77
+ fetchOptions.pick = opts.pick;
78
+ }
79
+ if (opts.watch !== undefined) {
80
+ fetchOptions.watch = opts.watch;
81
+ }
82
+ if (opts.deep !== undefined) {
83
+ fetchOptions.deep = opts.deep;
84
+ }
85
+ if (opts.getCachedData !== undefined) {
86
+ fetchOptions.getCachedData = opts.getCachedData;
87
+ }
88
+ else {
89
+ // Auto-add getCachedData to ensure cache is used in SPA navigation
90
+ fetchOptions.getCachedData = (cacheKey) => {
91
+ return nuxtApp.payload.data[cacheKey] || nuxtApp.static.data[cacheKey];
92
+ };
93
+ }
94
+ if (opts.refresh !== undefined) {
95
+ fetchOptions.refresh = opts.refresh;
96
+ }
97
+ if (opts.refreshInterval !== undefined) {
98
+ fetchOptions.refreshInterval = opts.refreshInterval;
99
+ }
100
+ if (opts.dedupe !== undefined) {
101
+ fetchOptions.dedupe = opts.dedupe;
102
+ }
103
+ const result = useFetch(finalUrl, fetchOptions);
104
+ // Map pending to loading for better naming
105
+ // useFetch returns AsyncData with 'pending', but UseEnfyraApiSSRReturn uses 'loading'
106
+ return {
107
+ ...result,
108
+ loading: result.pending,
109
+ pending: result.pending, // Keep for backward compatibility
110
+ };
111
+ }
112
+ const data = ref(null);
113
+ const error = ref(null);
114
+ const pending = ref(false);
115
+ const execute = async (executeOpts) => {
116
+ pending.value = true;
117
+ error.value = null;
118
+ try {
119
+ const config = useRuntimeConfig().public.enfyraSDK;
120
+ const apiUrl = getAppUrl();
121
+ const apiPrefix = config?.apiPrefix;
122
+ const basePath = (typeof path === "function" ? path() : path)
123
+ .replace(/^\/?api\/?/, "")
124
+ .replace(/^\/+/, "");
125
+ const finalBody = executeOpts?.body || unref(body);
126
+ const finalQuery = executeOpts?.query || unref(query);
127
+ const isBatchOperation = !opts.disableBatch &&
128
+ ((executeOpts?.ids &&
129
+ executeOpts.ids.length > 0 &&
130
+ (method.toLowerCase() === "patch" ||
131
+ method.toLowerCase() === "delete")) ||
132
+ (method.toLowerCase() === "post" &&
133
+ executeOpts?.files &&
134
+ Array.isArray(executeOpts.files) &&
135
+ executeOpts.files.length > 0));
136
+ const effectiveBatchSize = isBatchOperation
137
+ ? executeOpts?.batchSize ?? batchSize
138
+ : undefined;
139
+ const effectiveConcurrent = isBatchOperation
140
+ ? executeOpts?.concurrent ?? concurrent
141
+ : undefined;
142
+ const effectiveOnProgress = isBatchOperation
143
+ ? executeOpts?.onProgress ?? onProgress
144
+ : undefined;
145
+ const buildPath = (...segments) => {
146
+ return segments.filter(Boolean).join("/");
147
+ };
148
+ const fullBaseURL = normalizeUrl(apiUrl, apiPrefix || ENFYRA_API_PREFIX);
149
+ async function processBatch(items, processor) {
150
+ const results = [];
151
+ const progressResults = [];
152
+ const startTime = Date.now();
153
+ let completed = 0;
154
+ let failed = 0;
155
+ const chunks = effectiveBatchSize
156
+ ? Array.from({ length: Math.ceil(items.length / effectiveBatchSize) }, (_, i) => items.slice(i * effectiveBatchSize, i * effectiveBatchSize + effectiveBatchSize))
157
+ : [items];
158
+ const totalBatches = chunks.length;
159
+ let currentBatch = 0;
160
+ const updateProgress = (inProgress = 0) => {
161
+ if (effectiveOnProgress) {
162
+ const elapsed = Date.now() - startTime;
163
+ const progress = items.length > 0
164
+ ? Math.round((completed / items.length) * 100)
165
+ : 0;
166
+ const averageTime = completed > 0 ? elapsed / completed : undefined;
167
+ const operationsPerSecond = completed > 0 ? (completed / elapsed) * 1000 : undefined;
168
+ const estimatedTimeRemaining = averageTime && items.length > completed
169
+ ? Math.round(averageTime * (items.length - completed))
170
+ : undefined;
171
+ const progressData = {
172
+ progress,
173
+ completed,
174
+ total: items.length,
175
+ failed,
176
+ inProgress,
177
+ estimatedTimeRemaining,
178
+ averageTime,
179
+ currentBatch: currentBatch + 1,
180
+ totalBatches,
181
+ operationsPerSecond,
182
+ results: [...progressResults],
183
+ };
184
+ effectiveOnProgress(progressData);
185
+ }
186
+ };
187
+ updateProgress(0);
188
+ if (!effectiveBatchSize && !effectiveConcurrent) {
189
+ updateProgress(items.length);
190
+ const promises = items.map(async (item, index) => {
191
+ const itemStartTime = Date.now();
192
+ try {
193
+ const result = await processor(item, index);
194
+ const duration = Date.now() - itemStartTime;
195
+ completed++;
196
+ progressResults.push({
197
+ index,
198
+ status: "completed",
199
+ result,
200
+ duration,
201
+ });
202
+ updateProgress(items.length - completed);
203
+ return result;
204
+ }
205
+ catch (error) {
206
+ const duration = Date.now() - itemStartTime;
207
+ failed++;
208
+ completed++;
209
+ progressResults.push({
210
+ index,
211
+ status: "failed",
212
+ error: error,
213
+ duration,
214
+ });
215
+ updateProgress(items.length - completed);
216
+ throw error;
217
+ }
218
+ });
219
+ const results = await Promise.all(promises);
220
+ updateProgress(0);
221
+ return results;
222
+ }
223
+ for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
224
+ currentBatch = chunkIndex;
225
+ const chunk = chunks[chunkIndex];
226
+ if (effectiveConcurrent && chunk.length > effectiveConcurrent) {
227
+ for (let i = 0; i < chunk.length; i += effectiveConcurrent) {
228
+ const batch = chunk.slice(i, i + effectiveConcurrent);
229
+ const baseIndex = chunkIndex * (effectiveBatchSize || items.length) + i;
230
+ updateProgress(batch.length);
231
+ const batchPromises = batch.map(async (item, batchItemIndex) => {
232
+ const globalIndex = baseIndex + batchItemIndex;
233
+ const itemStartTime = Date.now();
234
+ try {
235
+ const result = await processor(item, globalIndex);
236
+ const duration = Date.now() - itemStartTime;
237
+ completed++;
238
+ progressResults.push({
239
+ index: globalIndex,
240
+ status: "completed",
241
+ result,
242
+ duration,
243
+ });
244
+ updateProgress(Math.max(0, batch.length - (batchItemIndex + 1)));
245
+ return result;
246
+ }
247
+ catch (error) {
248
+ const duration = Date.now() - itemStartTime;
249
+ failed++;
250
+ completed++;
251
+ progressResults.push({
252
+ index: globalIndex,
253
+ status: "failed",
254
+ error: error,
255
+ duration,
256
+ });
257
+ updateProgress(Math.max(0, batch.length - (batchItemIndex + 1)));
258
+ throw error;
259
+ }
260
+ });
261
+ const batchResults = await Promise.all(batchPromises);
262
+ results.push(...batchResults);
263
+ }
264
+ }
265
+ else {
266
+ const baseIndex = chunkIndex * (effectiveBatchSize || items.length);
267
+ updateProgress(chunk.length);
268
+ const chunkPromises = chunk.map(async (item, chunkItemIndex) => {
269
+ const globalIndex = baseIndex + chunkItemIndex;
270
+ const itemStartTime = Date.now();
271
+ try {
272
+ const result = await processor(item, globalIndex);
273
+ const duration = Date.now() - itemStartTime;
274
+ completed++;
275
+ progressResults.push({
276
+ index: globalIndex,
277
+ status: "completed",
278
+ result,
279
+ duration,
280
+ });
281
+ updateProgress(Math.max(0, chunk.length - (chunkItemIndex + 1)));
282
+ return result;
283
+ }
284
+ catch (error) {
285
+ const duration = Date.now() - itemStartTime;
286
+ failed++;
287
+ completed++;
288
+ progressResults.push({
289
+ index: globalIndex,
290
+ status: "failed",
291
+ error: error,
292
+ duration,
293
+ });
294
+ updateProgress(Math.max(0, chunk.length - (chunkItemIndex + 1)));
295
+ throw error;
296
+ }
297
+ });
298
+ const chunkResults = await Promise.all(chunkPromises);
299
+ results.push(...chunkResults);
300
+ }
301
+ }
302
+ updateProgress(0);
303
+ return results;
304
+ }
305
+ if (isBatchOperation && executeOpts?.ids && executeOpts.ids.length > 0) {
306
+ const responses = await processBatch(executeOpts.ids, async (id) => {
307
+ const finalPath = buildPath(basePath, id);
308
+ return $fetch(finalPath, {
309
+ baseURL: fullBaseURL,
310
+ method: method,
311
+ body: finalBody ? toRaw(finalBody) : undefined,
312
+ headers: opts.headers,
313
+ query: finalQuery,
314
+ });
315
+ });
316
+ data.value = responses;
317
+ return responses;
318
+ }
319
+ if (isBatchOperation &&
320
+ executeOpts?.files &&
321
+ Array.isArray(executeOpts.files) &&
322
+ executeOpts.files.length > 0) {
323
+ const responses = await processBatch(executeOpts.files, async (fileObj) => {
324
+ return $fetch(basePath, {
325
+ baseURL: fullBaseURL,
326
+ method: method,
327
+ body: fileObj,
328
+ headers: opts.headers,
329
+ query: finalQuery,
330
+ });
331
+ });
332
+ data.value = responses;
333
+ return responses;
334
+ }
335
+ const finalPath = executeOpts?.id
336
+ ? buildPath(basePath, executeOpts.id)
337
+ : basePath;
338
+ const response = await $fetch(finalPath, {
339
+ baseURL: fullBaseURL,
340
+ method: method,
341
+ body: finalBody ? toRaw(finalBody) : undefined,
342
+ headers: opts.headers,
343
+ query: finalQuery,
344
+ });
345
+ data.value = response;
346
+ return response;
347
+ }
348
+ catch (err) {
349
+ const apiError = handleError(err, errorContext, onError);
350
+ error.value = apiError;
351
+ return null;
352
+ }
353
+ finally {
354
+ pending.value = false;
355
+ }
356
+ };
357
+ return {
358
+ data,
359
+ error,
360
+ pending,
361
+ execute,
362
+ };
363
+ }