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