@enfyra/sdk-nuxt 0.3.19 → 0.3.20
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/auth.d.ts.map +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/module.cjs +38 -23
- package/dist/module.json +4 -1
- package/dist/module.mjs +37 -22
- package/dist/{composables/useEnfyraApi.mjs → runtime/composables/useEnfyraApi.js} +3 -3
- package/dist/{composables/useEnfyraAuth.mjs → runtime/composables/useEnfyraAuth.js} +1 -1
- package/dist/runtime/server/api/all.js +1 -1
- package/dist/runtime/server/api/login.post.js +2 -2
- package/dist/runtime/server/api/logout.post.js +2 -2
- package/dist/runtime/server/middleware/auth.js +2 -2
- package/dist/runtime/shims.d.ts +72 -0
- package/dist/runtime/utils/http.d.ts +0 -0
- package/dist/runtime/utils/server/proxy.d.ts +0 -0
- package/dist/{utils/server/proxy.mjs → runtime/utils/server/proxy.js} +2 -2
- package/dist/runtime/utils/server/refreshToken.d.ts +0 -0
- package/dist/{utils/server/refreshToken.mjs → runtime/utils/server/refreshToken.js} +2 -2
- package/dist/runtime/utils/url.d.ts +0 -0
- package/module.d.ts +16 -6
- package/package.json +9 -11
- package/src/module.ts +54 -34
- package/src/{composables → runtime/composables}/useEnfyraApi.ts +17 -17
- 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 +72 -0
- package/src/{utils → runtime/utils}/server/proxy.ts +1 -1
- package/src/{utils → runtime/utils}/server/refreshToken.ts +3 -3
- package/dist/composables/useEnfyraApi.d.ts +0 -8
- package/dist/composables/useEnfyraApi.d.ts.map +0 -1
- package/dist/composables/useEnfyraApi.js +0 -363
- package/dist/composables/useEnfyraAuth.d.ts +0 -89
- package/dist/composables/useEnfyraAuth.d.ts.map +0 -1
- package/dist/composables/useEnfyraAuth.js +0 -77
- package/dist/constants/config.d.ts +0 -2
- 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.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/utils/config.mjs +0 -16
- package/dist/utils/http.d.ts +0 -8
- package/dist/utils/http.d.ts.map +0 -1
- package/dist/utils/http.js +0 -57
- 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/{types/auth.d.ts → auth.d.ts} +0 -0
- /package/dist/{types/auth.js → auth.js} +0 -0
- /package/dist/{types/index.d.ts → index.d.ts} +0 -0
- /package/dist/{types/index.js → index.js} +0 -0
- /package/dist/{constants/auth.d.ts → runtime/composables/useEnfyraApi.d.ts} +0 -0
- /package/dist/{utils/config.d.ts → runtime/composables/useEnfyraAuth.d.ts} +0 -0
- /package/dist/{utils/server/proxy.d.ts → runtime/constants/auth.d.ts} +0 -0
- /package/dist/{constants/auth.mjs → runtime/constants/auth.js} +0 -0
- /package/dist/{utils/server/refreshToken.d.ts → runtime/constants/config.d.ts} +0 -0
- /package/dist/{constants → runtime/constants}/config.js +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
- /package/src/{utils → runtime/utils}/url.ts +0 -0
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
UseEnfyraApiSSRReturn,
|
|
7
7
|
UseEnfyraApiClientReturn,
|
|
8
8
|
BatchProgress,
|
|
9
|
-
} from "
|
|
9
|
+
} from "../../types";
|
|
10
10
|
import { $fetch } from "../utils/http";
|
|
11
11
|
import { getAppUrl, normalizeUrl } from "../utils/url";
|
|
12
12
|
import { ENFYRA_API_PREFIX } from "../constants/config";
|
|
@@ -68,13 +68,13 @@ export function useEnfyraApi<T = any>(
|
|
|
68
68
|
const clientHeaders = process.client
|
|
69
69
|
? {}
|
|
70
70
|
: useRequestHeaders([
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
"authorization",
|
|
72
|
+
"cookie",
|
|
73
|
+
"user-agent",
|
|
74
|
+
"accept",
|
|
75
|
+
"accept-language",
|
|
76
|
+
"referer",
|
|
77
|
+
]);
|
|
78
78
|
|
|
79
79
|
const serverHeaders = { ...clientHeaders };
|
|
80
80
|
delete serverHeaders.connection;
|
|
@@ -83,7 +83,7 @@ export function useEnfyraApi<T = any>(
|
|
|
83
83
|
delete serverHeaders["content-length"];
|
|
84
84
|
|
|
85
85
|
const nuxtApp = useNuxtApp()
|
|
86
|
-
|
|
86
|
+
|
|
87
87
|
const fetchOptions: any = {
|
|
88
88
|
method: method as any,
|
|
89
89
|
body: body,
|
|
@@ -141,7 +141,7 @@ export function useEnfyraApi<T = any>(
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
const result = useFetch<T>(finalUrl, fetchOptions);
|
|
144
|
-
|
|
144
|
+
|
|
145
145
|
// Map pending to loading for better naming
|
|
146
146
|
// useFetch returns AsyncData with 'pending', but UseEnfyraApiSSRReturn uses 'loading'
|
|
147
147
|
return {
|
|
@@ -207,13 +207,13 @@ export function useEnfyraApi<T = any>(
|
|
|
207
207
|
|
|
208
208
|
const chunks = effectiveBatchSize
|
|
209
209
|
? Array.from(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
210
|
+
{ length: Math.ceil(items.length / effectiveBatchSize) },
|
|
211
|
+
(_, i) =>
|
|
212
|
+
items.slice(
|
|
213
|
+
i * effectiveBatchSize,
|
|
214
|
+
i * effectiveBatchSize + effectiveBatchSize
|
|
215
|
+
)
|
|
216
|
+
)
|
|
217
217
|
: [items];
|
|
218
218
|
|
|
219
219
|
const totalBatches = chunks.length;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ref, computed } from "vue";
|
|
2
|
-
import type { LoginPayload, User, UseEnfyraAuthReturn } from "
|
|
2
|
+
import type { LoginPayload, User, UseEnfyraAuthReturn } from "../../types/auth";
|
|
3
3
|
import { useEnfyraApi } from "./useEnfyraApi";
|
|
4
4
|
|
|
5
5
|
const me = ref<User | null>(null);
|
|
@@ -33,7 +33,7 @@ export function useEnfyraAuth() {
|
|
|
33
33
|
|
|
34
34
|
try {
|
|
35
35
|
const queryParams: any = {};
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
if (options?.fields && options.fields.length > 0) {
|
|
38
38
|
queryParams.fields = options.fields.join(",");
|
|
39
39
|
}
|
|
@@ -12,8 +12,8 @@ import {
|
|
|
12
12
|
ACCESS_TOKEN_KEY,
|
|
13
13
|
REFRESH_TOKEN_KEY,
|
|
14
14
|
EXP_TIME_KEY,
|
|
15
|
-
} from "
|
|
16
|
-
import { normalizeUrl } from "
|
|
15
|
+
} from "../../constants/auth";
|
|
16
|
+
import { normalizeUrl } from "../../utils/url";
|
|
17
17
|
|
|
18
18
|
export default defineEventHandler(async (event) => {
|
|
19
19
|
const config = useRuntimeConfig();
|
|
@@ -5,8 +5,8 @@ import {
|
|
|
5
5
|
ACCESS_TOKEN_KEY,
|
|
6
6
|
REFRESH_TOKEN_KEY,
|
|
7
7
|
EXP_TIME_KEY,
|
|
8
|
-
} from "
|
|
9
|
-
import { normalizeUrl } from "
|
|
8
|
+
} from "../../constants/auth";
|
|
9
|
+
import { normalizeUrl } from "../../utils/url";
|
|
10
10
|
|
|
11
11
|
export default defineEventHandler(async (event) => {
|
|
12
12
|
const config = useRuntimeConfig();
|
|
@@ -3,8 +3,8 @@ import { useRuntimeConfig } from "#imports";
|
|
|
3
3
|
import {
|
|
4
4
|
validateTokens,
|
|
5
5
|
refreshAccessToken,
|
|
6
|
-
} from "
|
|
7
|
-
import { REFRESH_TOKEN_KEY } from "
|
|
6
|
+
} from "../../utils/server/refreshToken";
|
|
7
|
+
import { REFRESH_TOKEN_KEY } from "../../constants/auth";
|
|
8
8
|
|
|
9
9
|
export default defineEventHandler(async (event) => {
|
|
10
10
|
if (
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type shims for Nuxt auto-imports used in module runtime.
|
|
3
|
+
* These types are provided by Nuxt at runtime but need declarations for TypeScript.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
declare module "#imports" {
|
|
7
|
+
// Nuxt composables
|
|
8
|
+
export const useRuntimeConfig: () => {
|
|
9
|
+
public: {
|
|
10
|
+
enfyraSDK?: {
|
|
11
|
+
apiUrl?: string;
|
|
12
|
+
apiPrefix?: string;
|
|
13
|
+
configError?: boolean;
|
|
14
|
+
configErrorMessage?: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
[key: string]: any;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const useRequestHeaders: (
|
|
21
|
+
headers?: string[]
|
|
22
|
+
) => Record<string, string | undefined>;
|
|
23
|
+
|
|
24
|
+
export const useRequestURL: () => URL;
|
|
25
|
+
|
|
26
|
+
export const useFetch: <T = any>(
|
|
27
|
+
url: string | (() => string),
|
|
28
|
+
options?: any
|
|
29
|
+
) => any;
|
|
30
|
+
|
|
31
|
+
export const useNuxtApp: () => {
|
|
32
|
+
payload: {
|
|
33
|
+
data: Record<string, any>;
|
|
34
|
+
[key: string]: any;
|
|
35
|
+
};
|
|
36
|
+
static: {
|
|
37
|
+
data: Record<string, any>;
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
};
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Nuxt plugin
|
|
44
|
+
export const defineNuxtPlugin: (plugin: any) => any;
|
|
45
|
+
|
|
46
|
+
// Server utilities (from nitro)
|
|
47
|
+
export const defineCachedEventHandler: (
|
|
48
|
+
handler: any,
|
|
49
|
+
options?: any
|
|
50
|
+
) => any;
|
|
51
|
+
export const getCookie: (
|
|
52
|
+
event: any,
|
|
53
|
+
name: string
|
|
54
|
+
) => string | undefined;
|
|
55
|
+
export const setCookie: (
|
|
56
|
+
event: any,
|
|
57
|
+
name: string,
|
|
58
|
+
value: string,
|
|
59
|
+
options?: any
|
|
60
|
+
) => void;
|
|
61
|
+
export const deleteCookie: (
|
|
62
|
+
event: any,
|
|
63
|
+
name: string,
|
|
64
|
+
options?: any
|
|
65
|
+
) => void;
|
|
66
|
+
export const createError: (options: {
|
|
67
|
+
statusCode?: number;
|
|
68
|
+
statusMessage?: string;
|
|
69
|
+
message?: string;
|
|
70
|
+
data?: any;
|
|
71
|
+
}) => Error;
|
|
72
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { H3Event, proxyRequest } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "#imports";
|
|
3
3
|
import { ENFYRA_API_PREFIX } from "../../constants/config";
|
|
4
|
-
import { normalizeUrl } from "
|
|
4
|
+
import { normalizeUrl } from "../url";
|
|
5
5
|
|
|
6
6
|
export function proxyToAPI(event: H3Event, customPath?: string) {
|
|
7
7
|
const config = useRuntimeConfig();
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
REFRESH_TOKEN_KEY,
|
|
6
6
|
EXP_TIME_KEY,
|
|
7
7
|
} from "../../constants/auth";
|
|
8
|
-
import { normalizeUrl } from "
|
|
8
|
+
import { normalizeUrl } from "../url";
|
|
9
9
|
|
|
10
10
|
interface TokenValidationResult {
|
|
11
11
|
accessToken: string | null;
|
|
@@ -18,7 +18,7 @@ export function decodeJWT(token: string): any {
|
|
|
18
18
|
if (parts.length !== 3) {
|
|
19
19
|
return null;
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
|
|
22
22
|
// Decode the payload (second part)
|
|
23
23
|
const payload = parts[1];
|
|
24
24
|
const decodedPayload = Buffer.from(payload, "base64url").toString("utf-8");
|
|
@@ -34,7 +34,7 @@ export function isAccessTokenExpired(accessToken: string): boolean {
|
|
|
34
34
|
if (!decoded || !decoded.exp) {
|
|
35
35
|
return true;
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
// JWT exp is in seconds, Date.now() is in milliseconds
|
|
39
39
|
const expirationTime = decoded.exp * 1000;
|
|
40
40
|
return Date.now() >= expirationTime;
|
|
@@ -1,8 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -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
|
-
}
|