@movk/nuxt 1.8.2 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +16 -6
- package/README.md +16 -6
- package/dist/module.d.mts +6 -0
- package/dist/module.json +3 -3
- package/dist/module.mjs +21 -24
- package/dist/runtime/components/AutoForm.d.vue.ts +15 -1
- package/dist/runtime/components/AutoForm.vue +48 -50
- package/dist/runtime/components/AutoForm.vue.d.ts +15 -1
- package/dist/runtime/components/DataTable.d.vue.ts +4 -2
- package/dist/runtime/components/DataTable.vue +166 -174
- package/dist/runtime/components/DataTable.vue.d.ts +4 -2
- package/dist/runtime/components/DatePicker.d.vue.ts +6 -5
- package/dist/runtime/components/DatePicker.vue +96 -43
- package/dist/runtime/components/DatePicker.vue.d.ts +6 -5
- package/dist/runtime/components/MessageBox.d.vue.ts +1 -5
- package/dist/runtime/components/MessageBox.vue +9 -7
- package/dist/runtime/components/MessageBox.vue.d.ts +1 -5
- package/dist/runtime/components/SearchForm.d.vue.ts +12 -4
- package/dist/runtime/components/SearchForm.vue +130 -120
- package/dist/runtime/components/SearchForm.vue.d.ts +12 -4
- package/dist/runtime/components/Tree.d.vue.ts +3 -1
- package/dist/runtime/components/Tree.vue +102 -140
- package/dist/runtime/components/Tree.vue.d.ts +3 -1
- package/dist/runtime/components/theme-picker/ThemePicker.vue +5 -3
- package/dist/runtime/components/theme-picker/ThemePickerButton.vue +15 -6
- package/dist/runtime/composables/index.d.ts +1 -0
- package/dist/runtime/composables/index.js +1 -0
- package/dist/runtime/composables/useApiFetch.d.ts +7 -7
- package/dist/runtime/composables/useApiFetch.js +5 -23
- package/dist/runtime/composables/useAutoForm.js +2 -2
- package/dist/runtime/composables/useClientApiFetch.d.ts +5 -5
- package/dist/runtime/composables/useDateFormatter.d.ts +32 -34
- package/dist/runtime/composables/useDateFormatter.js +75 -232
- package/dist/runtime/composables/useDownloadWithProgress.d.ts +11 -8
- package/dist/runtime/composables/useDownloadWithProgress.js +49 -132
- package/dist/runtime/composables/useMessageBox.js +3 -2
- package/dist/runtime/composables/useMovkLocale.d.ts +16 -0
- package/dist/runtime/composables/useMovkLocale.js +20 -0
- package/dist/runtime/composables/useTheme.js +8 -18
- package/dist/runtime/composables/useUploadWithProgress.d.ts +9 -6
- package/dist/runtime/composables/useUploadWithProgress.js +50 -84
- package/dist/runtime/domains/ai/chat-transport.js +1 -0
- package/dist/runtime/domains/api/adapters/xhr.d.ts +17 -0
- package/dist/runtime/domains/api/adapters/xhr.js +55 -0
- package/dist/runtime/domains/api/auth.d.ts +4 -1
- package/dist/runtime/domains/api/auth.js +9 -13
- package/dist/runtime/domains/api/client.d.ts +13 -0
- package/dist/runtime/domains/api/client.js +35 -0
- package/dist/runtime/domains/api/download.d.ts +10 -0
- package/dist/runtime/domains/api/download.js +28 -0
- package/dist/runtime/domains/api/errors.d.ts +20 -2
- package/dist/runtime/domains/api/errors.js +61 -8
- package/dist/runtime/domains/api/fetch-key.d.ts +2 -2
- package/dist/runtime/domains/api/fetch-key.js +4 -4
- package/dist/runtime/domains/api/interceptors/request.d.ts +2 -6
- package/dist/runtime/domains/api/interceptors/request.js +3 -2
- package/dist/runtime/domains/api/interceptors/response.d.ts +8 -12
- package/dist/runtime/domains/api/interceptors/response.js +34 -20
- package/dist/runtime/domains/api/report.d.ts +25 -0
- package/dist/runtime/domains/api/report.js +51 -0
- package/dist/runtime/domains/api/response.d.ts +29 -4
- package/dist/runtime/domains/api/response.js +27 -6
- package/dist/runtime/domains/api/session.d.ts +12 -0
- package/dist/runtime/domains/api/session.js +9 -0
- package/dist/runtime/domains/api/stream.d.ts +2 -1
- package/dist/runtime/domains/api/stream.js +2 -1
- package/dist/runtime/domains/api/toast.d.ts +6 -3
- package/dist/runtime/domains/api/toast.js +7 -7
- package/dist/runtime/domains/auto-form/actions.d.ts +2 -1
- package/dist/runtime/domains/auto-form/actions.js +6 -4
- package/dist/runtime/domains/auto-form/array-ops.d.ts +3 -0
- package/dist/runtime/domains/auto-form/array-ops.js +9 -0
- package/dist/runtime/domains/auto-form/async-items.d.ts +10 -0
- package/dist/runtime/domains/auto-form/async-items.js +42 -0
- package/dist/runtime/domains/auto-form/components/Array.d.vue.ts +0 -4
- package/dist/runtime/domains/auto-form/components/Array.vue +81 -73
- package/dist/runtime/domains/auto-form/components/Array.vue.d.ts +0 -4
- package/dist/runtime/domains/auto-form/components/Children.d.vue.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Children.vue +0 -5
- package/dist/runtime/domains/auto-form/components/Children.vue.d.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Field.d.vue.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Field.vue +9 -2
- package/dist/runtime/domains/auto-form/components/Field.vue.d.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Layout.d.vue.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Layout.vue +3 -6
- package/dist/runtime/domains/auto-form/components/Layout.vue.d.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Nested.d.vue.ts +0 -2
- package/dist/runtime/domains/auto-form/components/Nested.vue +5 -9
- package/dist/runtime/domains/auto-form/components/Nested.vue.d.ts +0 -2
- package/dist/runtime/domains/auto-form/controls.d.ts +5 -572
- package/dist/runtime/domains/auto-form/controls.js +7 -20
- package/dist/runtime/domains/auto-form/fields.d.ts +21 -4
- package/dist/runtime/domains/auto-form/fields.js +52 -40
- package/dist/runtime/domains/auto-form/provider.d.ts +15 -6
- package/dist/runtime/domains/auto-form/provider.js +24 -10
- package/dist/runtime/domains/auto-form/schema.d.ts +9 -4
- package/dist/runtime/domains/auto-form/schema.js +91 -50
- package/dist/runtime/domains/auto-form/search-query.d.ts +10 -0
- package/dist/runtime/domains/auto-form/search-query.js +57 -0
- package/dist/runtime/domains/auto-form/useAutoFormCore.d.ts +62 -0
- package/dist/runtime/domains/auto-form/useAutoFormCore.js +108 -0
- package/dist/runtime/domains/auto-form/useSearchQuerySync.d.ts +11 -0
- package/dist/runtime/domains/auto-form/useSearchQuerySync.js +64 -0
- package/dist/runtime/domains/data-table/columns/constants.d.ts +2 -0
- package/dist/runtime/domains/data-table/columns/constants.js +1 -1
- package/dist/runtime/domains/data-table/columns/resolve-columns.d.ts +2 -1
- package/dist/runtime/domains/data-table/columns/resolve-columns.js +3 -2
- package/dist/runtime/domains/data-table/columns/resolve-data-column.d.ts +7 -2
- package/dist/runtime/domains/data-table/columns/resolve-data-column.js +10 -5
- package/dist/runtime/domains/data-table/columns/resolve-special-columns.js +14 -8
- package/dist/runtime/domains/data-table/components/ColumnSettings.d.vue.ts +20 -0
- package/dist/runtime/domains/data-table/components/ColumnSettings.vue +63 -0
- package/dist/runtime/domains/data-table/components/ColumnSettings.vue.d.ts +20 -0
- package/dist/runtime/domains/data-table/components/Pagination.d.vue.ts +2 -8
- package/dist/runtime/domains/data-table/components/Pagination.vue +34 -36
- package/dist/runtime/domains/data-table/components/Pagination.vue.d.ts +2 -8
- package/dist/runtime/domains/data-table/export.d.ts +23 -0
- package/dist/runtime/domains/data-table/export.js +43 -0
- package/dist/runtime/domains/data-table/load-more.d.ts +8 -0
- package/dist/runtime/domains/data-table/load-more.js +14 -0
- package/dist/runtime/domains/data-table/pagination.d.ts +29 -0
- package/dist/runtime/domains/data-table/pagination.js +42 -0
- package/dist/runtime/domains/data-table/persist.d.ts +12 -0
- package/dist/runtime/domains/data-table/persist.js +41 -0
- package/dist/runtime/domains/data-table/state/keys.d.ts +11 -0
- package/dist/runtime/domains/data-table/state/keys.js +44 -0
- package/dist/runtime/domains/data-table/state/models.d.ts +5 -0
- package/dist/runtime/domains/data-table/state/models.js +38 -0
- package/dist/runtime/domains/data-table/tree-layout.d.ts +13 -0
- package/dist/runtime/domains/data-table/tree-layout.js +30 -0
- package/dist/runtime/domains/theme/theme-color.d.ts +3 -0
- package/dist/runtime/domains/theme/theme-color.js +16 -0
- package/dist/runtime/domains/theme/theme-fouc.d.ts +5 -0
- package/dist/runtime/domains/theme/theme-fouc.js +43 -0
- package/dist/runtime/domains/tree/components/TreeToolbar.d.vue.ts +7 -5
- package/dist/runtime/domains/tree/components/TreeToolbar.vue +6 -4
- package/dist/runtime/domains/tree/components/TreeToolbar.vue.d.ts +7 -5
- package/dist/runtime/domains/tree/tree-disabled.d.ts +5 -0
- package/dist/runtime/domains/tree/tree-disabled.js +15 -0
- package/dist/runtime/domains/tree/tree-lazy.d.ts +8 -0
- package/dist/runtime/domains/tree/tree-lazy.js +19 -1
- package/dist/runtime/domains/tree/tree-search.d.ts +3 -1
- package/dist/runtime/domains/tree/tree-search.js +4 -0
- package/dist/runtime/domains/tree/useTreeLazy.d.ts +22 -0
- package/dist/runtime/domains/tree/useTreeLazy.js +57 -0
- package/dist/runtime/domains/tree/useTreeSearch.d.ts +27 -0
- package/dist/runtime/domains/tree/useTreeSearch.js +59 -0
- package/dist/runtime/domains/tree/useTreeSelection.d.ts +31 -0
- package/dist/runtime/domains/tree/useTreeSelection.js +89 -0
- package/dist/runtime/domains/zod/apply.d.ts +23 -0
- package/dist/runtime/domains/zod/apply.js +26 -0
- package/dist/runtime/domains/zod/error-map.d.ts +13 -0
- package/dist/runtime/domains/zod/error-map.js +19 -0
- package/dist/runtime/domains/zod/locale-map.d.ts +5 -0
- package/dist/runtime/domains/zod/locale-map.js +17 -0
- package/dist/runtime/domains/zod/sync.d.ts +12 -0
- package/dist/runtime/domains/zod/sync.js +30 -0
- package/dist/runtime/locale/en.d.ts +74 -0
- package/dist/runtime/locale/en.js +73 -0
- package/dist/runtime/locale/index.d.ts +8 -0
- package/dist/runtime/locale/index.js +15 -0
- package/dist/runtime/locale/zh_cn.d.ts +74 -0
- package/dist/runtime/locale/zh_cn.js +73 -0
- package/dist/runtime/plugins/api.factory.js +5 -22
- package/dist/runtime/plugins/theme.js +4 -42
- package/dist/runtime/plugins/zod.d.ts +2 -0
- package/dist/runtime/plugins/zod.js +16 -0
- package/dist/runtime/types/api/config.d.ts +12 -0
- package/dist/runtime/types/api/fetch.d.ts +3 -16
- package/dist/runtime/types/api/index.d.ts +1 -1
- package/dist/runtime/types/api/response.d.ts +33 -19
- package/dist/runtime/types/api/stream.d.ts +2 -3
- package/dist/runtime/types/api/transfer.d.ts +3 -7
- package/dist/runtime/types/auto-form/base.d.ts +9 -1
- package/dist/runtime/types/auto-form/component.d.ts +31 -3
- package/dist/runtime/types/auto-form/fields.d.ts +15 -1
- package/dist/runtime/types/auto-form/index.d.ts +3 -3
- package/dist/runtime/types/auto-form/search-form.d.ts +26 -12
- package/dist/runtime/types/auto-form/zod-factory.d.ts +2 -2
- package/dist/runtime/types/components/date-picker.d.ts +14 -3
- package/dist/runtime/types/components/index.d.ts +1 -6
- package/dist/runtime/types/components/message-box.d.ts +5 -18
- package/dist/runtime/types/components/tree.d.ts +17 -14
- package/dist/runtime/types/data-table/columns.d.ts +6 -14
- package/dist/runtime/types/data-table/component.d.ts +42 -1
- package/dist/runtime/types/data-table/index.d.ts +1 -1
- package/dist/runtime/types/data-table/pagination.d.ts +7 -8
- package/dist/runtime/types/index.d.ts +2 -0
- package/dist/runtime/types/locale.d.ts +82 -0
- package/dist/runtime/types/shared.d.ts +13 -0
- package/dist/runtime/types/zod-config.d.ts +38 -0
- package/dist/runtime/types/zod.d.ts +9 -1
- package/dist/runtime/utils/date/convert.d.ts +29 -0
- package/dist/runtime/utils/date/convert.js +93 -0
- package/dist/runtime/utils/date/guards.d.ts +10 -0
- package/dist/runtime/utils/date/guards.js +18 -0
- package/dist/runtime/utils/extend-theme.js +2 -1
- package/dist/runtime/utils/form-control.d.ts +0 -12
- package/dist/runtime/utils/form-control.js +3 -39
- package/dist/runtime/utils/interpolate.d.ts +2 -0
- package/dist/runtime/utils/interpolate.js +4 -0
- package/dist/runtime/utils/theme-defaults.d.ts +10 -0
- package/dist/runtime/utils/theme-defaults.js +9 -0
- package/dist/runtime/utils/tree-selection.d.ts +2 -15
- package/dist/runtime/utils/tv.d.ts +1 -1
- package/dist/runtime/vue/plugins/zod.d.ts +7 -0
- package/dist/runtime/vue/plugins/zod.js +12 -0
- package/dist/shared/{nuxt.C7xx2FPO.mjs → nuxt.CbmfnBRt.mjs} +92 -236
- package/dist/unplugin.d.mts +3 -2
- package/dist/unplugin.mjs +22 -22
- package/dist/vite.d.mts +1 -0
- package/dist/vite.mjs +2 -1
- package/package.json +40 -34
- package/vue-plugin.d.ts +7 -1
- package/dist/runtime/components/ColorChooser.d.vue.ts +0 -36
- package/dist/runtime/components/ColorChooser.vue +0 -328
- package/dist/runtime/components/ColorChooser.vue.d.ts +0 -36
- package/dist/runtime/components/PillGroup.d.vue.ts +0 -33
- package/dist/runtime/components/PillGroup.vue +0 -291
- package/dist/runtime/components/PillGroup.vue.d.ts +0 -33
- package/dist/runtime/components/Popconfirm.d.vue.ts +0 -30
- package/dist/runtime/components/Popconfirm.vue +0 -143
- package/dist/runtime/components/Popconfirm.vue.d.ts +0 -30
- package/dist/runtime/components/SlideVerify.d.vue.ts +0 -41
- package/dist/runtime/components/SlideVerify.vue +0 -174
- package/dist/runtime/components/SlideVerify.vue.d.ts +0 -41
- package/dist/runtime/components/input/AsPhoneNumberInput.d.vue.ts +0 -32
- package/dist/runtime/components/input/AsPhoneNumberInput.vue +0 -48
- package/dist/runtime/components/input/AsPhoneNumberInput.vue.d.ts +0 -32
- package/dist/runtime/components/input/WithCharacterLimit.d.vue.ts +0 -32
- package/dist/runtime/components/input/WithCharacterLimit.vue +0 -54
- package/dist/runtime/components/input/WithCharacterLimit.vue.d.ts +0 -32
- package/dist/runtime/components/input/WithClear.d.vue.ts +0 -33
- package/dist/runtime/components/input/WithClear.vue +0 -60
- package/dist/runtime/components/input/WithClear.vue.d.ts +0 -33
- package/dist/runtime/components/input/WithCopy.d.vue.ts +0 -33
- package/dist/runtime/components/input/WithCopy.vue +0 -66
- package/dist/runtime/components/input/WithCopy.vue.d.ts +0 -33
- package/dist/runtime/components/input/WithFloatingLabel.d.vue.ts +0 -33
- package/dist/runtime/components/input/WithFloatingLabel.vue +0 -75
- package/dist/runtime/components/input/WithFloatingLabel.vue.d.ts +0 -33
- package/dist/runtime/components/input/WithPasswordToggle.d.vue.ts +0 -32
- package/dist/runtime/components/input/WithPasswordToggle.vue +0 -61
- package/dist/runtime/components/input/WithPasswordToggle.vue.d.ts +0 -32
- package/dist/runtime/domains/api/interceptors/compose.d.ts +0 -24
- package/dist/runtime/domains/api/interceptors/compose.js +0 -18
- package/dist/runtime/domains/api/interceptors/error.d.ts +0 -13
- package/dist/runtime/domains/api/interceptors/error.js +0 -49
- package/dist/runtime/domains/api/transfer.d.ts +0 -69
- package/dist/runtime/domains/api/transfer.js +0 -82
- package/dist/runtime/types/components/color-chooser.d.ts +0 -109
- package/dist/runtime/types/components/input/as-phone-number-input.d.ts +0 -17
- package/dist/runtime/types/components/input/index.d.ts +0 -6
- package/dist/runtime/types/components/input/index.js +0 -0
- package/dist/runtime/types/components/input/with-character-limit.d.ts +0 -11
- package/dist/runtime/types/components/input/with-character-limit.js +0 -0
- package/dist/runtime/types/components/input/with-clear.d.ts +0 -10
- package/dist/runtime/types/components/input/with-clear.js +0 -0
- package/dist/runtime/types/components/input/with-copy.d.ts +0 -11
- package/dist/runtime/types/components/input/with-copy.js +0 -0
- package/dist/runtime/types/components/input/with-floating-label.d.ts +0 -12
- package/dist/runtime/types/components/input/with-floating-label.js +0 -0
- package/dist/runtime/types/components/input/with-password-toggle.d.ts +0 -7
- package/dist/runtime/types/components/input/with-password-toggle.js +0 -0
- package/dist/runtime/types/components/pill-group.d.ts +0 -103
- package/dist/runtime/types/components/pill-group.js +0 -0
- package/dist/runtime/types/components/popconfirm.d.ts +0 -74
- package/dist/runtime/types/components/popconfirm.js +0 -0
- package/dist/runtime/types/components/slide-verify.d.ts +0 -54
- package/dist/runtime/types/components/slide-verify.js +0 -0
- /package/dist/runtime/types/{components/color-chooser.js → locale.js} +0 -0
- /package/dist/runtime/types/{components/input/as-phone-number-input.js → zod-config.js} +0 -0
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { getPath } from "@movk/core";
|
|
2
|
-
import {
|
|
3
|
-
function
|
|
2
|
+
import { getUserSession } from "./session.js";
|
|
3
|
+
function getTokenFromSession(tokenPath, nuxtApp) {
|
|
4
|
+
let sessionData;
|
|
4
5
|
try {
|
|
5
|
-
|
|
6
|
-
return nuxtApp.vueApp.runWithContext(() => useUserSession());
|
|
6
|
+
sessionData = getUserSession(nuxtApp)?.session?.value;
|
|
7
7
|
} catch {
|
|
8
8
|
return null;
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
function getTokenFromSession(tokenPath) {
|
|
12
|
-
const userSession = getUserSession();
|
|
13
|
-
if (!userSession?.session?.value) return null;
|
|
14
|
-
const sessionData = userSession.session.value;
|
|
10
|
+
if (!sessionData) return null;
|
|
15
11
|
return getPath(sessionData, tokenPath) || null;
|
|
16
12
|
}
|
|
17
|
-
function getTokenFromPublicRuntimeConfig(tokenPath) {
|
|
13
|
+
function getTokenFromPublicRuntimeConfig(tokenPath, nuxtApp) {
|
|
18
14
|
try {
|
|
19
|
-
return getPath(
|
|
15
|
+
return getPath(nuxtApp.$config.public, tokenPath) || null;
|
|
20
16
|
} catch {
|
|
21
17
|
return null;
|
|
22
18
|
}
|
|
@@ -25,10 +21,10 @@ function buildAuthHeaderValue(token, config) {
|
|
|
25
21
|
const tokenType = config.tokenType === "Custom" ? config.customTokenType || "" : config.tokenType || "Bearer";
|
|
26
22
|
return tokenType ? `${tokenType} ${token}` : token;
|
|
27
23
|
}
|
|
28
|
-
export function getAuthHeaders(auth) {
|
|
24
|
+
export function getAuthHeaders(auth, nuxtApp) {
|
|
29
25
|
const headers = {};
|
|
30
26
|
if (!auth.enabled) return headers;
|
|
31
|
-
const token = auth.tokenSource === "public-runtime-config" ? getTokenFromPublicRuntimeConfig(auth.tokenPath || "apiToken") : getTokenFromSession(auth.sessionTokenPath || "token");
|
|
27
|
+
const token = auth.tokenSource === "public-runtime-config" ? getTokenFromPublicRuntimeConfig(auth.tokenPath || "apiToken", nuxtApp) : getTokenFromSession(auth.sessionTokenPath || "token", nuxtApp);
|
|
32
28
|
if (token) {
|
|
33
29
|
const headerName = auth.headerName || "Authorization";
|
|
34
30
|
headers[headerName] = buildAuthHeaderValue(token, auth);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { $Fetch } from 'nitropack/types';
|
|
2
|
+
import type { ApiClientDeps } from './report';
|
|
3
|
+
type FetchImpl = typeof globalThis.fetch;
|
|
4
|
+
/**
|
|
5
|
+
* 创建单端点的 movk 客户端
|
|
6
|
+
*
|
|
7
|
+
* @description 包装一个不带默认钩子的 ofetch 实例:
|
|
8
|
+
* - 每次调用把内置钩子排在用户钩子之前组合,用户传同名钩子不会覆盖内置拦截器
|
|
9
|
+
* - 失败在出口归一为 ApiError 并上报一次:ofetch 重试结束后才进入 catch,网络错误与超时同样覆盖
|
|
10
|
+
* - 传入 fetchImpl 时以其作为底层 fetch(如上传用的 XHR 适配器),缺省沿用全局 $fetch 的实现
|
|
11
|
+
*/
|
|
12
|
+
export declare function createApiClient(deps: ApiClientDeps, fetchImpl?: FetchImpl): $Fetch;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createOnRequest } from "./interceptors/request.js";
|
|
2
|
+
import { createOnResponse } from "./interceptors/response.js";
|
|
3
|
+
import { getErrorContext, toApiError } from "./errors.js";
|
|
4
|
+
import { reportFailure } from "./report.js";
|
|
5
|
+
function toArray(value) {
|
|
6
|
+
if (value === void 0) return [];
|
|
7
|
+
return Array.isArray(value) ? value : [value];
|
|
8
|
+
}
|
|
9
|
+
export function createApiClient(deps, fetchImpl) {
|
|
10
|
+
const { endpoint, config } = deps;
|
|
11
|
+
const defaults = { baseURL: config.baseURL, headers: config.headers };
|
|
12
|
+
const base = $fetch.create(defaults, fetchImpl ? { fetch: fetchImpl } : void 0);
|
|
13
|
+
const onRequest = createOnRequest(deps);
|
|
14
|
+
const onResponse = createOnResponse(deps);
|
|
15
|
+
const withHooks = (options = {}) => ({
|
|
16
|
+
...options,
|
|
17
|
+
onRequest: [onRequest, ...toArray(options.onRequest)],
|
|
18
|
+
onResponse: [onResponse, ...toArray(options.onResponse)]
|
|
19
|
+
});
|
|
20
|
+
const settle = async (error) => {
|
|
21
|
+
const apiError = toApiError(error, endpoint, config.response);
|
|
22
|
+
if (!apiError) return error;
|
|
23
|
+
await reportFailure(getErrorContext(error), apiError, deps);
|
|
24
|
+
return apiError;
|
|
25
|
+
};
|
|
26
|
+
const raw = async (request, options) => {
|
|
27
|
+
try {
|
|
28
|
+
return await base.raw(request, withHooks(options));
|
|
29
|
+
} catch (error) {
|
|
30
|
+
throw await settle(error);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const client = async (request, options) => (await raw(request, options))._data;
|
|
34
|
+
return Object.assign(client, { raw, create: base.create });
|
|
35
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 解析下载文件名
|
|
3
|
+
* @description 优先级:override > content-disposition > URL pathname 末段 > 'download'
|
|
4
|
+
*/
|
|
5
|
+
export declare function resolveFilename(headers: Headers, url: string, override?: string): string;
|
|
6
|
+
/**
|
|
7
|
+
* 逐块读取响应流并汇报进度
|
|
8
|
+
* @description total 为预期字节数;未知(0)时 onProgress 收到 null
|
|
9
|
+
*/
|
|
10
|
+
export declare function readWithProgress(stream: ReadableStream<Uint8Array>, total: number, onProgress: (percent: number | null) => void, type?: string): Promise<Blob>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { extractFilename } from "@movk/core";
|
|
2
|
+
export function resolveFilename(headers, url, override) {
|
|
3
|
+
if (override) return override;
|
|
4
|
+
const fromHeader = extractFilename(headers, "");
|
|
5
|
+
if (fromHeader) return fromHeader;
|
|
6
|
+
try {
|
|
7
|
+
const base = typeof location !== "undefined" ? location.origin : "http://localhost";
|
|
8
|
+
const pathname = new URL(url, base).pathname;
|
|
9
|
+
const last = pathname.split("/").filter(Boolean).pop();
|
|
10
|
+
if (last) return last;
|
|
11
|
+
} catch {
|
|
12
|
+
}
|
|
13
|
+
return "download";
|
|
14
|
+
}
|
|
15
|
+
export async function readWithProgress(stream, total, onProgress, type) {
|
|
16
|
+
const reader = stream.getReader();
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let received = 0;
|
|
19
|
+
if (total <= 0) onProgress(null);
|
|
20
|
+
while (true) {
|
|
21
|
+
const { done, value } = await reader.read();
|
|
22
|
+
if (done) break;
|
|
23
|
+
chunks.push(value);
|
|
24
|
+
received += value.length;
|
|
25
|
+
if (total > 0) onProgress(Math.min(100, Math.round(received / total * 100)));
|
|
26
|
+
}
|
|
27
|
+
return new Blob(chunks, type ? { type } : void 0);
|
|
28
|
+
}
|
|
@@ -1,2 +1,20 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { FetchContext } from 'ofetch';
|
|
2
|
+
import type { ApiError, ApiErrorKind, ApiResponseConfig } from '../../types/api';
|
|
3
|
+
export interface ApiErrorInput {
|
|
4
|
+
kind: ApiErrorKind;
|
|
5
|
+
endpoint: string;
|
|
6
|
+
message?: string;
|
|
7
|
+
code?: number | string;
|
|
8
|
+
response?: unknown;
|
|
9
|
+
}
|
|
10
|
+
export declare function createApiError(input: ApiErrorInput): ApiError;
|
|
11
|
+
export declare function isApiError(error: unknown): error is ApiError;
|
|
12
|
+
/**
|
|
13
|
+
* 把 ofetch 抛出的错误归一为 ApiError
|
|
14
|
+
* @returns 无法识别的错误(如用户钩子内抛出)返回 null,由调用方原样抛出
|
|
15
|
+
*/
|
|
16
|
+
export declare function toApiError(error: unknown, endpoint: string, responseConfig?: Partial<ApiResponseConfig>): ApiError | null;
|
|
17
|
+
/** 拦截器内抛出的业务错误不经 FetchError,借此携带 FetchContext 给上报环节 */
|
|
18
|
+
export declare function bindErrorContext(error: ApiError, context: FetchContext): ApiError;
|
|
19
|
+
/** 取回失败请求的 FetchContext;FetchError 自带 request / options / response */
|
|
20
|
+
export declare function getErrorContext(error: unknown): FetchContext | undefined;
|
|
@@ -1,10 +1,63 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { isPlainObject } from "@movk/core";
|
|
2
|
+
import { createError, isNuxtError } from "#imports";
|
|
3
|
+
import { extractMessage } from "./response.js";
|
|
4
|
+
const DEFAULT_MESSAGES = {
|
|
5
|
+
business: "\u8BF7\u6C42\u5931\u8D25",
|
|
6
|
+
http: "\u8BF7\u6C42\u5931\u8D25",
|
|
7
|
+
network: "\u7F51\u7EDC\u9519\u8BEF",
|
|
8
|
+
timeout: "\u8BF7\u6C42\u8D85\u65F6",
|
|
9
|
+
aborted: "\u8BF7\u6C42\u5DF2\u4E2D\u6B62"
|
|
10
|
+
};
|
|
11
|
+
function toStatusCode(code) {
|
|
12
|
+
const numeric = Number(code);
|
|
13
|
+
return Number.isInteger(numeric) && numeric >= 100 && numeric <= 599 ? numeric : void 0;
|
|
14
|
+
}
|
|
15
|
+
export function createApiError(input) {
|
|
16
|
+
const { kind, endpoint, message, code, response } = input;
|
|
17
|
+
const data = { kind, endpoint, code, response };
|
|
18
|
+
return createError({
|
|
19
|
+
statusCode: toStatusCode(code) ?? 500,
|
|
20
|
+
message: message || DEFAULT_MESSAGES[kind],
|
|
21
|
+
data
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function isApiError(error) {
|
|
25
|
+
return isNuxtError(error) && isPlainObject(error.data) && typeof error.data.kind === "string";
|
|
26
|
+
}
|
|
27
|
+
function isFetchError(error) {
|
|
28
|
+
return error instanceof Error && error.name === "FetchError";
|
|
29
|
+
}
|
|
30
|
+
function causeName(error) {
|
|
31
|
+
const cause = error.cause;
|
|
32
|
+
return cause instanceof Error || isPlainObject(cause) ? cause.name : void 0;
|
|
33
|
+
}
|
|
34
|
+
export function toApiError(error, endpoint, responseConfig = {}) {
|
|
35
|
+
if (isApiError(error)) return error;
|
|
36
|
+
if (!isFetchError(error)) {
|
|
37
|
+
return error instanceof Error && error.name === "AbortError" ? createApiError({ kind: "aborted", endpoint }) : null;
|
|
38
|
+
}
|
|
39
|
+
const name = causeName(error);
|
|
40
|
+
if (name === "AbortError") return createApiError({ kind: "aborted", endpoint });
|
|
41
|
+
if (name === "TimeoutError") return createApiError({ kind: "timeout", endpoint });
|
|
42
|
+
const { response } = error;
|
|
43
|
+
if (!response) return createApiError({ kind: "network", endpoint });
|
|
44
|
+
return createApiError({
|
|
45
|
+
kind: "http",
|
|
46
|
+
endpoint,
|
|
47
|
+
code: response.status,
|
|
48
|
+
response: error.data,
|
|
49
|
+
message: extractMessage(error.data, responseConfig) || `\u8BF7\u6C42\u5931\u8D25 (${response.status})`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const errorContexts = /* @__PURE__ */ new WeakMap();
|
|
53
|
+
export function bindErrorContext(error, context) {
|
|
54
|
+
errorContexts.set(error, context);
|
|
9
55
|
return error;
|
|
10
56
|
}
|
|
57
|
+
export function getErrorContext(error) {
|
|
58
|
+
if (error === null || typeof error !== "object") return void 0;
|
|
59
|
+
const bound = errorContexts.get(error);
|
|
60
|
+
if (bound) return bound;
|
|
61
|
+
if (!isFetchError(error)) return void 0;
|
|
62
|
+
return { request: error.request, options: error.options, response: error.response, error };
|
|
63
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { RequestToastOptions } from '../../types/api';
|
|
2
2
|
interface ApiFetchKeyInput {
|
|
3
3
|
endpoint?: string;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
businessCheck?: boolean;
|
|
5
|
+
unwrap?: boolean;
|
|
6
6
|
toast?: RequestToastOptions | false;
|
|
7
7
|
method?: string;
|
|
8
8
|
url: unknown;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { simpleHash } from "@movk/core";
|
|
2
2
|
export function buildApiFetchKey(input) {
|
|
3
|
-
const { endpoint,
|
|
3
|
+
const { endpoint, businessCheck, unwrap, toast, method, url, query, body } = input;
|
|
4
4
|
const payload = {
|
|
5
5
|
e: endpoint ?? null,
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
bc: businessCheck ?? null,
|
|
7
|
+
uw: unwrap ?? null,
|
|
8
8
|
t: toast ?? null,
|
|
9
9
|
m: method ?? "GET",
|
|
10
10
|
u: url,
|
|
@@ -17,7 +17,7 @@ export function buildApiFetchKey(input) {
|
|
|
17
17
|
} catch {
|
|
18
18
|
const toastTag = toast === false ? "off" : toast ? "obj" : "";
|
|
19
19
|
const urlTag = typeof url === "string" ? url : "<req>";
|
|
20
|
-
raw = `${endpoint ?? ""}|${
|
|
20
|
+
raw = `${endpoint ?? ""}|${businessCheck}|${unwrap}|${toastTag}|${payload.m}|${urlTag}|<body>`;
|
|
21
21
|
}
|
|
22
22
|
return `mvk-api:${simpleHash(raw)}`;
|
|
23
23
|
}
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import type { FetchContext } from 'ofetch';
|
|
2
|
-
import type {
|
|
3
|
-
import type { ResolvedEndpointConfig } from '../../../types/api/module';
|
|
4
|
-
import type { useNuxtApp } from '#imports';
|
|
5
|
-
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
2
|
+
import type { ApiClientDeps } from '../report';
|
|
6
3
|
/**
|
|
7
4
|
* 构造 onRequest 拦截器:注入认证头、debug 日志、派发 movk:api:request hook
|
|
8
5
|
*
|
|
9
6
|
* @description Headers 重建前会过滤 undefined 值,规避 ofetch 某些版本 `Record<string, string | undefined>` 导致的 TypeError
|
|
10
7
|
*/
|
|
11
|
-
export declare function createOnRequest(
|
|
12
|
-
export {};
|
|
8
|
+
export declare function createOnRequest(deps: ApiClientDeps): (context: FetchContext) => Promise<void>;
|
|
@@ -19,11 +19,12 @@ function toHeaderRecord(headers) {
|
|
|
19
19
|
}
|
|
20
20
|
return out;
|
|
21
21
|
}
|
|
22
|
-
export function createOnRequest(
|
|
22
|
+
export function createOnRequest(deps) {
|
|
23
|
+
const { config: resolvedConfig, publicConfig, nuxtApp } = deps;
|
|
23
24
|
const { auth: authConfig } = resolvedConfig;
|
|
24
25
|
return async function onRequest(context) {
|
|
25
26
|
if (authConfig.enabled) {
|
|
26
|
-
const authHeaders = getAuthHeaders(authConfig);
|
|
27
|
+
const authHeaders = getAuthHeaders(authConfig, nuxtApp);
|
|
27
28
|
const normalized = toHeaderRecord(context.options.headers);
|
|
28
29
|
const headers = new Headers(normalized);
|
|
29
30
|
for (const [key, value] of Object.entries(authHeaders)) {
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import type { FetchContext } from 'ofetch';
|
|
2
|
-
import type {
|
|
3
|
-
import type { ResolvedEndpointConfig } from '../../../types/api/module';
|
|
4
|
-
import type { useNuxtApp } from '#imports';
|
|
5
|
-
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
2
|
+
import type { ApiClientDeps } from '../report';
|
|
6
3
|
/**
|
|
7
4
|
* 构造 onResponse 拦截器
|
|
8
5
|
*
|
|
9
|
-
* @description
|
|
10
|
-
* -
|
|
11
|
-
* -
|
|
6
|
+
* @description 只负责成功响应的判定与转换,失败上报统一交给客户端出口:
|
|
7
|
+
* - 以流读取的 JSON 响应先解析为对象(含 HTTP 错误响应)
|
|
8
|
+
* - HTTP 4xx/5xx 直接返回,交给 ofetch 抛 FetchError(保留重试)
|
|
9
|
+
* - 真正的流(SSE、二进制)短路:只派发 movk:api:response,不解包不校验
|
|
10
|
+
* - 其余按策略(请求 > 端点 > 全局)判定:通过则解包并上报成功,失败抛业务 ApiError
|
|
12
11
|
*
|
|
13
|
-
* **契约**:拦截器输出 = 业务数据。`movk:api:response` hook 收到的 `context.response._data`
|
|
14
|
-
* 调用方设置 `skipUnwrap: true` 时不重写 `_data`,保留原始响应。
|
|
15
|
-
* 响应体是可读流时整段短路,`_data` 原样保留,调用方无需再传 `skipUnwrap` / `skipBusinessCheck`。
|
|
12
|
+
* **契约**:拦截器输出 = 业务数据。`movk:api:response` hook 收到的 `context.response._data` 已按策略处理。
|
|
16
13
|
*/
|
|
17
|
-
export declare function createOnResponse(
|
|
18
|
-
export {};
|
|
14
|
+
export declare function createOnResponse(deps: ApiClientDeps): (context: FetchContext) => Promise<void>;
|
|
@@ -1,35 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { bindErrorContext, createApiError } from "../errors.js";
|
|
2
|
+
import { interpretResponse, resolvePolicy } from "../response.js";
|
|
3
|
+
import { reportSuccess } from "../report.js";
|
|
2
4
|
import { isReadableStream } from "../stream.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function readApiContext(options) {
|
|
6
|
-
return options.context || {};
|
|
5
|
+
function isJsonResponse(response) {
|
|
6
|
+
return (response.headers.get("content-type") || "").toLowerCase().includes("application/json");
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
const
|
|
8
|
+
async function readJson(response) {
|
|
9
|
+
const text = await response.text();
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(text);
|
|
12
|
+
} catch {
|
|
13
|
+
return text;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function createOnResponse(deps) {
|
|
17
|
+
const { endpoint, config, publicConfig, nuxtApp } = deps;
|
|
10
18
|
return async function onResponse(context) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
if (isReadableStream(
|
|
19
|
+
const { response } = context;
|
|
20
|
+
if (!response) return;
|
|
21
|
+
if (isReadableStream(response._data) && isJsonResponse(response)) {
|
|
22
|
+
response._data = await readJson(response);
|
|
23
|
+
}
|
|
24
|
+
if (response.status >= 400) return;
|
|
25
|
+
if (isReadableStream(response._data)) {
|
|
14
26
|
if (publicConfig.debug) {
|
|
15
27
|
console.info("[@movk/nuxt] Response: <stream>");
|
|
16
28
|
}
|
|
17
29
|
await nuxtApp.callHook("movk:api:response", context);
|
|
18
30
|
return;
|
|
19
31
|
}
|
|
32
|
+
const raw = response._data;
|
|
20
33
|
if (publicConfig.debug) {
|
|
21
34
|
console.info("[@movk/nuxt] Response:", raw);
|
|
22
35
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
const result = interpretResponse(raw, resolvePolicy(config.response, context.options), config.response);
|
|
37
|
+
if (!result.ok) {
|
|
38
|
+
throw bindErrorContext(createApiError({
|
|
39
|
+
kind: "business",
|
|
40
|
+
endpoint,
|
|
41
|
+
code: result.code,
|
|
42
|
+
message: result.message,
|
|
43
|
+
response: raw
|
|
44
|
+
}), context);
|
|
30
45
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
throw createApiError(raw, extractMessage(raw, responseConfig));
|
|
46
|
+
response._data = result.data;
|
|
47
|
+
await reportSuccess(context, raw, deps);
|
|
34
48
|
};
|
|
35
49
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { FetchContext } from 'ofetch';
|
|
2
|
+
import type { ApiError, MovkApiPublicConfig } from '../../types/api';
|
|
3
|
+
import type { ResolvedEndpointConfig } from '../../types/api/module';
|
|
4
|
+
import type { useNuxtApp } from '#imports';
|
|
5
|
+
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
6
|
+
/** 单端点请求管线的共享依赖 */
|
|
7
|
+
export interface ApiClientDeps {
|
|
8
|
+
endpoint: string;
|
|
9
|
+
config: ResolvedEndpointConfig;
|
|
10
|
+
publicConfig: MovkApiPublicConfig;
|
|
11
|
+
nuxtApp: NuxtApp;
|
|
12
|
+
}
|
|
13
|
+
/** HTTP 状态码或业务码(兼容字符串)为 401 */
|
|
14
|
+
export declare function isUnauthorized(error: ApiError): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 成功上报:派发 `movk:api:response` 并触发成功 toast
|
|
17
|
+
* @description message 为字符串时作固定文案,为响应体时按 messageKey 抽取
|
|
18
|
+
*/
|
|
19
|
+
export declare function reportSuccess(context: FetchContext, message: unknown, deps: ApiClientDeps): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* 失败上报:401 → `movk:api:unauthorized`(接管后不再派发 error 与错误 toast)→ `movk:api:error` → 错误 toast
|
|
22
|
+
* @description 由客户端出口在 ofetch 重试结束后统一调用,每次请求只上报一次;中止不上报
|
|
23
|
+
*/
|
|
24
|
+
export declare function reportFailure(context: FetchContext | undefined, error: ApiError, deps: ApiClientDeps): Promise<void>;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { navigateTo } from "#imports";
|
|
2
|
+
import { getUserSession } from "./session.js";
|
|
3
|
+
import { showToast } from "./toast.js";
|
|
4
|
+
const UNAUTHORIZED_CODE = 401;
|
|
5
|
+
export function isUnauthorized(error) {
|
|
6
|
+
const code = error.data?.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
|
+
async function dispatchUnauthorized(context, deps) {
|
|
28
|
+
const result = { handled: false };
|
|
29
|
+
await deps.nuxtApp.callHook("movk:api:unauthorized", context, result);
|
|
30
|
+
if (!result.handled) {
|
|
31
|
+
await handleUnauthorized(deps.config.auth, deps.nuxtApp);
|
|
32
|
+
}
|
|
33
|
+
return result.handled;
|
|
34
|
+
}
|
|
35
|
+
export async function reportSuccess(context, message, deps) {
|
|
36
|
+
const { config, nuxtApp } = deps;
|
|
37
|
+
await nuxtApp.callHook("movk:api:response", context);
|
|
38
|
+
showToast("success", message, context.options.toast, config.toast, config.response, context.options.method, nuxtApp);
|
|
39
|
+
}
|
|
40
|
+
export async function reportFailure(context, error, deps) {
|
|
41
|
+
if (error.data?.kind === "aborted") return;
|
|
42
|
+
const { config, publicConfig, nuxtApp } = deps;
|
|
43
|
+
const fetchContext = context ?? { request: "", options: {} };
|
|
44
|
+
if (publicConfig.debug) {
|
|
45
|
+
console.error("[@movk/nuxt] Error:", error.data?.kind, error.statusCode, error.data?.response);
|
|
46
|
+
}
|
|
47
|
+
const handled = isUnauthorized(error) && await dispatchUnauthorized(fetchContext, deps);
|
|
48
|
+
if (handled) return;
|
|
49
|
+
await nuxtApp.callHook("movk:api:error", fetchContext, error);
|
|
50
|
+
showToast("error", error.message, fetchContext.options.toast, config.toast, config.response, fetchContext.options.method, nuxtApp);
|
|
51
|
+
}
|
|
@@ -1,4 +1,29 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
import type { ApiRequestOptions, ApiResponseConfig } from '../../types/api';
|
|
2
|
+
/** 单次请求最终生效的响应处理策略 */
|
|
3
|
+
export interface ResponsePolicy {
|
|
4
|
+
check: boolean;
|
|
5
|
+
unwrap: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** 业务判定结果 */
|
|
8
|
+
export type ResponseInterpretation = {
|
|
9
|
+
ok: true;
|
|
10
|
+
data: unknown;
|
|
11
|
+
} | {
|
|
12
|
+
ok: false;
|
|
13
|
+
code?: number | string;
|
|
14
|
+
message?: string;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* 合并请求级与端点级(已含全局)策略
|
|
18
|
+
* @description 优先级:请求显式值 > 端点 / 全局 response 配置 > 默认开启
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolvePolicy(config: Partial<ApiResponseConfig>, request?: ApiRequestOptions): ResponsePolicy;
|
|
21
|
+
/** 非普通对象(数组、null、基本类型)一律判负 */
|
|
22
|
+
export declare function isBusinessSuccess(response: unknown, config?: Partial<ApiResponseConfig>): boolean;
|
|
23
|
+
export declare function extractCode(response: unknown, config?: Partial<ApiResponseConfig>): number | string | undefined;
|
|
24
|
+
export declare function extractMessage(response: unknown, config?: Partial<ApiResponseConfig>): string | undefined;
|
|
25
|
+
export declare function extractData(response: unknown, config?: Partial<ApiResponseConfig>): unknown;
|
|
26
|
+
/**
|
|
27
|
+
* 按策略判定响应:校验失败返回业务码与消息,通过则按需解包
|
|
28
|
+
*/
|
|
29
|
+
export declare function interpretResponse(raw: unknown, policy: ResponsePolicy, config?: Partial<ApiResponseConfig>): ResponseInterpretation;
|
|
@@ -1,14 +1,35 @@
|
|
|
1
|
+
import { isPlainObject } from "@movk/core";
|
|
2
|
+
function asEnvelope(response) {
|
|
3
|
+
return isPlainObject(response) ? response : null;
|
|
4
|
+
}
|
|
5
|
+
export function resolvePolicy(config, request = {}) {
|
|
6
|
+
return {
|
|
7
|
+
check: request.businessCheck ?? config.businessCheck ?? true,
|
|
8
|
+
unwrap: request.unwrap ?? config.unwrap ?? true
|
|
9
|
+
};
|
|
10
|
+
}
|
|
1
11
|
export function isBusinessSuccess(response, config = {}) {
|
|
2
|
-
const
|
|
12
|
+
const envelope = asEnvelope(response);
|
|
13
|
+
if (!envelope) return false;
|
|
3
14
|
const successCodes = config.successCodes || [200, 0];
|
|
4
|
-
|
|
5
|
-
|
|
15
|
+
return successCodes.includes(envelope[config.codeKey || "code"]);
|
|
16
|
+
}
|
|
17
|
+
export function extractCode(response, config = {}) {
|
|
18
|
+
return asEnvelope(response)?.[config.codeKey || "code"];
|
|
6
19
|
}
|
|
7
20
|
export function extractMessage(response, config = {}) {
|
|
8
|
-
const
|
|
9
|
-
|
|
21
|
+
const envelope = asEnvelope(response);
|
|
22
|
+
if (!envelope) return void 0;
|
|
23
|
+
return envelope[config.messageKey || "message"] || envelope.message || envelope.msg;
|
|
10
24
|
}
|
|
11
25
|
export function extractData(response, config = {}) {
|
|
26
|
+
const envelope = asEnvelope(response);
|
|
12
27
|
const dataKey = config.dataKey || "data";
|
|
13
|
-
return dataKey in
|
|
28
|
+
return envelope && dataKey in envelope ? envelope[dataKey] : void 0;
|
|
29
|
+
}
|
|
30
|
+
export function interpretResponse(raw, policy, config = {}) {
|
|
31
|
+
if (policy.check && !isBusinessSuccess(raw, config)) {
|
|
32
|
+
return { ok: false, code: extractCode(raw, config), message: extractMessage(raw, config) };
|
|
33
|
+
}
|
|
34
|
+
return { ok: true, data: policy.unwrap ? extractData(raw, config) : raw };
|
|
14
35
|
}
|
|
@@ -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;useUserSession 依赖 useState,须用 nuxtApp.runWithContext 恢复 Nuxt 上下文 */
|
|
11
|
+
export declare function getUserSession(nuxtApp: ReturnType<typeof useNuxtApp>): UserSessionLike | null;
|
|
12
|
+
export {};
|
|
@@ -41,6 +41,7 @@ export declare function withStreamAccept(headers?: HeadersInit): Headers;
|
|
|
41
41
|
* 发起请求并取出 SSE 分片流
|
|
42
42
|
*
|
|
43
43
|
* @description 走 `$fetch.raw` 而非原生 fetch:端点 baseURL、鉴权头、业务码校验、toast 与
|
|
44
|
-
* `movk:api:*` hook
|
|
44
|
+
* `movk:api:*` hook 全部复用。固定按流读取,网关把 SSE 标成非 `text/event-stream` 也不受影响;
|
|
45
|
+
* JSON 响应(错误信封)由拦截器还原后照常校验,真正的流则短路。
|
|
45
46
|
*/
|
|
46
47
|
export declare function openEventStream<T>($fetch: $Fetch, url: string, options?: ApiStreamOptions<T>): Promise<AsyncGenerator<T>>;
|
|
@@ -62,13 +62,14 @@ export function withStreamAccept(headers) {
|
|
|
62
62
|
export async function openEventStream($fetch, url, options = {}) {
|
|
63
63
|
const { parse, headers, ...fetchOptions } = options;
|
|
64
64
|
const response = await $fetch.raw(url, {
|
|
65
|
+
responseType: "stream",
|
|
65
66
|
...fetchOptions,
|
|
66
67
|
headers: withStreamAccept(headers)
|
|
67
68
|
});
|
|
68
69
|
const body = response._data;
|
|
69
70
|
if (!isReadableStream(body)) {
|
|
70
71
|
throw new Error(
|
|
71
|
-
`[@movk/nuxt] \u671F\u671B SSE \u6D41\uFF0C\u5B9E\u9645\u6536\u5230 ${describe(body)}\u3002\u8BF7\u786E\u8BA4\u63A5\u53E3\u8FD4\u56DE
|
|
72
|
+
`[@movk/nuxt] \u671F\u671B SSE \u6D41\uFF0C\u5B9E\u9645\u6536\u5230 ${describe(body)}\u3002\u8BF7\u786E\u8BA4\u63A5\u53E3\u8FD4\u56DE\u4E8B\u4EF6\u6D41`
|
|
72
73
|
);
|
|
73
74
|
}
|
|
74
75
|
return iterateStream(toEventStream(body, { parse }));
|
|
@@ -1,19 +1,22 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ApiResponseConfig, ApiToastConfig, RequestToastOptions } from '../../types/api';
|
|
2
|
+
import type { useNuxtApp } from '#imports';
|
|
2
3
|
type ToastType = 'success' | 'error';
|
|
4
|
+
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
3
5
|
/**
|
|
4
6
|
* 触发 Toast 提示
|
|
5
7
|
*
|
|
6
8
|
* @description 决策收敛在本函数内:是否启用 / 类型是否启用 / 提示文案选择 / payload 构造。
|
|
7
9
|
*
|
|
8
10
|
* @param type 提示类型
|
|
9
|
-
* @param source
|
|
11
|
+
* @param source 字符串作为文案;对象按 responseConfig 抽取 message;其余走类型默认文案
|
|
10
12
|
* @param requestOptions 单次请求覆盖配置;为 false 时整体禁用
|
|
11
13
|
* @param globalConfig 全局 Toast 配置(已合并端点级覆盖)
|
|
12
14
|
* @param responseConfig 用于从 ApiResponse 抽取 message 的字段映射;source 为字符串时可省略
|
|
13
15
|
* @param method 当前请求的 HTTP 方法,用于匹配 globalConfig 的方法白名单;缺省按 GET 处理
|
|
16
|
+
* @param nuxtApp 创建客户端时捕获的实例;拦截器中 await 之后已无上下文,须显式传入
|
|
14
17
|
* @description 开关优先级:请求级 show 显式声明 > 请求级 successMessage / errorMessage >
|
|
15
18
|
* 全局 typeConfig.show / typeConfig.methods > 全局 enabled。
|
|
16
19
|
* 请求级 `show: true` 或自定义文案均可在全局关闭时单次开启。
|
|
17
20
|
*/
|
|
18
|
-
export declare function showToast(type: ToastType, source:
|
|
21
|
+
export declare function showToast(type: ToastType, source: unknown, requestOptions: RequestToastOptions | false | undefined, globalConfig: Partial<ApiToastConfig>, responseConfig?: Partial<ApiResponseConfig>, method?: string, nuxtApp?: NuxtApp): void;
|
|
19
22
|
export {};
|