@metajoy-preview/config 0.0.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.md ADDED
@@ -0,0 +1,147 @@
1
+ # @metajoy/config
2
+
3
+ Metajoy 的配置工具包,用来统一定义、加载和监听项目配置。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install @metajoy/config
9
+ # 或
10
+ pnpm add @metajoy/config
11
+ ```
12
+
13
+ ## 快速开始
14
+
15
+ 在项目根目录创建 `metajoy.config.ts`:
16
+
17
+ ```ts
18
+ import { defineConfig } from "@metajoy/config";
19
+
20
+ export default defineConfig({
21
+ api: {
22
+ baseUrl: "http://localhost:8080",
23
+ docPath: "/swagger/doc.json",
24
+ },
25
+ });
26
+ ```
27
+
28
+ 在代码中读取配置:
29
+
30
+ ```ts
31
+ import { loadConfig } from "@metajoy/config";
32
+
33
+ const { config } = await loadConfig();
34
+ console.log(config.api.baseUrl);
35
+ ```
36
+
37
+ ## 导出 API
38
+
39
+ ### defineConfig
40
+
41
+ 用于定义配置,提供类型提示:
42
+
43
+ ```ts
44
+ import { defineConfig } from "@metajoy/config";
45
+
46
+ export default defineConfig({
47
+ api: {
48
+ baseUrl: "https://api.example.com",
49
+ },
50
+ });
51
+ ```
52
+
53
+ ### loadConfig
54
+
55
+ 加载并解析配置:
56
+
57
+ ```ts
58
+ import { loadConfig } from "@metajoy/config";
59
+
60
+ const result = await loadConfig();
61
+ console.log(result.config);
62
+ ```
63
+
64
+ ### watchConfig
65
+
66
+ 监听配置变更(适合开发环境热更新):
67
+
68
+ ```ts
69
+ import { watchConfig } from "@metajoy/config";
70
+
71
+ await watchConfig({
72
+ onUpdate(event) {
73
+ console.log("config updated", event.newConfig);
74
+ },
75
+ });
76
+ ```
77
+
78
+ ## 配置结构
79
+
80
+ 当前支持的配置如下:
81
+
82
+ ```ts
83
+ interface MetajoyConfig {
84
+ api?: {
85
+ baseUrl?: string;
86
+ docPath?: string; // 默认 "/swagger/doc.json"
87
+ overrides?: Record<string, string | { baseUrl?: string; docPath?: string }>;
88
+ };
89
+ }
90
+ ```
91
+
92
+ 示例:
93
+
94
+ ```ts
95
+ import { defineConfig } from "@metajoy/config";
96
+
97
+ export default defineConfig({
98
+ api: {
99
+ baseUrl: "http://gateway:8080",
100
+ docPath: "/swagger/doc.json",
101
+ overrides: {
102
+ auth: "http://auth-service:8080",
103
+ user: {
104
+ baseUrl: "http://user-service:8080",
105
+ docPath: "/api/doc.json",
106
+ },
107
+ },
108
+ },
109
+ });
110
+ ```
111
+
112
+ ## 配置来源
113
+
114
+ 底层基于 c12,默认配置名为 `metajoy`,并启用 package.json 配置读取。
115
+
116
+ 常见来源包括:
117
+
118
+ - `metajoy.config.ts` / `metajoy.config.js`
119
+ - `package.json` 中的 `metajoy` 字段
120
+
121
+ ## TypeScript 类型
122
+
123
+ 可直接使用以下类型:
124
+
125
+ - `MetajoyConfig`
126
+ - `MetajoyConfigDefaults`
127
+ - `MetajoyConfigWithDefaults`
128
+
129
+ 示例:
130
+
131
+ ```ts
132
+ import type { MetajoyConfig } from "@metajoy/config";
133
+
134
+ const config: MetajoyConfig = {
135
+ api: {
136
+ baseUrl: "http://localhost:8080",
137
+ },
138
+ };
139
+ ```
140
+
141
+ ## 开发
142
+
143
+ ```bash
144
+ vp install
145
+ vp test
146
+ vp pack
147
+ ```
@@ -0,0 +1,114 @@
1
+ import * as _$c12 from "c12";
2
+ import { ConfigWatcher, InputConfig, LoadConfigOptions, ResolvedConfig, WatchConfigOptions } from "c12";
3
+ import { Readable } from "node:stream";
4
+
5
+ //#region ../../node_modules/.pnpm/giget@3.2.0/node_modules/giget/dist/_chunks/libs/nypm.d.mts
6
+ type e = `npm` | `yarn` | `pnpm` | `bun` | `deno`;
7
+ type t = {
8
+ name: e;
9
+ command: string;
10
+ version?: string;
11
+ buildMeta?: string;
12
+ majorVersion?: string;
13
+ lockFile?: string | string[];
14
+ files?: string[];
15
+ };
16
+ type n = {
17
+ cwd?: string;
18
+ env?: Record<string, string>;
19
+ silent?: boolean;
20
+ packageManager?: t | e;
21
+ installPeerDependencies?: boolean;
22
+ dev?: boolean;
23
+ workspace?: boolean | string;
24
+ global?: boolean;
25
+ corepack?: boolean;
26
+ dry?: boolean;
27
+ };
28
+ type r = {
29
+ exec?: {
30
+ command: string;
31
+ args: string[];
32
+ };
33
+ };
34
+ declare function i(e?: Pick<n, `cwd` | `silent` | `packageManager` | `dry` | `corepack`> & {
35
+ frozenLockFile?: boolean;
36
+ ignoreWorkspace?: boolean;
37
+ }): Promise<r>;
38
+ //#endregion
39
+ //#region ../../node_modules/.pnpm/giget@3.2.0/node_modules/giget/dist/index.d.mts
40
+ type TarOutput = Readable | ReadableStream<Uint8Array>;
41
+ interface TemplateInfo {
42
+ name: string;
43
+ tar: string | ((options?: {
44
+ auth?: string;
45
+ }) => TarOutput | Promise<TarOutput>);
46
+ version?: string;
47
+ subdir?: string;
48
+ url?: string;
49
+ defaultDir?: string;
50
+ headers?: Record<string, string | undefined>;
51
+ source?: never;
52
+ dir?: never;
53
+ [key: string]: any;
54
+ }
55
+ type TemplateProvider = (input: string, options: {
56
+ auth?: string;
57
+ }) => TemplateInfo | Promise<TemplateInfo> | null; //#endregion
58
+ //#region src/giget.d.ts
59
+ type InstallOptions = Parameters<typeof i>[0];
60
+ interface DownloadTemplateOptions {
61
+ provider?: string;
62
+ force?: boolean;
63
+ forceClean?: boolean;
64
+ offline?: boolean;
65
+ preferOffline?: boolean;
66
+ providers?: Record<string, TemplateProvider>;
67
+ dir?: string;
68
+ registry?: false | string;
69
+ cwd?: string;
70
+ auth?: string;
71
+ install?: boolean | InstallOptions;
72
+ silent?: boolean;
73
+ }
74
+ //#endregion
75
+ //#region src/config.d.ts
76
+ interface ApiConfig {
77
+ /**
78
+ * 接口基础地址
79
+ * @example "https://api.example.com"
80
+ */
81
+ baseUrl?: string;
82
+ /**
83
+ * 接口文档路径
84
+ * @default "/swagger/doc.json"
85
+ */
86
+ docPath?: string;
87
+ /**
88
+ * 接口覆盖配置
89
+ * @example { "users": "https://users.example.com", "posts": "https://posts.example.com" }
90
+ */
91
+ overrides?: Record<string, string | Pick<ApiConfig, "baseUrl" | "docPath">>;
92
+ }
93
+ interface MetajoyConfig {
94
+ /**
95
+ * 接口配置
96
+ */
97
+ api?: ApiConfig;
98
+ }
99
+ //#endregion
100
+ //#region src/index.d.ts
101
+ declare function defineConfig(config: InputConfig<MetajoyConfig> & {
102
+ extends?: string | (string | [string, DownloadTemplateOptions])[];
103
+ }): _$c12.C12InputConfig<MetajoyConfig, _$c12.ConfigLayerMeta> & MetajoyConfig & {
104
+ extends?: string | (string | [string, DownloadTemplateOptions])[];
105
+ };
106
+ declare const _defaults: {
107
+ api: {};
108
+ };
109
+ type MetajoyConfigDefaults = typeof _defaults;
110
+ type MetajoyConfigWithDefaults = MetajoyConfig & MetajoyConfigDefaults;
111
+ declare function loadConfig(options?: LoadConfigOptions<MetajoyConfig>): Promise<ResolvedConfig<MetajoyConfigWithDefaults, _$c12.ConfigLayerMeta>>;
112
+ declare function watchConfig(options?: WatchConfigOptions<MetajoyConfig>): Promise<ConfigWatcher<MetajoyConfigWithDefaults>>;
113
+ //#endregion
114
+ export { type MetajoyConfig, MetajoyConfigDefaults, MetajoyConfigWithDefaults, defineConfig, loadConfig, watchConfig };
package/dist/index.mjs ADDED
@@ -0,0 +1,19 @@
1
+ import { loadConfig as loadConfig$1, watchConfig as watchConfig$1 } from "c12";
2
+ import { merge } from "lodash-es";
3
+ //#region src/index.ts
4
+ function defineConfig(config) {
5
+ return config;
6
+ }
7
+ const _options = {
8
+ name: "metajoy",
9
+ packageJson: true,
10
+ defaults: { api: {} }
11
+ };
12
+ async function loadConfig(options) {
13
+ return await loadConfig$1(merge({}, _options, options));
14
+ }
15
+ async function watchConfig(options) {
16
+ return await watchConfig$1(merge({}, _options, options));
17
+ }
18
+ //#endregion
19
+ export { defineConfig, loadConfig, watchConfig };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@metajoy-preview/config",
3
+ "version": "0.0.1",
4
+ "description": "Metajoy 项目的配置工具",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./dist/index.mjs",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "dependencies": {
17
+ "c12": "4.0.0-beta.5",
18
+ "lodash-es": "^4.18.1"
19
+ },
20
+ "devDependencies": {
21
+ "@types/lodash-es": "^4.17.12",
22
+ "giget": "^3.2.0"
23
+ },
24
+ "inlinedDependencies": {
25
+ "giget": "3.2.0"
26
+ },
27
+ "scripts": {
28
+ "build": "vp pack",
29
+ "dev": "vp pack --watch",
30
+ "test": "vp test",
31
+ "check": "vp check"
32
+ }
33
+ }