typespeed 2.5.0 → 2.5.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 (96) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.en.md +169 -0
  3. package/README.md +123 -37
  4. package/dist/default/rabbitmq.class.js +18 -1
  5. package/dist/default/redis.class.js +14 -1
  6. package/dist/default/socket-io.class.js +53 -4
  7. package/dist/typespeed.d.ts +7 -7
  8. package/package.json +7 -2
  9. package/.github/workflows/test.yml +0 -50
  10. package/app/nodemon.json +0 -5
  11. package/app/package.json +0 -17
  12. package/app/src/aop-test.class.ts +0 -29
  13. package/app/src/config-development.json +0 -26
  14. package/app/src/config-production.json +0 -15
  15. package/app/src/config.json +0 -50
  16. package/app/src/custom-log.class.ts +0 -33
  17. package/app/src/entities/mutil-users.class.ts +0 -5
  18. package/app/src/entities/user-dto.class.ts +0 -3
  19. package/app/src/first-page.class.ts +0 -46
  20. package/app/src/jwt-authentication.class.ts +0 -30
  21. package/app/src/main.ts +0 -24
  22. package/app/src/second-page.class.ts +0 -43
  23. package/app/src/test-bind.class.ts +0 -41
  24. package/app/src/test-database.class.ts +0 -81
  25. package/app/src/test-log.class.ts +0 -11
  26. package/app/src/test-mq.class.ts +0 -21
  27. package/app/src/test-orm.class.ts +0 -63
  28. package/app/src/test-redis.class.ts +0 -45
  29. package/app/src/test-request.class.ts +0 -37
  30. package/app/src/test-socket.class.ts +0 -56
  31. package/app/src/user-model.class.ts +0 -50
  32. package/app/src/views/index.html +0 -1
  33. package/app/src/views/socket.html +0 -23
  34. package/app/src/views/upload.html +0 -14
  35. package/app/static/favicon.ico +0 -0
  36. package/app/static/k.jpg +0 -0
  37. package/app/tsconfig.json +0 -20
  38. package/introduction/decorator/example-import.class.ts +0 -6
  39. package/introduction/decorator/main-app.ts +0 -6
  40. package/introduction/decorator/main-decorator.ts +0 -108
  41. package/introduction/decorator/main-import.ts +0 -1
  42. package/introduction/decorator/main-reflect.ts +0 -25
  43. package/introduction/decorator-next/dist/main.js +0 -116
  44. package/introduction/decorator-next/package.json +0 -14
  45. package/introduction/decorator-next/src/main.ts +0 -64
  46. package/introduction/decorator-next/tsconfig.json +0 -17
  47. package/introduction/decorator-next//346/225/231/347/250/213.md +0 -384
  48. package/introduction/tsconfig.json +0 -23
  49. package/introduction/web-services/db-init.ts +0 -39
  50. package/introduction/web-services/db-singleton.ts +0 -59
  51. package/introduction/web-services/http-if.ts +0 -10
  52. package/introduction/web-services/http-init.ts +0 -6
  53. package/introduction/web-services/http-router.ts +0 -28
  54. package/src/bind.decorator.ts +0 -50
  55. package/src/core.decorator.ts +0 -184
  56. package/src/database.decorator.ts +0 -446
  57. package/src/decorator-utils.ts +0 -53
  58. package/src/default/default-authentication.class.ts +0 -10
  59. package/src/default/express-server.class.ts +0 -150
  60. package/src/default/log-default.class.ts +0 -9
  61. package/src/default/node-cache.class.ts +0 -40
  62. package/src/default/pages/404.html +0 -226
  63. package/src/default/pages/500.html +0 -713
  64. package/src/default/rabbitmq.class.ts +0 -61
  65. package/src/default/read-write-db.class.ts +0 -53
  66. package/src/default/redis.class.ts +0 -79
  67. package/src/default/socket-io.class.ts +0 -81
  68. package/src/factory/authentication-factory.class.ts +0 -9
  69. package/src/factory/cache-factory.class.ts +0 -7
  70. package/src/factory/data-source-factory.class.ts +0 -4
  71. package/src/factory/log-factory.class.ts +0 -8
  72. package/src/factory/server-factory.class.ts +0 -6
  73. package/src/route.decorator.ts +0 -334
  74. package/src/scaffold/command.ts +0 -40
  75. package/src/scaffold/templates/front-page.class.ts.tpl +0 -12
  76. package/src/scaffold/templates/main.ts.tpl +0 -13
  77. package/src/scaffold/templates/nodemon.json.tpl +0 -7
  78. package/src/scaffold/templates/package.json.tpl +0 -16
  79. package/src/scaffold/templates/tsconfig.json.tpl +0 -17
  80. package/src/typespeed.d.ts +0 -403
  81. package/src/typespeed.ts +0 -139
  82. package/test/bind.test.ts +0 -28
  83. package/test/database.test.ts +0 -76
  84. package/test/decorator-utils.test.ts +0 -42
  85. package/test/first-page.test.ts +0 -42
  86. package/test/hooks.ts +0 -13
  87. package/test/mq.test.ts +0 -17
  88. package/test/orm.test.ts +0 -66
  89. package/test/redis.test.ts +0 -48
  90. package/test/request.test.ts +0 -51
  91. package/test/second-page.test.ts +0 -50
  92. package/test/socket.test.ts +0 -60
  93. package/test-env/docker-compose.test.yml +0 -31
  94. package/test-env/mysql-init/init.sql +0 -8
  95. package/test-env//346/234/254/345/234/260Docker/346/265/213/350/257/225/346/226/271/346/241/210.md +0 -69
  96. package/tsconfig.json +0 -25
