imean-service-engine 2.0.0 → 2.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.
- package/dist/index.d.mts +77 -52
- package/dist/index.d.ts +77 -52
- package/dist/index.js +2078 -1945
- package/dist/index.mjs +2076 -1944
- package/package.json +9 -2
- package/.vscode/settings.json +0 -8
- package/src/core/checker.ts +0 -33
- package/src/core/decorators.test.ts +0 -96
- package/src/core/decorators.ts +0 -68
- package/src/core/engine.test.ts +0 -218
- package/src/core/engine.ts +0 -635
- package/src/core/errors.ts +0 -28
- package/src/core/factory.test.ts +0 -73
- package/src/core/factory.ts +0 -92
- package/src/core/logger.ts +0 -65
- package/src/core/testing.ts +0 -73
- package/src/core/types.ts +0 -191
- package/src/index.ts +0 -49
- package/src/metadata/README.md +0 -422
- package/src/metadata/metadata.test.ts +0 -369
- package/src/metadata/metadata.ts +0 -512
- package/src/plugins/action/action-plugin.test.ts +0 -660
- package/src/plugins/action/decorator.ts +0 -14
- package/src/plugins/action/index.ts +0 -4
- package/src/plugins/action/plugin.ts +0 -349
- package/src/plugins/action/types.ts +0 -49
- package/src/plugins/action/utils.test.ts +0 -196
- package/src/plugins/action/utils.ts +0 -111
- package/src/plugins/cache/adapter.test.ts +0 -689
- package/src/plugins/cache/adapter.ts +0 -324
- package/src/plugins/cache/cache-plugin.test.ts +0 -269
- package/src/plugins/cache/decorator.ts +0 -26
- package/src/plugins/cache/index.ts +0 -20
- package/src/plugins/cache/plugin.ts +0 -299
- package/src/plugins/cache/types.ts +0 -69
- package/src/plugins/client-code/client-code-plugin.test.ts +0 -511
- package/src/plugins/client-code/format.ts +0 -9
- package/src/plugins/client-code/generator.test.ts +0 -52
- package/src/plugins/client-code/generator.ts +0 -263
- package/src/plugins/client-code/index.ts +0 -15
- package/src/plugins/client-code/plugin.ts +0 -158
- package/src/plugins/client-code/types.ts +0 -52
- package/src/plugins/client-code/utils.ts +0 -164
- package/src/plugins/graceful-shutdown/graceful-shutdown-plugin.test.ts +0 -401
- package/src/plugins/graceful-shutdown/index.ts +0 -3
- package/src/plugins/graceful-shutdown/plugin.ts +0 -279
- package/src/plugins/graceful-shutdown/types.ts +0 -17
- package/src/plugins/rate-limit/rate-limit-plugin.example.ts +0 -171
- package/src/plugins/route/components/Layout.tsx +0 -42
- package/src/plugins/route/components/ServiceStatusPage.tsx +0 -141
- package/src/plugins/route/decorator.ts +0 -50
- package/src/plugins/route/index.ts +0 -16
- package/src/plugins/route/plugin.ts +0 -218
- package/src/plugins/route/route-plugin.test.ts +0 -759
- package/src/plugins/route/types.ts +0 -72
- package/src/plugins/schedule/README.md +0 -309
- package/src/plugins/schedule/decorator.ts +0 -25
- package/src/plugins/schedule/index.ts +0 -12
- package/src/plugins/schedule/mock-etcd.ts +0 -145
- package/src/plugins/schedule/plugin.ts +0 -164
- package/src/plugins/schedule/schedule-plugin.test.ts +0 -312
- package/src/plugins/schedule/scheduler.ts +0 -164
- package/src/plugins/schedule/types.ts +0 -94
- package/src/plugins/schedule/utils.test.ts +0 -163
- package/src/plugins/schedule/utils.ts +0 -41
- package/tests/integration/client.test.ts +0 -203
- package/tests/integration/dev-service.ts +0 -301
- package/tests/integration/generated/client.ts +0 -123
- package/tests/integration/start-service.ts +0 -21
- package/tsconfig.json +0 -27
- package/tsup.config.ts +0 -16
- package/vitest.config.ts +0 -19
package/src/core/factory.test.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
2
|
-
import { CachePlugin } from "../plugins/cache";
|
|
3
|
-
import { RoutePlugin } from "../plugins/route";
|
|
4
|
-
import { Factory } from "./factory";
|
|
5
|
-
|
|
6
|
-
describe("Factory", () => {
|
|
7
|
-
beforeEach(() => {
|
|
8
|
-
// 每个测试都会创建新的 Factory,使用不同的 key,自动隔离
|
|
9
|
-
// 不需要手动清空注册表
|
|
10
|
-
});
|
|
11
|
-
describe("create", () => {
|
|
12
|
-
it("应该创建类型化的 Module 装饰器和 Microservice 类", () => {
|
|
13
|
-
const { Module, Microservice } = Factory.create(new CachePlugin());
|
|
14
|
-
|
|
15
|
-
expect(Module).toBeDefined();
|
|
16
|
-
expect(typeof Module).toBe("function");
|
|
17
|
-
expect(Microservice).toBeDefined();
|
|
18
|
-
expect(typeof Microservice).toBe("function");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it("应该创建引擎实例并注册插件", () => {
|
|
22
|
-
const { Microservice } = Factory.create(new CachePlugin());
|
|
23
|
-
|
|
24
|
-
const engine = new Microservice({
|
|
25
|
-
name: "test-service",
|
|
26
|
-
version: "1.0.0",
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
expect(engine).toBeDefined();
|
|
30
|
-
expect(engine.getModules).toBeDefined();
|
|
31
|
-
expect(engine.start).toBeDefined();
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("应该在构造函数中正确注册所有插件", () => {
|
|
35
|
-
const cachePlugin = new CachePlugin();
|
|
36
|
-
const routePlugin = new RoutePlugin();
|
|
37
|
-
|
|
38
|
-
const { Microservice } = Factory.create(cachePlugin, routePlugin);
|
|
39
|
-
|
|
40
|
-
const engine = new Microservice({
|
|
41
|
-
name: "test-service",
|
|
42
|
-
version: "1.0.0",
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// 验证插件已注册(通过检查模块配置类型)
|
|
46
|
-
const modules = engine.getModules();
|
|
47
|
-
expect(modules).toBeDefined();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("应该只包含显式注册的插件", () => {
|
|
51
|
-
const { Module, Microservice } = Factory.create(
|
|
52
|
-
new CachePlugin(),
|
|
53
|
-
new RoutePlugin()
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
// 先创建引擎实例
|
|
57
|
-
const engine = new Microservice({
|
|
58
|
-
name: "test-service",
|
|
59
|
-
version: "1.0.0",
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
// 然后使用装饰器
|
|
63
|
-
@Module("test-module", {
|
|
64
|
-
routePrefix: "/api",
|
|
65
|
-
cacheDefaultTtl: 5000,
|
|
66
|
-
})
|
|
67
|
-
class TestService {}
|
|
68
|
-
|
|
69
|
-
expect(TestService).toBeDefined();
|
|
70
|
-
expect(engine.getModules()).toHaveLength(1);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
package/src/core/factory.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { createClassDecorator } from "../metadata/metadata";
|
|
2
|
-
import { Microservice } from "./engine";
|
|
3
|
-
import { MicroserviceOptions, ModuleDecorator, Plugin } from "./types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 从插件数组中提取聚合的配置类型
|
|
7
|
-
*/
|
|
8
|
-
type ExtractPluginOptions<T extends readonly Plugin<any>[]> =
|
|
9
|
-
T extends readonly [infer P1, ...infer Rest]
|
|
10
|
-
? P1 extends Plugin<infer T1>
|
|
11
|
-
? Rest extends readonly Plugin<any>[]
|
|
12
|
-
? T1 & ExtractPluginOptions<Rest>
|
|
13
|
-
: T1
|
|
14
|
-
: ExtractPluginOptions<Rest extends readonly Plugin<any>[] ? Rest : []>
|
|
15
|
-
: Record<string, any>;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 从 Factory.create 返回值中提取 Microservice 实例类型
|
|
19
|
-
*/
|
|
20
|
-
export type FactoryMicroservice<T extends ReturnType<typeof Factory.create>> =
|
|
21
|
-
T["Microservice"] extends new (options: any) => infer Instance
|
|
22
|
-
? Instance
|
|
23
|
-
: never;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 从 Factory.create 返回值中提取 Module 装饰器类型
|
|
27
|
-
*/
|
|
28
|
-
export type FactoryModule<T extends ReturnType<typeof Factory.create>> =
|
|
29
|
-
T["Module"];
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Factory - 创建类型化的引擎实例
|
|
33
|
-
*
|
|
34
|
-
* 注意:所有插件都必须显式注册,不会自动包含任何默认插件
|
|
35
|
-
*/
|
|
36
|
-
export class Factory {
|
|
37
|
-
/**
|
|
38
|
-
* 创建类型化的引擎工厂
|
|
39
|
-
*
|
|
40
|
-
* @param plugins 插件列表(必须显式提供所有需要的插件)
|
|
41
|
-
* @returns 包含类型化的 Module 装饰器和 Microservice 类的对象
|
|
42
|
-
*/
|
|
43
|
-
static create<TPlugins extends readonly Plugin<any>[]>(
|
|
44
|
-
...plugins: TPlugins
|
|
45
|
-
): {
|
|
46
|
-
Module: ModuleDecorator<ExtractPluginOptions<TPlugins>>;
|
|
47
|
-
Microservice: new (options: MicroserviceOptions) => Microservice<
|
|
48
|
-
ExtractPluginOptions<TPlugins>
|
|
49
|
-
>;
|
|
50
|
-
} {
|
|
51
|
-
type TAggregatedOptions = ExtractPluginOptions<TPlugins>;
|
|
52
|
-
|
|
53
|
-
// 为每个工厂创建过程生成唯一的 key
|
|
54
|
-
// 使用 Symbol 和时间戳确保唯一性
|
|
55
|
-
const moduleMetadataKey = Symbol.for(
|
|
56
|
-
`imean:moduleMetadata:${Date.now()}:${Math.random()}`
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
// 创建一个类型化的 Microservice 类
|
|
60
|
-
class TypedMicroservice extends Microservice<TAggregatedOptions> {
|
|
61
|
-
constructor(options: MicroserviceOptions) {
|
|
62
|
-
// 传递唯一的 key 给父类,用于模块发现
|
|
63
|
-
super(options, moduleMetadataKey);
|
|
64
|
-
// 注册所有插件(在父类构造函数之后,直接访问 protected 成员)
|
|
65
|
-
for (const plugin of plugins) {
|
|
66
|
-
this.registerPlugin(plugin);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 创建类型化的 Module 装饰器
|
|
72
|
-
// 使用唯一的 key,这样不同工厂创建的装饰器会使用不同的 key
|
|
73
|
-
const baseDecorator = createClassDecorator(moduleMetadataKey);
|
|
74
|
-
|
|
75
|
-
const Module: ModuleDecorator<TAggregatedOptions> = (
|
|
76
|
-
name: string,
|
|
77
|
-
options?: TAggregatedOptions
|
|
78
|
-
) => {
|
|
79
|
-
// 创建类级别的元数据(每个类独有的)
|
|
80
|
-
const classMetadata = {
|
|
81
|
-
name: name || undefined,
|
|
82
|
-
options: (options || {}) as TAggregatedOptions,
|
|
83
|
-
};
|
|
84
|
-
return baseDecorator(classMetadata) as any;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
Module,
|
|
89
|
-
Microservice: TypedMicroservice,
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
}
|
package/src/core/logger.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import winston, { format } from "winston";
|
|
2
|
-
|
|
3
|
-
let TestHook: ((msg: string) => void) | null = null;
|
|
4
|
-
|
|
5
|
-
export function setTestHook(hook: (msg: string) => void) {
|
|
6
|
-
TestHook = hook;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
// 自定义日志格式,使输出更美观
|
|
10
|
-
const customFormat = format.printf((info) => {
|
|
11
|
-
const { level, timestamp, message, ...meta } = info;
|
|
12
|
-
const splat = info[Symbol.for("splat")] as any[] | undefined;
|
|
13
|
-
|
|
14
|
-
// 构建基础消息
|
|
15
|
-
let msg = `${timestamp} [${level}] ${message}`;
|
|
16
|
-
|
|
17
|
-
// 添加额外的元数据
|
|
18
|
-
if (splat && Array.isArray(splat) && splat.length > 0) {
|
|
19
|
-
const metaStr = splat
|
|
20
|
-
.map((item: any) => {
|
|
21
|
-
if (item instanceof Error) {
|
|
22
|
-
return item.stack || item.message;
|
|
23
|
-
}
|
|
24
|
-
if (typeof item === "object") {
|
|
25
|
-
return JSON.stringify(item, null, 2);
|
|
26
|
-
}
|
|
27
|
-
return String(item);
|
|
28
|
-
})
|
|
29
|
-
.join("\n");
|
|
30
|
-
msg += `\n${metaStr}`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// 添加其他元数据
|
|
34
|
-
const metaKeys = Object.keys(meta).filter(
|
|
35
|
-
(key) => key !== Symbol.for("splat").toString()
|
|
36
|
-
);
|
|
37
|
-
if (metaKeys.length > 0) {
|
|
38
|
-
const metaObj: Record<string, any> = {};
|
|
39
|
-
for (const key of metaKeys) {
|
|
40
|
-
metaObj[key] = meta[key];
|
|
41
|
-
}
|
|
42
|
-
msg += `\n${JSON.stringify(metaObj, null, 2)}`;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (TestHook) {
|
|
46
|
-
TestHook(msg);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return msg;
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
const logger = winston.createLogger({
|
|
53
|
-
level: process.env.LOG_LEVEL || "info",
|
|
54
|
-
transports: [
|
|
55
|
-
new winston.transports.Console({
|
|
56
|
-
format: format.combine(
|
|
57
|
-
format.colorize({ all: true }),
|
|
58
|
-
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss.SSS" }),
|
|
59
|
-
customFormat
|
|
60
|
-
),
|
|
61
|
-
}),
|
|
62
|
-
],
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
export default logger;
|
package/src/core/testing.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 测试辅助模块
|
|
3
|
-
* 提供常用的测试工具函数和类型,简化测试代码编写
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { Factory } from "./factory";
|
|
7
|
-
import { MicroserviceOptions, Plugin } from "./types";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 默认测试配置
|
|
11
|
-
*/
|
|
12
|
-
export const DEFAULT_TEST_OPTIONS = {
|
|
13
|
-
name: "test-service",
|
|
14
|
-
version: "1.0.0",
|
|
15
|
-
} as const;
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 创建测试引擎
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* // 只传插件
|
|
23
|
-
* const { engine, Module } = Testing.createTestEngine({
|
|
24
|
-
* plugins: [new CachePlugin()]
|
|
25
|
-
* });
|
|
26
|
-
*
|
|
27
|
-
* // 传插件和 options
|
|
28
|
-
* const { engine, Module } = Testing.createTestEngine({
|
|
29
|
-
* plugins: [new CachePlugin()],
|
|
30
|
-
* options: { prefix: "/api" }
|
|
31
|
-
* });
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
function createTestEngine<TPlugins extends readonly Plugin<any>[]>(config: {
|
|
35
|
-
plugins: TPlugins;
|
|
36
|
-
options?: Partial<MicroserviceOptions>;
|
|
37
|
-
}): ReturnType<typeof Factory.create<TPlugins>> & {
|
|
38
|
-
engine: InstanceType<
|
|
39
|
-
ReturnType<typeof Factory.create<TPlugins>>["Microservice"]
|
|
40
|
-
>;
|
|
41
|
-
} {
|
|
42
|
-
const factory = Factory.create(...config.plugins);
|
|
43
|
-
const Microservice = factory.Microservice;
|
|
44
|
-
const engine = new Microservice({
|
|
45
|
-
...DEFAULT_TEST_OPTIONS,
|
|
46
|
-
...config.options,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
...factory,
|
|
51
|
-
engine,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 等待指定时间(用于异步测试)
|
|
57
|
-
*
|
|
58
|
-
* @param ms 等待的毫秒数
|
|
59
|
-
* @returns Promise
|
|
60
|
-
*/
|
|
61
|
-
function wait(ms: number): Promise<void> {
|
|
62
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 测试辅助工具命名空间
|
|
67
|
-
* 所有测试相关的工具函数都通过这个命名空间导出,避免命名冲突
|
|
68
|
-
*/
|
|
69
|
-
export const Testing = {
|
|
70
|
-
createTestEngine,
|
|
71
|
-
DEFAULT_TEST_OPTIONS,
|
|
72
|
-
wait,
|
|
73
|
-
} as const;
|
package/src/core/types.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 核心类型定义
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { Microservice } from "./engine";
|
|
6
|
-
|
|
7
|
-
// 模块类类型
|
|
8
|
-
export type Class = new (...args: any[]) => any;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 插件Module配置Schema(运行时+类型双维度)
|
|
12
|
-
*/
|
|
13
|
-
export interface PluginModuleOptionsSchema<T = Record<string, any>> {
|
|
14
|
-
// 类型标记:仅用于TypeScript推导,运行时无值
|
|
15
|
-
_type: T;
|
|
16
|
-
// 运行时校验规则(可选)
|
|
17
|
-
validate?: (options: T) => boolean | string;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 插件优先级
|
|
22
|
-
* 数值越小,优先级越高(越先执行)
|
|
23
|
-
* 引擎会自动按优先级排序,用户无需关心注册顺序
|
|
24
|
-
*/
|
|
25
|
-
export enum PluginPriority {
|
|
26
|
-
/**
|
|
27
|
-
* 系统级优先级:系统核心功能插件(优雅停机等)
|
|
28
|
-
* 最高优先级,应该最先执行
|
|
29
|
-
*/
|
|
30
|
-
SYSTEM = 50,
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 最高优先级:安全相关插件(限流、认证等)
|
|
34
|
-
* 应该最先执行,快速拒绝无效请求
|
|
35
|
-
*/
|
|
36
|
-
SECURITY = 100,
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 高优先级:日志、监控等插件
|
|
40
|
-
* 记录所有请求,包括被安全插件拒绝的
|
|
41
|
-
*/
|
|
42
|
-
LOGGING = 200,
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 中优先级:业务逻辑插件(数据转换等)
|
|
46
|
-
* 在安全和日志之后执行
|
|
47
|
-
*/
|
|
48
|
-
BUSINESS = 300,
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 低优先级:性能优化插件(缓存等)
|
|
52
|
-
* 在业务逻辑之后执行,避免重复计算
|
|
53
|
-
*/
|
|
54
|
-
PERFORMANCE = 400,
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 最低优先级:路由插件
|
|
58
|
-
* 必须最后执行,注册HTTP路由
|
|
59
|
-
*/
|
|
60
|
-
ROUTE = 1000,
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 核心插件接口
|
|
65
|
-
*/
|
|
66
|
-
export interface Plugin<TModuleOptions = Record<string, any>> {
|
|
67
|
-
// 插件唯一名称
|
|
68
|
-
name: string;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 插件优先级(可选)
|
|
72
|
-
* 如果不指定,默认为 PluginPriority.BUSINESS
|
|
73
|
-
* 引擎会自动按优先级排序,用户无需关心注册顺序
|
|
74
|
-
*
|
|
75
|
-
* 优先级规则:
|
|
76
|
-
* - 数值越小,优先级越高(越先执行)
|
|
77
|
-
* - 相同优先级按注册顺序执行
|
|
78
|
-
*/
|
|
79
|
-
priority?: PluginPriority | number;
|
|
80
|
-
|
|
81
|
-
// 声明插件的Module配置Schema(核心:用于类型推导+运行时校验)
|
|
82
|
-
getModuleOptionsSchema?: () => PluginModuleOptionsSchema<TModuleOptions>;
|
|
83
|
-
// 生命周期钩子
|
|
84
|
-
onInit?: (engine: Microservice) => void;
|
|
85
|
-
onModuleLoad?: (modules: ModuleMetadata[]) => void;
|
|
86
|
-
// Handler加载钩子:平铺的HandlerMetadata数组,每个装饰器都是独立条目
|
|
87
|
-
// 引擎会自动按优先级排序,用户无需关心注册顺序
|
|
88
|
-
onHandlerLoad?: (handlers: HandlerMetadata[]) => void;
|
|
89
|
-
onBeforeStart?: (engine: Microservice) => void;
|
|
90
|
-
onAfterStart?: (engine: Microservice) => void | Promise<void>;
|
|
91
|
-
onDestroy?: () => void | Promise<void>;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 引擎创建选项
|
|
96
|
-
*/
|
|
97
|
-
export interface MicroserviceOptions {
|
|
98
|
-
name: string; // 服务名称
|
|
99
|
-
version: string; // 语义化版本
|
|
100
|
-
hostname?: string; // 绑定主机名(默认0.0.0.0)
|
|
101
|
-
prefix?: string; // 路由前缀(默认"",用于Action插件的路由注册)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Module装饰器类型(由引擎实例创建,自动推导类型)
|
|
106
|
-
* 使用最新的 Stage 3 装饰器标准
|
|
107
|
-
*/
|
|
108
|
-
export type ModuleDecorator<TOptions = Record<string, any>> = (
|
|
109
|
-
name: string,
|
|
110
|
-
options?: TOptions
|
|
111
|
-
) => (target: Class, context: ClassDecoratorContext) => void;
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* 模块元数据
|
|
115
|
-
*/
|
|
116
|
-
export interface ModuleMetadata<TOptions = Record<string, any>> {
|
|
117
|
-
name: string;
|
|
118
|
-
clazz: Class;
|
|
119
|
-
options: TOptions;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Handler包装函数类型
|
|
124
|
-
* 插件只需要提供这个函数,引擎自动管理包装链
|
|
125
|
-
*
|
|
126
|
-
* @param next 调用下一个包装层或原始方法
|
|
127
|
-
* @param instance 模块实例
|
|
128
|
-
* @param args 方法参数
|
|
129
|
-
* @returns 方法执行结果
|
|
130
|
-
*/
|
|
131
|
-
export type HandlerWrapper = (
|
|
132
|
-
next: () => Promise<any> | any,
|
|
133
|
-
instance: any,
|
|
134
|
-
...args: any[]
|
|
135
|
-
) => Promise<any> | any;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Handler元数据(单组元数据)
|
|
139
|
-
* 提供简单的 wrap API,引擎自动管理包装链和执行顺序
|
|
140
|
-
*/
|
|
141
|
-
export interface HandlerMetadata {
|
|
142
|
-
type: string; // 元数据类型(如route/cache/rate-limit)
|
|
143
|
-
options: Record<string, any>; // 该类型对应的自定义配置
|
|
144
|
-
method: Function; // 关联的模块方法(原始方法,仅供内部使用)
|
|
145
|
-
methodName: string; // 方法名称
|
|
146
|
-
module: Class; // 所属模块类
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* 包装当前方法(引擎自动管理包装链)
|
|
150
|
-
*
|
|
151
|
-
* 插件只需要调用这个方法,引擎会自动:
|
|
152
|
-
* - 按插件优先级构建包装链
|
|
153
|
-
* - 确保 RoutePlugin 最后执行
|
|
154
|
-
* - 自动应用包装到原型
|
|
155
|
-
*
|
|
156
|
-
* @param wrapper 包装函数
|
|
157
|
-
* - next: 调用下一个包装层或原始方法
|
|
158
|
-
* - instance: 模块实例
|
|
159
|
-
* - args: 方法参数
|
|
160
|
-
*
|
|
161
|
-
* @example
|
|
162
|
-
* ```typescript
|
|
163
|
-
* handler.wrap(async (next, instance, ...args) => {
|
|
164
|
-
* // 前置逻辑
|
|
165
|
-
* const result = await next();
|
|
166
|
-
* // 后置逻辑
|
|
167
|
-
* return result;
|
|
168
|
-
* });
|
|
169
|
-
* ```
|
|
170
|
-
*/
|
|
171
|
-
wrap(wrapper: HandlerWrapper): void;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Stage 3 装饰器上下文类型
|
|
176
|
-
*/
|
|
177
|
-
export type ClassDecoratorContext = {
|
|
178
|
-
kind: "class";
|
|
179
|
-
name: string | undefined;
|
|
180
|
-
addInitializer(initializer: () => void): void;
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
export type ClassMethodDecoratorContext = {
|
|
184
|
-
kind: "method";
|
|
185
|
-
name: string | symbol;
|
|
186
|
-
static: boolean;
|
|
187
|
-
private: boolean;
|
|
188
|
-
addInitializer(initializer: () => void): void;
|
|
189
|
-
};
|
|
190
|
-
export { Microservice };
|
|
191
|
-
|
package/src/index.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IMean Service Engine - 微服务引擎框架
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
// 核心类型
|
|
6
|
-
export * from "./core/types";
|
|
7
|
-
|
|
8
|
-
// 导出插件优先级枚举,方便插件开发者使用
|
|
9
|
-
export { PluginPriority } from "./core/types";
|
|
10
|
-
|
|
11
|
-
// 装饰器
|
|
12
|
-
export { Handler } from "./core/decorators";
|
|
13
|
-
|
|
14
|
-
// 异常
|
|
15
|
-
export * from "./core/errors";
|
|
16
|
-
|
|
17
|
-
// 预检
|
|
18
|
-
export * from "./core/checker";
|
|
19
|
-
|
|
20
|
-
// Route插件
|
|
21
|
-
export * from "./plugins/route";
|
|
22
|
-
|
|
23
|
-
// Action插件
|
|
24
|
-
export * from "./plugins/action";
|
|
25
|
-
|
|
26
|
-
// Cache插件
|
|
27
|
-
export * from "./plugins/cache";
|
|
28
|
-
|
|
29
|
-
// GracefulShutdown插件
|
|
30
|
-
export * from "./plugins/graceful-shutdown";
|
|
31
|
-
|
|
32
|
-
// Schedule插件
|
|
33
|
-
export * from "./plugins/schedule";
|
|
34
|
-
|
|
35
|
-
// clientCode插件
|
|
36
|
-
export * from "./plugins/client-code";
|
|
37
|
-
|
|
38
|
-
// 便捷导出:引擎创建函数(仅通过 Factory 创建)
|
|
39
|
-
export { Factory } from "./core/factory";
|
|
40
|
-
export type { MicroserviceOptions } from "./core/types";
|
|
41
|
-
|
|
42
|
-
// 测试辅助工具(仅在测试环境中使用)
|
|
43
|
-
export * from "./core/testing";
|
|
44
|
-
|
|
45
|
-
// 导出 zod(固定版本,用户可以直接从主包导入)
|
|
46
|
-
export { z } from "zod";
|
|
47
|
-
|
|
48
|
-
// 导出 hono
|
|
49
|
-
export { Context, MiddlewareHandler } from "hono";
|