@lacqjs/nuxt-dict 0.0.2

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.
Files changed (35) hide show
  1. package/README.md +0 -0
  2. package/dist/module.d.mts +13 -0
  3. package/dist/module.d.ts +13 -0
  4. package/dist/module.json +13 -0
  5. package/dist/module.mjs +83 -0
  6. package/dist/runtime/composables/useDict.d.ts +14 -0
  7. package/dist/runtime/composables/useDict.js +59 -0
  8. package/dist/runtime/composables/useDictOptions.d.ts +13 -0
  9. package/dist/runtime/composables/useDictOptions.js +17 -0
  10. package/dist/runtime/composables/useDictTree.d.ts +13 -0
  11. package/dist/runtime/composables/useDictTree.js +53 -0
  12. package/dist/runtime/composables/useLocale.d.ts +10 -0
  13. package/dist/runtime/composables/useLocale.js +20 -0
  14. package/dist/runtime/core/adapter.d.ts +17 -0
  15. package/dist/runtime/core/adapter.js +50 -0
  16. package/dist/runtime/core/cache/indexeddb-cache.d.ts +48 -0
  17. package/dist/runtime/core/cache/indexeddb-cache.js +142 -0
  18. package/dist/runtime/core/cache/memory-cache.d.ts +25 -0
  19. package/dist/runtime/core/cache/memory-cache.js +79 -0
  20. package/dist/runtime/core/cache/version-check.d.ts +26 -0
  21. package/dist/runtime/core/cache/version-check.js +38 -0
  22. package/dist/runtime/core/dict-manager.d.ts +74 -0
  23. package/dist/runtime/core/dict-manager.js +217 -0
  24. package/dist/runtime/options.d.ts +3 -0
  25. package/dist/runtime/options.js +33 -0
  26. package/dist/runtime/plugins/dict.d.ts +2 -0
  27. package/dist/runtime/plugins/dict.js +130 -0
  28. package/dist/runtime/types/index.d.ts +171 -0
  29. package/dist/runtime/types/index.js +0 -0
  30. package/dist/runtime/utils/dict-translator.d.ts +34 -0
  31. package/dist/runtime/utils/dict-translator.js +30 -0
  32. package/dist/runtime/utils/logger.d.ts +10 -0
  33. package/dist/runtime/utils/logger.js +33 -0
  34. package/dist/types.d.mts +3 -0
  35. package/package.json +88 -0
