@yumerijs/core 2.1.0 → 2.1.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/dist/config.d.ts CHANGED
@@ -1,122 +1,31 @@
1
- /**
2
- * @time: 2025/05/24 12:18
3
- * @author: FireGuo
4
- * WindyPear-Team All right reserved
5
- **/
6
- /**
7
- * 插件配置模式定义类
8
- * 用于描述插件配置项的类型、默认值、描述等信息
9
- */
10
- export declare class ConfigSchema {
11
- /**
12
- * 配置项类型
13
- */
14
- type: 'string' | 'number' | 'boolean' | 'object' | 'array';
15
- /**
16
- * 默认值
17
- */
18
- default?: any;
19
- /**
20
- * 配置项描述
21
- */
1
+ export declare function fallback<T>(schema: Schema<T>, config: T): T;
2
+ export declare class Schema<T = any> {
3
+ _type?: T;
4
+ type: string;
5
+ isRequired?: boolean;
22
6
  description?: string;
23
- /**
24
- * 是否必需
25
- */
26
- required?: boolean;
27
- /**
28
- * 枚举值列表(可选项)
29
- */
30
- enum?: any[];
31
- /**
32
- * 数组项类型定义(当type为array时使用)
33
- */
34
- items?: ConfigSchema;
35
- /**
36
- * 对象属性定义(当type为object时使用)
37
- */
38
- properties?: Record<string, ConfigSchema>;
39
- /**
40
- * 创建配置模式对象
41
- * @param type 配置项类型
42
- * @param options 配置项选项
43
- */
44
- constructor(type: 'string' | 'number' | 'boolean' | 'object' | 'array', options?: {
45
- default?: any;
46
- description?: string;
47
- required?: boolean;
48
- enum?: any[];
49
- items?: ConfigSchema;
50
- properties?: Record<string, ConfigSchema>;
7
+ defaultValue?: any;
8
+ properties?: Record<string, Schema<any>>;
9
+ items?: Schema<any>;
10
+ enum?: T[];
11
+ constructor(definition: Omit<Schema<T>, '_type' | 'required' | 'default'> & {
12
+ enum?: T[];
51
13
  });
52
- /**
53
- * 创建字符串类型配置模式
54
- * @param options 配置项选项
55
- * @returns 配置模式对象
56
- */
57
- static string(options?: Omit<ConfigSchema, 'type' | 'items' | 'properties'>): ConfigSchema;
58
- /**
59
- * 创建数字类型配置模式
60
- * @param options 配置项选项
61
- * @returns 配置模式对象
62
- */
63
- static number(options?: Omit<ConfigSchema, 'type' | 'items' | 'properties'>): ConfigSchema;
64
- /**
65
- * 创建布尔类型配置模式
66
- * @param options 配置项选项
67
- * @returns 配置模式对象
68
- */
69
- static boolean(options?: Omit<ConfigSchema, 'type' | 'items' | 'properties'>): ConfigSchema;
70
- /**
71
- * 创建对象类型配置模式
72
- * @param properties 对象属性定义
73
- * @param options 配置项选项
74
- * @returns 配置模式对象
75
- */
76
- static object(properties: Record<string, ConfigSchema>, options?: Omit<ConfigSchema, 'type' | 'items' | 'properties'>): ConfigSchema;
77
- /**
78
- * 创建数组类型配置模式
79
- * @param items 数组项类型定义
80
- * @param options 配置项选项
81
- * @returns 配置模式对象
82
- */
83
- static array(items: ConfigSchema, options?: Omit<ConfigSchema, 'type' | 'items' | 'properties'>): ConfigSchema;
14
+ static string(description?: string): Schema<string>;
15
+ static number(description?: string): Schema<number>;
16
+ static boolean(description?: string): Schema<boolean>;
17
+ static array<T>(inner: Schema<T>, description?: string): Schema<T[]>;
18
+ static object<T extends {}>(properties: {
19
+ [K in keyof T]: Schema<T[K]>;
20
+ }, description?: string): Schema<T>;
21
+ static extend<T extends {}, U extends {}>(base: Schema<T>, extension: {
22
+ [K in keyof U]: Schema<U[K]>;
23
+ }, description?: string): Schema<T & U>;
24
+ static enum<L extends string | number>(values: L[], description?: string): Schema<L>;
25
+ required(this: this): this;
26
+ default(this: this, value: T): this;
84
27
  }
85
- export declare class Config {
86
- /**
87
- * 配置名称
88
- */
89
- name: string;
90
- /**
91
- * 配置内容
92
- */
93
- content: {
94
- [name: string]: any;
95
- };
96
- /**
97
- * 配置模式
98
- */
99
- schema?: Record<string, ConfigSchema>;
100
- /**
101
- * 创建配置对象
102
- * @param name 配置名称
103
- * @param content 配置内容
104
- * @param schema 配置模式
105
- */
106
- constructor(name: string, content?: {
107
- [name: string]: any;
108
- }, schema?: Record<string, ConfigSchema>);
109
- /**
110
- * 获取配置项值
111
- * @param key 配置项键名
112
- * @param defaultValue 默认值
113
- * @returns 配置项值
114
- */
115
- get<T>(key: string, defaultValue?: T): T;
116
- /**
117
- * 设置配置项值
118
- * @param key 配置项键名
119
- * @param value 配置项值
120
- */
121
- set(key: string, value: any): void;
28
+ export { Schema as ConfigSchema };
29
+ export interface Config {
30
+ [key: string]: any;
122
31
  }
package/dist/config.js CHANGED
@@ -1,151 +1,83 @@
1
1
  "use strict";
2
- /**
3
- * @time: 2025/05/24 12:18
4
- * @author: FireGuo
5
- * WindyPear-Team All right reserved
6
- **/
7
2
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.Config = exports.ConfigSchema = void 0;
9
- /**
10
- * 插件配置模式定义类
11
- * 用于描述插件配置项的类型、默认值、描述等信息
12
- */
13
- class ConfigSchema {
14
- /**
15
- * 配置项类型
16
- */
3
+ exports.ConfigSchema = exports.Schema = void 0;
4
+ exports.fallback = fallback;
5
+ function isNullable(value) {
6
+ return value === null || value === undefined;
7
+ }
8
+ function fallback(schema, config) {
9
+ if (!schema)
10
+ return config;
11
+ let result = config;
12
+ if (isNullable(result)) {
13
+ result = schema.defaultValue;
14
+ }
15
+ if (schema.type === 'object') {
16
+ if (typeof result !== 'object' || result === null) {
17
+ result = {};
18
+ }
19
+ for (const key in schema.properties) {
20
+ const innerSchema = schema.properties[key];
21
+ result[key] = fallback(innerSchema, result[key]);
22
+ }
23
+ }
24
+ else if (schema.type === 'array' && schema.items) {
25
+ if (!Array.isArray(result)) {
26
+ result = [];
27
+ }
28
+ result = result.map((item) => fallback(schema.items, item));
29
+ }
30
+ return result;
31
+ }
32
+ class Schema {
33
+ _type; // Phantom type
17
34
  type;
18
- /**
19
- * 默认值
20
- */
21
- default;
22
- /**
23
- * 配置项描述
24
- */
35
+ isRequired;
25
36
  description;
26
- /**
27
- * 是否必需
28
- */
29
- required;
30
- /**
31
- * 枚举值列表(可选项)
32
- */
33
- enum;
34
- /**
35
- * 数组项类型定义(当type为array时使用)
36
- */
37
- items;
38
- /**
39
- * 对象属性定义(当type为object时使用)
40
- */
37
+ defaultValue;
41
38
  properties;
42
- /**
43
- * 创建配置模式对象
44
- * @param type 配置项类型
45
- * @param options 配置项选项
46
- */
47
- constructor(type, options) {
48
- this.type = type;
49
- if (options) {
50
- this.default = options.default;
51
- this.description = options.description;
52
- this.required = options.required;
53
- this.enum = options.enum;
54
- this.items = options.items;
55
- this.properties = options.properties;
56
- }
39
+ items;
40
+ enum;
41
+ constructor(definition) {
42
+ this.type = definition.type;
43
+ this.isRequired = definition.isRequired;
44
+ this.description = definition.description;
45
+ this.defaultValue = definition.defaultValue;
46
+ this.properties = definition.properties;
47
+ this.items = definition.items;
48
+ this.enum = definition.enum;
57
49
  }
58
- /**
59
- * 创建字符串类型配置模式
60
- * @param options 配置项选项
61
- * @returns 配置模式对象
62
- */
63
- static string(options) {
64
- return new ConfigSchema('string', options);
50
+ static string(description) {
51
+ return new Schema({ type: 'string', description });
65
52
  }
66
- /**
67
- * 创建数字类型配置模式
68
- * @param options 配置项选项
69
- * @returns 配置模式对象
70
- */
71
- static number(options) {
72
- return new ConfigSchema('number', options);
53
+ static number(description) {
54
+ return new Schema({ type: 'number', description });
73
55
  }
74
- /**
75
- * 创建布尔类型配置模式
76
- * @param options 配置项选项
77
- * @returns 配置模式对象
78
- */
79
- static boolean(options) {
80
- return new ConfigSchema('boolean', options);
56
+ static boolean(description) {
57
+ return new Schema({ type: 'boolean', description });
81
58
  }
82
- /**
83
- * 创建对象类型配置模式
84
- * @param properties 对象属性定义
85
- * @param options 配置项选项
86
- * @returns 配置模式对象
87
- */
88
- static object(properties, options) {
89
- return new ConfigSchema('object', { ...options, properties });
59
+ static array(inner, description) {
60
+ return new Schema({ type: 'array', items: inner, description });
90
61
  }
91
- /**
92
- * 创建数组类型配置模式
93
- * @param items 数组项类型定义
94
- * @param options 配置项选项
95
- * @returns 配置模式对象
96
- */
97
- static array(items, options) {
98
- return new ConfigSchema('array', { ...options, items });
62
+ static object(properties, description) {
63
+ return new Schema({ type: 'object', properties, description });
99
64
  }
100
- }
101
- exports.ConfigSchema = ConfigSchema;
102
- class Config {
103
- /**
104
- * 配置名称
105
- */
106
- name = '';
107
- /**
108
- * 配置内容
109
- */
110
- content = {};
111
- /**
112
- * 配置模式
113
- */
114
- schema;
115
- /**
116
- * 创建配置对象
117
- * @param name 配置名称
118
- * @param content 配置内容
119
- * @param schema 配置模式
120
- */
121
- constructor(name, content, schema) {
122
- this.name = name;
123
- this.content = content || {}; // 如果 content 是 undefined,则赋值为空对象
124
- this.schema = schema;
65
+ static extend(base, extension, description) {
66
+ const combinedProperties = { ...base.properties, ...extension };
67
+ return new Schema({ type: 'object', properties: combinedProperties, description: description || base.description });
125
68
  }
126
- /**
127
- * 获取配置项值
128
- * @param key 配置项键名
129
- * @param defaultValue 默认值
130
- * @returns 配置项值
131
- */
132
- get(key, defaultValue) {
133
- if (this.content[key] !== undefined) {
134
- return this.content[key];
135
- }
136
- // 如果有schema,尝试从schema中获取默认值
137
- if (this.schema && this.schema[key] && this.schema[key].default !== undefined) {
138
- return this.schema[key].default;
139
- }
140
- return defaultValue;
69
+ static enum(values, description) {
70
+ const type = typeof values[0] === 'string' ? 'string' : typeof values[0] === 'number' ? 'number' : 'string'; // Infer type based on first value
71
+ return new Schema({ type, enum: values, description });
72
+ }
73
+ required() {
74
+ this.isRequired = true;
75
+ return this;
141
76
  }
142
- /**
143
- * 设置配置项值
144
- * @param key 配置项键名
145
- * @param value 配置项值
146
- */
147
- set(key, value) {
148
- this.content[key] = value;
77
+ default(value) {
78
+ this.defaultValue = value;
79
+ return this;
149
80
  }
150
81
  }
151
- exports.Config = Config;
82
+ exports.Schema = Schema;
83
+ exports.ConfigSchema = Schema;
package/dist/context.d.ts CHANGED
@@ -2,9 +2,8 @@ import { Core } from './core';
2
2
  import { Route } from './route';
3
3
  import { HookHandler } from './hook';
4
4
  import { Middleware } from './middleware';
5
- import { Config } from './config';
6
5
  interface Plugin {
7
- apply: (ctx: Context, config: Config) => Promise<void>;
6
+ apply: (ctx: Context, config: any) => any;
8
7
  disable: (ctx: Context) => Promise<void>;
9
8
  depend: Array<string>;
10
9
  provide: Array<string>;
@@ -102,7 +101,7 @@ export declare class Context {
102
101
  * @param plugin 插件实例
103
102
  * @param config 插件配置
104
103
  */
105
- apply(plugin: Plugin, config: Config): Promise<void>;
104
+ apply(plugin: Plugin, config: any): Promise<void>;
106
105
  /**
107
106
  * 注册 i18n
108
107
  * @param content 内容(可以是嵌套对象或单个key)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",