@movk/nuxt 1.8.2 → 1.8.3
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.json +1 -1
- package/dist/module.mjs +8 -4
- package/dist/runtime/domains/api/auth.js +6 -9
- package/dist/runtime/domains/api/interceptors/error.js +2 -25
- package/dist/runtime/domains/api/interceptors/response.d.ts +2 -0
- package/dist/runtime/domains/api/interceptors/response.js +7 -2
- package/dist/runtime/domains/api/interceptors/unauthorized.d.ts +12 -0
- package/dist/runtime/domains/api/interceptors/unauthorized.js +34 -0
- package/dist/runtime/domains/api/session.d.ts +12 -0
- package/dist/runtime/domains/api/session.js +9 -0
- package/dist/runtime/domains/api/transfer.js +8 -3
- package/dist/shared/{nuxt.C7xx2FPO.mjs → nuxt.DfVF6ZZv.mjs} +11 -1
- package/dist/unplugin.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +2 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { resolveAlias, useLogger, addPlugin, defineNuxtModule, createResolver, addComponentsDir, addImportsDir } from '@nuxt/kit';
|
|
2
2
|
import defu, { defu as defu$1 } from 'defu';
|
|
3
|
-
import { U as UI_COMPONENTS, r as resolveMovkIcons, f as formatMissingCollections, a as addTemplates } from './shared/nuxt.
|
|
3
|
+
import { U as UI_COMPONENTS, r as resolveMovkIcons, f as formatMissingCollections, a as addTemplates } from './shared/nuxt.DfVF6ZZv.mjs';
|
|
4
4
|
import { getPackageJsonMetadata } from '../dist/runtime/utils/meta.js';
|
|
5
5
|
import { kebabCase } from 'scule';
|
|
6
6
|
import { updateSiteConfig } from 'nuxt-site-config/kit';
|
|
@@ -15,7 +15,7 @@ import 'pathe';
|
|
|
15
15
|
import 'tinyglobby';
|
|
16
16
|
|
|
17
17
|
const name = "@movk/nuxt";
|
|
18
|
-
const version = "1.8.
|
|
18
|
+
const version = "1.8.3";
|
|
19
19
|
|
|
20
20
|
const MOVK_CSS_IMPORT = /@import\s+["']@movk\/nuxt["']/;
|
|
21
21
|
async function checkMovkCss(nuxt) {
|
|
@@ -149,16 +149,20 @@ function addFontLinks(nuxt, font) {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
function resolveModuleDependencies(nuxt) {
|
|
152
|
-
const
|
|
152
|
+
const { options } = nuxt;
|
|
153
|
+
const ui = options.ui ??= {};
|
|
153
154
|
ui.fonts ??= false;
|
|
154
155
|
return {
|
|
155
156
|
"@nuxt/image": { version: ">=2.0.0" },
|
|
156
157
|
"@nuxt/ui": { version: ">=4.6.0" },
|
|
157
158
|
"@vueuse/nuxt": { version: ">=14.2.1" },
|
|
158
|
-
"nuxt-auth-utils": { version: ">=0.5.29" },
|
|
159
|
+
...usesSessionAuth(options.movk?.api) && { "nuxt-auth-utils": { version: ">=0.5.29" } },
|
|
159
160
|
"nuxt-site-config": { version: ">=4.0.8" }
|
|
160
161
|
};
|
|
161
162
|
}
|
|
163
|
+
function usesSessionAuth(api) {
|
|
164
|
+
return api?.enabled !== false && api?.auth?.enabled === true && (api.auth.tokenSource ?? "session") === "session";
|
|
165
|
+
}
|
|
162
166
|
const movkModule = defineNuxtModule({
|
|
163
167
|
meta: {
|
|
164
168
|
name,
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { getPath } from "@movk/core";
|
|
2
|
-
import { useNuxtApp, useRuntimeConfig
|
|
3
|
-
|
|
2
|
+
import { useNuxtApp, useRuntimeConfig } from "#imports";
|
|
3
|
+
import { getUserSession } from "./session.js";
|
|
4
|
+
function getTokenFromSession(tokenPath) {
|
|
5
|
+
let sessionData;
|
|
4
6
|
try {
|
|
5
|
-
|
|
6
|
-
return nuxtApp.vueApp.runWithContext(() => useUserSession());
|
|
7
|
+
sessionData = getUserSession(useNuxtApp())?.session?.value;
|
|
7
8
|
} catch {
|
|
8
9
|
return null;
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
function getTokenFromSession(tokenPath) {
|
|
12
|
-
const userSession = getUserSession();
|
|
13
|
-
if (!userSession?.session?.value) return null;
|
|
14
|
-
const sessionData = userSession.session.value;
|
|
11
|
+
if (!sessionData) return null;
|
|
15
12
|
return getPath(sessionData, tokenPath) || null;
|
|
16
13
|
}
|
|
17
14
|
function getTokenFromPublicRuntimeConfig(tokenPath) {
|
|
@@ -1,26 +1,8 @@
|
|
|
1
|
-
import { navigateTo, useUserSession } from "#imports";
|
|
2
1
|
import { showToast } from "../toast.js";
|
|
2
|
+
import { dispatchUnauthorized } from "./unauthorized.js";
|
|
3
3
|
function readApiContext(options) {
|
|
4
4
|
return options.context || {};
|
|
5
5
|
}
|
|
6
|
-
async function handleUnauthorized(authConfig, nuxtApp) {
|
|
7
|
-
const unauthorizedConfig = authConfig.unauthorized;
|
|
8
|
-
if (!unauthorizedConfig) return;
|
|
9
|
-
if (unauthorizedConfig.clearSession) {
|
|
10
|
-
try {
|
|
11
|
-
const userSession = nuxtApp.vueApp.runWithContext(() => useUserSession());
|
|
12
|
-
if (userSession?.clear) {
|
|
13
|
-
await userSession.clear();
|
|
14
|
-
await userSession.fetch();
|
|
15
|
-
}
|
|
16
|
-
} catch {
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
if (unauthorizedConfig.redirect) {
|
|
20
|
-
const loginPath = unauthorizedConfig.loginPath || "/login";
|
|
21
|
-
await nuxtApp.runWithContext(() => navigateTo(loginPath));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
6
|
export function createOnResponseError(resolvedConfig, publicConfig, nuxtApp) {
|
|
25
7
|
const { auth: authConfig, toast: toastConfig, response: responseConfig } = resolvedConfig;
|
|
26
8
|
return async function onResponseError(context) {
|
|
@@ -29,12 +11,7 @@ export function createOnResponseError(resolvedConfig, publicConfig, nuxtApp) {
|
|
|
29
11
|
const { toast } = readApiContext(context.options);
|
|
30
12
|
let unauthorizedHandled = false;
|
|
31
13
|
if (response.status === 401) {
|
|
32
|
-
|
|
33
|
-
await nuxtApp.callHook("movk:api:unauthorized", context, result);
|
|
34
|
-
unauthorizedHandled = result.handled;
|
|
35
|
-
if (!unauthorizedHandled) {
|
|
36
|
-
await handleUnauthorized(authConfig, nuxtApp);
|
|
37
|
-
}
|
|
14
|
+
unauthorizedHandled = await dispatchUnauthorized(context, authConfig, nuxtApp);
|
|
38
15
|
}
|
|
39
16
|
if (!unauthorizedHandled) {
|
|
40
17
|
await nuxtApp.callHook("movk:api:error", context);
|
|
@@ -9,6 +9,8 @@ type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
|
9
9
|
* @description 业务校验链路:
|
|
10
10
|
* - 校验通过(或 skipBusinessCheck)→ 按 skipUnwrap 决定是否解包 _data → 派发 movk:api:response hook → 触发成功 toast
|
|
11
11
|
* - 校验失败 → 派发 movk:api:error hook → 触发错误 toast → 抛 createApiError
|
|
12
|
+
* - 业务码为 401 → 先走与 HTTP 401 相同的未授权处理;业务侧接管时不再派发 movk:api:error
|
|
13
|
+
* - HTTP 4xx/5xx 直接返回:ofetch 先调 onResponse 再调 onResponseError,在此抛错会让后者(含 401 处理)失效
|
|
12
14
|
*
|
|
13
15
|
* **契约**:拦截器输出 = 业务数据。`movk:api:response` hook 收到的 `context.response._data` 已解包;
|
|
14
16
|
* 调用方设置 `skipUnwrap: true` 时不重写 `_data`,保留原始响应。
|
|
@@ -2,12 +2,14 @@ import { extractData, extractMessage, isBusinessSuccess } from "../response.js";
|
|
|
2
2
|
import { isReadableStream } from "../stream.js";
|
|
3
3
|
import { createApiError } from "../errors.js";
|
|
4
4
|
import { showToast } from "../toast.js";
|
|
5
|
+
import { dispatchUnauthorized, isUnauthorized } from "./unauthorized.js";
|
|
5
6
|
function readApiContext(options) {
|
|
6
7
|
return options.context || {};
|
|
7
8
|
}
|
|
8
9
|
export function createOnResponse(resolvedConfig, publicConfig, nuxtApp) {
|
|
9
|
-
const { toast: toastConfig, response: responseConfig } = resolvedConfig;
|
|
10
|
+
const { auth: authConfig, toast: toastConfig, response: responseConfig } = resolvedConfig;
|
|
10
11
|
return async function onResponse(context) {
|
|
12
|
+
if (context.response && context.response.status >= 400) return;
|
|
11
13
|
const raw = context.response?._data;
|
|
12
14
|
const { toast, skipBusinessCheck, skipUnwrap } = readApiContext(context.options);
|
|
13
15
|
if (isReadableStream(raw)) {
|
|
@@ -28,7 +30,10 @@ export function createOnResponse(resolvedConfig, publicConfig, nuxtApp) {
|
|
|
28
30
|
showToast("success", raw, toast, toastConfig, responseConfig, context.options.method);
|
|
29
31
|
return;
|
|
30
32
|
}
|
|
31
|
-
await
|
|
33
|
+
const unauthorizedHandled = isUnauthorized(void 0, raw, responseConfig) && await dispatchUnauthorized(context, authConfig, nuxtApp);
|
|
34
|
+
if (!unauthorizedHandled) {
|
|
35
|
+
await nuxtApp.callHook("movk:api:error", context);
|
|
36
|
+
}
|
|
32
37
|
showToast("error", raw, toast, toastConfig, responseConfig, context.options.method);
|
|
33
38
|
throw createApiError(raw, extractMessage(raw, responseConfig));
|
|
34
39
|
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { FetchContext } from 'ofetch';
|
|
2
|
+
import type { ApiAuthConfig, ApiResponse, ApiResponseConfig } from '../../../types/api';
|
|
3
|
+
import type { useNuxtApp } from '#imports';
|
|
4
|
+
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
5
|
+
/** HTTP 状态码或业务码(按 codeKey 读取,兼容字符串)为 401 */
|
|
6
|
+
export declare function isUnauthorized(status: number | undefined, raw: ApiResponse | null | undefined, responseConfig?: Partial<ApiResponseConfig>): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* 派发 `movk:api:unauthorized`,业务未接管时执行默认的清理会话与重定向
|
|
9
|
+
* @returns 业务侧是否已接管(`result.handled`)
|
|
10
|
+
*/
|
|
11
|
+
export declare function dispatchUnauthorized(context: FetchContext, authConfig: ApiAuthConfig, nuxtApp: NuxtApp): Promise<boolean>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { navigateTo } from "#imports";
|
|
2
|
+
import { getUserSession } from "../session.js";
|
|
3
|
+
const UNAUTHORIZED_CODE = 401;
|
|
4
|
+
export function isUnauthorized(status, raw, responseConfig = {}) {
|
|
5
|
+
if (status === UNAUTHORIZED_CODE) return true;
|
|
6
|
+
const code = raw?.[responseConfig.codeKey || "code"];
|
|
7
|
+
return code !== void 0 && code !== null && Number(code) === UNAUTHORIZED_CODE;
|
|
8
|
+
}
|
|
9
|
+
async function handleUnauthorized(authConfig, nuxtApp) {
|
|
10
|
+
const unauthorizedConfig = authConfig.unauthorized;
|
|
11
|
+
if (!unauthorizedConfig) return;
|
|
12
|
+
if (unauthorizedConfig.clearSession) {
|
|
13
|
+
const userSession = getUserSession(nuxtApp);
|
|
14
|
+
if (userSession?.clear) {
|
|
15
|
+
try {
|
|
16
|
+
await userSession.clear();
|
|
17
|
+
await userSession.fetch?.();
|
|
18
|
+
} catch {
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (unauthorizedConfig.redirect) {
|
|
23
|
+
const loginPath = unauthorizedConfig.loginPath || "/login";
|
|
24
|
+
await nuxtApp.runWithContext(() => navigateTo(loginPath));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function dispatchUnauthorized(context, authConfig, nuxtApp) {
|
|
28
|
+
const result = { handled: false };
|
|
29
|
+
await nuxtApp.callHook("movk:api:unauthorized", context, result);
|
|
30
|
+
if (!result.handled) {
|
|
31
|
+
await handleUnauthorized(authConfig, nuxtApp);
|
|
32
|
+
}
|
|
33
|
+
return result.handled;
|
|
34
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { useNuxtApp } from '#imports';
|
|
2
|
+
/** movk 用到的 nuxt-auth-utils 会话能力子集 */
|
|
3
|
+
interface UserSessionLike {
|
|
4
|
+
session?: {
|
|
5
|
+
value?: unknown;
|
|
6
|
+
};
|
|
7
|
+
clear?: () => Promise<void>;
|
|
8
|
+
fetch?: () => Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
/** 未安装 nuxt-auth-utils 或不在可用上下文中时返回 null */
|
|
11
|
+
export declare function getUserSession(nuxtApp: ReturnType<typeof useNuxtApp>): UserSessionLike | null;
|
|
12
|
+
export {};
|
|
@@ -4,6 +4,7 @@ import { resolveEndpointConfig } from "./endpoint-config.js";
|
|
|
4
4
|
import { createApiError } from "./errors.js";
|
|
5
5
|
import { extractData, extractMessage, isBusinessSuccess } from "./response.js";
|
|
6
6
|
import { showToast } from "./toast.js";
|
|
7
|
+
import { dispatchUnauthorized, isUnauthorized } from "./interceptors/unauthorized.js";
|
|
7
8
|
export function prepareTransfer(publicConfig, url, options) {
|
|
8
9
|
const config = resolveEndpointConfig(publicConfig, options.endpoint);
|
|
9
10
|
const fullUrl = `${config.baseURL || ""}${url}`;
|
|
@@ -47,13 +48,14 @@ export function parseXhrHeaders(raw) {
|
|
|
47
48
|
}
|
|
48
49
|
export async function finalizeTransfer(nuxtApp, ctx) {
|
|
49
50
|
const { raw, fallback, config, publicConfig, requestToast, skipBusinessCheck, fetchContext } = ctx;
|
|
50
|
-
const { response: responseConfig, toast: toastConfig } = config;
|
|
51
|
+
const { auth: authConfig, response: responseConfig, toast: toastConfig } = config;
|
|
52
|
+
const status = fetchContext.response?.status;
|
|
51
53
|
const method = fetchContext.options?.method;
|
|
52
54
|
if (publicConfig.debug) {
|
|
53
55
|
console.info("[@movk/nuxt] Transfer response:", raw ?? fallback);
|
|
54
56
|
}
|
|
55
57
|
const hasRaw = raw !== null;
|
|
56
|
-
const isSuccess = hasRaw ? skipBusinessCheck || isBusinessSuccess(raw, responseConfig) : fallback?.isSuccess ?? false;
|
|
58
|
+
const isSuccess = !(status !== void 0 && status >= 400) && (hasRaw ? skipBusinessCheck || isBusinessSuccess(raw, responseConfig) : fallback?.isSuccess ?? false);
|
|
57
59
|
if (isSuccess) {
|
|
58
60
|
const data = hasRaw ? skipBusinessCheck ? raw : extractData(raw, responseConfig) : fallback?.data ?? null;
|
|
59
61
|
if (fetchContext.response) {
|
|
@@ -69,7 +71,10 @@ export async function finalizeTransfer(nuxtApp, ctx) {
|
|
|
69
71
|
fetchContext.response._data = raw ?? void 0;
|
|
70
72
|
}
|
|
71
73
|
fetchContext.error = apiError;
|
|
72
|
-
await
|
|
74
|
+
const unauthorizedHandled = isUnauthorized(status, raw, responseConfig) && await dispatchUnauthorized(fetchContext, authConfig, nuxtApp);
|
|
75
|
+
if (!unauthorizedHandled) {
|
|
76
|
+
await nuxtApp.callHook("movk:api:error", fetchContext);
|
|
77
|
+
}
|
|
73
78
|
showToast("error", raw ?? message, requestToast, toastConfig, responseConfig, method);
|
|
74
79
|
return { data: null, error: apiError, aborted: false };
|
|
75
80
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'node:url';
|
|
2
2
|
import { kebabCase } from 'scule';
|
|
3
|
-
import { addTypeTemplate, addTemplate, getLayerDirectories } from '@nuxt/kit';
|
|
3
|
+
import { addTypeTemplate, addTemplate, hasNuxtModule, getLayerDirectories } from '@nuxt/kit';
|
|
4
4
|
import { resolveFontSource, resolveFontFamily } from '../../dist/runtime/domains/theme/theme-font.js';
|
|
5
5
|
import { readFileSync } from 'node:fs';
|
|
6
6
|
import { createRequire } from 'node:module';
|
|
@@ -643,6 +643,16 @@ export {}
|
|
|
643
643
|
`;
|
|
644
644
|
}
|
|
645
645
|
});
|
|
646
|
+
templates.push({
|
|
647
|
+
filename: "movk-session.ts",
|
|
648
|
+
write: true,
|
|
649
|
+
// meta.name 为 'auth-utils';经 modules 数组手动注册时按包名匹配
|
|
650
|
+
getContents: () => nuxt && (hasNuxtModule("auth-utils", nuxt) || hasNuxtModule("nuxt-auth-utils", nuxt)) ? `import { useUserSession as _useUserSession } from '#imports'
|
|
651
|
+
|
|
652
|
+
export const useUserSession: (() => unknown) | undefined = _useUserSession
|
|
653
|
+
` : `export const useUserSession: (() => unknown) | undefined = undefined
|
|
654
|
+
`
|
|
655
|
+
});
|
|
646
656
|
templates.push({
|
|
647
657
|
filename: "types/movk-api-endpoints.d.ts",
|
|
648
658
|
getContents: () => {
|
package/dist/unplugin.mjs
CHANGED
|
@@ -8,7 +8,7 @@ import MagicString from 'magic-string';
|
|
|
8
8
|
import { resolvePathSync } from 'mlly';
|
|
9
9
|
import fs from 'node:fs';
|
|
10
10
|
import path from 'node:path';
|
|
11
|
-
import { U as UI_COMPONENTS, g as getTemplates, r as resolveMovkIcons, f as formatMissingCollections } from './shared/nuxt.
|
|
11
|
+
import { U as UI_COMPONENTS, g as getTemplates, r as resolveMovkIcons, f as formatMissingCollections } from './shared/nuxt.DfVF6ZZv.mjs';
|
|
12
12
|
import { kebabCase } from 'scule';
|
|
13
13
|
import { getDefaultConfig } from '../dist/runtime/utils/theme-defaults.js';
|
|
14
14
|
import { getPackageJsonMetadata } from '../dist/runtime/utils/meta.js';
|
package/dist/vite.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@movk/nuxt",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.8.
|
|
4
|
+
"version": "1.8.3",
|
|
5
5
|
"packageManager": "pnpm@12.11.2",
|
|
6
6
|
"description": "An engineering suite built on Nuxt UI: schema-driven AutoForm (Zod v4), a full-featured DataTable, standalone UI components and composables. Get the full capabilities — including API integration with auth & progress — in Nuxt 4, and use its UI, forms, tables and theming in plain Vue + Vite via the Vite plugin.",
|
|
7
7
|
"author": "YiXuan <mhaibaraai@gmail.com>",
|
|
@@ -174,6 +174,7 @@
|
|
|
174
174
|
"eslint": "^10.12.0",
|
|
175
175
|
"happy-dom": "^20.14.6",
|
|
176
176
|
"nuxt": "^4.6.1",
|
|
177
|
+
"ofetch": "^1.5.1",
|
|
177
178
|
"release-it": "^21.1.1",
|
|
178
179
|
"typescript": "^6.0.3",
|
|
179
180
|
"unbuild": "^3.6.1",
|