@@ -0,0 +1,171 @@
1
+ import type { ShallowRef, Ref, ComputedRef } from 'vue';
2
+ /** 仓库名字面量联合类型,由 addTypeTemplate 根据 stores 配置动态生成 */
3
+ import type { StoreKey } from '#build/types/nuxt-dict-store-names';
4
+ export type { StoreKey };
5
+ /** 字典项 */
6
+ export interface DictItem {
7
+ code: string | number;
8
+ label: string;
9
+ [key: string]: unknown;
10
+ }
11
+ /** 树形字典节点,用于级联选择器 */
12
+ export interface TreeNode extends DictItem {
13
+ children?: TreeNode[];
14
+ }
15
+ /** 单个字典类型的数据结构 */
16
+ export interface DictEntry {
17
+ type: string;
18
+ items: DictItem[];
19
+ tree?: TreeNode[];
20
+ }
21
+ /** API 返回的字典响应格式 */
22
+ export interface DictResponse {
23
+ version: string;
24
+ data: Record<string, DictEntry>;
25
+ }
26
+ /** IndexedDB 中存储的缓存条目 */
27
+ export interface CacheEntry<T = DictEntry> {
28
+ data: T;
29
+ timestamp: number;
30
+ version: string;
31
+ }
32
+ /** 字典适配器接口,允许用户自定义数据源(REST / GraphQL / 本地文件等) */
33
+ export interface DictAdapter {
34
+ fetchDict(storeName: string, options: {
35
+ types: string[];
36
+ locale: string;
37
+ }): Promise<DictResponse>;
38
+ fetchVersion(storeName: string): Promise<string>;
39
+ }
40
+ /** 语言来源类型 */
41
+ export type LocaleSource = 'cookie' | 'header' | 'query';
42
+ /** 单个仓库的 API 配置。未配置项从全局 `api` 继承。 */
43
+ export interface StoreApiOptions {
44
+ /** API 基础地址,默认继承全局 `api.baseURL` */
45
+ baseURL?: string;
46
+ /** 字典列表接口路径,默认继承全局 `api.dictEndpoint` */
47
+ dictEndpoint?: string;
48
+ /** 版本号接口路径,默认继承全局 `api.versionEndpoint` */
49
+ versionEndpoint?: string;
50
+ /** 自定义字典适配器,不传则使用默认 REST 适配器(继承全局 api 配置) */
51
+ adapter?: DictAdapter;
52
+ }
53
+ /** 模块配置项(用户可传,字段均可选) */
54
+ export interface ModuleOptions {
55
+ /** 是否启用字典模块,默认 `true` */
56
+ enable?: boolean;
57
+ /** 日志级别,0=静默, 1=错误, 2=警告, 3=信息, 4=调试, 5=详细,默认 `3` */
58
+ logLevel?: number;
59
+ api?: {
60
+ /** API 基础地址,支持绝对 URL(外部接口)或相对路径(本地接口),默认 `'/api'` */
61
+ baseURL?: string;
62
+ /** 字典列表接口路径,默认 `'/dict/list'` */
63
+ dictEndpoint?: string;
64
+ /** 版本号接口路径,默认 `'/dict/version'` */
65
+ versionEndpoint?: string;
66
+ /** 自定义字典适配器,不传则使用默认 REST 适配器 */
67
+ adapter?: DictAdapter;
68
+ };
69
+ cache?: {
70
+ /** 内存缓存最大条目数,默认 `200` */
71
+ memoryMax?: number;
72
+ /** 内存缓存 TTL(毫秒),`0` 表示永不过期,默认 `0` */
73
+ ttl?: number;
74
+ indexedDB?: {
75
+ /** 是否启用 IndexedDB 持久缓存,默认 `true` */
76
+ enabled?: boolean;
77
+ /** IndexedDB 数据库名称,默认 `'nuxt-dict'` */
78
+ dbName?: string;
79
+ };
80
+ };
81
+ locale?: {
82
+ /** 兜底语言,无法从指定来源检测到语言时使用,默认 `'zh-CN'` */
83
+ default?: string;
84
+ /** 语言检测来源,`'cookie'` / `'header'` / `'query'`,默认 `'cookie'` */
85
+ source?: LocaleSource;
86
+ /** `source` 为 `'cookie'` 时读取的 cookie 名称,默认 `'lang'` */
87
+ cookieKey?: string;
88
+ /** `source` 为 `'query'` 时读取的 URL 查询参数名,默认 `'lang'` */
89
+ queryKey?: string;
90
+ /** `source` 为 `'header'` 时读取的请求头名称,默认 `'accept-language'` */
91
+ headerKey?: string;
92
+ /** 发给字典 API 时,语言值的查询参数名,设为 `''` 则不传,默认 `'lang'` */
93
+ paramKey?: string;
94
+ /** 发给字典 API 时,语言值的请求头名,设为 `''` 则不传,默认 `'X-Locale'` */
95
+ apiHeaderKey?: string;
96
+ };
97
+ /** 仓库 API 配置映射。key 为仓库名,value 为该仓库的 API 端点配置。
98
+ * 未配置的字段从全局 `api` 继承。未在此列出的仓库使用全局 `api` 配置。
99
+ * @example { payment: { baseURL: 'https://pay-api.com' }, logistics: { dictEndpoint: '/logistics/list' } }
100
+ */
101
+ stores?: Record<string, StoreApiOptions>;
102
+ ssr?: {
103
+ /** 服务端预取的字典类型列表,加速首屏渲染 */
104
+ prefetch?: string[];
105
+ };
106
+ version?: {
107
+ /** localStorage 中存储版本号的 key,默认 `'__NUXT_DICT_VERSION__'` */
108
+ storageKey?: string;
109
+ };
110
+ }
111
+ /** 模块配置项(内部解析后,字段均必填) */
112
+ export interface ResolvedModuleOptions {
113
+ enable: boolean;
114
+ logLevel: number;
115
+ api: {
116
+ baseURL: string;
117
+ dictEndpoint: string;
118
+ versionEndpoint: string;
119
+ adapter?: DictAdapter;
120
+ };
121
+ cache: {
122
+ memoryMax: number;
123
+ ttl: number;
124
+ indexedDB: {
125
+ enabled: boolean;
126
+ dbName: string;
127
+ };
128
+ };
129
+ locale: {
130
+ default: string;
131
+ source: LocaleSource;
132
+ cookieKey: string;
133
+ queryKey: string;
134
+ headerKey: string;
135
+ paramKey: string;
136
+ apiHeaderKey: string;
137
+ };
138
+ /** 仓库 API 配置映射(解析后) */
139
+ stores: Record<string, StoreApiOptions>;
140
+ ssr: {
141
+ prefetch: string[];
142
+ };
143
+ version: {
144
+ storageKey: string;
145
+ };
146
+ }
147
+ /** useDict 返回类型 */
148
+ export interface UseDictReturn {
149
+ data: ShallowRef<DictItem[] | null>;
150
+ translate: (code: string | number) => string;
151
+ loading: Ref<boolean>;
152
+ error: Ref<string | null>;
153
+ refresh: () => Promise<void>;
154
+ }
155
+ /** useDictOptions 返回类型 */
156
+ export interface UseDictOptionsReturn {
157
+ options: ComputedRef<{
158
+ label: string;
159
+ value: string | number;
160
+ }[]>;
161
+ loading: Ref<boolean>;
162
+ refresh: () => Promise<void>;
163
+ }
164
+ /** useDictTree 返回类型 */
165
+ export interface UseDictTreeReturn {
166
+ tree: ShallowRef<TreeNode[] | null>;
167
+ translate: (code: string | number) => string;
168
+ findPath: (code: string | number) => string[];
169
+ loading: Ref<boolean>;
170
+ refresh: () => Promise<void>;
171
+ }
File without changes
@@ -0,0 +1,34 @@
1
+ import type { DictManager } from '../core/dict-manager.js';
2
+ /**
3
+ * 创建字典翻译器,封装 translate / translatePath 方法。
4
+ * 作为 $dict 注入到 NuxtApp,供组件直接调用翻译。
5
+ * 使用包装层而非直接暴露 DictManager,以控制公开 API 范围。
6
+ *
7
+ * @example
8
+ * // 默认存储库 'dicts'
9
+ * $dict.translate('gender', code)
10
+ * $dict.translatePath('region', code)
11
+ *
12
+ * // 指定存储库,storeName 作为第一个参数
13
+ * $dict.translate('dicts2', 'gender', code)
14
+ * $dict.translatePath('dicts2', 'region', code)
15
+ * $dict.translatePath('dicts2', 'region', code, ' -> ')
16
+ *
17
+ * // 注意:默认存储库 + 自定义分隔符需显式传 storeName
18
+ * $dict.translatePath('dicts', 'region', code, ' -> ')
19
+ */
20
+ export declare function createDictTranslator(manager: DictManager): {
21
+ /**
22
+ * 通过字典类型和编码获取翻译文本。
23
+ * 2 参:默认存储库;3 参:指定存储库(storeName 为第一个参数)
24
+ */
25
+ translate(storeOrType: string, codeOrType: string | number, code?: string | number): string;
26
+ /**
27
+ * 获取树形字典中某个编码的完整层级路径。
28
+ * 2 参:默认存储库 + 默认分隔符
29
+ * 3 参:指定存储库 + 默认分隔符(storeName 为第一个参数)
30
+ * 4 参:指定存储库 + 自定义分隔符
31
+ * 默认存储库 + 自定义分隔符需显式传 'dicts' 作为 storeName
32
+ */
33
+ translatePath(storeOrType: string, codeOrType: string | number, separatorOrCode?: string | number, separator?: string): string;
34
+ };
@@ -0,0 +1,30 @@
1
+ export function createDictTranslator(manager) {
2
+ return {
3
+ /**
4
+ * 通过字典类型和编码获取翻译文本。
5
+ * 2 参:默认存储库;3 参:指定存储库(storeName 为第一个参数)
6
+ */
7
+ translate(storeOrType, codeOrType, code) {
8
+ if (code !== void 0) {
9
+ return manager.translate(codeOrType, code, storeOrType);
10
+ }
11
+ return manager.translate(storeOrType, codeOrType);
12
+ },
13
+ /**
14
+ * 获取树形字典中某个编码的完整层级路径。
15
+ * 2 参:默认存储库 + 默认分隔符
16
+ * 3 参:指定存储库 + 默认分隔符(storeName 为第一个参数)
17
+ * 4 参:指定存储库 + 自定义分隔符
18
+ * 默认存储库 + 自定义分隔符需显式传 'dicts' 作为 storeName
19
+ */
20
+ translatePath(storeOrType, codeOrType, separatorOrCode, separator) {
21
+ if (separator !== void 0) {
22
+ return manager.translatePath(codeOrType, separatorOrCode, separator, storeOrType);
23
+ }
24
+ if (separatorOrCode !== void 0) {
25
+ return manager.translatePath(codeOrType, separatorOrCode, void 0, storeOrType);
26
+ }
27
+ return manager.translatePath(storeOrType, codeOrType);
28
+ }
29
+ };
30
+ }
@@ -0,0 +1,10 @@
1
+ import type { ConsolaInstance } from 'consola';
2
+ /**
3
+ * 创建带标签的 consola logger(单例模式)。
4
+ * 同一 tag 多次调用返回同一个实例,确保日志配置一致。
5
+ * @param tag 日志标签(模块名)
6
+ * @param options.level 日志级别,支持数字或字符串
7
+ */
8
+ export declare function createLogger(tag: string, options?: {
9
+ level?: number | string;
10
+ }): ConsolaInstance;
@@ -0,0 +1,33 @@
1
+ import { consola } from "consola";
2
+ const globalLoggers = /* @__PURE__ */ new Map();
3
+ export function createLogger(tag, options = {}) {
4
+ if (globalLoggers.has(tag)) {
5
+ const cached = globalLoggers.get(tag);
6
+ if (options.level !== void 0) {
7
+ cached.level = resolveLogLevel(options.level);
8
+ }
9
+ return cached;
10
+ }
11
+ const logger = consola.create({}).withTag(tag);
12
+ if (options.level !== void 0) {
13
+ logger.level = resolveLogLevel(options.level);
14
+ }
15
+ globalLoggers.set(tag, logger);
16
+ return logger;
17
+ }
18
+ function resolveLogLevel(level) {
19
+ if (typeof level === "number") return level;
20
+ const map = {
21
+ silent: -999,
22
+ fatal: 0,
23
+ error: 0,
24
+ warn: 1,
25
+ log: 2,
26
+ info: 3,
27
+ success: 3,
28
+ debug: 4,
29
+ trace: 5,
30
+ verbose: 999
31
+ };
32
+ return map[level.toLowerCase()] ?? 3;
33
+ }
@@ -0,0 +1,3 @@
1
+ export { type ModuleOptions } from '../dist/runtime/types/index.js'
2
+
3
+ export { default } from './module.mjs'
package/package.json ADDED
@@ -0,0 +1,88 @@
1
+ {
2
+ "name": "@lacqjs/nuxt-dict",
3
+ "version": "0.0.2",
4
+ "description": "Nuxt 数据字典模块,提供扁平/树形字典翻译、多语言国际化、三级缓存与 SSR 预取。",
5
+ "keywords": [
6
+ "@lacqjs/nuxt-dict",
7
+ "nuxt",
8
+ "nuxt-dict",
9
+ "dict"
10
+ ],
11
+ "license": "MIT",
12
+ "author": {
13
+ "name": "miaozf",
14
+ "email": "miaozf@example.com"
15
+ },
16
+ "contributors": [],
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "type": "module",
21
+ "main": "./dist/module.mjs",
22
+ "types": "./dist/types.d.mts",
23
+ "typesVersions": {
24
+ "*": {
25
+ ".": [
26
+ "./dist/types.d.mts"
27
+ ]
28
+ }
29
+ },
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/types.d.mts",
33
+ "import": "./dist/module.mjs"
34
+ }
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "registry": "https://registry.npmjs.org/"
39
+ },
40
+ "dependencies": {
41
+ "@nuxt/kit": "^4.4.8",
42
+ "compare-versions": "^6.1.1",
43
+ "consola": "^3.4.2",
44
+ "defu": "^6.1.7"
45
+ },
46
+ "devDependencies": {
47
+ "@nuxt/devtools": "^3.2.4",
48
+ "@nuxt/module-builder": "^1.0.2",
49
+ "@nuxt/schema": "^4.4.8",
50
+ "@types/node": "^25.9.2",
51
+ "changelogen": "^0.6.2",
52
+ "element-plus": "^2.14.1",
53
+ "nuxt": "^4.4.8",
54
+ "oxfmt": "^0.54.0",
55
+ "oxlint": "^1.69.0",
56
+ "typescript": "~5.9.3",
57
+ "vant": "^4.9.24",
58
+ "vue-tsc": "^3.3.4",
59
+ "ws": "^8.21.0"
60
+ },
61
+ "engines": {
62
+ "node": ">=22.0.0",
63
+ "npm": ">=10.0.0",
64
+ "pnpm": ">=10.22.0",
65
+ "yarn": ">=1.22.22"
66
+ },
67
+ "scripts": {
68
+ "cleanup": "pnpx nuxt cleanup || npx nuxt cleanup",
69
+ "clean:install": "pnpm run cleanup && pnpm run docs:cleanup && pnpm run playground:cleanup && pnpm install",
70
+ "dev": "pnpm run dev:prepare && nuxi dev playground",
71
+ "dev:build": "nuxi build playground",
72
+ "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
73
+ "docs:dev": "nuxi dev docs",
74
+ "docs:build": "nuxi build docs",
75
+ "docs:cleanup": "pnpx rimraf docs/.nuxt docs/.output docs/dist docs/node_modules",
76
+ "playground:cleanup": "pnpx rimraf playground/.nuxt playground/.output playground/dist playground/node_modules",
77
+ "release": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push --follow-tags",
78
+ "release:major": "pnpm lint && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push --follow-tags",
79
+ "release:e2e": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release && pnpm publish && git push --follow-tags",
80
+ "release:e2e:major": "pnpm lint && pnpm e2e && pnpm typecheck && pnpm prepack && changelogen --release --major && pnpm publish && git push --follow-tags",
81
+ "lint": "oxlint . && oxlint docs --no-error-on-unmatched-pattern",
82
+ "lint:fix": "oxlint --fix . && oxlint --fix docs --no-error-on-unmatched-pattern",
83
+ "fmt": "oxfmt",
84
+ "fmt:check": "oxfmt --check",
85
+ "e2e": "cd playground && pnpm e2e",
86
+ "typecheck": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
87
+ }
88
+ }