adorn-api 1.0.0
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 +249 -0
- package/dist/cli/generate-routes.js +101 -0
- package/dist/cli/generate-swagger.js +197 -0
- package/dist/controllers/advanced.controller.js +131 -0
- package/dist/controllers/user.controller.js +121 -0
- package/dist/entities/user.entity.js +1 -0
- package/dist/index.js +2 -0
- package/dist/lib/common.js +62 -0
- package/dist/lib/decorators.js +116 -0
- package/dist/middleware/auth.middleware.js +13 -0
- package/dist/routes.js +80 -0
- package/dist/server.js +18 -0
- package/dist/src/cli/generate-routes.js +105 -0
- package/dist/src/cli/generate-swagger.js +197 -0
- package/dist/src/index.js +4 -0
- package/dist/src/lib/common.js +62 -0
- package/dist/src/lib/decorators.js +116 -0
- package/dist/src/routes.js +80 -0
- package/dist/src/server.js +18 -0
- package/dist/tests/example-app/controllers/advanced.controller.js +130 -0
- package/dist/tests/example-app/controllers/controllers/advanced.controller.js +131 -0
- package/dist/tests/example-app/controllers/controllers/user.controller.js +121 -0
- package/dist/tests/example-app/controllers/user.controller.js +121 -0
- package/dist/tests/example-app/entities/entities/user.entity.js +1 -0
- package/dist/tests/example-app/entities/user.entity.js +1 -0
- package/dist/tests/example-app/middleware/auth.middleware.js +13 -0
- package/dist/tests/example-app/middleware/middleware/auth.middleware.js +13 -0
- package/dist/tests/example-app/routes.js +80 -0
- package/dist/tests/example-app/server.js +23 -0
- package/package.json +34 -0
- package/scripts/run-example.js +32 -0
- package/src/cli/generate-routes.ts +123 -0
- package/src/cli/generate-swagger.ts +216 -0
- package/src/index.js +20 -0
- package/src/index.ts +4 -0
- package/src/lib/common.js +68 -0
- package/src/lib/common.ts +35 -0
- package/src/lib/decorators.js +128 -0
- package/src/lib/decorators.ts +136 -0
- package/swagger.json +238 -0
- package/tests/e2e.test.ts +72 -0
- package/tests/example-app/controllers/advanced.controller.ts +52 -0
- package/tests/example-app/controllers/user.controller.ts +35 -0
- package/tests/example-app/entities/user.entity.ts +8 -0
- package/tests/example-app/middleware/auth.middleware.ts +16 -0
- package/tests/example-app/routes.ts +102 -0
- package/tests/example-app/server.ts +30 -0
- package/tests/generators.test.ts +48 -0
- package/tests/utils.ts +46 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
// src/controllers/advanced.controller.ts
|
|
36
|
+
import { Controller, Get, Post, FromBody, FromPath } from "../lib/decorators.js";
|
|
37
|
+
import { PaginationQuery } from "../lib/common.js";
|
|
38
|
+
// --- 1. Advanced Request DTOs ---
|
|
39
|
+
// INHERITANCE: UserListRequest automatically gets 'page' and 'limit' from PaginationQuery
|
|
40
|
+
// AND the generator will find them because we scan the Type properties.
|
|
41
|
+
let UserListRequest = (() => {
|
|
42
|
+
let _classSuper = PaginationQuery;
|
|
43
|
+
let _tenantId_decorators;
|
|
44
|
+
let _tenantId_initializers = [];
|
|
45
|
+
let _tenantId_extraInitializers = [];
|
|
46
|
+
return class UserListRequest extends _classSuper {
|
|
47
|
+
static {
|
|
48
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
49
|
+
_tenantId_decorators = [FromPath()];
|
|
50
|
+
__esDecorate(null, null, _tenantId_decorators, { kind: "field", name: "tenantId", static: false, private: false, access: { has: obj => "tenantId" in obj, get: obj => obj.tenantId, set: (obj, value) => { obj.tenantId = value; } }, metadata: _metadata }, _tenantId_initializers, _tenantId_extraInitializers);
|
|
51
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
52
|
+
}
|
|
53
|
+
// Implicitly @FromQuery because it's a GET request and extends a class
|
|
54
|
+
search;
|
|
55
|
+
tenantId = __runInitializers(this, _tenantId_initializers, void 0);
|
|
56
|
+
constructor() {
|
|
57
|
+
super(...arguments);
|
|
58
|
+
__runInitializers(this, _tenantId_extraInitializers);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
})();
|
|
62
|
+
export { UserListRequest };
|
|
63
|
+
// COMPOSITION: Using the Type Helper for safety, but class for Decorators
|
|
64
|
+
// We implement the Type Helper to ensure our Class matches the Entity rule
|
|
65
|
+
let CreateUserDto = (() => {
|
|
66
|
+
let _name_decorators;
|
|
67
|
+
let _name_initializers = [];
|
|
68
|
+
let _name_extraInitializers = [];
|
|
69
|
+
let _email_decorators;
|
|
70
|
+
let _email_initializers = [];
|
|
71
|
+
let _email_extraInitializers = [];
|
|
72
|
+
return class CreateUserDto {
|
|
73
|
+
static {
|
|
74
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
75
|
+
_name_decorators = [FromBody()];
|
|
76
|
+
_email_decorators = [FromBody()];
|
|
77
|
+
__esDecorate(null, null, _name_decorators, { kind: "field", name: "name", static: false, private: false, access: { has: obj => "name" in obj, get: obj => obj.name, set: (obj, value) => { obj.name = value; } }, metadata: _metadata }, _name_initializers, _name_extraInitializers);
|
|
78
|
+
__esDecorate(null, null, _email_decorators, { kind: "field", name: "email", static: false, private: false, access: { has: obj => "email" in obj, get: obj => obj.email, set: (obj, value) => { obj.email = value; } }, metadata: _metadata }, _email_initializers, _email_extraInitializers);
|
|
79
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
80
|
+
}
|
|
81
|
+
name = __runInitializers(this, _name_initializers, void 0);
|
|
82
|
+
email = (__runInitializers(this, _name_extraInitializers), __runInitializers(this, _email_initializers, void 0));
|
|
83
|
+
constructor() {
|
|
84
|
+
__runInitializers(this, _email_extraInitializers);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
})();
|
|
88
|
+
export { CreateUserDto };
|
|
89
|
+
// --- 2. The Controller ---
|
|
90
|
+
let AdvancedController = (() => {
|
|
91
|
+
let _classDecorators = [Controller("advanced")];
|
|
92
|
+
let _classDescriptor;
|
|
93
|
+
let _classExtraInitializers = [];
|
|
94
|
+
let _classThis;
|
|
95
|
+
let _instanceExtraInitializers = [];
|
|
96
|
+
let _listUsers_decorators;
|
|
97
|
+
let _create_decorators;
|
|
98
|
+
var AdvancedController = class {
|
|
99
|
+
static { _classThis = this; }
|
|
100
|
+
static {
|
|
101
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
102
|
+
_listUsers_decorators = [Get("/{tenantId}/users")];
|
|
103
|
+
_create_decorators = [Post("/")];
|
|
104
|
+
__esDecorate(this, null, _listUsers_decorators, { kind: "method", name: "listUsers", static: false, private: false, access: { has: obj => "listUsers" in obj, get: obj => obj.listUsers }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
105
|
+
__esDecorate(this, null, _create_decorators, { kind: "method", name: "create", static: false, private: false, access: { has: obj => "create" in obj, get: obj => obj.create }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
106
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
107
|
+
AdvancedController = _classThis = _classDescriptor.value;
|
|
108
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
109
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
110
|
+
}
|
|
111
|
+
async listUsers(req) {
|
|
112
|
+
return [
|
|
113
|
+
{ id: "1", name: "Alice", email: "a@a.com", isActive: true, createdAt: "now" }
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
async create(req) {
|
|
117
|
+
return {
|
|
118
|
+
id: "123",
|
|
119
|
+
name: req.name,
|
|
120
|
+
email: req.email,
|
|
121
|
+
isActive: true,
|
|
122
|
+
createdAt: "now"
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
constructor() {
|
|
126
|
+
__runInitializers(this, _instanceExtraInitializers);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
return AdvancedController = _classThis;
|
|
130
|
+
})();
|
|
131
|
+
export { AdvancedController };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
// src/controllers/user.controller.ts
|
|
36
|
+
import { Controller, Get, Post, FromQuery, FromPath, FromBody } from "../lib/decorators.js";
|
|
37
|
+
// --- DTO Definitions (The substitute for Parameter Decorators) ---
|
|
38
|
+
let GetUserRequest = (() => {
|
|
39
|
+
let _userId_decorators;
|
|
40
|
+
let _userId_initializers = [];
|
|
41
|
+
let _userId_extraInitializers = [];
|
|
42
|
+
let _details_decorators;
|
|
43
|
+
let _details_initializers = [];
|
|
44
|
+
let _details_extraInitializers = [];
|
|
45
|
+
return class GetUserRequest {
|
|
46
|
+
static {
|
|
47
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
48
|
+
_userId_decorators = [FromPath()];
|
|
49
|
+
_details_decorators = [FromQuery()];
|
|
50
|
+
__esDecorate(null, null, _userId_decorators, { kind: "field", name: "userId", static: false, private: false, access: { has: obj => "userId" in obj, get: obj => obj.userId, set: (obj, value) => { obj.userId = value; } }, metadata: _metadata }, _userId_initializers, _userId_extraInitializers);
|
|
51
|
+
__esDecorate(null, null, _details_decorators, { kind: "field", name: "details", static: false, private: false, access: { has: obj => "details" in obj, get: obj => obj.details, set: (obj, value) => { obj.details = value; } }, metadata: _metadata }, _details_initializers, _details_extraInitializers);
|
|
52
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
53
|
+
}
|
|
54
|
+
userId = __runInitializers(this, _userId_initializers, void 0);
|
|
55
|
+
details = (__runInitializers(this, _userId_extraInitializers), __runInitializers(this, _details_initializers, void 0));
|
|
56
|
+
constructor() {
|
|
57
|
+
__runInitializers(this, _details_extraInitializers);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
})();
|
|
61
|
+
export { GetUserRequest };
|
|
62
|
+
let CreateUserRequest = (() => {
|
|
63
|
+
let _name_decorators;
|
|
64
|
+
let _name_initializers = [];
|
|
65
|
+
let _name_extraInitializers = [];
|
|
66
|
+
let _email_decorators;
|
|
67
|
+
let _email_initializers = [];
|
|
68
|
+
let _email_extraInitializers = [];
|
|
69
|
+
return class CreateUserRequest {
|
|
70
|
+
static {
|
|
71
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
72
|
+
_name_decorators = [FromBody()];
|
|
73
|
+
_email_decorators = [FromBody()];
|
|
74
|
+
__esDecorate(null, null, _name_decorators, { kind: "field", name: "name", static: false, private: false, access: { has: obj => "name" in obj, get: obj => obj.name, set: (obj, value) => { obj.name = value; } }, metadata: _metadata }, _name_initializers, _name_extraInitializers);
|
|
75
|
+
__esDecorate(null, null, _email_decorators, { kind: "field", name: "email", static: false, private: false, access: { has: obj => "email" in obj, get: obj => obj.email, set: (obj, value) => { obj.email = value; } }, metadata: _metadata }, _email_initializers, _email_extraInitializers);
|
|
76
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
77
|
+
}
|
|
78
|
+
name = __runInitializers(this, _name_initializers, void 0);
|
|
79
|
+
email = (__runInitializers(this, _name_extraInitializers), __runInitializers(this, _email_initializers, void 0));
|
|
80
|
+
constructor() {
|
|
81
|
+
__runInitializers(this, _email_extraInitializers);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
})();
|
|
85
|
+
export { CreateUserRequest };
|
|
86
|
+
// --- The Controller ---
|
|
87
|
+
let UserController = (() => {
|
|
88
|
+
let _classDecorators = [Controller("users")];
|
|
89
|
+
let _classDescriptor;
|
|
90
|
+
let _classExtraInitializers = [];
|
|
91
|
+
let _classThis;
|
|
92
|
+
let _instanceExtraInitializers = [];
|
|
93
|
+
let _getUser_decorators;
|
|
94
|
+
let _createUser_decorators;
|
|
95
|
+
var UserController = class {
|
|
96
|
+
static { _classThis = this; }
|
|
97
|
+
static {
|
|
98
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
99
|
+
_getUser_decorators = [Get("/{userId}")];
|
|
100
|
+
_createUser_decorators = [Post("/")];
|
|
101
|
+
__esDecorate(this, null, _getUser_decorators, { kind: "method", name: "getUser", static: false, private: false, access: { has: obj => "getUser" in obj, get: obj => obj.getUser }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
102
|
+
__esDecorate(this, null, _createUser_decorators, { kind: "method", name: "createUser", static: false, private: false, access: { has: obj => "createUser" in obj, get: obj => obj.createUser }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
103
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
104
|
+
UserController = _classThis = _classDescriptor.value;
|
|
105
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
106
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
107
|
+
}
|
|
108
|
+
// Strong typing: 'req' is checked at edit time.
|
|
109
|
+
async getUser(req) {
|
|
110
|
+
return `Getting user ${req.userId} with details: ${req.details}`;
|
|
111
|
+
}
|
|
112
|
+
async createUser(req) {
|
|
113
|
+
console.log(`Creating user ${req.name}`);
|
|
114
|
+
}
|
|
115
|
+
constructor() {
|
|
116
|
+
__runInitializers(this, _instanceExtraInitializers);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return UserController = _classThis;
|
|
120
|
+
})();
|
|
121
|
+
export { UserController };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
27
|
+
};
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
34
|
+
};
|
|
35
|
+
// src/controllers/user.controller.ts
|
|
36
|
+
import { Controller, Get, Post, FromQuery, FromPath, FromBody } from "../../../src/index.js";
|
|
37
|
+
// --- DTO Definitions (The substitute for Parameter Decorators) ---
|
|
38
|
+
let GetUserRequest = (() => {
|
|
39
|
+
let _userId_decorators;
|
|
40
|
+
let _userId_initializers = [];
|
|
41
|
+
let _userId_extraInitializers = [];
|
|
42
|
+
let _details_decorators;
|
|
43
|
+
let _details_initializers = [];
|
|
44
|
+
let _details_extraInitializers = [];
|
|
45
|
+
return class GetUserRequest {
|
|
46
|
+
static {
|
|
47
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
48
|
+
_userId_decorators = [FromPath()];
|
|
49
|
+
_details_decorators = [FromQuery()];
|
|
50
|
+
__esDecorate(null, null, _userId_decorators, { kind: "field", name: "userId", static: false, private: false, access: { has: obj => "userId" in obj, get: obj => obj.userId, set: (obj, value) => { obj.userId = value; } }, metadata: _metadata }, _userId_initializers, _userId_extraInitializers);
|
|
51
|
+
__esDecorate(null, null, _details_decorators, { kind: "field", name: "details", static: false, private: false, access: { has: obj => "details" in obj, get: obj => obj.details, set: (obj, value) => { obj.details = value; } }, metadata: _metadata }, _details_initializers, _details_extraInitializers);
|
|
52
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
53
|
+
}
|
|
54
|
+
userId = __runInitializers(this, _userId_initializers, void 0);
|
|
55
|
+
details = (__runInitializers(this, _userId_extraInitializers), __runInitializers(this, _details_initializers, void 0));
|
|
56
|
+
constructor() {
|
|
57
|
+
__runInitializers(this, _details_extraInitializers);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
})();
|
|
61
|
+
export { GetUserRequest };
|
|
62
|
+
let CreateUserRequest = (() => {
|
|
63
|
+
let _name_decorators;
|
|
64
|
+
let _name_initializers = [];
|
|
65
|
+
let _name_extraInitializers = [];
|
|
66
|
+
let _email_decorators;
|
|
67
|
+
let _email_initializers = [];
|
|
68
|
+
let _email_extraInitializers = [];
|
|
69
|
+
return class CreateUserRequest {
|
|
70
|
+
static {
|
|
71
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
72
|
+
_name_decorators = [FromBody()];
|
|
73
|
+
_email_decorators = [FromBody()];
|
|
74
|
+
__esDecorate(null, null, _name_decorators, { kind: "field", name: "name", static: false, private: false, access: { has: obj => "name" in obj, get: obj => obj.name, set: (obj, value) => { obj.name = value; } }, metadata: _metadata }, _name_initializers, _name_extraInitializers);
|
|
75
|
+
__esDecorate(null, null, _email_decorators, { kind: "field", name: "email", static: false, private: false, access: { has: obj => "email" in obj, get: obj => obj.email, set: (obj, value) => { obj.email = value; } }, metadata: _metadata }, _email_initializers, _email_extraInitializers);
|
|
76
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
77
|
+
}
|
|
78
|
+
name = __runInitializers(this, _name_initializers, void 0);
|
|
79
|
+
email = (__runInitializers(this, _name_extraInitializers), __runInitializers(this, _email_initializers, void 0));
|
|
80
|
+
constructor() {
|
|
81
|
+
__runInitializers(this, _email_extraInitializers);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
})();
|
|
85
|
+
export { CreateUserRequest };
|
|
86
|
+
// --- The Controller ---
|
|
87
|
+
let UserController = (() => {
|
|
88
|
+
let _classDecorators = [Controller("users")];
|
|
89
|
+
let _classDescriptor;
|
|
90
|
+
let _classExtraInitializers = [];
|
|
91
|
+
let _classThis;
|
|
92
|
+
let _instanceExtraInitializers = [];
|
|
93
|
+
let _getUser_decorators;
|
|
94
|
+
let _createUser_decorators;
|
|
95
|
+
var UserController = class {
|
|
96
|
+
static { _classThis = this; }
|
|
97
|
+
static {
|
|
98
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
99
|
+
_getUser_decorators = [Get("/{userId}")];
|
|
100
|
+
_createUser_decorators = [Post("/")];
|
|
101
|
+
__esDecorate(this, null, _getUser_decorators, { kind: "method", name: "getUser", static: false, private: false, access: { has: obj => "getUser" in obj, get: obj => obj.getUser }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
102
|
+
__esDecorate(this, null, _createUser_decorators, { kind: "method", name: "createUser", static: false, private: false, access: { has: obj => "createUser" in obj, get: obj => obj.createUser }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
103
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
104
|
+
UserController = _classThis = _classDescriptor.value;
|
|
105
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
106
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
107
|
+
}
|
|
108
|
+
// Strong typing: 'req' is checked at edit time.
|
|
109
|
+
async getUser(req) {
|
|
110
|
+
return `Getting user ${req.userId} with details: ${req.details}`;
|
|
111
|
+
}
|
|
112
|
+
async createUser(req) {
|
|
113
|
+
console.log(`Creating user ${req.name}`);
|
|
114
|
+
}
|
|
115
|
+
constructor() {
|
|
116
|
+
__runInitializers(this, _instanceExtraInitializers);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return UserController = _classThis;
|
|
120
|
+
})();
|
|
121
|
+
export { UserController };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function authenticationMiddleware(req, res, next) {
|
|
2
|
+
const token = req.headers.authorization;
|
|
3
|
+
if (!token) {
|
|
4
|
+
res.status(401).json({ message: "No token provided" });
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
if (token === "Bearer secret") {
|
|
8
|
+
next();
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
res.status(403).json({ message: "Invalid token" });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function authenticationMiddleware(req, res, next) {
|
|
2
|
+
const token = req.headers.authorization;
|
|
3
|
+
if (!token) {
|
|
4
|
+
res.status(401).json({ message: "No token provided" });
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
if (token === "Bearer secret") {
|
|
8
|
+
next();
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
res.status(403).json({ message: "Invalid token" });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AdvancedController } from './controllers/advanced.controller.js';
|
|
2
|
+
import { UserController } from './controllers/user.controller.js';
|
|
3
|
+
export function RegisterRoutes(app) {
|
|
4
|
+
app.get('/advanced/:tenantId/users', async (req, res) => {
|
|
5
|
+
const controller = new AdvancedController();
|
|
6
|
+
try {
|
|
7
|
+
const input = {};
|
|
8
|
+
// Map Query
|
|
9
|
+
Object.assign(input, req.query);
|
|
10
|
+
// Map Params
|
|
11
|
+
Object.assign(input, req.params);
|
|
12
|
+
// Map Body
|
|
13
|
+
Object.assign(input, req.body);
|
|
14
|
+
// In a real app, you would run 'zod' or 'class-validator' here on 'input'
|
|
15
|
+
const response = await controller.listUsers(input);
|
|
16
|
+
res.status(200).json(response);
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
console.error(err);
|
|
20
|
+
res.status(500).send(err.message);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
app.post('/advanced/', async (req, res) => {
|
|
24
|
+
const controller = new AdvancedController();
|
|
25
|
+
try {
|
|
26
|
+
const input = {};
|
|
27
|
+
// Map Query
|
|
28
|
+
Object.assign(input, req.query);
|
|
29
|
+
// Map Params
|
|
30
|
+
Object.assign(input, req.params);
|
|
31
|
+
// Map Body
|
|
32
|
+
Object.assign(input, req.body);
|
|
33
|
+
// In a real app, you would run 'zod' or 'class-validator' here on 'input'
|
|
34
|
+
const response = await controller.create(input);
|
|
35
|
+
res.status(200).json(response);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
console.error(err);
|
|
39
|
+
res.status(500).send(err.message);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
app.get('/users/:userId', async (req, res) => {
|
|
43
|
+
const controller = new UserController();
|
|
44
|
+
try {
|
|
45
|
+
const input = {};
|
|
46
|
+
// Map Query
|
|
47
|
+
Object.assign(input, req.query);
|
|
48
|
+
// Map Params
|
|
49
|
+
Object.assign(input, req.params);
|
|
50
|
+
// Map Body
|
|
51
|
+
Object.assign(input, req.body);
|
|
52
|
+
// In a real app, you would run 'zod' or 'class-validator' here on 'input'
|
|
53
|
+
const response = await controller.getUser(input);
|
|
54
|
+
res.status(200).json(response);
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
console.error(err);
|
|
58
|
+
res.status(500).send(err.message);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
app.post('/users/', async (req, res) => {
|
|
62
|
+
const controller = new UserController();
|
|
63
|
+
try {
|
|
64
|
+
const input = {};
|
|
65
|
+
// Map Query
|
|
66
|
+
Object.assign(input, req.query);
|
|
67
|
+
// Map Params
|
|
68
|
+
Object.assign(input, req.params);
|
|
69
|
+
// Map Body
|
|
70
|
+
Object.assign(input, req.body);
|
|
71
|
+
// In a real app, you would run 'zod' or 'class-validator' here on 'input'
|
|
72
|
+
const response = await controller.createUser(input);
|
|
73
|
+
res.status(200).json(response);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
console.error(err);
|
|
77
|
+
res.status(500).send(err.message);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// tests/example-app/server.ts
|
|
2
|
+
// Example Express server using adorn-api
|
|
3
|
+
import express from "express";
|
|
4
|
+
import bodyParser from "body-parser";
|
|
5
|
+
import { readFileSync } from "fs";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
|
+
import { dirname, join } from "path";
|
|
8
|
+
import swaggerUi from "swagger-ui-express";
|
|
9
|
+
// Import generated routes
|
|
10
|
+
import { RegisterRoutes } from "./routes.js";
|
|
11
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
+
const __dirname = dirname(__filename);
|
|
13
|
+
const app = express();
|
|
14
|
+
app.use(bodyParser.json());
|
|
15
|
+
// Register the Generated Routes
|
|
16
|
+
RegisterRoutes(app);
|
|
17
|
+
// Serve Swagger UI
|
|
18
|
+
const swaggerDoc = JSON.parse(readFileSync(join(__dirname, "../../swagger.json"), "utf-8"));
|
|
19
|
+
app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDoc));
|
|
20
|
+
app.listen(3000, () => {
|
|
21
|
+
console.log("🚀 Example server running on http://localhost:3000");
|
|
22
|
+
console.log("📄 Swagger running on http://localhost:3000/docs");
|
|
23
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "adorn-api",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"gen:spec": "ts-node --esm src/cli/generate-swagger.ts",
|
|
9
|
+
"gen:routes": "ts-node --esm src/cli/generate-routes.ts",
|
|
10
|
+
"gen": "npm run gen:spec && npm run gen:routes",
|
|
11
|
+
"example": "node scripts/run-example.js",
|
|
12
|
+
"test": "vitest run"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "",
|
|
16
|
+
"license": "ISC",
|
|
17
|
+
"type": "module",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"body-parser": "^2.2.0",
|
|
20
|
+
"express": "^4.21.1",
|
|
21
|
+
"reflect-metadata": "^0.2.2",
|
|
22
|
+
"swagger-ui-express": "^5.0.1",
|
|
23
|
+
"ts-morph": "^27.0.2",
|
|
24
|
+
"typescript": "^5.9.3",
|
|
25
|
+
"zod": "^4.2.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/express": "^5.0.0",
|
|
29
|
+
"@types/node": "^25.0.3",
|
|
30
|
+
"@types/swagger-ui-express": "^4.1.8",
|
|
31
|
+
"ts-node": "^10.9.2",
|
|
32
|
+
"vitest": "^2.1.8"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
4
|
+
|
|
5
|
+
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
|
|
6
|
+
const loader = pathToFileURL(path.join(projectRoot, "node_modules", "ts-node", "esm.mjs")).toString();
|
|
7
|
+
const server = path.join(projectRoot, "tests", "example-app", "server.ts");
|
|
8
|
+
|
|
9
|
+
const child = spawn(
|
|
10
|
+
process.execPath,
|
|
11
|
+
["--loader", loader, server],
|
|
12
|
+
{
|
|
13
|
+
stdio: "inherit",
|
|
14
|
+
env: {
|
|
15
|
+
...process.env,
|
|
16
|
+
TS_NODE_PROJECT: path.join(projectRoot, "tsconfig.json"),
|
|
17
|
+
TS_NODE_PREFER_TS_EXTS: "true",
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
["SIGINT", "SIGTERM"].forEach(signal => {
|
|
23
|
+
process.on(signal, () => {
|
|
24
|
+
if (!child.killed) {
|
|
25
|
+
child.kill(signal);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
child.on("exit", code => {
|
|
31
|
+
process.exitCode = code ?? 0;
|
|
32
|
+
});
|