@lacqjs/nuxt-dict 0.0.3 → 0.0.5
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.md +3 -4
- package/dist/module.json +1 -1
- package/dist/module.mjs +4 -6
- package/dist/runtime/composables/useDict.js +15 -3
- package/dist/runtime/composables/useDictTree.js +22 -4
- package/dist/runtime/core/adapter.js +6 -2
- package/dist/runtime/core/cache/indexeddb-cache.js +3 -1
- package/dist/runtime/core/dict-manager.d.ts +1 -1
- package/dist/runtime/core/dict-manager.js +2 -2
- package/dist/runtime/plugins/dict.js +6 -4
- package/package.json +11 -10
package/README.md
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
<p align="center">
|
|
3
2
|
<h1 align="center">@lacqjs/nuxt-dict</h1>
|
|
4
3
|
</p>
|
|
@@ -26,7 +25,7 @@ pnpm add @lacqjs/nuxt-dict
|
|
|
26
25
|
// nuxt.config.ts
|
|
27
26
|
export default defineNuxtConfig({
|
|
28
27
|
modules: ['@lacqjs/nuxt-dict'],
|
|
29
|
-
})
|
|
28
|
+
});
|
|
30
29
|
```
|
|
31
30
|
|
|
32
31
|
```vue
|
|
@@ -37,8 +36,8 @@ export default defineNuxtConfig({
|
|
|
37
36
|
</template>
|
|
38
37
|
|
|
39
38
|
<script setup lang="ts">
|
|
40
|
-
const { data: options } = useDict('gender')
|
|
41
|
-
const value = ref('')
|
|
39
|
+
const { data: options } = useDict('gender');
|
|
40
|
+
const value = ref('');
|
|
42
41
|
</script>
|
|
43
42
|
```
|
|
44
43
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { fileURLToPath } from 'url';
|
|
2
1
|
import { defineNuxtModule, useLogger, createResolver, addPlugin, addImportsDir, addTypeTemplate } from '@nuxt/kit';
|
|
3
2
|
import { defaultOptions } from '../dist/runtime/options.js';
|
|
4
3
|
import { createLogger } from '../dist/runtime/utils/logger.js';
|
|
5
4
|
export { createDictTranslator } from '../dist/runtime/utils/dict-translator.js';
|
|
6
5
|
|
|
7
6
|
const name = "@lacqjs/nuxt-dict";
|
|
8
|
-
const version = "0.0.
|
|
7
|
+
const version = "0.0.5";
|
|
9
8
|
const devDependencies = {
|
|
10
9
|
nuxt: "^4.4.8"};
|
|
11
10
|
const pkg = {
|
|
@@ -181,11 +180,10 @@ const module$1 = defineNuxtModule().with({
|
|
|
181
180
|
return;
|
|
182
181
|
}
|
|
183
182
|
const resolver = createResolver(import.meta.url);
|
|
184
|
-
const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
|
|
185
183
|
_nuxt.options.runtimeConfig.public.dict = _options;
|
|
186
|
-
_nuxt.options.build.transpile.push(resolver.resolve(
|
|
187
|
-
addPlugin(
|
|
188
|
-
addImportsDir(resolver.resolve(
|
|
184
|
+
_nuxt.options.build.transpile.push(resolver.resolve("./runtime"));
|
|
185
|
+
addPlugin(resolver.resolve("./runtime/plugins/dict"));
|
|
186
|
+
addImportsDir(resolver.resolve("./runtime/composables"));
|
|
189
187
|
registerTypeTemplates(resolver, _options.stores);
|
|
190
188
|
_nuxt.hook("prepare:types", ({ references }) => {
|
|
191
189
|
references.push({ types: pkg.name });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { shallowRef, ref, watch, onMounted, onBeforeUnmount } from "vue";
|
|
1
|
+
import { shallowRef, ref, watch, onMounted, onBeforeUnmount, triggerRef } from "vue";
|
|
2
2
|
import { useNuxtApp } from "#imports";
|
|
3
3
|
import { DEFAULT_STORE_NAME } from "../core/cache/indexeddb-cache.js";
|
|
4
4
|
const activeInstances = /* @__PURE__ */ new Map();
|
|
@@ -7,7 +7,12 @@ async function fetchDictData(manager, dictType, storeName, data, loading, error,
|
|
|
7
7
|
error.value = null;
|
|
8
8
|
try {
|
|
9
9
|
const entry = await (mode === "refresh" ? manager.refresh(dictType, storeName) : manager.getDict(dictType, storeName));
|
|
10
|
-
data.value
|
|
10
|
+
if (data.value) {
|
|
11
|
+
data.value.splice(0, data.value.length, ...entry.items);
|
|
12
|
+
triggerRef(data);
|
|
13
|
+
} else {
|
|
14
|
+
data.value = entry.items;
|
|
15
|
+
}
|
|
11
16
|
if (itemMap) {
|
|
12
17
|
itemMap.clear();
|
|
13
18
|
for (const item of entry.items) {
|
|
@@ -77,5 +82,12 @@ export function useDict(storeOrType, maybeType) {
|
|
|
77
82
|
fetchDictData(manager, dictType, storeName, data, loading, error, "load", itemMap);
|
|
78
83
|
}
|
|
79
84
|
);
|
|
80
|
-
return {
|
|
85
|
+
return {
|
|
86
|
+
data,
|
|
87
|
+
translate,
|
|
88
|
+
getDictItem,
|
|
89
|
+
loading,
|
|
90
|
+
error,
|
|
91
|
+
refresh
|
|
92
|
+
};
|
|
81
93
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { shallowRef, ref, watch, onMounted } from "vue";
|
|
1
|
+
import { shallowRef, ref, watch, onMounted, triggerRef } from "vue";
|
|
2
2
|
import { useNuxtApp } from "#imports";
|
|
3
3
|
import { DEFAULT_STORE_NAME } from "../core/cache/indexeddb-cache.js";
|
|
4
4
|
export function useDictTree(storeOrType, maybeType) {
|
|
@@ -29,7 +29,13 @@ export function useDictTree(storeOrType, maybeType) {
|
|
|
29
29
|
loading.value = true;
|
|
30
30
|
try {
|
|
31
31
|
const entry = await manager.getDict(dictType, storeName);
|
|
32
|
-
|
|
32
|
+
const newTree = entry.tree ?? null;
|
|
33
|
+
if (tree.value && newTree) {
|
|
34
|
+
tree.value.splice(0, tree.value.length, ...newTree);
|
|
35
|
+
triggerRef(tree);
|
|
36
|
+
} else {
|
|
37
|
+
tree.value = newTree;
|
|
38
|
+
}
|
|
33
39
|
if (entry.tree) buildMaps(entry.tree, nodeMap, pathMap);
|
|
34
40
|
} finally {
|
|
35
41
|
loading.value = false;
|
|
@@ -39,7 +45,13 @@ export function useDictTree(storeOrType, maybeType) {
|
|
|
39
45
|
loading.value = true;
|
|
40
46
|
try {
|
|
41
47
|
const entry = await manager.refresh(dictType, storeName);
|
|
42
|
-
|
|
48
|
+
const newTree = entry.tree ?? null;
|
|
49
|
+
if (tree.value && newTree) {
|
|
50
|
+
tree.value.splice(0, tree.value.length, ...newTree);
|
|
51
|
+
triggerRef(tree);
|
|
52
|
+
} else {
|
|
53
|
+
tree.value = newTree;
|
|
54
|
+
}
|
|
43
55
|
if (entry.tree) buildMaps(entry.tree, nodeMap, pathMap);
|
|
44
56
|
} finally {
|
|
45
57
|
loading.value = false;
|
|
@@ -47,7 +59,13 @@ export function useDictTree(storeOrType, maybeType) {
|
|
|
47
59
|
}
|
|
48
60
|
onMounted(load);
|
|
49
61
|
watch(() => manager.locale.value, load);
|
|
50
|
-
return {
|
|
62
|
+
return {
|
|
63
|
+
tree,
|
|
64
|
+
translate,
|
|
65
|
+
findPath,
|
|
66
|
+
loading,
|
|
67
|
+
refresh
|
|
68
|
+
};
|
|
51
69
|
}
|
|
52
70
|
function buildMaps(nodes, nodeMap, pathMap, ancestors = []) {
|
|
53
71
|
for (const node of nodes) {
|
|
@@ -20,7 +20,9 @@ async function fetchDictImpl(_storeName, config, types, locale) {
|
|
|
20
20
|
}
|
|
21
21
|
return response.json();
|
|
22
22
|
} catch (e) {
|
|
23
|
-
throw new Error(`Failed to fetch dictionary: ${e instanceof Error ? e.message : String(e)}`, {
|
|
23
|
+
throw new Error(`Failed to fetch dictionary: ${e instanceof Error ? e.message : String(e)}`, {
|
|
24
|
+
cause: e
|
|
25
|
+
});
|
|
24
26
|
}
|
|
25
27
|
}
|
|
26
28
|
async function fetchVersionImpl(_storeName, config) {
|
|
@@ -36,7 +38,9 @@ async function fetchVersionImpl(_storeName, config) {
|
|
|
36
38
|
}
|
|
37
39
|
return data.version;
|
|
38
40
|
} catch (e) {
|
|
39
|
-
throw new Error(`Failed to fetch version: ${e instanceof Error ? e.message : String(e)}`, {
|
|
41
|
+
throw new Error(`Failed to fetch version: ${e instanceof Error ? e.message : String(e)}`, {
|
|
42
|
+
cause: e
|
|
43
|
+
});
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
export function createDefaultAdapter(options) {
|
|
@@ -27,7 +27,9 @@ export class IndexedDBCache {
|
|
|
27
27
|
resolve();
|
|
28
28
|
});
|
|
29
29
|
request.addEventListener("error", () => {
|
|
30
|
-
reject(
|
|
30
|
+
reject(
|
|
31
|
+
new Error("Failed to open IndexedDB: " + (request.error?.message || "unknown error"))
|
|
32
|
+
);
|
|
31
33
|
});
|
|
32
34
|
request.addEventListener("blocked", () => {
|
|
33
35
|
reject(new Error("IndexedDB is blocked by another tab"));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { IndexedDBCache } from './cache/indexeddb-cache.js';
|
|
2
1
|
import type { DictAdapter, DictEntry, DictItem, TranslateOptions, TranslatePathOptions, GetDictItemOptions } from '../types/index.js';
|
|
2
|
+
import { IndexedDBCache } from './cache/indexeddb-cache.js';
|
|
3
3
|
/**
|
|
4
4
|
* DictManager 构造参数。
|
|
5
5
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { shallowRef } from "vue";
|
|
2
|
-
import { MemoryCache } from "./cache/memory-cache.js";
|
|
3
2
|
import { DEFAULT_STORE_NAME } from "./cache/indexeddb-cache.js";
|
|
3
|
+
import { MemoryCache } from "./cache/memory-cache.js";
|
|
4
4
|
import { VersionCheck } from "./cache/version-check.js";
|
|
5
5
|
export class DictManager {
|
|
6
6
|
memoryCache;
|
|
@@ -192,7 +192,7 @@ export class DictManager {
|
|
|
192
192
|
const key = this.buildKey(type, storeName);
|
|
193
193
|
const entry = this.memoryCache.get(key);
|
|
194
194
|
if (!entry) return String(value);
|
|
195
|
-
const item = entry.data
|
|
195
|
+
const item = entry.data?.items?.find((i) => this.codeMatch(i.value, value));
|
|
196
196
|
if (!item) return String(value);
|
|
197
197
|
return item[field] ?? item.label;
|
|
198
198
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { watch } from "vue";
|
|
2
2
|
import { defineNuxtPlugin, useRequestEvent, useCookie, useRoute } from "#imports";
|
|
3
|
-
import { DictManager } from "../core/dict-manager.js";
|
|
4
|
-
import { IndexedDBCache, DEFAULT_STORE_NAME } from "../core/cache/indexeddb-cache.js";
|
|
5
3
|
import { createDefaultAdapter } from "../core/adapter.js";
|
|
4
|
+
import { IndexedDBCache, DEFAULT_STORE_NAME } from "../core/cache/indexeddb-cache.js";
|
|
5
|
+
import { DictManager } from "../core/dict-manager.js";
|
|
6
|
+
import { defaultOptions } from "../options.js";
|
|
6
7
|
import { createDictTranslator } from "../utils/dict-translator.js";
|
|
7
8
|
import { createLogger } from "../utils/logger.js";
|
|
8
|
-
import { defaultOptions } from "../options.js";
|
|
9
9
|
function resolveServerLocale(options) {
|
|
10
10
|
const event = useRequestEvent();
|
|
11
11
|
if (!event) return options.locale.default;
|
|
@@ -78,7 +78,9 @@ function createAdapters(options, logger) {
|
|
|
78
78
|
apiHeaderKey: options.locale.apiHeaderKey
|
|
79
79
|
});
|
|
80
80
|
adapters.set(DEFAULT_STORE_NAME, defaultAdapter);
|
|
81
|
-
logger.debug(
|
|
81
|
+
logger.debug(
|
|
82
|
+
`Dict adapter created for default store '${DEFAULT_STORE_NAME}': ${options.api.baseURL}${options.api.dictEndpoint}`
|
|
83
|
+
);
|
|
82
84
|
for (const [storeName, storeApi] of Object.entries(options.stores)) {
|
|
83
85
|
const adapter = storeApi.adapter ?? createDefaultAdapter({
|
|
84
86
|
baseURL: resolveBaseURL(storeApi.baseURL ?? options.api.baseURL),
|
package/package.json
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lacqjs/nuxt-dict",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Nuxt 数据字典模块,提供扁平/树形字典翻译、多语言国际化、三级缓存与 SSR 预取。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"@lacqjs/nuxt-dict",
|
|
7
|
+
"dict",
|
|
7
8
|
"nuxt",
|
|
8
|
-
"nuxt-dict"
|
|
9
|
-
"dict"
|
|
9
|
+
"nuxt-dict"
|
|
10
10
|
],
|
|
11
|
+
"homepage": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
11
12
|
"license": "MIT",
|
|
12
13
|
"author": {
|
|
13
14
|
"name": "miaozhongfei",
|
|
14
15
|
"email": "miaozhongfei@example.com"
|
|
15
16
|
},
|
|
16
|
-
"repository": "miaozhongfei/nuxt-dict",
|
|
17
|
-
"homepage": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
18
|
-
"documentation": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
19
17
|
"contributors": [],
|
|
18
|
+
"repository": "miaozhongfei/nuxt-dict",
|
|
20
19
|
"files": [
|
|
21
20
|
"dist"
|
|
22
21
|
],
|
|
@@ -67,6 +66,7 @@
|
|
|
67
66
|
"pnpm": ">=10.22.0",
|
|
68
67
|
"yarn": ">=1.22.22"
|
|
69
68
|
},
|
|
69
|
+
"documentation": "https://miaozhongfei.github.io/nuxt-dict/",
|
|
70
70
|
"scripts": {
|
|
71
71
|
"cleanup": "pnpx nuxt cleanup || npx nuxt cleanup",
|
|
72
72
|
"clean:install": "pnpm run cleanup && pnpm run docs:cleanup && pnpm run playground:cleanup && pnpm install",
|
|
@@ -78,10 +78,11 @@
|
|
|
78
78
|
"docs:generate": "nuxi generate docs",
|
|
79
79
|
"docs:cleanup": "pnpx rimraf docs/.nuxt docs/.output docs/dist docs/node_modules docs/.data",
|
|
80
80
|
"playground:cleanup": "pnpx rimraf playground/.nuxt playground/.output playground/dist playground/node_modules",
|
|
81
|
-
"
|
|
82
|
-
"release
|
|
83
|
-
"release:
|
|
84
|
-
"release:e2e
|
|
81
|
+
"demo:base-api:dev": "nuxi dev examples/demo-base-api",
|
|
82
|
+
"release": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push origin dev --follow-tags",
|
|
83
|
+
"release:major": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push origin dev --follow-tags",
|
|
84
|
+
"release:e2e": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push origin dev --follow-tags",
|
|
85
|
+
"release:e2e:major": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push origin dev --follow-tags",
|
|
85
86
|
"lint": "oxlint . && oxlint docs --no-error-on-unmatched-pattern",
|
|
86
87
|
"lint:fix": "oxlint --fix . && oxlint --fix docs --no-error-on-unmatched-pattern",
|
|
87
88
|
"fmt": "oxfmt",
|