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,123 @@
|
|
|
1
|
+
// src/cli/generate-routes.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { Project, ClassDeclaration } from "ts-morph";
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
|
|
6
|
+
const PROJECT_ROOT = "./tsconfig.json";
|
|
7
|
+
const OUTPUT_FILE = "./tests/example-app/routes.ts"; // We generate this file!
|
|
8
|
+
|
|
9
|
+
const project = new Project({ tsConfigFilePath: PROJECT_ROOT });
|
|
10
|
+
|
|
11
|
+
let routeCode = `/* tslint:disable */
|
|
12
|
+
/* eslint-disable */
|
|
13
|
+
// WARNING: This file was auto-generated by adorn-api. Do not edit.
|
|
14
|
+
import { Express, Request, Response } from 'express';
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
// Helper to keep track of imports we need to add to the generated file
|
|
18
|
+
const imports = new Set<string>();
|
|
19
|
+
|
|
20
|
+
function processController(classDec: ClassDeclaration) {
|
|
21
|
+
const className = classDec.getName();
|
|
22
|
+
if (!className) return "";
|
|
23
|
+
|
|
24
|
+
// Track import (assuming controllers are in ./controllers/)
|
|
25
|
+
const sourceFile = classDec.getSourceFile();
|
|
26
|
+
const exampleAppRoot = path.join(process.cwd(), "tests", "example-app");
|
|
27
|
+
const relativeDir = path.relative(exampleAppRoot, sourceFile.getDirectoryPath());
|
|
28
|
+
const relativeSegments = relativeDir ? relativeDir.split(path.sep) : [];
|
|
29
|
+
const baseName = sourceFile.getBaseName().replace(/\.ts$/, ".js");
|
|
30
|
+
const importPath = `./${path.posix.join(...relativeSegments, baseName)}`;
|
|
31
|
+
imports.add(`import { ${className} } from '${importPath}';`);
|
|
32
|
+
|
|
33
|
+
const controllerDec = classDec.getDecorators().find(d => d.getName() === "Controller");
|
|
34
|
+
if (!controllerDec) return "";
|
|
35
|
+
|
|
36
|
+
const basePath = controllerDec.getArguments()[0]?.getText().replace(/['"]/g, "") || "/";
|
|
37
|
+
let methodBlocks = "";
|
|
38
|
+
|
|
39
|
+
classDec.getMethods().forEach(method => {
|
|
40
|
+
const getDec = method.getDecorator("Get");
|
|
41
|
+
const postDec = method.getDecorator("Post");
|
|
42
|
+
const putDec = method.getDecorator("Put");
|
|
43
|
+
const deleteDec = method.getDecorator("Delete");
|
|
44
|
+
const decorator = getDec || postDec || putDec || deleteDec;
|
|
45
|
+
if (!decorator) return;
|
|
46
|
+
|
|
47
|
+
const httpMethod = getDec ? "get" : postDec ? "post" : putDec ? "put" : "delete";
|
|
48
|
+
const pathArg = decorator.getArguments()[0]?.getText().replace(/['"]/g, "") || "/";
|
|
49
|
+
const fullPath = `/${basePath}${pathArg}`.replace("//", "/").replace(/{/g, ":").replace(/}/g, ""); // Convert {id} to :id for Express
|
|
50
|
+
|
|
51
|
+
const methodName = method.getName();
|
|
52
|
+
const params = method.getParameters();
|
|
53
|
+
|
|
54
|
+
// Authentication check
|
|
55
|
+
const authDec = method.getDecorator("Authorized");
|
|
56
|
+
const controllerAuthDec = classDec.getDecorator("Authorized");
|
|
57
|
+
const hasAuth = !!authDec || !!controllerAuthDec;
|
|
58
|
+
const middlewareArgs = hasAuth ? "authenticationMiddleware, " : "";
|
|
59
|
+
|
|
60
|
+
// Logic to instantiate the DTO
|
|
61
|
+
let paramInstantiation = "";
|
|
62
|
+
if (params.length > 0) {
|
|
63
|
+
const paramName = params[0].getName();
|
|
64
|
+
const paramType = params[0].getType().getSymbol()?.getName();
|
|
65
|
+
|
|
66
|
+
// We assume the DTO is a class we can instantiate, or a shape we construct
|
|
67
|
+
// Here we map request parts to the DTO
|
|
68
|
+
paramInstantiation = `
|
|
69
|
+
const input: any = {};
|
|
70
|
+
// Map Query
|
|
71
|
+
Object.assign(input, req.query);
|
|
72
|
+
// Map Params
|
|
73
|
+
Object.assign(input, req.params);
|
|
74
|
+
// Map Body
|
|
75
|
+
Object.assign(input, req.body);
|
|
76
|
+
|
|
77
|
+
// In a real app, you would run 'zod' or 'class-validator' here on 'input'
|
|
78
|
+
`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
methodBlocks += `
|
|
82
|
+
app.${httpMethod}('${fullPath}', ${middlewareArgs}async (req: Request, res: Response) => {
|
|
83
|
+
const controller = new ${className}();
|
|
84
|
+
try {
|
|
85
|
+
${paramInstantiation}
|
|
86
|
+
const response = await controller.${methodName}(${params.length > 0 ? 'input' : ''});
|
|
87
|
+
res.status(200).json(response);
|
|
88
|
+
} catch (err: any) {
|
|
89
|
+
console.error(err);
|
|
90
|
+
res.status(500).send(err.message);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
`;
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return methodBlocks;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const sourceFiles = project.getSourceFiles("tests/example-app/controllers/**/*.ts");
|
|
100
|
+
let allRoutes = "";
|
|
101
|
+
|
|
102
|
+
sourceFiles.forEach(file => {
|
|
103
|
+
file.getClasses().forEach(c => {
|
|
104
|
+
allRoutes += processController(c);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Add authentication middleware import if needed
|
|
109
|
+
if (allRoutes.includes('authenticationMiddleware')) {
|
|
110
|
+
routeCode += `import { authenticationMiddleware } from './middleware/auth.middleware.js';\n`;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Prepend imports
|
|
114
|
+
routeCode += Array.from(imports).join('\n');
|
|
115
|
+
routeCode += `
|
|
116
|
+
|
|
117
|
+
export function RegisterRoutes(app: Express) {
|
|
118
|
+
${allRoutes}
|
|
119
|
+
}
|
|
120
|
+
`;
|
|
121
|
+
|
|
122
|
+
fs.writeFileSync(OUTPUT_FILE, routeCode);
|
|
123
|
+
console.log(`✅ Generated Routes at ${OUTPUT_FILE}`);
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// src/cli/generate-swagger.ts
|
|
2
|
+
import { Project, Type, SyntaxKind, ClassDeclaration, Symbol as MorphSymbol, PropertyDeclaration } from "ts-morph";
|
|
3
|
+
import * as fs from "fs";
|
|
4
|
+
|
|
5
|
+
const PROJECT_ROOT = "./tsconfig.json";
|
|
6
|
+
const OUTPUT_FILE = "./swagger.json";
|
|
7
|
+
|
|
8
|
+
const project = new Project({ tsConfigFilePath: PROJECT_ROOT });
|
|
9
|
+
|
|
10
|
+
const openApiSpec: any = {
|
|
11
|
+
openapi: "3.0.0",
|
|
12
|
+
info: { title: "Adorn API", version: "2.0.0" },
|
|
13
|
+
paths: {},
|
|
14
|
+
components: {
|
|
15
|
+
schemas: {},
|
|
16
|
+
securitySchemes: {
|
|
17
|
+
bearerAuth: {
|
|
18
|
+
type: "http",
|
|
19
|
+
scheme: "bearer",
|
|
20
|
+
bearerFormat: "JWT"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// --- Helper: Deep Type Resolver ---
|
|
27
|
+
// This converts TypeScript Types (Generics, Interfaces, Primitives) into JSON Schema
|
|
28
|
+
function resolveSchema(type: Type, collectedSchemas: Record<string, any>): any {
|
|
29
|
+
// 1. Handle Primitives
|
|
30
|
+
if (type.isString() || type.isStringLiteral()) return { type: "string" };
|
|
31
|
+
if (type.isNumber() || type.isNumberLiteral()) return { type: "integer" };
|
|
32
|
+
if (type.isBoolean() || type.isBooleanLiteral()) return { type: "boolean" };
|
|
33
|
+
if (type.isArray()) {
|
|
34
|
+
const arrayType = type.getArrayElementType();
|
|
35
|
+
return {
|
|
36
|
+
type: "array",
|
|
37
|
+
items: arrayType ? resolveSchema(arrayType, collectedSchemas) : {},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 2. Handle Union Types (Enums)
|
|
42
|
+
if (type.isUnion()) {
|
|
43
|
+
const unionTypes = type.getUnionTypes();
|
|
44
|
+
|
|
45
|
+
// Extract literal values (e.g., "active", 100)
|
|
46
|
+
const literals = unionTypes
|
|
47
|
+
.map(t => t.isLiteral() ? t.getLiteralValue() : null)
|
|
48
|
+
.filter(val => val !== null);
|
|
49
|
+
|
|
50
|
+
// If we found literals, it's an Enum
|
|
51
|
+
if (literals.length > 0) {
|
|
52
|
+
const isString = typeof literals[0] === 'string';
|
|
53
|
+
return {
|
|
54
|
+
type: isString ? 'string' : 'integer',
|
|
55
|
+
enum: literals
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// If it's a mix of objects (e.g. User | Admin), OpenApi uses 'oneOf'
|
|
60
|
+
// Simplified for this demo: take the first non-null type
|
|
61
|
+
return resolveSchema(unionTypes[0], collectedSchemas);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 3. Handle Objects (Classes, Interfaces, Nested Literals)
|
|
65
|
+
if (type.isObject()) {
|
|
66
|
+
// Recursion Guard: If we've seen this type name before in components, reference it!
|
|
67
|
+
// (This prevents infinite loops and reduces swagger size)
|
|
68
|
+
const symbol = type.getSymbol();
|
|
69
|
+
const typeName = symbol?.getName();
|
|
70
|
+
|
|
71
|
+
// Common excluded types
|
|
72
|
+
if (typeName === "Promise") {
|
|
73
|
+
return resolveSchema(type.getTypeArguments()[0], collectedSchemas);
|
|
74
|
+
}
|
|
75
|
+
if (typeName === "Date") return { type: "string", format: "date-time" };
|
|
76
|
+
|
|
77
|
+
// If it's a named class/interface (e.g. "Address"), and we haven't processed it,
|
|
78
|
+
// we could add it to components.schemas. For now, we inline it for simplicity.
|
|
79
|
+
|
|
80
|
+
const properties = type.getProperties();
|
|
81
|
+
const schema: any = { type: "object", properties: {}, required: [] };
|
|
82
|
+
|
|
83
|
+
properties.forEach((prop) => {
|
|
84
|
+
const propName = prop.getName();
|
|
85
|
+
if (propName.startsWith("_")) return; // Skip privates
|
|
86
|
+
|
|
87
|
+
// Get the type of the property using getTypeAtLocation
|
|
88
|
+
const declarations = prop.getDeclarations();
|
|
89
|
+
if (declarations.length > 0) {
|
|
90
|
+
const propType = prop.getTypeAtLocation(declarations[0]);
|
|
91
|
+
|
|
92
|
+
if (propType) {
|
|
93
|
+
// RECURSION HERE: Pass the property type back into resolveSchema
|
|
94
|
+
schema.properties[propName] = resolveSchema(propType, collectedSchemas);
|
|
95
|
+
|
|
96
|
+
if (!prop.isOptional()) {
|
|
97
|
+
schema.required.push(propName);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return schema;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { type: "string" }; // Fallback
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function processController(classDec: ClassDeclaration) {
|
|
110
|
+
const controllerDec = classDec.getDecorators().find((d) => d.getName() === "Controller");
|
|
111
|
+
if (!controllerDec) return;
|
|
112
|
+
|
|
113
|
+
const basePath = controllerDec.getArguments()[0]?.getText().replace(/['"]/g, "") || "/";
|
|
114
|
+
|
|
115
|
+
classDec.getMethods().forEach((method) => {
|
|
116
|
+
const getDec = method.getDecorator("Get");
|
|
117
|
+
const postDec = method.getDecorator("Post");
|
|
118
|
+
const putDec = method.getDecorator("Put");
|
|
119
|
+
const deleteDec = method.getDecorator("Delete");
|
|
120
|
+
const decorator = getDec || postDec || putDec || deleteDec;
|
|
121
|
+
if (!decorator) return;
|
|
122
|
+
|
|
123
|
+
const httpMethod = getDec ? "get" : postDec ? "post" : putDec ? "put" : "delete";
|
|
124
|
+
const pathArg = decorator.getArguments()[0]?.getText().replace(/['"]/g, "") || "/";
|
|
125
|
+
const fullPath = `/${basePath}${pathArg}`.replace("//", "/");
|
|
126
|
+
|
|
127
|
+
const parameters: any[] = [];
|
|
128
|
+
const requestBody: any = { content: {} };
|
|
129
|
+
|
|
130
|
+
// --- 1. Request Analysis (Input DTOs) ---
|
|
131
|
+
const params = method.getParameters();
|
|
132
|
+
if (params.length > 0) {
|
|
133
|
+
const param = params[0];
|
|
134
|
+
const paramType = param.getType(); // This resolves generics and inheritance!
|
|
135
|
+
|
|
136
|
+
// Iterate ALL properties of the type (inherited included)
|
|
137
|
+
paramType.getProperties().forEach(prop => {
|
|
138
|
+
const propName = prop.getName();
|
|
139
|
+
|
|
140
|
+
// We need to check Decorators.
|
|
141
|
+
// Note: In mapped types/generics, getting decorators is hard.
|
|
142
|
+
// We fallback to checking the source declaration.
|
|
143
|
+
const declarations = prop.getDeclarations();
|
|
144
|
+
let isQuery = false;
|
|
145
|
+
let isBody = false;
|
|
146
|
+
let isPath = false;
|
|
147
|
+
|
|
148
|
+
// Check decorators on the definition
|
|
149
|
+
declarations.forEach(decl => {
|
|
150
|
+
if (decl.getKind() === SyntaxKind.PropertyDeclaration) {
|
|
151
|
+
const pDecl = decl as PropertyDeclaration;
|
|
152
|
+
if (pDecl.getDecorator("FromQuery")) isQuery = true;
|
|
153
|
+
if (pDecl.getDecorator("FromPath")) isPath = true;
|
|
154
|
+
if (pDecl.getDecorator("FromBody")) isBody = true;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// IMPLICIT RULES:
|
|
159
|
+
// If it's a GET, and not path, default to Query.
|
|
160
|
+
// If it's a POST, and not path/query, default to Body.
|
|
161
|
+
if (!isQuery && !isPath && !isBody) {
|
|
162
|
+
if (httpMethod === "get") isQuery = true;
|
|
163
|
+
else isBody = true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const propType = prop.getTypeAtLocation(param.getSourceFile());
|
|
167
|
+
const propTypeSchema = resolveSchema(propType, openApiSpec.components.schemas);
|
|
168
|
+
|
|
169
|
+
if (isPath) {
|
|
170
|
+
parameters.push({ name: propName, in: "path", required: true, schema: propTypeSchema });
|
|
171
|
+
} else if (isQuery) {
|
|
172
|
+
parameters.push({ name: propName, in: "query", required: !prop.isOptional(), schema: propTypeSchema });
|
|
173
|
+
} else if (isBody) {
|
|
174
|
+
if (!requestBody.content["application/json"]) {
|
|
175
|
+
requestBody.content["application/json"] = { schema: { type: "object", properties: {}, required: [] } };
|
|
176
|
+
}
|
|
177
|
+
const bodySchema = requestBody.content["application/json"].schema;
|
|
178
|
+
bodySchema.properties[propName] = propTypeSchema;
|
|
179
|
+
if (!prop.isOptional()) bodySchema.required.push(propName);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// --- 2. Authentication Check ---
|
|
185
|
+
const authDec = method.getDecorator("Authorized");
|
|
186
|
+
const controllerAuthDec = classDec.getDecorator("Authorized");
|
|
187
|
+
|
|
188
|
+
const isAuth = !!authDec || !!controllerAuthDec;
|
|
189
|
+
|
|
190
|
+
// --- 3. Response Analysis (Return Type) ---
|
|
191
|
+
const returnType = method.getReturnType(); // e.g. Promise<EntityResponse<User>>
|
|
192
|
+
const responseSchema = resolveSchema(returnType, openApiSpec.components.schemas);
|
|
193
|
+
|
|
194
|
+
if (!openApiSpec.paths[fullPath]) openApiSpec.paths[fullPath] = {};
|
|
195
|
+
openApiSpec.paths[fullPath][httpMethod] = {
|
|
196
|
+
operationId: method.getName(),
|
|
197
|
+
parameters,
|
|
198
|
+
requestBody: Object.keys(requestBody.content).length ? requestBody : undefined,
|
|
199
|
+
security: isAuth ? [{ bearerAuth: [] }] : undefined,
|
|
200
|
+
responses: {
|
|
201
|
+
200: {
|
|
202
|
+
description: "Success",
|
|
203
|
+
content: {
|
|
204
|
+
"application/json": { schema: responseSchema }
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
console.log("🔍 Scanning...");
|
|
213
|
+
const sourceFiles = project.getSourceFiles("tests/example-app/**/*.ts");
|
|
214
|
+
sourceFiles.forEach(file => file.getClasses().forEach(processController));
|
|
215
|
+
fs.writeFileSync(OUTPUT_FILE, JSON.stringify(openApiSpec, null, 2));
|
|
216
|
+
console.log(`✅ Generated ${OUTPUT_FILE}`);
|
package/src/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
// src/index.ts
|
|
18
|
+
// Main entry point for adorn-api library
|
|
19
|
+
__exportStar(require("./lib/decorators.js"), exports);
|
|
20
|
+
__exportStar(require("./lib/common.js"), exports);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
exports.PaginationQuery = void 0;
|
|
38
|
+
// src/lib/common.ts
|
|
39
|
+
var decorators_js_1 = require("./decorators.js");
|
|
40
|
+
// --- 2. Pagination Logic ---
|
|
41
|
+
// We need a Concrete Class for Pagination because it carries Metadata (@FromQuery)
|
|
42
|
+
var PaginationQuery = function () {
|
|
43
|
+
var _a;
|
|
44
|
+
var _page_decorators;
|
|
45
|
+
var _page_initializers = [];
|
|
46
|
+
var _page_extraInitializers = [];
|
|
47
|
+
var _limit_decorators;
|
|
48
|
+
var _limit_initializers = [];
|
|
49
|
+
var _limit_extraInitializers = [];
|
|
50
|
+
return _a = /** @class */ (function () {
|
|
51
|
+
function PaginationQuery() {
|
|
52
|
+
this.page = __runInitializers(this, _page_initializers, 1);
|
|
53
|
+
this.limit = (__runInitializers(this, _page_extraInitializers), __runInitializers(this, _limit_initializers, 20));
|
|
54
|
+
__runInitializers(this, _limit_extraInitializers);
|
|
55
|
+
}
|
|
56
|
+
return PaginationQuery;
|
|
57
|
+
}()),
|
|
58
|
+
(function () {
|
|
59
|
+
var _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
60
|
+
_page_decorators = [(0, decorators_js_1.FromQuery)()];
|
|
61
|
+
_limit_decorators = [(0, decorators_js_1.FromQuery)()];
|
|
62
|
+
__esDecorate(null, null, _page_decorators, { kind: "field", name: "page", static: false, private: false, access: { has: function (obj) { return "page" in obj; }, get: function (obj) { return obj.page; }, set: function (obj, value) { obj.page = value; } }, metadata: _metadata }, _page_initializers, _page_extraInitializers);
|
|
63
|
+
__esDecorate(null, null, _limit_decorators, { kind: "field", name: "limit", static: false, private: false, access: { has: function (obj) { return "limit" in obj; }, get: function (obj) { return obj.limit; }, set: function (obj, value) { obj.limit = value; } }, metadata: _metadata }, _limit_initializers, _limit_extraInitializers);
|
|
64
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
65
|
+
})(),
|
|
66
|
+
_a;
|
|
67
|
+
}();
|
|
68
|
+
exports.PaginationQuery = PaginationQuery;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/lib/common.ts
|
|
2
|
+
import { FromQuery } from "./decorators.js";
|
|
3
|
+
|
|
4
|
+
// --- 1. Generic Response Wrappers ---
|
|
5
|
+
|
|
6
|
+
// Helper to clean up intersection types in tooltips/logs
|
|
7
|
+
export type Jsonify<T> = { [K in keyof T]: T[K] } & {};
|
|
8
|
+
|
|
9
|
+
export type EntityResponse<TEntity> = Jsonify<TEntity>;
|
|
10
|
+
|
|
11
|
+
// --- 2. Pagination Logic ---
|
|
12
|
+
|
|
13
|
+
// We need a Concrete Class for Pagination because it carries Metadata (@FromQuery)
|
|
14
|
+
export class PaginationQuery {
|
|
15
|
+
@FromQuery()
|
|
16
|
+
page: number = 1;
|
|
17
|
+
|
|
18
|
+
@FromQuery()
|
|
19
|
+
limit: number = 20;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// The generic type for list queries
|
|
23
|
+
export type ListQuery<TFilters extends object = object> = PaginationQuery & TFilters;
|
|
24
|
+
|
|
25
|
+
// --- 3. CRUD Input Helpers ---
|
|
26
|
+
// Note: You cannot "extend" these Mapped Types in a class directly in TS.
|
|
27
|
+
// These are best used for Type Checking interfaces or Return Types.
|
|
28
|
+
|
|
29
|
+
export type CreateInput<
|
|
30
|
+
TEntityJson extends object,
|
|
31
|
+
TRequiredKeys extends keyof TEntityJson,
|
|
32
|
+
TOptionalKeys extends keyof TEntityJson = never,
|
|
33
|
+
> = Pick<TEntityJson, TRequiredKeys> & Partial<Pick<TEntityJson, TOptionalKeys>>;
|
|
34
|
+
|
|
35
|
+
export type UpdateInput<TCreateInput> = Partial<TCreateInput>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// src/lib/decorators.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AUTH_META = exports.SCHEMA_META = void 0;
|
|
5
|
+
exports.Get = Get;
|
|
6
|
+
exports.Post = Post;
|
|
7
|
+
exports.Put = Put;
|
|
8
|
+
exports.Delete = Delete;
|
|
9
|
+
exports.Controller = Controller;
|
|
10
|
+
exports.FromQuery = FromQuery;
|
|
11
|
+
exports.FromPath = FromPath;
|
|
12
|
+
exports.FromBody = FromBody;
|
|
13
|
+
exports.Authorized = Authorized;
|
|
14
|
+
// Context metadata symbol for standard decorators
|
|
15
|
+
var META_KEY = Symbol('adorn:route');
|
|
16
|
+
exports.SCHEMA_META = Symbol('adorn:schema');
|
|
17
|
+
exports.AUTH_META = Symbol('adorn:auth');
|
|
18
|
+
// -- Method Decorator --
|
|
19
|
+
// Using Standard TC39 Signature
|
|
20
|
+
function Get(path) {
|
|
21
|
+
return function (originalMethod, context) {
|
|
22
|
+
// In standard decorators, we can attach metadata to the class prototype via context
|
|
23
|
+
context.addInitializer(function () {
|
|
24
|
+
var routes = this[META_KEY] || [];
|
|
25
|
+
routes.push({
|
|
26
|
+
method: 'get',
|
|
27
|
+
path: path,
|
|
28
|
+
methodName: String(context.name),
|
|
29
|
+
});
|
|
30
|
+
this[META_KEY] = routes;
|
|
31
|
+
});
|
|
32
|
+
return originalMethod;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function Post(path) {
|
|
36
|
+
return function (originalMethod, context) {
|
|
37
|
+
context.addInitializer(function () {
|
|
38
|
+
var routes = this[META_KEY] || [];
|
|
39
|
+
routes.push({
|
|
40
|
+
method: 'post',
|
|
41
|
+
path: path,
|
|
42
|
+
methodName: String(context.name),
|
|
43
|
+
});
|
|
44
|
+
this[META_KEY] = routes;
|
|
45
|
+
});
|
|
46
|
+
return originalMethod;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function Put(path) {
|
|
50
|
+
return function (originalMethod, context) {
|
|
51
|
+
context.addInitializer(function () {
|
|
52
|
+
var routes = this[META_KEY] || [];
|
|
53
|
+
routes.push({
|
|
54
|
+
method: 'put',
|
|
55
|
+
path: path,
|
|
56
|
+
methodName: String(context.name),
|
|
57
|
+
});
|
|
58
|
+
this[META_KEY] = routes;
|
|
59
|
+
});
|
|
60
|
+
return originalMethod;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function Delete(path) {
|
|
64
|
+
return function (originalMethod, context) {
|
|
65
|
+
context.addInitializer(function () {
|
|
66
|
+
var routes = this[META_KEY] || [];
|
|
67
|
+
routes.push({
|
|
68
|
+
method: 'delete',
|
|
69
|
+
path: path,
|
|
70
|
+
methodName: String(context.name),
|
|
71
|
+
});
|
|
72
|
+
this[META_KEY] = routes;
|
|
73
|
+
});
|
|
74
|
+
return originalMethod;
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// -- Class Decorator --
|
|
78
|
+
function Controller(basePath) {
|
|
79
|
+
return function (target, context) {
|
|
80
|
+
// We attach the base path to the class constructor
|
|
81
|
+
context.addInitializer(function () {
|
|
82
|
+
this._basePath = basePath;
|
|
83
|
+
});
|
|
84
|
+
return target;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// -- DTO Field Decorators --
|
|
88
|
+
// Since we can't decorate parameters, we decorate fields in a class
|
|
89
|
+
// e.g. class GetUserParams { @FromPath id: string }
|
|
90
|
+
function FromQuery(name) {
|
|
91
|
+
return function (target, context) {
|
|
92
|
+
context.addInitializer(function () {
|
|
93
|
+
var meta = this[exports.SCHEMA_META] || {};
|
|
94
|
+
meta[context.name] = { type: 'query' };
|
|
95
|
+
this[exports.SCHEMA_META] = meta;
|
|
96
|
+
});
|
|
97
|
+
return function (initialValue) { return initialValue; };
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
function FromPath(name) {
|
|
101
|
+
return function (target, context) {
|
|
102
|
+
context.addInitializer(function () {
|
|
103
|
+
var meta = this[exports.SCHEMA_META] || {};
|
|
104
|
+
meta[context.name] = { type: 'path' };
|
|
105
|
+
this[exports.SCHEMA_META] = meta;
|
|
106
|
+
});
|
|
107
|
+
return function (initialValue) { return initialValue; };
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function FromBody() {
|
|
111
|
+
return function (target, context) {
|
|
112
|
+
context.addInitializer(function () {
|
|
113
|
+
var meta = this[exports.SCHEMA_META] || {};
|
|
114
|
+
meta[context.name] = { type: 'body' };
|
|
115
|
+
this[exports.SCHEMA_META] = meta;
|
|
116
|
+
});
|
|
117
|
+
return function (initialValue) { return initialValue; };
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// -- Authentication Decorator --
|
|
121
|
+
function Authorized(role) {
|
|
122
|
+
return function (target, context) {
|
|
123
|
+
context.addInitializer(function () {
|
|
124
|
+
this[exports.AUTH_META] = role || 'default';
|
|
125
|
+
});
|
|
126
|
+
return target;
|
|
127
|
+
};
|
|
128
|
+
}
|