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.
Files changed (72) hide show
  1. package/dist/index.d.mts +77 -52
  2. package/dist/index.d.ts +77 -52
  3. package/dist/index.js +2078 -1945
  4. package/dist/index.mjs +2076 -1944
  5. package/package.json +9 -2
  6. package/.vscode/settings.json +0 -8
  7. package/src/core/checker.ts +0 -33
  8. package/src/core/decorators.test.ts +0 -96
  9. package/src/core/decorators.ts +0 -68
  10. package/src/core/engine.test.ts +0 -218
  11. package/src/core/engine.ts +0 -635
  12. package/src/core/errors.ts +0 -28
  13. package/src/core/factory.test.ts +0 -73
  14. package/src/core/factory.ts +0 -92
  15. package/src/core/logger.ts +0 -65
  16. package/src/core/testing.ts +0 -73
  17. package/src/core/types.ts +0 -191
  18. package/src/index.ts +0 -49
  19. package/src/metadata/README.md +0 -422
  20. package/src/metadata/metadata.test.ts +0 -369
  21. package/src/metadata/metadata.ts +0 -512
  22. package/src/plugins/action/action-plugin.test.ts +0 -660
  23. package/src/plugins/action/decorator.ts +0 -14
  24. package/src/plugins/action/index.ts +0 -4
  25. package/src/plugins/action/plugin.ts +0 -349
  26. package/src/plugins/action/types.ts +0 -49
  27. package/src/plugins/action/utils.test.ts +0 -196
  28. package/src/plugins/action/utils.ts +0 -111
  29. package/src/plugins/cache/adapter.test.ts +0 -689
  30. package/src/plugins/cache/adapter.ts +0 -324
  31. package/src/plugins/cache/cache-plugin.test.ts +0 -269
  32. package/src/plugins/cache/decorator.ts +0 -26
  33. package/src/plugins/cache/index.ts +0 -20
  34. package/src/plugins/cache/plugin.ts +0 -299
  35. package/src/plugins/cache/types.ts +0 -69
  36. package/src/plugins/client-code/client-code-plugin.test.ts +0 -511
  37. package/src/plugins/client-code/format.ts +0 -9
  38. package/src/plugins/client-code/generator.test.ts +0 -52
  39. package/src/plugins/client-code/generator.ts +0 -263
  40. package/src/plugins/client-code/index.ts +0 -15
  41. package/src/plugins/client-code/plugin.ts +0 -158
  42. package/src/plugins/client-code/types.ts +0 -52
  43. package/src/plugins/client-code/utils.ts +0 -164
  44. package/src/plugins/graceful-shutdown/graceful-shutdown-plugin.test.ts +0 -401
  45. package/src/plugins/graceful-shutdown/index.ts +0 -3
  46. package/src/plugins/graceful-shutdown/plugin.ts +0 -279
  47. package/src/plugins/graceful-shutdown/types.ts +0 -17
  48. package/src/plugins/rate-limit/rate-limit-plugin.example.ts +0 -171
  49. package/src/plugins/route/components/Layout.tsx +0 -42
  50. package/src/plugins/route/components/ServiceStatusPage.tsx +0 -141
  51. package/src/plugins/route/decorator.ts +0 -50
  52. package/src/plugins/route/index.ts +0 -16
  53. package/src/plugins/route/plugin.ts +0 -218
  54. package/src/plugins/route/route-plugin.test.ts +0 -759
  55. package/src/plugins/route/types.ts +0 -72
  56. package/src/plugins/schedule/README.md +0 -309
  57. package/src/plugins/schedule/decorator.ts +0 -25
  58. package/src/plugins/schedule/index.ts +0 -12
  59. package/src/plugins/schedule/mock-etcd.ts +0 -145
  60. package/src/plugins/schedule/plugin.ts +0 -164
  61. package/src/plugins/schedule/schedule-plugin.test.ts +0 -312
  62. package/src/plugins/schedule/scheduler.ts +0 -164
  63. package/src/plugins/schedule/types.ts +0 -94
  64. package/src/plugins/schedule/utils.test.ts +0 -163
  65. package/src/plugins/schedule/utils.ts +0 -41
  66. package/tests/integration/client.test.ts +0 -203
  67. package/tests/integration/dev-service.ts +0 -301
  68. package/tests/integration/generated/client.ts +0 -123
  69. package/tests/integration/start-service.ts +0 -21
  70. package/tsconfig.json +0 -27
  71. package/tsup.config.ts +0 -16
  72. package/vitest.config.ts +0 -19