@@ -1,56 +0,0 @@
1
- import { SocketIo, getMapping, component, io } from "../../src/typespeed";
2
-
3
- @component
4
- export default class TestSocket {
5
-
6
- static names = ["LiLei", "HanMeiMei"];
7
-
8
- static loginUsers: Map<string, string> = new Map<string, string>();
9
-
10
- @SocketIo.onConnected
11
- public connected(socket, next) {
12
- // 从 names 里面取出一个名字
13
- let name = TestSocket.names.pop();
14
- TestSocket.loginUsers.set(socket.id, name);
15
- //io.sockets.emit("all", "We have a new member: " + name);
16
- //console.log(socket.handshake);
17
- //console.log(socket.id);
18
- //next(new Error("test-error"));
19
- }
20
-
21
- @SocketIo.onDisconnect
22
- public disconnet(socket, reason) {
23
- io.sockets.emit("all", "We lost a member by: " + reason);
24
- }
25
-
26
- @SocketIo.onEvent("test-error")
27
- public testError(socket, message) {
28
- throw new Error("test-error");
29
- }
30
-
31
- @SocketIo.onError
32
- public error(socket, err) {
33
- io.sockets.emit("all", "We have a problem!");
34
- }
35
-
36
- @SocketIo.onEvent("say")
37
- public say(socket, message) {
38
- io.sockets.emit("all", TestSocket.loginUsers.get(socket.id) + " said: " + message);
39
- }
40
-
41
- @SocketIo.onEvent("join")
42
- public join(socket, message) {
43
- socket.join("private-room");
44
- io.to("private-room").emit("all", TestSocket.loginUsers.get(socket.id) + " joined private-room");
45
- }
46
-
47
- @SocketIo.onEvent("say-inroom")
48
- public sayInRoom(socket, message) {
49
- io.to("private-room").emit("all", TestSocket.loginUsers.get(socket.id) + " said in Room: " + message);
50
- }
51
-
52
- @getMapping("/socketIo")
53
- public socketIoPage(req, res) {
54
- res.render("socket");
55
- }
56
- }
@@ -1,50 +0,0 @@
1
- import { log, Model } from "../../src/typespeed";
2
- import UserDto from "./entities/user-dto.class";
3
-
4
- export default class UserModel extends Model {
5
-
6
-
7
- async getUsers() {
8
- const users = await this.findAll({
9
- id: { $lt: 10, $lte: 20 }, "name": { $like: "%a%" },
10
- $or: [{ id: 1 }, { id: 2 }]
11
- }, "id asc", "*", { page: 1, pageSize: 10 });
12
- log("users", users);
13
- return "getUsers";
14
- }
15
-
16
- async getUser(id: number) {
17
- const user = await this.find({ id: id }, "id asc", "*");
18
- log("user", user);
19
- return "getUser";
20
- }
21
-
22
- async newUsers() {
23
- const newId = this.random(1000, 2000);
24
- await this.create([
25
- new UserDto(newId, "UserDto " + newId),
26
- new UserDto(newId+1, "UserDto " + (newId+1)),
27
- { id: newId+2, name: "UserDto " + (newId+2) },
28
- ]);
29
- return newId;
30
- }
31
-
32
- async remove(id: number) {
33
- const result = await this.delete({ id: id });
34
- return "remove rows: " + result;
35
- }
36
-
37
- async count():Promise<number> {
38
- const result = await this.findCount("1");
39
- return result;
40
- }
41
-
42
- async editUser(id: number, name: string) {
43
- const effectRows = await this.update({ id: id }, { name: name });
44
- return effectRows;
45
- }
46
-
47
- private random(min, max) {
48
- return Math.round(Math.random() * (max - min)) + min;
49
- }
50
- }
@@ -1 +0,0 @@
1
- Hello {{ name }}!
@@ -1,23 +0,0 @@
1
- <html>
2
-
3
- <head>
4
- <title>Socket Test</title>
5
- <script src="https://cdn.socket.io/4.7.1/socket.io.min.js"></script>
6
- <script>
7
- var socket = io('http://localhost:8081');
8
- socket.on('connect', function() {
9
- console.log('connected');
10
- });
11
- socket.on('all', function(data) {
12
- console.log(data);
13
- });
14
- </script>
15
- </head>
16
- <body>
17
- <button onclick="socket.emit('say', 'I say some thing')">TestSay</button>
18
- <button onclick="socket.emit('join', 'I want to join the room')">Join</button>
19
- <button onclick="socket.emit('say-inroom', 'say some in room')">InRoom</button>
20
- <button onclick="socket.emit('test-error', 'this is a error')">Test Error</button>
21
- <button onclick="socket.close()">Close</button>
22
- </body>
23
- </html>
@@ -1,14 +0,0 @@
1
- <html>
2
-
3
- <head>
4
- </head>
5
-
6
- <body>
7
- <form action="/upload" enctype="multipart/form-data" method="post">
8
- <input type="text" name="title"><br>
9
- <input type="file" name="upload" multiple="multiple"><br>
10
- <input type="submit" value="Upload">
11
- </form>
12
- </body>
13
-
14
- </html>
Binary file
package/app/static/k.jpg DELETED
Binary file
package/app/tsconfig.json DELETED
@@ -1,20 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "module": "CommonJS",
4
- "declaration": true,
5
- "emitDecoratorMetadata": true,
6
- "experimentalDecorators": true,
7
- "allowSyntheticDefaultImports": true,
8
- "forceConsistentCasingInFileNames": true,
9
- "target": "ES2017",
10
- "sourceMap": true,
11
- "outDir": "./dist",
12
- "baseUrl": "./",
13
- "typeRoots": [
14
- "node_modules/@types"
15
- ],
16
- },
17
- "include": [
18
- "./src/**/*.ts"
19
- ]
20
- }
@@ -1,6 +0,0 @@
1
- @app
2
- export default class ExampleImport {}
3
-
4
- function app(target: any) {
5
- console.log("import 载入 ExampleImport 类的 @app 类装饰器。");
6
- }
@@ -1,6 +0,0 @@
1
- @app
2
- class MainApp {}
3
-
4
- function app(target: any) {
5
- console.log("执行 @app 类装饰器");
6
- }
@@ -1,108 +0,0 @@
1
- import "reflect-metadata";
2
-
3
- @atClassWithArgs(1, 2, 3)
4
- class SecondClass {}
5
-
6
- @atClass
7
- export default class FirstClass {
8
-
9
- @atPropertyWithArgs("Li", "Mei")
10
- private name: string;
11
-
12
- @atProperty
13
- private age: number;
14
-
15
- @atAccessor
16
- get newname(): string {
17
- return this.name;
18
- }
19
-
20
- @atMethod
21
- changeName(name: string): SecondClass {
22
- this.name = name;
23
- return new SecondClass();
24
- }
25
-
26
- @atMethodWithArgs("New", "Type")
27
- change(name: string, @atParameter age: number): void {
28
- this.age = age;
29
- this.name = name;
30
- }
31
-
32
- getName(): string {
33
- return this.name;
34
- }
35
- }
36
-
37
- const obj = new FirstClass();
38
-
39
- console.log("FirstClass对象调用getName()取得装饰器赋值:", obj.getName());
40
-
41
- // 类装饰器
42
- function atClass(target: any) {
43
- console.log("类装饰器,类名是:", target.name);
44
- }
45
-
46
- // 类装饰器,带参数
47
- function atClassWithArgs(...args: any[]) {
48
- return function (target: any) {
49
- console.log("类装饰器有参数,参数值:", args.join(","));
50
- }
51
- }
52
-
53
- // 方法装饰器
54
- function atMethod(target: any, propertyKey: string) {
55
- const returnType: any = Reflect.getMetadata("design:returntype", target, propertyKey);
56
- console.log("方法装饰器,获得返回类型:", returnType.name);
57
- }
58
-
59
- // 方法装饰器,带参数
60
- function atMethodWithArgs(...args: any[]) {
61
- return function (target: any, propertyKey: string) {
62
- console.log("方法装饰器有参数,参数值:", args.join(","));
63
- }
64
- }
65
-
66
- // 成员变量装饰器
67
- function atProperty(target: any, propertyKey: string) {
68
- const propertyType: any = Reflect.getMetadata("design:type", target, propertyKey);
69
- console.log("变量装饰器,获得变量类型:", propertyType.name);
70
- }
71
-
72
- // 成员变量装饰器,带参数
73
- function atPropertyWithArgs(...args: any[]) {
74
- return function (target: any, propertyKey: string) {
75
- console.log("变量装饰器有参数,参数值:", args.join(","));
76
- Object.defineProperty(target, propertyKey, {
77
- get: () => {
78
- return args;
79
- }
80
- });
81
- }
82
- }
83
-
84
- // 访问器装饰器
85
- function atAccessor(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
86
- const returnType: any = Reflect.getMetadata("design:type", target, propertyKey);
87
- console.log("访问器装饰器,访问器类型是:", returnType, ",值是:", target[propertyKey]);
88
- }
89
-
90
- // 访问器装饰器,带参数
91
- function atAccessorWithArgs(...args: any[]) {
92
- return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
93
- console.log("访问器装饰器,参数值:", args.join(","));
94
- }
95
- }
96
-
97
- // 参数装饰器
98
- function atParameter(target: any, propertyKey: string, parameterIndex: number) {
99
- const parameterType: any = Reflect.getMetadata("design:paramtypes", target, propertyKey);
100
- console.log("参数装饰器,参数位置在:", parameterIndex, "参数类型是:", parameterType[parameterIndex].name);
101
- }
102
-
103
- // 参数装饰器,带参数
104
- function atParameterWithArgs(...args: any[]) {
105
- return function (target: any, propertyKey: string, parameterIndex: number) {
106
- console.log("参数装饰器有参数,参数值:", args.join(","));
107
- }
108
- }
@@ -1 +0,0 @@
1
- import("./example-import.class");
@@ -1,25 +0,0 @@
1
- import "reflect-metadata";
2
-
3
- class MainReflect {
4
- @injectAge(10)
5
- private age: number;
6
-
7
- @findReturn
8
- getAge(): Number {
9
- return this.age;
10
- }
11
- }
12
-
13
- const mainReflect = new MainReflect();
14
- console.log("获得Age值:", mainReflect.getAge());
15
-
16
- function injectAge(arg: Number) {
17
- return function (target: any, propertyKey: string) {
18
- Object.defineProperty(target, propertyKey, {get: () => {return arg;}});
19
- }
20
- }
21
-
22
- function findReturn(target: any, propertyKey: string) {
23
- const returnType: any = Reflect.getMetadata("design:returntype", target, propertyKey);
24
- console.log(target[propertyKey].name, "的返回类型是:", returnType.name);
25
- }
@@ -1,116 +0,0 @@
1
- "use strict";
2
- var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
3
- var useValue = arguments.length > 2;
4
- for (var i = 0; i < initializers.length; i++) {
5
- value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
6
- }
7
- return useValue ? value : void 0;
8
- };
9
- var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
10
- function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
11
- var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
12
- var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
13
- var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
14
- var _, done = false;
15
- for (var i = decorators.length - 1; i >= 0; i--) {
16
- var context = {};
17
- for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
18
- for (var p in contextIn.access) context.access[p] = contextIn.access[p];
19
- context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
20
- var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
21
- if (kind === "accessor") {
22
- if (result === void 0) continue;
23
- if (result === null || typeof result !== "object") throw new TypeError("Object expected");
24
- if (_ = accept(result.get)) descriptor.get = _;
25
- if (_ = accept(result.set)) descriptor.set = _;
26
- if (_ = accept(result.init)) initializers.unshift(_);
27
- }
28
- else if (_ = accept(result)) {
29
- if (kind === "field") initializers.unshift(_);
30
- else descriptor[key] = _;
31
- }
32
- }
33
- if (target) Object.defineProperty(target, contextIn.name, descriptor);
34
- done = true;
35
- };
36
- var __importDefault = (this && this.__importDefault) || function (mod) {
37
- return (mod && mod.__esModule) ? mod : { "default": mod };
38
- };
39
- Object.defineProperty(exports, "__esModule", { value: true });
40
- // Symbol.metadata polyfill:Node 22/24/26 都没有 Symbol.metadata,
41
- // 标准装饰器的 context.metadata 依赖它,缺失会 TypeError,必须最先执行。
42
- Symbol.metadata ??= Symbol("Symbol.metadata");
43
- const express_1 = __importDefault(require("express"));
44
- const typespeed_1 = require("../../../dist/typespeed");
45
- class CacheBean {
46
- name = "cache-bean";
47
- }
48
- let TestService = (() => {
49
- let _classDecorators = [typespeed_1.component];
50
- let _classDescriptor;
51
- let _classExtraInitializers = [];
52
- let _classThis;
53
- let _instanceExtraInitializers = [];
54
- let _getCache_decorators;
55
- let _cache_decorators;
56
- let _cache_initializers = [];
57
- let _cache_extraInitializers = [];
58
- let _test_decorators;
59
- var TestService = class {
60
- static { _classThis = this; }
61
- static {
62
- const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
63
- _getCache_decorators = [(0, typespeed_1.bean)(CacheBean)];
64
- _cache_decorators = [(0, typespeed_1.autoware)(CacheBean)];
65
- _test_decorators = [(0, typespeed_1.getMapping)("/std/test/:id"), (0, typespeed_1.bind)({ id: "reqParam" })];
66
- __esDecorate(this, null, _getCache_decorators, { kind: "method", name: "getCache", static: false, private: false, access: { has: obj => "getCache" in obj, get: obj => obj.getCache }, metadata: _metadata }, null, _instanceExtraInitializers);
67
- __esDecorate(this, null, _test_decorators, { kind: "method", name: "test", static: false, private: false, access: { has: obj => "test" in obj, get: obj => obj.test }, metadata: _metadata }, null, _instanceExtraInitializers);
68
- __esDecorate(null, null, _cache_decorators, { kind: "field", name: "cache", static: false, private: false, access: { has: obj => "cache" in obj, get: obj => obj.cache, set: (obj, value) => { obj.cache = value; } }, metadata: _metadata }, _cache_initializers, _cache_extraInitializers);
69
- __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
70
- TestService = _classThis = _classDescriptor.value;
71
- if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
72
- __runInitializers(_classThis, _classExtraInitializers);
73
- }
74
- getCache() { return new CacheBean(); }
75
- cache = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _cache_initializers, void 0));
76
- // 标准模式:无参数装饰器,@bind 声明「参数名 → 来源」
77
- async test(id) {
78
- return { id, cache: this.cache.name };
79
- }
80
- constructor() {
81
- __runInitializers(this, _cache_extraInitializers);
82
- }
83
- };
84
- return TestService = _classThis;
85
- })();
86
- // 验证 1:@component / @autoware / @bean
87
- const svc = (0, typespeed_1.getComponent)(TestService);
88
- console.log("[component] registered:", svc !== undefined);
89
- console.log("[autoware(CacheBean)] injected:", svc && svc.cache?.name);
90
- console.log("[bean(CacheBean)] factory:", (0, typespeed_1.getBean)(CacheBean)?.name);
91
- // 验证 2:@getMapping + @bind 路由,走 setRouter 真实 HTTP 请求
92
- const app = (0, express_1.default)();
93
- (0, typespeed_1.setRouter)(app);
94
- const server = app.listen(0, () => {
95
- const addr = server.address();
96
- const port = addr.port;
97
- httpGet(`http://127.0.0.1:${port}/std/test/123`, (body) => {
98
- console.log("[route @bind] response:", body);
99
- const parsed = JSON.parse(body);
100
- const ok = svc !== undefined
101
- && svc.cache?.name === "cache-bean"
102
- && (0, typespeed_1.getBean)(CacheBean)?.name === "cache-bean"
103
- && parsed.id === "123"
104
- && parsed.cache === "cache-bean";
105
- console.log(ok ? "STANDARD DECORATORS OK" : "STANDARD DECORATORS FAILED");
106
- server.close();
107
- process.exit(ok ? 0 : 1);
108
- });
109
- });
110
- function httpGet(url, cb) {
111
- require("http").get(url, (res) => {
112
- let data = "";
113
- res.on("data", (chunk) => { data += chunk; });
114
- res.on("end", () => cb(data));
115
- });
116
- }
@@ -1,14 +0,0 @@
1
- {
2
- "name": "typespeed-example-std",
3
- "version": "1.0.0",
4
- "private": true,
5
- "description": "typespeed 标准装饰器(TC39 Stage 2.7)示例:experimentalDecorators: false + Symbol.metadata polyfill",
6
- "scripts": {
7
- "build": "tsc -p .",
8
- "start": "node dist/main.js"
9
- },
10
- "devDependencies": {
11
- "typescript": "^5.9.3",
12
- "@types/node": "^18.19.3"
13
- }
14
- }
@@ -1,64 +0,0 @@
1
- // Symbol.metadata polyfill:Node 22/24/26 都没有 Symbol.metadata,
2
- // 标准装饰器的 context.metadata 依赖它,缺失会 TypeError,必须最先执行。
3
- (Symbol as { metadata?: symbol }).metadata ??= Symbol("Symbol.metadata");
4
-
5
- import express from "express";
6
- import {
7
- component, getComponent, autoware, bean, getBean,
8
- getMapping, bind, setRouter,
9
- } from "../../../dist/typespeed";
10
-
11
- class CacheBean {
12
- name = "cache-bean";
13
- }
14
-
15
- @component
16
- class TestService {
17
-
18
- @bean(CacheBean)
19
- getCache() { return new CacheBean(); }
20
-
21
- @autoware(CacheBean)
22
- cache!: CacheBean;
23
-
24
- // 标准模式:无参数装饰器,@bind 声明「参数名 → 来源」
25
- @getMapping("/std/test/:id")
26
- @bind({ id: "reqParam" })
27
- async test(id: string) {
28
- return { id, cache: this.cache.name };
29
- }
30
- }
31
-
32
- // 验证 1:@component / @autoware / @bean
33
- const svc = getComponent(TestService);
34
- console.log("[component] registered:", svc !== undefined);
35
- console.log("[autoware(CacheBean)] injected:", svc && (svc as any).cache?.name);
36
- console.log("[bean(CacheBean)] factory:", getBean(CacheBean)?.name);
37
-
38
- // 验证 2:@getMapping + @bind 路由,走 setRouter 真实 HTTP 请求
39
- const app = express();
40
- setRouter(app);
41
- const server = app.listen(0, () => {
42
- const addr = server.address() as any;
43
- const port = addr.port;
44
- httpGet(`http://127.0.0.1:${port}/std/test/123`, (body) => {
45
- console.log("[route @bind] response:", body);
46
- const parsed = JSON.parse(body);
47
- const ok = svc !== undefined
48
- && (svc as any).cache?.name === "cache-bean"
49
- && getBean(CacheBean)?.name === "cache-bean"
50
- && parsed.id === "123"
51
- && parsed.cache === "cache-bean";
52
- console.log(ok ? "STANDARD DECORATORS OK" : "STANDARD DECORATORS FAILED");
53
- server.close();
54
- process.exit(ok ? 0 : 1);
55
- });
56
- });
57
-
58
- function httpGet(url: string, cb: (body: string) => void) {
59
- require("http").get(url, (res: any) => {
60
- let data = "";
61
- res.on("data", (chunk: any) => { data += chunk; });
62
- res.on("end", () => cb(data));
63
- });
64
- }
@@ -1,17 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "es2022",
4
- "module": "commonjs",
5
- "moduleResolution": "node",
6
- "useDefineForClassFields": true,
7
- "experimentalDecorators": false,
8
- "emitDecoratorMetadata": false,
9
- "lib": ["es2022", "esnext.decorators", "dom"],
10
- "esModuleInterop": true,
11
- "skipLibCheck": true,
12
- "strict": false,
13
- "outDir": "./dist",
14
- "baseUrl": "."
15
- },
16
- "include": ["./src/**/*.ts"]
17
- }