@enfyra/sdk-nuxt 0.3.19 → 0.3.21
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/module.cjs +57 -22
- package/dist/module.d.ts +4 -7
- package/dist/module.d.ts.map +1 -0
- package/dist/module.json +4 -1
- package/dist/module.mjs +56 -21
- package/dist/{composables → runtime/composables}/useEnfyraApi.d.ts +1 -1
- package/dist/runtime/composables/useEnfyraApi.d.ts.map +1 -0
- package/dist/{composables/useEnfyraApi.mjs → runtime/composables/useEnfyraApi.js} +6 -7
- package/dist/{composables → runtime/composables}/useEnfyraAuth.d.ts +1 -1
- package/dist/runtime/composables/useEnfyraAuth.d.ts.map +1 -0
- package/dist/{composables/useEnfyraAuth.mjs → runtime/composables/useEnfyraAuth.js} +1 -1
- package/dist/runtime/constants/auth.d.ts +4 -0
- package/dist/runtime/constants/auth.d.ts.map +1 -0
- package/dist/runtime/constants/config.d.ts.map +1 -0
- package/dist/runtime/plugin/config-error.client.d.ts +3 -0
- package/dist/runtime/plugin/config-error.client.d.ts.map +1 -0
- package/dist/runtime/server/api/all.d.ts +3 -0
- package/dist/runtime/server/api/all.d.ts.map +1 -0
- package/dist/runtime/server/api/all.js +1 -1
- package/dist/runtime/server/api/login.post.d.ts +5 -0
- package/dist/runtime/server/api/login.post.d.ts.map +1 -0
- package/dist/runtime/server/api/login.post.js +2 -2
- package/dist/runtime/server/api/logout.post.d.ts +3 -0
- package/dist/runtime/server/api/logout.post.d.ts.map +1 -0
- package/dist/runtime/server/api/logout.post.js +2 -2
- package/dist/runtime/server/middleware/auth.d.ts +3 -0
- package/dist/runtime/server/middleware/auth.d.ts.map +1 -0
- package/dist/runtime/server/middleware/auth.js +2 -2
- package/dist/runtime/shims.d.ts +64 -0
- package/dist/runtime/utils/http.d.ts.map +1 -0
- package/dist/runtime/utils/server/proxy.d.ts +3 -0
- package/dist/runtime/utils/server/proxy.d.ts.map +1 -0
- package/dist/{utils/server/proxy.mjs → runtime/utils/server/proxy.js} +2 -2
- package/dist/runtime/utils/server/refreshToken.d.ts +11 -0
- package/dist/runtime/utils/server/refreshToken.d.ts.map +1 -0
- package/dist/{utils/server/refreshToken.mjs → runtime/utils/server/refreshToken.js} +2 -2
- package/dist/runtime/utils/url.d.ts +4 -0
- package/dist/runtime/utils/url.d.ts.map +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/module.d.ts +16 -6
- package/package.json +12 -14
- package/src/module.ts +73 -34
- package/src/{composables → runtime/composables}/useEnfyraApi.ts +21 -23
- package/src/{composables → runtime/composables}/useEnfyraAuth.ts +2 -2
- package/src/runtime/server/api/all.ts +1 -1
- package/src/runtime/server/api/login.post.ts +2 -2
- package/src/runtime/server/api/logout.post.ts +2 -2
- package/src/runtime/server/middleware/auth.ts +2 -2
- package/src/runtime/shims.d.ts +64 -0
- package/src/{utils → runtime/utils}/server/proxy.ts +1 -1
- package/src/{utils → runtime/utils}/server/refreshToken.ts +3 -6
- package/src/{utils → runtime/utils}/url.ts +3 -13
- package/src/types/index.ts +1 -2
- package/dist/composables/useEnfyraApi.d.ts.map +0 -1
- package/dist/composables/useEnfyraApi.js +0 -363
- package/dist/composables/useEnfyraAuth.d.ts.map +0 -1
- package/dist/composables/useEnfyraAuth.js +0 -77
- package/dist/constants/auth.d.ts +0 -0
- package/dist/constants/config.d.ts.map +0 -1
- package/dist/constants/config.mjs +0 -1
- package/dist/runtime/plugin/config-error.client.mjs +0 -107
- package/dist/runtime/server/api/all.mjs +0 -5
- package/dist/runtime/server/api/login.post.mjs +0 -62
- package/dist/runtime/server/api/logout.post.mjs +0 -35
- package/dist/runtime/server/middleware/auth.mjs +0 -36
- package/dist/types/auth.js +0 -1
- package/dist/types/index.js +0 -1
- package/dist/utils/config.d.ts +0 -0
- package/dist/utils/config.mjs +0 -16
- package/dist/utils/http.d.ts.map +0 -1
- package/dist/utils/http.js +0 -57
- package/dist/utils/server/proxy.d.ts +0 -0
- package/dist/utils/server/refreshToken.d.ts +0 -0
- package/dist/utils/url.d.ts +0 -12
- package/dist/utils/url.d.ts.map +0 -1
- package/dist/utils/url.js +0 -77
- package/src/types/nuxt-imports.d.ts +0 -61
- package/src/utils/config.ts +0 -22
- /package/dist/{constants/auth.mjs → runtime/constants/auth.js} +0 -0
- /package/dist/{constants → runtime/constants}/config.d.ts +0 -0
- /package/dist/{constants → runtime/constants}/config.js +0 -0
- /package/dist/{utils → runtime/utils}/http.d.ts +0 -0
- /package/dist/{utils/http.mjs → runtime/utils/http.js} +0 -0
- /package/dist/{utils/url.mjs → runtime/utils/url.js} +0 -0
- /package/src/{constants → runtime/constants}/auth.ts +0 -0
- /package/src/{constants → runtime/constants}/config.ts +0 -0
- /package/src/{utils → runtime/utils}/http.ts +0 -0
|
@@ -1,363 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useEnfyraAuth.d.ts","sourceRoot":"","sources":["../../src/composables/useEnfyraAuth.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,IAAI,EAAuB,MAAM,eAAe,CAAC;AAM7E,wBAAgB,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;8BAgDG,YAAY;;mCAzBP;QAAE,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;KAAE;;EAqEzD"}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { ref, computed } from "vue";
|
|
2
|
-
import { useEnfyraApi } from "./useEnfyraApi";
|
|
3
|
-
const me = ref(null);
|
|
4
|
-
const isLoading = ref(false);
|
|
5
|
-
export function useEnfyraAuth() {
|
|
6
|
-
const { data: loginData, execute: executeLogin, error: loginError, } = useEnfyraApi("/login", {
|
|
7
|
-
method: "post",
|
|
8
|
-
errorContext: "Login",
|
|
9
|
-
});
|
|
10
|
-
const { execute: executeLogout } = useEnfyraApi("/logout", {
|
|
11
|
-
method: "post",
|
|
12
|
-
errorContext: "Logout",
|
|
13
|
-
});
|
|
14
|
-
const { data: meData, execute: executeFetchUser, error: fetchUserError, } = useEnfyraApi("/me", {
|
|
15
|
-
errorContext: "Fetch User Profile",
|
|
16
|
-
});
|
|
17
|
-
const fetchUser = async (options) => {
|
|
18
|
-
isLoading.value = true;
|
|
19
|
-
try {
|
|
20
|
-
const queryParams = {};
|
|
21
|
-
if (options?.fields && options.fields.length > 0) {
|
|
22
|
-
queryParams.fields = options.fields.join(",");
|
|
23
|
-
}
|
|
24
|
-
await executeFetchUser({
|
|
25
|
-
query: queryParams,
|
|
26
|
-
});
|
|
27
|
-
if (fetchUserError.value) {
|
|
28
|
-
me.value = null;
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
me.value = meData.value?.data?.[0];
|
|
32
|
-
}
|
|
33
|
-
finally {
|
|
34
|
-
isLoading.value = false;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
const login = async (payload) => {
|
|
38
|
-
isLoading.value = true;
|
|
39
|
-
try {
|
|
40
|
-
await executeLogin({ body: payload });
|
|
41
|
-
if (loginError.value) {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
return loginData.value;
|
|
45
|
-
}
|
|
46
|
-
finally {
|
|
47
|
-
isLoading.value = false;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const logout = async () => {
|
|
51
|
-
isLoading.value = true;
|
|
52
|
-
try {
|
|
53
|
-
await executeLogout();
|
|
54
|
-
me.value = null;
|
|
55
|
-
if (typeof window !== "undefined") {
|
|
56
|
-
window.location.reload();
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
me.value = null;
|
|
61
|
-
if (typeof window !== "undefined") {
|
|
62
|
-
window.location.reload();
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
finally {
|
|
66
|
-
isLoading.value = false;
|
|
67
|
-
}
|
|
68
|
-
};
|
|
69
|
-
const isLoggedIn = computed(() => !!me.value);
|
|
70
|
-
return {
|
|
71
|
-
me,
|
|
72
|
-
login,
|
|
73
|
-
logout,
|
|
74
|
-
fetchUser,
|
|
75
|
-
isLoggedIn,
|
|
76
|
-
};
|
|
77
|
-
}
|
package/dist/constants/auth.d.ts
DELETED
|
File without changes
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/constants/config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,gBAAgB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const ENFYRA_API_PREFIX = "/enfyra/api";
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { defineNuxtPlugin, useRuntimeConfig } from "#imports";
|
|
2
|
-
export default defineNuxtPlugin(() => {
|
|
3
|
-
const config = useRuntimeConfig().public?.enfyraSDK;
|
|
4
|
-
if (config?.configError) {
|
|
5
|
-
if (typeof window !== "undefined" && document) {
|
|
6
|
-
let showError = function() {
|
|
7
|
-
const errorDiv = document.createElement("div");
|
|
8
|
-
errorDiv.id = "enfyra-config-error";
|
|
9
|
-
errorDiv.style.cssText = `
|
|
10
|
-
position: fixed;
|
|
11
|
-
top: 0;
|
|
12
|
-
left: 0;
|
|
13
|
-
right: 0;
|
|
14
|
-
bottom: 0;
|
|
15
|
-
background: rgba(0, 0, 0, 0.9);
|
|
16
|
-
display: flex;
|
|
17
|
-
align-items: center;
|
|
18
|
-
justify-content: center;
|
|
19
|
-
z-index: 999999;
|
|
20
|
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
21
|
-
`;
|
|
22
|
-
const content = document.createElement("div");
|
|
23
|
-
content.style.cssText = `
|
|
24
|
-
background: white;
|
|
25
|
-
padding: 40px;
|
|
26
|
-
border-radius: 12px;
|
|
27
|
-
max-width: 600px;
|
|
28
|
-
width: 90%;
|
|
29
|
-
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
|
30
|
-
`;
|
|
31
|
-
content.innerHTML = `
|
|
32
|
-
<div style="display: flex; align-items: center; margin-bottom: 20px;">
|
|
33
|
-
<svg width="32" height="32" viewBox="0 0 24 24" fill="none" style="margin-right: 12px;">
|
|
34
|
-
<circle cx="12" cy="12" r="10" stroke="#ef4444" stroke-width="2"/>
|
|
35
|
-
<path d="M12 8v4M12 16h.01" stroke="#ef4444" stroke-width="2" stroke-linecap="round"/>
|
|
36
|
-
</svg>
|
|
37
|
-
<h1 style="margin: 0; color: #1f2937; font-size: 24px; font-weight: 600;">
|
|
38
|
-
Configuration Error
|
|
39
|
-
</h1>
|
|
40
|
-
</div>
|
|
41
|
-
|
|
42
|
-
<div style="color: #4b5563; margin-bottom: 24px; line-height: 1.6;">
|
|
43
|
-
<p style="margin: 0 0 16px 0; font-size: 16px;">
|
|
44
|
-
<strong>Enfyra SDK</strong> is not configured properly.
|
|
45
|
-
</p>
|
|
46
|
-
<p style="margin: 0; color: #6b7280; font-size: 14px;">
|
|
47
|
-
${config?.configErrorMessage || "Missing required configuration"}
|
|
48
|
-
</p>
|
|
49
|
-
</div>
|
|
50
|
-
|
|
51
|
-
<div style="background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin-bottom: 24px;">
|
|
52
|
-
<p style="margin: 0 0 12px 0; color: #6b7280; font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px;">
|
|
53
|
-
Add to your nuxt.config.ts:
|
|
54
|
-
</p>
|
|
55
|
-
<pre style="margin: 0; background: #1f2937; color: #f3f4f6; padding: 16px; border-radius: 6px; overflow-x: auto; font-size: 14px; line-height: 1.4;"><code>export default defineNuxtConfig({
|
|
56
|
-
modules: ["@enfyra/sdk-nuxt"],
|
|
57
|
-
enfyraSDK: {
|
|
58
|
-
<span style="color: #fbbf24;">apiUrl</span>: <span style="color: #34d399;">"https://your-api-url"</span>
|
|
59
|
-
}
|
|
60
|
-
})</code></pre>
|
|
61
|
-
</div>
|
|
62
|
-
|
|
63
|
-
<div style="display: flex; gap: 12px;">
|
|
64
|
-
<button onclick="location.reload()" style="
|
|
65
|
-
background: #3b82f6;
|
|
66
|
-
color: white;
|
|
67
|
-
border: none;
|
|
68
|
-
padding: 10px 20px;
|
|
69
|
-
border-radius: 6px;
|
|
70
|
-
font-size: 14px;
|
|
71
|
-
font-weight: 500;
|
|
72
|
-
cursor: pointer;
|
|
73
|
-
transition: background 0.2s;
|
|
74
|
-
" onmouseover="this.style.background='#2563eb'" onmouseout="this.style.background='#3b82f6'">
|
|
75
|
-
Reload Page
|
|
76
|
-
</button>
|
|
77
|
-
<button onclick="document.getElementById('enfyra-config-error').style.display='none'" style="
|
|
78
|
-
background: #f3f4f6;
|
|
79
|
-
color: #4b5563;
|
|
80
|
-
border: 1px solid #e5e7eb;
|
|
81
|
-
padding: 10px 20px;
|
|
82
|
-
border-radius: 6px;
|
|
83
|
-
font-size: 14px;
|
|
84
|
-
font-weight: 500;
|
|
85
|
-
cursor: pointer;
|
|
86
|
-
transition: all 0.2s;
|
|
87
|
-
" onmouseover="this.style.background='#e5e7eb'" onmouseout="this.style.background='#f3f4f6'">
|
|
88
|
-
Dismiss
|
|
89
|
-
</button>
|
|
90
|
-
</div>
|
|
91
|
-
`;
|
|
92
|
-
errorDiv.appendChild(content);
|
|
93
|
-
document.body.appendChild(errorDiv);
|
|
94
|
-
console.error(
|
|
95
|
-
"%c\u26A0\uFE0F Enfyra SDK Configuration Error",
|
|
96
|
-
"color: #ef4444; font-size: 16px; font-weight: bold;",
|
|
97
|
-
"\n\n" + (config?.configErrorMessage || "Missing required configuration") + '\n\nPlease add the following to your nuxt.config.ts:\n\nenfyraSDK: {\n apiUrl: "https://your-api-url"\n}'
|
|
98
|
-
);
|
|
99
|
-
};
|
|
100
|
-
if (document.readyState === "loading") {
|
|
101
|
-
document.addEventListener("DOMContentLoaded", showError);
|
|
102
|
-
} else {
|
|
103
|
-
showError();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
defineEventHandler,
|
|
3
|
-
readBody,
|
|
4
|
-
setCookie,
|
|
5
|
-
createError,
|
|
6
|
-
sendError,
|
|
7
|
-
getHeader
|
|
8
|
-
} from "h3";
|
|
9
|
-
import { useRuntimeConfig } from "#imports";
|
|
10
|
-
import { $fetch } from "ofetch";
|
|
11
|
-
import {
|
|
12
|
-
ACCESS_TOKEN_KEY,
|
|
13
|
-
REFRESH_TOKEN_KEY,
|
|
14
|
-
EXP_TIME_KEY
|
|
15
|
-
} from "../../../constants/auth";
|
|
16
|
-
import { normalizeUrl } from "../../../utils/url";
|
|
17
|
-
export default defineEventHandler(async (event) => {
|
|
18
|
-
const config = useRuntimeConfig();
|
|
19
|
-
const apiUrl = config.public.enfyraSDK?.apiUrl;
|
|
20
|
-
try {
|
|
21
|
-
const body = await readBody(event);
|
|
22
|
-
const response = await $fetch(normalizeUrl(apiUrl, "/auth/login"), {
|
|
23
|
-
method: "POST",
|
|
24
|
-
body,
|
|
25
|
-
headers: {
|
|
26
|
-
cookie: getHeader(event, "cookie") || ""
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
const { accessToken, refreshToken, expTime } = response;
|
|
30
|
-
const cookieOptions = {
|
|
31
|
-
httpOnly: true,
|
|
32
|
-
secure: true,
|
|
33
|
-
sameSite: "lax",
|
|
34
|
-
path: "/"
|
|
35
|
-
};
|
|
36
|
-
setCookie(event, ACCESS_TOKEN_KEY, accessToken, cookieOptions);
|
|
37
|
-
setCookie(event, REFRESH_TOKEN_KEY, refreshToken, cookieOptions);
|
|
38
|
-
setCookie(event, EXP_TIME_KEY, String(expTime), cookieOptions);
|
|
39
|
-
return { accessToken };
|
|
40
|
-
} catch (err) {
|
|
41
|
-
const statusCode = err?.response?.status || err?.statusCode || 401;
|
|
42
|
-
const errorData = err?.response?._data || err?.data;
|
|
43
|
-
let errorMessage = "Authentication failed";
|
|
44
|
-
let errorCode = "AUTHENTICATION_ERROR";
|
|
45
|
-
if (errorData?.error) {
|
|
46
|
-
errorMessage = errorData.error.message || errorData.message || errorMessage;
|
|
47
|
-
errorCode = errorData.error.code || errorCode;
|
|
48
|
-
}
|
|
49
|
-
return sendError(
|
|
50
|
-
event,
|
|
51
|
-
createError({
|
|
52
|
-
statusCode,
|
|
53
|
-
statusMessage: errorMessage,
|
|
54
|
-
data: {
|
|
55
|
-
code: errorCode,
|
|
56
|
-
details: errorData?.error?.details,
|
|
57
|
-
correlationId: errorData?.error?.correlationId
|
|
58
|
-
}
|
|
59
|
-
})
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { defineEventHandler, getHeader, getCookie, deleteCookie } from "h3";
|
|
2
|
-
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
import { $fetch } from "ofetch";
|
|
4
|
-
import {
|
|
5
|
-
ACCESS_TOKEN_KEY,
|
|
6
|
-
REFRESH_TOKEN_KEY,
|
|
7
|
-
EXP_TIME_KEY
|
|
8
|
-
} from "../../../constants/auth";
|
|
9
|
-
import { normalizeUrl } from "../../../utils/url";
|
|
10
|
-
export default defineEventHandler(async (event) => {
|
|
11
|
-
const config = useRuntimeConfig();
|
|
12
|
-
const apiUrl = config.public?.enfyraSDK?.apiUrl;
|
|
13
|
-
const refreshToken = getCookie(event, REFRESH_TOKEN_KEY);
|
|
14
|
-
try {
|
|
15
|
-
const result = await $fetch(normalizeUrl(apiUrl, "/auth/logout"), {
|
|
16
|
-
method: "POST",
|
|
17
|
-
headers: {
|
|
18
|
-
cookie: getHeader(event, "cookie") || "",
|
|
19
|
-
authorization: event.context.proxyHeaders?.authorization
|
|
20
|
-
},
|
|
21
|
-
body: {
|
|
22
|
-
refreshToken
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
deleteCookie(event, ACCESS_TOKEN_KEY);
|
|
26
|
-
deleteCookie(event, REFRESH_TOKEN_KEY);
|
|
27
|
-
deleteCookie(event, EXP_TIME_KEY);
|
|
28
|
-
return result;
|
|
29
|
-
} catch (err) {
|
|
30
|
-
deleteCookie(event, ACCESS_TOKEN_KEY);
|
|
31
|
-
deleteCookie(event, REFRESH_TOKEN_KEY);
|
|
32
|
-
deleteCookie(event, EXP_TIME_KEY);
|
|
33
|
-
return { success: false, message: "Logout completed locally" };
|
|
34
|
-
}
|
|
35
|
-
});
|