@@ -1,301 +0,0 @@
1
- /**
2
- * 集成测试用的开发微服务
3
- * 包含所有测试用例需要的 Action handlers
4
- */
5
-
6
- import { Context } from "hono";
7
- import { z } from "zod";
8
- import { Factory } from "../../src/core/factory";
9
- import { Action, ActionPlugin } from "../../src/plugins/action";
10
- import { Cache, CachePlugin } from "../../src/plugins/cache";
11
- import { ClientCodePlugin } from "../../src/plugins/client-code";
12
-
13
- // 使用 Factory 创建类型化的引擎
14
- const { Module, Microservice } = Factory.create(
15
- new ActionPlugin(),
16
- new CachePlugin(),
17
- new ClientCodePlugin({
18
- clientSavePath: "./tests/integration/generated/client.ts",
19
- })
20
- );
21
-
22
- const engine = new Microservice({
23
- name: "integration-test-service",
24
- version: "1.0.0",
25
- prefix: "/api",
26
- });
27
-
28
- // 模拟用户数据
29
- const users = new Map<string, { id: string; name: string; age: number }>([
30
- ["1", { id: "1", name: "张三", age: 25 }],
31
- ["2", { id: "2", name: "李四", age: 30 }],
32
- ]);
33
-
34
- // 重试计数器(需要在每个测试前重置)
35
- // 使用 Map 来跟踪每个操作的失败次数,避免不同测试之间的干扰
36
- export const failureCounter = new Map<string, number>();
37
- let alwaysFailCount = 0;
38
-
39
- // 重置计数器的函数
40
- export function resetCounters() {
41
- failureCounter.clear();
42
- alwaysFailCount = 0;
43
- }
44
-
45
- @Module("tests")
46
- class TestService {
47
- /**
48
- * 获取用户
49
- */
50
- @Action({
51
- description: "获取用户",
52
- params: [z.string()],
53
- returns: z.object({
54
- id: z.string(),
55
- name: z.string(),
56
- age: z.number(),
57
- }),
58
- })
59
- getUser(id: string) {
60
- const user = users.get(id);
61
- if (!user) {
62
- throw new Error("用户不存在");
63
- }
64
- return user;
65
- }
66
-
67
- /**
68
- * 创建用户
69
- */
70
- @Action({
71
- description: "创建用户",
72
- params: [z.string(), z.number()],
73
- returns: z.object({
74
- id: z.string(),
75
- name: z.string(),
76
- age: z.number(),
77
- }),
78
- })
79
- createUser(name: string, age: number) {
80
- const id = (users.size + 1).toString();
81
- const user = { id, name, age };
82
- users.set(id, user);
83
- return user;
84
- }
85
-
86
- /**
87
- * 更新用户(幂等操作)
88
- */
89
- @Action({
90
- description: "更新用户",
91
- params: [z.string(), z.string(), z.number()],
92
- returns: z.object({
93
- id: z.string(),
94
- name: z.string(),
95
- age: z.number(),
96
- }),
97
- idempotence: true,
98
- })
99
- updateUser(id: string, name: string, age: number) {
100
- const user = { id, name, age };
101
- users.set(id, user);
102
- return user;
103
- }
104
-
105
- /**
106
- * 可重试操作(前两次失败,第三次成功)
107
- */
108
- @Action({
109
- description: "可重试操作",
110
- params: [z.string()],
111
- returns: z.object({ id: z.string() }),
112
- idempotence: true, // 标记为幂等操作,支持重试
113
- })
114
- retryableOperation(id: string) {
115
- // 获取当前操作的失败次数
116
- const failCount = failureCounter.get(id) || 0;
117
- // 如果失败次数小于2,增加失败次数并抛出错误
118
- if (failCount < 2) {
119
- failureCounter.set(id, failCount + 1);
120
- throw new Error("临时错误");
121
- }
122
- // 第三次尝试时成功,清除计数器
123
- failureCounter.delete(id);
124
- return { id };
125
- }
126
-
127
- /**
128
- * 总是失败的操作
129
- */
130
- @Action({
131
- description: "总是失败的操作",
132
- params: [z.string()],
133
- returns: z.object({ id: z.string() }),
134
- })
135
- alwaysFailOperation(id: string) {
136
- const failCount = failureCounter.get(id) || 0;
137
- failureCounter.set(id, failCount + 1);
138
- throw new Error("永久性错误");
139
- }
140
-
141
- /**
142
- * 上传文件
143
- */
144
- @Action({
145
- description: "上传文件",
146
- params: [z.instanceof(Uint8Array)],
147
- returns: z.instanceof(Uint8Array),
148
- })
149
- uploadFile(buffer: Uint8Array) {
150
- // 反转数组
151
- return new Uint8Array(buffer.reverse());
152
- }
153
-
154
- /**
155
- * 无参数无返回值
156
- */
157
- @Action({
158
- description: "无参数无返回值",
159
- params: [],
160
- returns: z.void(),
161
- })
162
- noReturnAction() {
163
- return undefined;
164
- }
165
-
166
- /**
167
- * 可选参数
168
- */
169
- @Action({
170
- description: "可选参数",
171
- params: [z.string(), z.string().nullable().optional()],
172
- returns: z.string(),
173
- })
174
- optionalParams(required: string, optional?: string | null) {
175
- return required;
176
- }
177
-
178
- /**
179
- * 缓存函数参数
180
- */
181
- @Action({
182
- description: "缓存函数参数",
183
- params: [z.string(), z.string()],
184
- returns: z.string(),
185
- })
186
- @Cache({ key: (key: string) => ({ cacheKey: `cache-fn:${key}` }), ttl: 5000 })
187
- async cacheFn(key: string, value: string) {
188
- // 模拟慢操作
189
- await new Promise((resolve) => setTimeout(resolve, 1000));
190
- return key;
191
- }
192
-
193
- /**
194
- * 缓存结果
195
- */
196
- @Action({
197
- description: "缓存结果",
198
- params: [z.string()],
199
- returns: z.string(),
200
- })
201
- @Cache({ key: (key: string) => ({ cacheKey: `cache-result:${key}` }), ttl: 1000 })
202
- async cacheResultAction(key: string) {
203
- // 模拟慢操作
204
- await new Promise((resolve) => setTimeout(resolve, 1000));
205
- return key;
206
- }
207
-
208
- /**
209
- * 默认参数
210
- */
211
- @Action({
212
- description: "默认参数",
213
- params: [z.string().default("test"), z.number().default(1)],
214
- returns: z.string(),
215
- })
216
- defaultParamAction(param1?: string, param2?: number) {
217
- return `${param1 || "test"}-${param2 || 1}`;
218
- }
219
-
220
- /**
221
- * 默认返回值
222
- */
223
- @Action({
224
- description: "默认返回值",
225
- params: [],
226
- returns: z.object({ a: z.string() }).default({ a: "test" }),
227
- })
228
- defaultReturnAction() {
229
- return { a: "test" };
230
- }
231
-
232
- /**
233
- * 流式返回
234
- */
235
- @Action({
236
- description: "流式返回数字",
237
- params: [z.number()],
238
- returns: z.number(),
239
- stream: true,
240
- })
241
- async *streamNumbers(count: number) {
242
- for (let i = 0; i < count; i++) {
243
- yield i;
244
- }
245
- }
246
-
247
- /**
248
- * 返回 unknown 类型
249
- */
250
- @Action({
251
- description: "返回 unknown 类型",
252
- params: [z.instanceof(RegExp)],
253
- returns: z.unknown(),
254
- })
255
- unknownReturnAction(regex: RegExp) {
256
- return regex;
257
- }
258
-
259
- /**
260
- * 返回 Record 类型
261
- */
262
- @Action({
263
- description: "返回 Record 类型",
264
- params: [
265
- z.array(
266
- z.object({
267
- cells: z.record(
268
- z.string(),
269
- z.object({ value: z.string() })
270
- ),
271
- })
272
- ),
273
- ],
274
- returns: z.array(
275
- z.object({
276
- cells: z.record(z.string(), z.object({ value: z.string() })),
277
- })
278
- ),
279
- })
280
- recordReturnAction(
281
- data: Array<{ cells: Record<string, { value: string }> }>
282
- ) {
283
- return data;
284
- }
285
-
286
- /**
287
- * 请求上下文注入
288
- */
289
- @Action({
290
- description: "请求上下文注入",
291
- params: [],
292
- returns: z.string(),
293
- })
294
- requestContextAction(ctx: Context) {
295
- return ctx.req.path;
296
- }
297
- }
298
-
299
- // 导出引擎以便测试使用
300
- export { engine };
301
-
@@ -1,123 +0,0 @@
1
- // 这个文件是自动生成的,请不要手动修改
2
-
3
- import { MicroserviceClient as BaseMicroserviceClient } from "imean-service-client";
4
- export * from "imean-service-client";
5
-
6
- export interface TestsModule {
7
- /**
8
- * 获取用户
9
- */
10
- getUser: (id: string) => Promise<{ id: string; name: string; age: number }>;
11
-
12
- /**
13
- * 创建用户
14
- */
15
- createUser: (
16
- name: string,
17
- age: number,
18
- ) => Promise<{ id: string; name: string; age: number }>;
19
-
20
- /**
21
- * 更新用户
22
- */
23
- updateUser: (
24
- id: string,
25
- name: string,
26
- age: number,
27
- ) => Promise<{ id: string; name: string; age: number }>;
28
-
29
- /**
30
- * 可重试操作
31
- */
32
- retryableOperation: (id: string) => Promise<{ id: string }>;
33
-
34
- /**
35
- * 总是失败的操作
36
- */
37
- alwaysFailOperation: (id: string) => Promise<{ id: string }>;
38
-
39
- /**
40
- * 上传文件
41
- */
42
- uploadFile: (buffer: Uint8Array) => Promise<Uint8Array>;
43
-
44
- /**
45
- * 无参数无返回值
46
- */
47
- noReturnAction: () => Promise<void>;
48
-
49
- /**
50
- * 可选参数
51
- */
52
- optionalParams: (
53
- required: string,
54
- optional?: string | null,
55
- ) => Promise<string>;
56
-
57
- /**
58
- * 缓存函数参数
59
- */
60
- cacheFn: (key: string, value: string) => Promise<string>;
61
-
62
- /**
63
- * 缓存结果
64
- */
65
- cacheResultAction: (key: string) => Promise<string>;
66
-
67
- /**
68
- * 默认参数
69
- */
70
- defaultParamAction: (param1?: string, param2?: number) => Promise<string>;
71
-
72
- /**
73
- * 默认返回值
74
- */
75
- defaultReturnAction: () => Promise<{ a: string }>;
76
-
77
- /**
78
- * 流式返回数字
79
- */
80
- streamNumbers: (count: number) => Promise<AsyncIterable<number>>;
81
-
82
- /**
83
- * 返回 unknown 类型
84
- */
85
- unknownReturnAction: (regex: unknown) => Promise<unknown>;
86
-
87
- /**
88
- * 返回 Record 类型
89
- */
90
- recordReturnAction: (
91
- data: { cells: Record<string, { value: string }> }[],
92
- ) => Promise<{ cells: Record<string, { value: string }> }[]>;
93
-
94
- /**
95
- * 请求上下文注入
96
- */
97
- requestContextAction: () => Promise<string>;
98
- }
99
-
100
- export class MicroserviceClient extends BaseMicroserviceClient {
101
- constructor(options: any) {
102
- super(options);
103
- }
104
-
105
- public readonly tests = this.registerModule<TestsModule>("tests", {
106
- getUser: { idempotent: false, stream: false },
107
- createUser: { idempotent: false, stream: false },
108
- updateUser: { idempotent: true, stream: false },
109
- retryableOperation: { idempotent: true, stream: false },
110
- alwaysFailOperation: { idempotent: false, stream: false },
111
- uploadFile: { idempotent: false, stream: false },
112
- noReturnAction: { idempotent: false, stream: false },
113
- optionalParams: { idempotent: false, stream: false },
114
- cacheFn: { idempotent: false, stream: false },
115
- cacheResultAction: { idempotent: false, stream: false },
116
- defaultParamAction: { idempotent: false, stream: false },
117
- defaultReturnAction: { idempotent: false, stream: false },
118
- streamNumbers: { idempotent: false, stream: true },
119
- unknownReturnAction: { idempotent: false, stream: false },
120
- recordReturnAction: { idempotent: false, stream: false },
121
- requestContextAction: { idempotent: false, stream: false },
122
- });
123
- }
@@ -1,21 +0,0 @@
1
- /**
2
- * 启动集成测试服务的脚本
3
- * 用于开发阶段手动启动服务并生成客户端代码
4
- */
5
-
6
- import { engine } from "./dev-service";
7
-
8
- engine.start().then(() => {
9
- const port = engine.getPort();
10
- console.log(`\n✅ Integration test service started on port ${port}`);
11
- console.log(`📥 Client code available at http://localhost:${port}/api/client.ts`);
12
- console.log(`📁 Client code saved to: ./tests/integration/generated/client.ts\n`);
13
-
14
- // 保持进程运行
15
- process.on("SIGINT", async () => {
16
- console.log("\n🛑 Shutting down service...");
17
- await engine.stop();
18
- process.exit(0);
19
- });
20
- });
21
-
package/tsconfig.json DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2022",
4
- "module": "commonjs",
5
- "lib": ["ES2022"],
6
- "types": ["node"],
7
- "moduleDetection": "force",
8
- "outDir": "./dist",
9
- "rootDir": "./src",
10
- "jsx": "react-jsx",
11
- "jsxImportSource": "hono/jsx",
12
- "strict": true,
13
- "esModuleInterop": true,
14
- "skipLibCheck": true,
15
- "forceConsistentCasingInFileNames": true,
16
- "declaration": true,
17
- "declarationMap": true,
18
- "sourceMap": true,
19
- "experimentalDecorators": false,
20
- "emitDecoratorMetadata": false,
21
- "resolveJsonModule": true,
22
- "moduleResolution": "node"
23
- },
24
- "include": ["src/**/*"],
25
- "exclude": ["node_modules", "dist"]
26
- }
27
-
package/tsup.config.ts DELETED
@@ -1,16 +0,0 @@
1
- import { defineConfig } from "tsup";
2
-
3
- export default defineConfig({
4
- entry: ["src/index.ts"],
5
- format: ["cjs", "esm"],
6
- dts: true,
7
- clean: true,
8
- sourcemap: false,
9
- minify: false,
10
- splitting: false,
11
- treeshake: true,
12
- external: [],
13
- esbuildOptions(options) {
14
- options.conditions = ["browser", "worker"];
15
- },
16
- });
package/vitest.config.ts DELETED
@@ -1,19 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- globals: true,
6
- environment: 'node',
7
- include: ['src/**/*.test.ts', 'tests/**/*.test.ts'],
8
- coverage: {
9
- provider: 'v8',
10
- reporter: ['text', 'json', 'html'],
11
- exclude: [
12
- 'node_modules/',
13
- 'dist/',
14
- '**/*.test.ts',
15
- '**/*.config.ts'
16
- ]
17
- }
18
- }
19
- });