@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
package/package.json
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enfyra/sdk-nuxt",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Nuxt SDK for Enfyra CMS",
|
|
5
6
|
"repository": {
|
|
6
7
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/
|
|
8
|
+
"url": "https://github.com/enfyra/sdk-nuxt.git"
|
|
8
9
|
},
|
|
9
10
|
"bugs": {
|
|
10
|
-
"url": "https://github.com/
|
|
11
|
+
"url": "https://github.com/enfyra/sdk-nuxt/issues"
|
|
11
12
|
},
|
|
12
|
-
"homepage": "https://github.com/
|
|
13
|
+
"homepage": "https://github.com/enfyra/sdk-nuxt#readme",
|
|
13
14
|
"main": "./dist/module.mjs",
|
|
14
15
|
"types": "./dist/types.d.ts",
|
|
15
16
|
"exports": {
|
|
@@ -18,17 +19,13 @@
|
|
|
18
19
|
"require": "./dist/module.cjs",
|
|
19
20
|
"types": "./dist/types.d.ts"
|
|
20
21
|
},
|
|
21
|
-
"./
|
|
22
|
-
"types": "./
|
|
23
|
-
"import": "./
|
|
24
|
-
},
|
|
25
|
-
"./src/composables/useEnfyraAuth": {
|
|
26
|
-
"types": "./src/composables/useEnfyraAuth.ts",
|
|
27
|
-
"import": "./src/composables/useEnfyraAuth.ts"
|
|
22
|
+
"./runtime/*": {
|
|
23
|
+
"types": "./dist/runtime/*.d.ts",
|
|
24
|
+
"import": "./dist/runtime/*.js"
|
|
28
25
|
},
|
|
29
26
|
"./types": {
|
|
30
|
-
"types": "./dist/
|
|
31
|
-
"import": "./dist/
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
32
29
|
}
|
|
33
30
|
},
|
|
34
31
|
"files": [
|
|
@@ -61,6 +58,7 @@
|
|
|
61
58
|
"vue": "^3.0.0"
|
|
62
59
|
},
|
|
63
60
|
"dependencies": {
|
|
61
|
+
"defu": "^6.1.4",
|
|
64
62
|
"h3": "^1.15.4",
|
|
65
63
|
"ofetch": "^1.3.3"
|
|
66
64
|
},
|
|
@@ -74,4 +72,4 @@
|
|
|
74
72
|
"vite-plugin-dts": "^4.3.0",
|
|
75
73
|
"vitest": "^3.2.4"
|
|
76
74
|
}
|
|
77
|
-
}
|
|
75
|
+
}
|
package/src/module.ts
CHANGED
|
@@ -2,10 +2,13 @@ import {
|
|
|
2
2
|
defineNuxtModule,
|
|
3
3
|
createResolver,
|
|
4
4
|
addServerHandler,
|
|
5
|
-
|
|
5
|
+
addImports,
|
|
6
6
|
addPlugin,
|
|
7
|
+
addTypeTemplate,
|
|
8
|
+
useLogger,
|
|
7
9
|
} from "@nuxt/kit";
|
|
8
|
-
import {
|
|
10
|
+
import { defu } from "defu";
|
|
11
|
+
import { ENFYRA_API_PREFIX } from "./runtime/constants/config";
|
|
9
12
|
|
|
10
13
|
export interface ModuleOptions {
|
|
11
14
|
apiUrl: string;
|
|
@@ -16,12 +19,18 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
16
19
|
meta: {
|
|
17
20
|
name: "@enfyra/sdk-nuxt",
|
|
18
21
|
configKey: "enfyraSDK",
|
|
22
|
+
compatibility: {
|
|
23
|
+
nuxt: ">=3.0.0",
|
|
24
|
+
},
|
|
19
25
|
},
|
|
20
26
|
defaults: {
|
|
21
27
|
apiUrl: "",
|
|
22
28
|
apiPrefix: ENFYRA_API_PREFIX,
|
|
23
29
|
},
|
|
24
30
|
setup(options, nuxt) {
|
|
31
|
+
const logger = useLogger("enfyra-sdk");
|
|
32
|
+
const { resolve } = createResolver(import.meta.url);
|
|
33
|
+
|
|
25
34
|
const normalizedOptions = {
|
|
26
35
|
...options,
|
|
27
36
|
apiUrl:
|
|
@@ -30,53 +39,84 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
30
39
|
: options.apiUrl,
|
|
31
40
|
apiPrefix:
|
|
32
41
|
typeof options.apiPrefix === "string"
|
|
33
|
-
?
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
? options.apiPrefix.trim()
|
|
43
|
+
? "/" +
|
|
44
|
+
options.apiPrefix
|
|
45
|
+
.replace(/^\/+|\/+$/g, "")
|
|
46
|
+
.replace(/\/+/g, "/")
|
|
47
|
+
: ENFYRA_API_PREFIX
|
|
36
48
|
: ENFYRA_API_PREFIX,
|
|
37
49
|
};
|
|
38
50
|
|
|
39
51
|
const apiPrefix = normalizedOptions.apiPrefix;
|
|
40
|
-
const { resolve } = createResolver(import.meta.url);
|
|
41
52
|
|
|
42
53
|
if (!normalizedOptions.apiUrl) {
|
|
43
|
-
|
|
44
|
-
`
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
54
|
+
logger.warn(
|
|
55
|
+
`Missing required configuration:\n` +
|
|
56
|
+
`- apiUrl is required\n` +
|
|
57
|
+
`Please configure it in your nuxt.config.ts:\n` +
|
|
58
|
+
`enfyraSDK: {\n` +
|
|
59
|
+
` apiUrl: 'https://your-api-url'\n` +
|
|
60
|
+
`}`
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
nuxt.options.runtimeConfig.public.enfyraSDK = defu(
|
|
64
|
+
nuxt.options.runtimeConfig.public.enfyraSDK || {},
|
|
65
|
+
{
|
|
66
|
+
...normalizedOptions,
|
|
67
|
+
apiPrefix: apiPrefix,
|
|
68
|
+
configError: true,
|
|
69
|
+
configErrorMessage:
|
|
70
|
+
"Enfyra SDK: apiUrl is required. Please configure it in nuxt.config.ts",
|
|
71
|
+
}
|
|
50
72
|
);
|
|
51
|
-
|
|
52
|
-
nuxt.options.runtimeConfig.public.enfyraSDK = {
|
|
53
|
-
...normalizedOptions,
|
|
54
|
-
apiPrefix: apiPrefix,
|
|
55
|
-
configError: true,
|
|
56
|
-
configErrorMessage: 'Enfyra SDK: apiUrl is required. Please configure it in nuxt.config.ts'
|
|
57
|
-
};
|
|
58
73
|
} else {
|
|
59
|
-
nuxt.options.runtimeConfig.public.enfyraSDK =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
74
|
+
nuxt.options.runtimeConfig.public.enfyraSDK = defu(
|
|
75
|
+
nuxt.options.runtimeConfig.public.enfyraSDK || {},
|
|
76
|
+
{
|
|
77
|
+
...normalizedOptions,
|
|
78
|
+
apiPrefix: apiPrefix,
|
|
79
|
+
}
|
|
80
|
+
);
|
|
63
81
|
}
|
|
64
82
|
|
|
65
|
-
|
|
66
83
|
if (!normalizedOptions.apiUrl) {
|
|
67
84
|
addPlugin({
|
|
68
85
|
src: resolve("./runtime/plugin/config-error.client"),
|
|
69
|
-
mode:
|
|
86
|
+
mode: "client",
|
|
70
87
|
});
|
|
71
88
|
}
|
|
72
|
-
|
|
73
|
-
addImportsDir(resolve("./composables"));
|
|
74
89
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
90
|
+
addImports([
|
|
91
|
+
{
|
|
92
|
+
name: "useEnfyraApi",
|
|
93
|
+
from: resolve("./runtime/composables/useEnfyraApi"),
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "useEnfyraAuth",
|
|
97
|
+
from: resolve("./runtime/composables/useEnfyraAuth"),
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
addTypeTemplate({
|
|
101
|
+
filename: "types/enfyra-sdk.d.ts",
|
|
102
|
+
getContents: () => `
|
|
103
|
+
import type { ApiOptions, UseEnfyraApiSSRReturn, UseEnfyraApiClientReturn } from '@enfyra/sdk-nuxt/types'
|
|
104
|
+
|
|
105
|
+
declare module '#imports' {
|
|
106
|
+
export function useEnfyraApi<T = any>(
|
|
107
|
+
path: (() => string) | string,
|
|
108
|
+
opts: ApiOptions<T> & { ssr: true }
|
|
109
|
+
): UseEnfyraApiSSRReturn<T>
|
|
110
|
+
|
|
111
|
+
export function useEnfyraApi<T = any>(
|
|
112
|
+
path: (() => string) | string,
|
|
113
|
+
opts?: ApiOptions<T> & { ssr?: false | undefined }
|
|
114
|
+
): UseEnfyraApiClientReturn<T>
|
|
115
|
+
|
|
116
|
+
export function useEnfyraAuth(): import('@enfyra/sdk-nuxt/types').UseEnfyraAuthReturn
|
|
117
|
+
}
|
|
118
|
+
`,
|
|
119
|
+
});
|
|
80
120
|
|
|
81
121
|
addServerHandler({
|
|
82
122
|
handler: resolve("./runtime/server/middleware/auth"),
|
|
@@ -95,7 +135,6 @@ export default defineNuxtModule<ModuleOptions>({
|
|
|
95
135
|
method: "post",
|
|
96
136
|
});
|
|
97
137
|
|
|
98
|
-
|
|
99
138
|
addServerHandler({
|
|
100
139
|
route: "/assets/**",
|
|
101
140
|
handler: resolve("./runtime/server/api/all"),
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ref, unref, toRaw } from "vue";
|
|
1
|
+
import { ref, unref, toRaw, computed } from "vue";
|
|
2
2
|
import type {
|
|
3
3
|
ApiOptions,
|
|
4
4
|
ApiError,
|
|
@@ -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,
|
|
@@ -94,7 +94,6 @@ export function useEnfyraApi<T = any>(
|
|
|
94
94
|
},
|
|
95
95
|
};
|
|
96
96
|
|
|
97
|
-
// Pass all useFetch-specific options
|
|
98
97
|
if (key !== undefined) {
|
|
99
98
|
fetchOptions.key = key;
|
|
100
99
|
}
|
|
@@ -125,7 +124,6 @@ export function useEnfyraApi<T = any>(
|
|
|
125
124
|
if (opts.getCachedData !== undefined) {
|
|
126
125
|
fetchOptions.getCachedData = opts.getCachedData;
|
|
127
126
|
} else {
|
|
128
|
-
// Auto-add getCachedData to ensure cache is used in SPA navigation
|
|
129
127
|
fetchOptions.getCachedData = (cacheKey: string) => {
|
|
130
128
|
return nuxtApp.payload.data[cacheKey] || nuxtApp.static.data[cacheKey]
|
|
131
129
|
}
|
|
@@ -141,13 +139,13 @@ export function useEnfyraApi<T = any>(
|
|
|
141
139
|
}
|
|
142
140
|
|
|
143
141
|
const result = useFetch<T>(finalUrl, fetchOptions);
|
|
142
|
+
|
|
144
143
|
|
|
145
|
-
|
|
146
|
-
|
|
144
|
+
const loading = computed(() => result.status.value === 'pending');
|
|
145
|
+
|
|
147
146
|
return {
|
|
148
147
|
...result,
|
|
149
|
-
loading
|
|
150
|
-
pending: result.pending, // Keep for backward compatibility
|
|
148
|
+
loading,
|
|
151
149
|
} as UseEnfyraApiSSRReturn<T>;
|
|
152
150
|
}
|
|
153
151
|
const data = ref<T | null>(null);
|
|
@@ -207,13 +205,13 @@ export function useEnfyraApi<T = any>(
|
|
|
207
205
|
|
|
208
206
|
const chunks = effectiveBatchSize
|
|
209
207
|
? Array.from(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
208
|
+
{ length: Math.ceil(items.length / effectiveBatchSize) },
|
|
209
|
+
(_, i) =>
|
|
210
|
+
items.slice(
|
|
211
|
+
i * effectiveBatchSize,
|
|
212
|
+
i * effectiveBatchSize + effectiveBatchSize
|
|
213
|
+
)
|
|
214
|
+
)
|
|
217
215
|
: [items];
|
|
218
216
|
|
|
219
217
|
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,64 @@
|
|
|
1
|
+
declare module "#imports" {
|
|
2
|
+
export const useRuntimeConfig: () => {
|
|
3
|
+
public: {
|
|
4
|
+
enfyraSDK?: {
|
|
5
|
+
apiUrl?: string;
|
|
6
|
+
apiPrefix?: string;
|
|
7
|
+
configError?: boolean;
|
|
8
|
+
configErrorMessage?: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
[key: string]: any;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const useRequestHeaders: (
|
|
15
|
+
headers?: string[]
|
|
16
|
+
) => Record<string, string | undefined>;
|
|
17
|
+
|
|
18
|
+
export const useRequestURL: () => URL;
|
|
19
|
+
|
|
20
|
+
export const useFetch: <T = any>(
|
|
21
|
+
url: string | (() => string),
|
|
22
|
+
options?: any
|
|
23
|
+
) => any;
|
|
24
|
+
|
|
25
|
+
export const useNuxtApp: () => {
|
|
26
|
+
payload: {
|
|
27
|
+
data: Record<string, any>;
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
};
|
|
30
|
+
static: {
|
|
31
|
+
data: Record<string, any>;
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
};
|
|
34
|
+
[key: string]: any;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const defineNuxtPlugin: (plugin: any) => any;
|
|
38
|
+
|
|
39
|
+
export const defineCachedEventHandler: (
|
|
40
|
+
handler: any,
|
|
41
|
+
options?: any
|
|
42
|
+
) => any;
|
|
43
|
+
export const getCookie: (
|
|
44
|
+
event: any,
|
|
45
|
+
name: string
|
|
46
|
+
) => string | undefined;
|
|
47
|
+
export const setCookie: (
|
|
48
|
+
event: any,
|
|
49
|
+
name: string,
|
|
50
|
+
value: string,
|
|
51
|
+
options?: any
|
|
52
|
+
) => void;
|
|
53
|
+
export const deleteCookie: (
|
|
54
|
+
event: any,
|
|
55
|
+
name: string,
|
|
56
|
+
options?: any
|
|
57
|
+
) => void;
|
|
58
|
+
export const createError: (options: {
|
|
59
|
+
statusCode?: number;
|
|
60
|
+
statusMessage?: string;
|
|
61
|
+
message?: string;
|
|
62
|
+
data?: any;
|
|
63
|
+
}) => Error;
|
|
64
|
+
}
|
|
@@ -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,8 +18,7 @@ export function decodeJWT(token: string): any {
|
|
|
18
18
|
if (parts.length !== 3) {
|
|
19
19
|
return null;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
// Decode the payload (second part)
|
|
21
|
+
|
|
23
22
|
const payload = parts[1];
|
|
24
23
|
const decodedPayload = Buffer.from(payload, "base64url").toString("utf-8");
|
|
25
24
|
return JSON.parse(decodedPayload);
|
|
@@ -34,8 +33,7 @@ export function isAccessTokenExpired(accessToken: string): boolean {
|
|
|
34
33
|
if (!decoded || !decoded.exp) {
|
|
35
34
|
return true;
|
|
36
35
|
}
|
|
37
|
-
|
|
38
|
-
// JWT exp is in seconds, Date.now() is in milliseconds
|
|
36
|
+
|
|
39
37
|
const expirationTime = decoded.exp * 1000;
|
|
40
38
|
return Date.now() >= expirationTime;
|
|
41
39
|
}
|
|
@@ -84,7 +82,6 @@ export async function refreshAccessToken(
|
|
|
84
82
|
return newAccessToken;
|
|
85
83
|
} catch (error) {
|
|
86
84
|
console.warn("Token refresh failed:", error);
|
|
87
|
-
// On refresh failure, clear all auth-related cookies at server side
|
|
88
85
|
deleteCookie(event, ACCESS_TOKEN_KEY);
|
|
89
86
|
deleteCookie(event, REFRESH_TOKEN_KEY);
|
|
90
87
|
deleteCookie(event, EXP_TIME_KEY);
|
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Normalizes URL segments to avoid double slashes when concatenating
|
|
3
|
-
* Removes trailing slashes from base URL and leading slashes from path segments
|
|
4
|
-
* Also normalizes multiple consecutive slashes within segments
|
|
5
|
-
*/
|
|
6
1
|
export function normalizeUrl(...segments: (string | undefined | null)[]): string {
|
|
7
2
|
const validSegments = segments.filter((s): s is string => Boolean(s));
|
|
8
3
|
if (validSegments.length === 0) return '';
|
|
9
4
|
|
|
10
|
-
// First segment is the base URL - remove trailing slashes
|
|
11
5
|
let result = validSegments[0].replace(/\/+$/, '');
|
|
12
6
|
|
|
13
|
-
// For remaining segments, remove leading and trailing slashes, normalize internal slashes, then join
|
|
14
7
|
for (let i = 1; i < validSegments.length; i++) {
|
|
15
8
|
const segment = validSegments[i]
|
|
16
|
-
.replace(/^\/+/, '')
|
|
17
|
-
.replace(/\/+$/, '')
|
|
18
|
-
.replace(/\/+/g, '/');
|
|
9
|
+
.replace(/^\/+/, '')
|
|
10
|
+
.replace(/\/+$/, '')
|
|
11
|
+
.replace(/\/+/g, '/');
|
|
19
12
|
if (segment) {
|
|
20
13
|
result += '/' + segment;
|
|
21
14
|
}
|
|
@@ -24,9 +17,6 @@ export function normalizeUrl(...segments: (string | undefined | null)[]): string
|
|
|
24
17
|
return result;
|
|
25
18
|
}
|
|
26
19
|
|
|
27
|
-
/**
|
|
28
|
-
* Joins URL paths safely, avoiding double slashes
|
|
29
|
-
*/
|
|
30
20
|
export function joinUrlPath(...paths: (string | undefined | null)[]): string {
|
|
31
21
|
const validPaths = paths.filter((p): p is string => Boolean(p));
|
|
32
22
|
if (validPaths.length === 0) return '';
|
package/src/types/index.ts
CHANGED
|
@@ -120,9 +120,8 @@ import type { AsyncData } from 'nuxt/app';
|
|
|
120
120
|
export interface UseEnfyraApiSSRReturn<T> extends Omit<AsyncData<T | null, ApiError>, 'pending' | 'error'> {
|
|
121
121
|
data: Ref<T | null>;
|
|
122
122
|
loading: Ref<boolean>;
|
|
123
|
-
error: Ref<ApiError | null>;
|
|
123
|
+
error: Ref<ApiError | null>;
|
|
124
124
|
refresh: () => Promise<void>;
|
|
125
|
-
// Keep pending for backward compatibility, but it's an alias of loading
|
|
126
125
|
pending?: Ref<boolean>;
|
|
127
126
|
}
|
|
128
127
|
|
|
@@ -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"}
|