@movk/nuxt 1.8.3 → 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 +15 -22
- 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 +6 -7
- 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 -14
- package/dist/runtime/domains/api/interceptors/response.js +34 -25
- 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 +1 -1
- package/dist/runtime/domains/api/session.js +1 -1
- 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.DfVF6ZZv.mjs → nuxt.CbmfnBRt.mjs} +82 -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 +39 -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 -26
- package/dist/runtime/domains/api/interceptors/unauthorized.d.ts +0 -12
- package/dist/runtime/domains/api/interceptors/unauthorized.js +0 -34
- package/dist/runtime/domains/api/transfer.d.ts +0 -69
- package/dist/runtime/domains/api/transfer.js +0 -87
- 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,23 +1,23 @@
|
|
|
1
1
|
import { ref, shallowRef } from "vue";
|
|
2
2
|
import { useNuxtApp, useRuntimeConfig } from "#imports";
|
|
3
3
|
import { triggerDownload } from "@movk/core";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
tryParseJsonResponse
|
|
10
|
-
} from "../domains/api/transfer.js";
|
|
4
|
+
import { resolveEndpointConfig } from "../domains/api/endpoint-config.js";
|
|
5
|
+
import { createApiError, isApiError } from "../domains/api/errors.js";
|
|
6
|
+
import { isAbortError, isReadableStream } from "../domains/api/stream.js";
|
|
7
|
+
import { readWithProgress, resolveFilename } from "../domains/api/download.js";
|
|
8
|
+
import { showToast } from "../domains/api/toast.js";
|
|
11
9
|
export function useDownloadWithProgress() {
|
|
12
10
|
const publicConfig = useRuntimeConfig().public.movkApi;
|
|
13
11
|
const nuxtApp = useNuxtApp();
|
|
12
|
+
const { $api } = nuxtApp;
|
|
14
13
|
const progress = ref(0);
|
|
15
14
|
const status = ref("idle");
|
|
16
15
|
const data = shallowRef(null);
|
|
17
16
|
const error = ref(null);
|
|
18
17
|
let controller = null;
|
|
19
18
|
const abort = () => {
|
|
20
|
-
controller
|
|
19
|
+
if (!controller) return;
|
|
20
|
+
controller.abort();
|
|
21
21
|
controller = null;
|
|
22
22
|
status.value = "aborted";
|
|
23
23
|
progress.value = 0;
|
|
@@ -25,149 +25,66 @@ export function useDownloadWithProgress() {
|
|
|
25
25
|
const download = async (url, options = {}) => {
|
|
26
26
|
const {
|
|
27
27
|
method = "GET",
|
|
28
|
-
body,
|
|
29
28
|
filename,
|
|
30
|
-
|
|
31
|
-
toast,
|
|
32
|
-
endpoint,
|
|
33
|
-
skipBusinessCheck,
|
|
29
|
+
endpoint: endpointName,
|
|
34
30
|
onSuccess,
|
|
35
|
-
onError
|
|
31
|
+
onError,
|
|
32
|
+
...requestOptions
|
|
36
33
|
} = options;
|
|
34
|
+
const endpoint = endpointName || publicConfig.defaultEndpoint || "default";
|
|
35
|
+
const client = $api.use(endpoint);
|
|
37
36
|
const urlStr = typeof url === "string" ? url : url.url;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
skipBusinessCheck
|
|
43
|
-
});
|
|
37
|
+
abort();
|
|
38
|
+
const current = new AbortController();
|
|
39
|
+
controller = current;
|
|
40
|
+
const isCurrent = () => controller === current;
|
|
44
41
|
progress.value = 0;
|
|
45
42
|
status.value = "pending";
|
|
46
43
|
data.value = null;
|
|
47
44
|
error.value = null;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
const fail = (cause) => {
|
|
46
|
+
status.value = "error";
|
|
47
|
+
error.value = cause;
|
|
48
|
+
onError?.(cause);
|
|
49
|
+
return { data: null, error: cause, aborted: false };
|
|
53
50
|
};
|
|
54
|
-
if (method && method !== "GET" && body !== void 0 && body !== null) {
|
|
55
|
-
const isBodyInit = typeof body === "string" || body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams || body instanceof ArrayBuffer || body instanceof ReadableStream || ArrayBuffer.isView(body);
|
|
56
|
-
if (isBodyInit) {
|
|
57
|
-
fetchInit.body = body;
|
|
58
|
-
} else {
|
|
59
|
-
fetchInit.body = JSON.stringify(body);
|
|
60
|
-
if (!("content-type" in headers) && !("Content-Type" in headers)) {
|
|
61
|
-
fetchInit.headers["Content-Type"] = "application/json";
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
51
|
try {
|
|
66
|
-
const response = await
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
52
|
+
const response = await client.raw(url, {
|
|
53
|
+
...requestOptions,
|
|
54
|
+
method,
|
|
55
|
+
responseType: "stream",
|
|
56
|
+
retry: false,
|
|
57
|
+
signal: current.signal
|
|
70
58
|
});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const result3 = await finalizeTransfer(nuxtApp, {
|
|
78
|
-
raw,
|
|
79
|
-
fallback: { isSuccess: false, data: null, message: `HTTP ${response.status}` },
|
|
80
|
-
config,
|
|
81
|
-
publicConfig,
|
|
82
|
-
requestToast: toast,
|
|
83
|
-
skipBusinessCheck,
|
|
84
|
-
fetchContext
|
|
85
|
-
});
|
|
86
|
-
status.value = "error";
|
|
87
|
-
error.value = result3.error;
|
|
88
|
-
onError?.(result3.error);
|
|
89
|
-
return result3;
|
|
90
|
-
}
|
|
91
|
-
const result2 = await finalizeTransfer(nuxtApp, {
|
|
92
|
-
raw: null,
|
|
93
|
-
fallback: { isSuccess: false, data: null, message: `HTTP ${response.status} ${response.statusText}` },
|
|
94
|
-
config,
|
|
95
|
-
publicConfig,
|
|
96
|
-
requestToast: toast,
|
|
97
|
-
fetchContext
|
|
98
|
-
});
|
|
99
|
-
status.value = "error";
|
|
100
|
-
error.value = result2.error;
|
|
101
|
-
onError?.(result2.error);
|
|
102
|
-
return result2;
|
|
103
|
-
}
|
|
104
|
-
if (isJson) {
|
|
105
|
-
const text = await response.text();
|
|
106
|
-
const raw = tryParseJsonResponse(response.headers, text);
|
|
107
|
-
const result2 = await finalizeTransfer(nuxtApp, {
|
|
108
|
-
raw,
|
|
109
|
-
fallback: { isSuccess: false, data: null, message: "\u54CD\u5E94\u89E3\u6790\u5931\u8D25" },
|
|
110
|
-
config,
|
|
111
|
-
publicConfig,
|
|
112
|
-
requestToast: toast,
|
|
113
|
-
skipBusinessCheck,
|
|
114
|
-
fetchContext
|
|
115
|
-
});
|
|
116
|
-
if (result2.error) {
|
|
117
|
-
status.value = "error";
|
|
118
|
-
error.value = result2.error;
|
|
119
|
-
onError?.(result2.error);
|
|
120
|
-
} else {
|
|
121
|
-
status.value = "success";
|
|
122
|
-
data.value = result2.data;
|
|
123
|
-
progress.value = 100;
|
|
124
|
-
}
|
|
125
|
-
controller = null;
|
|
126
|
-
return result2;
|
|
59
|
+
if (!isCurrent()) return { data: null, error: null, aborted: true };
|
|
60
|
+
if (!isReadableStream(response._data)) {
|
|
61
|
+
status.value = "success";
|
|
62
|
+
data.value = response._data;
|
|
63
|
+
progress.value = 100;
|
|
64
|
+
return { data: data.value, error: null, aborted: false };
|
|
127
65
|
}
|
|
128
|
-
const
|
|
129
|
-
const
|
|
66
|
+
const total = Number.parseInt(response.headers.get("content-length") || "", 10) || 0;
|
|
67
|
+
const blob = await readWithProgress(response._data, total, (percent) => {
|
|
68
|
+
if (isCurrent()) progress.value = percent;
|
|
69
|
+
}, response.headers.get("content-type") || void 0);
|
|
70
|
+
if (!isCurrent()) return { data: null, error: null, aborted: true };
|
|
130
71
|
const finalFilename = resolveFilename(response.headers, urlStr, filename);
|
|
131
|
-
const reader = response.body?.getReader();
|
|
132
|
-
if (!reader) throw new Error("\u65E0\u6CD5\u8BFB\u53D6\u54CD\u5E94\u6D41");
|
|
133
|
-
if (total <= 0) progress.value = null;
|
|
134
|
-
const chunks = [];
|
|
135
|
-
let received = 0;
|
|
136
|
-
while (true) {
|
|
137
|
-
const { done, value } = await reader.read();
|
|
138
|
-
if (done) break;
|
|
139
|
-
chunks.push(value);
|
|
140
|
-
received += value.length;
|
|
141
|
-
if (total > 0) progress.value = Math.round(received / total * 100);
|
|
142
|
-
}
|
|
143
|
-
const blob = new Blob(chunks);
|
|
144
72
|
triggerDownload(blob, finalFilename);
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
fallback: { isSuccess: true, data: blob, message: `\u4E0B\u8F7D\u6210\u529F: ${finalFilename}` },
|
|
148
|
-
config,
|
|
149
|
-
publicConfig,
|
|
150
|
-
requestToast: toast,
|
|
151
|
-
fetchContext
|
|
152
|
-
});
|
|
73
|
+
const config = resolveEndpointConfig(publicConfig, endpoint);
|
|
74
|
+
showToast("success", `\u4E0B\u8F7D\u6210\u529F: ${finalFilename}`, requestOptions.toast, config.toast, config.response, method, nuxtApp);
|
|
153
75
|
status.value = "success";
|
|
154
|
-
data.value =
|
|
76
|
+
data.value = blob;
|
|
155
77
|
progress.value = 100;
|
|
156
|
-
controller = null;
|
|
157
78
|
onSuccess?.(finalFilename);
|
|
158
|
-
return
|
|
159
|
-
} catch (
|
|
160
|
-
|
|
161
|
-
const isAborted = err instanceof DOMException && err.name === "AbortError";
|
|
162
|
-
if (isAborted) {
|
|
163
|
-
status.value = "aborted";
|
|
79
|
+
return { data: data.value, error: null, aborted: false };
|
|
80
|
+
} catch (cause) {
|
|
81
|
+
if (!isCurrent() || isAbortError(cause) || isApiError(cause) && cause.data?.kind === "aborted") {
|
|
164
82
|
return { data: null, error: null, aborted: true };
|
|
165
83
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return { data: null, error: downloadError, aborted: false };
|
|
84
|
+
if (isApiError(cause)) return fail(cause);
|
|
85
|
+
return fail(createApiError({ kind: "network", endpoint, message: "\u4E0B\u8F7D\u4E2D\u65AD" }));
|
|
86
|
+
} finally {
|
|
87
|
+
if (isCurrent()) controller = null;
|
|
171
88
|
}
|
|
172
89
|
};
|
|
173
90
|
return { progress, status, data, error, download, abort };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { defineAsyncComponent } from "vue";
|
|
1
2
|
import { useOverlay } from "#imports";
|
|
2
|
-
|
|
3
|
+
const MessageBox = defineAsyncComponent(() => import("../components/MessageBox.vue"));
|
|
3
4
|
export function useMessageBox() {
|
|
4
5
|
const overlay = useOverlay();
|
|
5
6
|
function createInstance() {
|
|
6
|
-
return overlay.create(
|
|
7
|
+
return overlay.create(MessageBox, { destroyOnClose: true });
|
|
7
8
|
}
|
|
8
9
|
async function alert(options) {
|
|
9
10
|
await createInstance().open({ ...options, mode: "alert" });
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ComputedRef, Ref } from 'vue';
|
|
2
|
+
import type { Locale, Messages } from '@nuxt/ui';
|
|
3
|
+
import type { MovkLocaleMessages } from '../types/locale';
|
|
4
|
+
import type { MovkTranslator } from '../locale';
|
|
5
|
+
export type { MovkTranslator };
|
|
6
|
+
export interface MovkLocaleContext {
|
|
7
|
+
code: ComputedRef<string>;
|
|
8
|
+
t: MovkTranslator;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 读取 `@movk/nuxt` 组件文案:跟随 `<UApp :locale>`,按 locale code 选择内置字典,
|
|
12
|
+
* 并以 `locale.messages.movk` 覆盖;没有内置字典的语言回退到英文。
|
|
13
|
+
*/
|
|
14
|
+
export declare function useMovkLocale(localeOverride?: Ref<Locale<Messages> | undefined>): MovkLocaleContext;
|
|
15
|
+
/** 在 Nuxt UI locale 上覆盖 `@movk/nuxt` 文案,返回可直接传给 `<UApp :locale>` 的 locale */
|
|
16
|
+
export declare function extendMovkLocale(locale: Locale<Messages>, movk: MovkLocaleMessages): Locale<Messages>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { defu } from "defu";
|
|
3
|
+
import { useLocale } from "@nuxt/ui/composables/useLocale";
|
|
4
|
+
import { createTranslator, en, movkLocales } from "../locale/index.js";
|
|
5
|
+
export function useMovkLocale(localeOverride) {
|
|
6
|
+
const { locale } = useLocale(localeOverride);
|
|
7
|
+
const code = computed(() => locale.value.code);
|
|
8
|
+
const messages = computed(() => defu(
|
|
9
|
+
locale.value.messages.movk,
|
|
10
|
+
movkLocales[code.value] ?? en
|
|
11
|
+
));
|
|
12
|
+
return { code, t: createTranslator(() => messages.value) };
|
|
13
|
+
}
|
|
14
|
+
export function extendMovkLocale(locale, movk) {
|
|
15
|
+
const current = locale.messages.movk;
|
|
16
|
+
return {
|
|
17
|
+
...locale,
|
|
18
|
+
messages: { ...locale.messages, movk: defu(movk, current) }
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { useAppConfig, useColorMode, useSiteConfig } from "#imports";
|
|
2
|
+
import { resolveThemeColor } from "../domains/theme/theme-color.js";
|
|
2
3
|
import { resolveThemeIcons, themeIcons } from "../domains/theme/theme-icons.js";
|
|
3
4
|
import { resolveActiveRadius, resolveRadiusStyle } from "../domains/theme/theme-radius.js";
|
|
4
5
|
import { omit, kebabCase } from "@movk/core";
|
|
5
6
|
import { useLocalStorage } from "@vueuse/core";
|
|
6
7
|
import colors from "tailwindcss/colors";
|
|
7
8
|
import { computed } from "vue";
|
|
8
|
-
import { getDefaultConfig } from "../utils/theme-defaults.js";
|
|
9
|
+
import { getDefaultConfig, UI_COLOR_DEFAULTS } from "../utils/theme-defaults.js";
|
|
9
10
|
const UI_DEFAULT_RADIUS = 0.25;
|
|
10
11
|
export function useTheme() {
|
|
11
12
|
const { movk, ui } = useAppConfig();
|
|
12
13
|
const colorMode = useColorMode();
|
|
13
|
-
const name = kebabCase(useSiteConfig().name);
|
|
14
|
-
const color = computed(() =>
|
|
14
|
+
const name = kebabCase(useSiteConfig().name ?? "movk");
|
|
15
|
+
const color = computed(() => resolveThemeColor(ui.colors.neutral, colorMode.value));
|
|
15
16
|
const defaultConfig = getDefaultConfig();
|
|
16
17
|
const _radius = useLocalStorage(`${name}-ui-radius`, null, { writeDefaults: false });
|
|
17
18
|
const _iconSet = useLocalStorage(`${name}-ui-icons`, movk?.icons ?? defaultConfig.icons);
|
|
@@ -90,7 +91,7 @@ export function useTheme() {
|
|
|
90
91
|
return activeRadius.value !== configuredRadius || _blackAsPrimary.value;
|
|
91
92
|
});
|
|
92
93
|
const hasConfigChanges = computed(() => {
|
|
93
|
-
return ui.colors.primary !==
|
|
94
|
+
return ui.colors.primary !== UI_COLOR_DEFAULTS.primary || ui.colors.neutral !== UI_COLOR_DEFAULTS.neutral || _iconSet.value !== (movk?.icons ?? defaultConfig.icons);
|
|
94
95
|
});
|
|
95
96
|
function exportCSS() {
|
|
96
97
|
const lines = [
|
|
@@ -118,16 +119,7 @@ export function useTheme() {
|
|
|
118
119
|
}
|
|
119
120
|
function exportConfig() {
|
|
120
121
|
const config = {};
|
|
121
|
-
const
|
|
122
|
-
primary: "blue",
|
|
123
|
-
neutral: "slate",
|
|
124
|
-
secondary: "blue",
|
|
125
|
-
success: "green",
|
|
126
|
-
info: "blue",
|
|
127
|
-
warning: "yellow",
|
|
128
|
-
error: "red"
|
|
129
|
-
};
|
|
130
|
-
const colorEntries = Object.entries(defaultColors).filter(([key, def]) => ui.colors[key] !== def);
|
|
122
|
+
const colorEntries = Object.entries(UI_COLOR_DEFAULTS).filter(([key, def]) => ui.colors[key] !== def);
|
|
131
123
|
if (colorEntries.length) {
|
|
132
124
|
config.ui = { colors: Object.fromEntries(colorEntries.map(([key]) => [key, ui.colors[key]])) };
|
|
133
125
|
}
|
|
@@ -140,12 +132,10 @@ export function useTheme() {
|
|
|
140
132
|
return `export default defineAppConfig(${configString})`;
|
|
141
133
|
}
|
|
142
134
|
function resetTheme() {
|
|
143
|
-
const defaultPrimary = "blue";
|
|
144
|
-
const defaultNeutral = "slate";
|
|
145
135
|
const defaultIcon = movk?.icons ?? defaultConfig.icons;
|
|
146
|
-
ui.colors.primary =
|
|
136
|
+
ui.colors.primary = UI_COLOR_DEFAULTS.primary;
|
|
147
137
|
window.localStorage.removeItem(`${name}-ui-primary`);
|
|
148
|
-
ui.colors.neutral =
|
|
138
|
+
ui.colors.neutral = UI_COLOR_DEFAULTS.neutral;
|
|
149
139
|
window.localStorage.removeItem(`${name}-ui-neutral`);
|
|
150
140
|
_radius.value = null;
|
|
151
141
|
_iconSet.value = defaultIcon;
|
|
@@ -5,24 +5,27 @@ import type { ApiError, TransferRequestOptions, TransferResult, TransferStatus }
|
|
|
5
5
|
* 上传选项(带进度监控)
|
|
6
6
|
*/
|
|
7
7
|
export interface UploadWithProgressOptions<T = unknown> extends TransferRequestOptions {
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* 文件字段名
|
|
10
|
+
* @defaultValue 'file'
|
|
11
|
+
*/
|
|
9
12
|
fieldName?: string;
|
|
10
13
|
/** 额外的表单字段 */
|
|
11
14
|
fields?: Record<string, string | Blob>;
|
|
12
15
|
/** 超时毫秒数,0 或未设置不超时 */
|
|
13
|
-
|
|
16
|
+
timeout?: number;
|
|
14
17
|
/** 是否跨域携带凭证 */
|
|
15
18
|
withCredentials?: boolean;
|
|
16
19
|
/** 上传成功回调(解包后业务数据) */
|
|
17
20
|
onSuccess?: (data: T) => void;
|
|
18
21
|
/** 上传失败回调;中止时不触发 */
|
|
19
|
-
onError?: (error: ApiError
|
|
22
|
+
onError?: (error: ApiError) => void;
|
|
20
23
|
}
|
|
21
24
|
/**
|
|
22
25
|
* 带进度监控的文件上传 composable(仅客户端)
|
|
23
26
|
*
|
|
24
|
-
* @description
|
|
25
|
-
* 返回的 `data`
|
|
27
|
+
* @description 以 XHR 适配器作为底层 fetch 接入 `$api` 同一条管线:端点 / 认证 / 业务校验 / toast / hook / 错误归一全部复用。
|
|
28
|
+
* 返回的 `data` 按端点策略处理,与 `useApiFetch<T>` 一致。
|
|
26
29
|
*
|
|
27
30
|
* @example
|
|
28
31
|
* ```ts
|
|
@@ -39,7 +42,7 @@ export declare function useUploadWithProgress<T = unknown>(): {
|
|
|
39
42
|
/** 解包后的业务数据 */
|
|
40
43
|
data: Ref<T | null>;
|
|
41
44
|
/** 错误信息 */
|
|
42
|
-
error: Ref<ApiError |
|
|
45
|
+
error: Ref<ApiError | null>;
|
|
43
46
|
/** 执行上传 */
|
|
44
47
|
upload: (url: NitroFetchRequest, files: File | File[], options?: UploadWithProgressOptions<T>) => Promise<TransferResult<T>>;
|
|
45
48
|
/** 中止上传 */
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { ref, shallowRef } from "vue";
|
|
2
2
|
import { useNuxtApp, useRuntimeConfig } from "#imports";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
prepareTransfer,
|
|
8
|
-
tryParseJsonResponse
|
|
9
|
-
} from "../domains/api/transfer.js";
|
|
3
|
+
import { createApiClient } from "../domains/api/client.js";
|
|
4
|
+
import { resolveEndpointConfig } from "../domains/api/endpoint-config.js";
|
|
5
|
+
import { isApiError } from "../domains/api/errors.js";
|
|
6
|
+
import { xhrFetch } from "../domains/api/adapters/xhr.js";
|
|
10
7
|
export function useUploadWithProgress() {
|
|
11
8
|
const publicConfig = useRuntimeConfig().public.movkApi;
|
|
12
9
|
const nuxtApp = useNuxtApp();
|
|
@@ -14,103 +11,72 @@ export function useUploadWithProgress() {
|
|
|
14
11
|
const status = ref("idle");
|
|
15
12
|
const data = shallowRef(null);
|
|
16
13
|
const error = ref(null);
|
|
17
|
-
let
|
|
14
|
+
let controller = null;
|
|
18
15
|
const abort = () => {
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (!controller) return;
|
|
17
|
+
controller.abort();
|
|
18
|
+
controller = null;
|
|
21
19
|
status.value = "aborted";
|
|
22
20
|
progress.value = 0;
|
|
23
21
|
};
|
|
24
|
-
const upload = (url, files, options = {}) => {
|
|
22
|
+
const upload = async (url, files, options = {}) => {
|
|
25
23
|
const {
|
|
26
24
|
fieldName = "file",
|
|
27
25
|
fields = {},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
endpoint,
|
|
31
|
-
skipBusinessCheck,
|
|
32
|
-
timeoutMs,
|
|
26
|
+
endpoint: endpointName,
|
|
27
|
+
timeout,
|
|
33
28
|
withCredentials,
|
|
34
29
|
onSuccess,
|
|
35
|
-
onError
|
|
30
|
+
onError,
|
|
31
|
+
...requestOptions
|
|
36
32
|
} = options;
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
headers: userHeaders,
|
|
41
|
-
toast,
|
|
42
|
-
skipBusinessCheck
|
|
43
|
-
});
|
|
33
|
+
const endpoint = endpointName || publicConfig.defaultEndpoint || "default";
|
|
34
|
+
const config = resolveEndpointConfig(publicConfig, endpoint);
|
|
35
|
+
const send = createApiClient({ endpoint, config, publicConfig, nuxtApp }, xhrFetch);
|
|
44
36
|
const formData = new FormData();
|
|
45
37
|
const fileArray = Array.isArray(files) ? files : [files];
|
|
46
38
|
fileArray.forEach((file) => formData.append(fieldName, file));
|
|
47
39
|
Object.entries(fields).forEach(([key, value]) => formData.append(key, value));
|
|
40
|
+
abort();
|
|
41
|
+
const current = new AbortController();
|
|
42
|
+
controller = current;
|
|
43
|
+
const isCurrent = () => controller === current;
|
|
48
44
|
progress.value = 0;
|
|
49
45
|
status.value = "pending";
|
|
50
46
|
data.value = null;
|
|
51
47
|
error.value = null;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
const timer = timeout && timeout > 0 ? setTimeout(() => current.abort(new DOMException(`\u4E0A\u4F20\u8D85\u65F6\uFF08${timeout}ms\uFF09`, "TimeoutError")), timeout) : void 0;
|
|
49
|
+
try {
|
|
50
|
+
const result = await send(url, {
|
|
51
|
+
...requestOptions,
|
|
52
|
+
method: "POST",
|
|
53
|
+
body: formData,
|
|
54
|
+
retry: false,
|
|
55
|
+
signal: current.signal,
|
|
56
|
+
credentials: withCredentials ? "include" : void 0,
|
|
57
|
+
onUploadProgress: (event) => {
|
|
58
|
+
if (event.lengthComputable && isCurrent()) {
|
|
59
|
+
progress.value = Math.round(event.loaded / event.total * 100);
|
|
60
|
+
}
|
|
60
61
|
}
|
|
61
62
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
status.value = "error";
|
|
80
|
-
error.value = result.error;
|
|
81
|
-
onError?.(result.error);
|
|
82
|
-
} else {
|
|
83
|
-
status.value = "success";
|
|
84
|
-
data.value = result.data;
|
|
85
|
-
progress.value = 100;
|
|
86
|
-
if (result.data !== null) onSuccess?.(result.data);
|
|
87
|
-
}
|
|
88
|
-
resolve(result);
|
|
89
|
-
};
|
|
90
|
-
xhr.addEventListener("load", () => {
|
|
91
|
-
const responseHeaders = parseXhrHeaders(xhr.getAllResponseHeaders());
|
|
92
|
-
const raw = tryParseJsonResponse(responseHeaders, xhr.responseText);
|
|
93
|
-
if (xhr.status < 200 || xhr.status >= 300) {
|
|
94
|
-
void finishWith(raw, { isSuccess: false, message: `HTTP ${xhr.status} ${xhr.statusText}` });
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
void finishWith(raw, raw ? void 0 : { isSuccess: true, message: "\u4E0A\u4F20\u6210\u529F" });
|
|
98
|
-
});
|
|
99
|
-
xhr.addEventListener("error", () => {
|
|
100
|
-
void finishWith(null, { isSuccess: false, message: "\u7F51\u7EDC\u9519\u8BEF" });
|
|
101
|
-
});
|
|
102
|
-
xhr.addEventListener("timeout", () => {
|
|
103
|
-
void finishWith(null, { isSuccess: false, message: `\u4E0A\u4F20\u8D85\u65F6\uFF08${timeoutMs}ms\uFF09` });
|
|
104
|
-
});
|
|
105
|
-
xhr.addEventListener("abort", () => {
|
|
106
|
-
currentXhr = null;
|
|
107
|
-
status.value = "aborted";
|
|
108
|
-
resolve({ data: null, error: null, aborted: true });
|
|
109
|
-
});
|
|
110
|
-
xhr.open("POST", fullUrl);
|
|
111
|
-
Object.entries(headers).forEach(([k, v]) => xhr.setRequestHeader(k, v));
|
|
112
|
-
xhr.send(formData);
|
|
113
|
-
});
|
|
63
|
+
if (!isCurrent()) return { data: null, error: null, aborted: true };
|
|
64
|
+
status.value = "success";
|
|
65
|
+
data.value = result;
|
|
66
|
+
progress.value = 100;
|
|
67
|
+
onSuccess?.(result);
|
|
68
|
+
return { data: result, error: null, aborted: false };
|
|
69
|
+
} catch (cause) {
|
|
70
|
+
if (!isApiError(cause)) throw cause;
|
|
71
|
+
if (!isCurrent()) return { data: null, error: null, aborted: true };
|
|
72
|
+
status.value = "error";
|
|
73
|
+
error.value = cause;
|
|
74
|
+
onError?.(cause);
|
|
75
|
+
return { data: null, error: cause, aborted: false };
|
|
76
|
+
} finally {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
if (isCurrent()) controller = null;
|
|
79
|
+
}
|
|
114
80
|
};
|
|
115
81
|
return { progress, status, data, error, upload, abort };
|
|
116
82
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** XHR 适配器额外接受的请求选项 */
|
|
2
|
+
export interface XhrRequestInit extends RequestInit {
|
|
3
|
+
/** 上传进度回调 */
|
|
4
|
+
onUploadProgress?: (event: ProgressEvent) => void;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 把 XHR 的 getAllResponseHeaders() 字符串解析为标准 Headers
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseXhrHeaders(raw: string): Headers;
|
|
10
|
+
/**
|
|
11
|
+
* 与 fetch 签名兼容的 XMLHttpRequest 实现
|
|
12
|
+
*
|
|
13
|
+
* @description 作为 ofetch 的底层 fetch 使用,让上传进度也走 $api 的完整管线。
|
|
14
|
+
* fetch 规范至今无法在主流浏览器上报告上传进度,故保留 XHR。
|
|
15
|
+
* 中止 / 超时沿用 signal 的 reason(ofetch 超时会以 TimeoutError 中止),网络失败按 fetch 语义抛 TypeError。
|
|
16
|
+
*/
|
|
17
|
+
export declare function xhrFetch(input: RequestInfo | URL, init?: XhrRequestInit): Promise<Response>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
const NULL_BODY_STATUSES = /* @__PURE__ */ new Set([101, 204, 205, 304]);
|
|
2
|
+
export function parseXhrHeaders(raw) {
|
|
3
|
+
const headers = new Headers();
|
|
4
|
+
if (!raw) return headers;
|
|
5
|
+
for (const line of raw.trim().split(/\r?\n/)) {
|
|
6
|
+
const idx = line.indexOf(":");
|
|
7
|
+
if (idx <= 0) continue;
|
|
8
|
+
const name = line.slice(0, idx).trim();
|
|
9
|
+
const value = line.slice(idx + 1).trim();
|
|
10
|
+
if (name) headers.append(name, value);
|
|
11
|
+
}
|
|
12
|
+
return headers;
|
|
13
|
+
}
|
|
14
|
+
function abortReason(signal) {
|
|
15
|
+
return signal.reason ?? new DOMException("The operation was aborted.", "AbortError");
|
|
16
|
+
}
|
|
17
|
+
export function xhrFetch(input, init = {}) {
|
|
18
|
+
const { signal, onUploadProgress } = init;
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
if (signal?.aborted) {
|
|
21
|
+
reject(abortReason(signal));
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const xhr = new XMLHttpRequest();
|
|
25
|
+
const url = input instanceof Request ? input.url : String(input);
|
|
26
|
+
xhr.open(init.method || "GET", url);
|
|
27
|
+
xhr.responseType = "blob";
|
|
28
|
+
xhr.withCredentials = init.credentials === "include";
|
|
29
|
+
new Headers(init.headers).forEach((value, key) => xhr.setRequestHeader(key, value));
|
|
30
|
+
if (onUploadProgress) {
|
|
31
|
+
xhr.upload.addEventListener("progress", onUploadProgress);
|
|
32
|
+
}
|
|
33
|
+
const onAbort = () => xhr.abort();
|
|
34
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
35
|
+
const cleanup = () => signal?.removeEventListener("abort", onAbort);
|
|
36
|
+
xhr.addEventListener("load", () => {
|
|
37
|
+
cleanup();
|
|
38
|
+
const body = NULL_BODY_STATUSES.has(xhr.status) ? null : xhr.response;
|
|
39
|
+
resolve(new Response(body, {
|
|
40
|
+
status: xhr.status,
|
|
41
|
+
statusText: xhr.statusText,
|
|
42
|
+
headers: parseXhrHeaders(xhr.getAllResponseHeaders())
|
|
43
|
+
}));
|
|
44
|
+
});
|
|
45
|
+
xhr.addEventListener("error", () => {
|
|
46
|
+
cleanup();
|
|
47
|
+
reject(new TypeError("Network request failed"));
|
|
48
|
+
});
|
|
49
|
+
xhr.addEventListener("abort", () => {
|
|
50
|
+
cleanup();
|
|
51
|
+
reject(signal ? abortReason(signal) : new DOMException("The operation was aborted.", "AbortError"));
|
|
52
|
+
});
|
|
53
|
+
xhr.send(init.body);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
import type { ApiAuthConfig } from '../../types/api';
|
|
2
|
-
|
|
2
|
+
import type { useNuxtApp } from '#imports';
|
|
3
|
+
type NuxtApp = ReturnType<typeof useNuxtApp>;
|
|
4
|
+
export declare function getAuthHeaders(auth: ApiAuthConfig, nuxtApp: NuxtApp): Record<string, string>;
|
|
5
|
+
export {};
|