raiton 4.0.0 → 5.4.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -0
- package/README.md +37 -2
- package/build/bin/index.mjs +568 -275
- package/examples/user-controller.example.ts +53 -0
- package/package.json +6 -8
- package/source/bin/cli-tools.ts +2 -0
- package/source/commands/build.command.ts +2 -1
- package/source/commands/develop.command.ts +1 -0
- package/source/commands/start.command.ts +15 -10
- package/source/core/application.ts +40 -31
- package/source/core/artifacts/artifact.ts +10 -0
- package/source/core/artifacts/index.ts +1 -0
- package/source/core/artifacts/runner.ts +3 -0
- package/source/core/builder.ts +42 -13
- package/source/core/hooks.ts +0 -8
- package/source/core/index.ts +15 -15
- package/source/core/injection/injection.ts +7 -4
- package/source/core/middleware/pipeline.ts +2 -8
- package/source/core/plugins/scope.ts +18 -6
- package/source/core/router/handler.ts +24 -78
- package/source/core/router/handler.utils.ts +135 -0
- package/source/core/router/matcher.ts +13 -9
- package/source/core/router/router.ts +5 -1
- package/source/core/thread.ts +19 -16
- package/source/framework/artifacts.ts +7 -1
- package/source/framework/constants/decorators.constant.ts +7 -0
- package/source/framework/decorators/api-response.decorator.ts +127 -0
- package/source/framework/decorators/api.decorator.ts +185 -0
- package/source/framework/decorators/index.ts +4 -2
- package/source/framework/decorators/{parametrable.ts → parametrable.decorator.ts} +1 -1
- package/source/framework/encryption.ts +2 -1
- package/source/framework/enums/encrypted.enum.ts +2 -2
- package/source/framework/enums/http-parameters.enum.ts +1 -0
- package/source/framework/enums/runtime.enum.ts +2 -2
- package/source/framework/env.ts +2 -2
- package/source/framework/exceptions/http-exception.ts +1 -1
- package/source/framework/index.ts +1 -0
- package/source/framework/plugins/body-parser.plugin.ts +28 -11
- package/source/framework/plugins/index.ts +3 -2
- package/source/framework/plugins/openapi.plugin.ts +98 -0
- package/source/framework/plugins/security/cors.ts +59 -25
- package/source/framework/responses/helpers.ts +4 -3
- package/source/framework/responses/http-throwable.ts +2 -2
- package/source/framework/responses/http.ts +3 -3
- package/source/framework/runtime/bun/server.ts +6 -2
- package/source/framework/runtime/deno/server.ts +93 -42
- package/source/framework/runtime/index.ts +18 -13
- package/source/framework/runtime/node/reply.ts +37 -0
- package/source/framework/runtime/node/request.ts +47 -0
- package/source/framework/runtime/node/server.ts +5 -33
- package/source/framework/runtime/web/server.ts +73 -57
- package/source/framework/utilities/cookie.util.ts +11 -0
- package/source/framework/utilities/index.ts +3 -1
- package/source/framework/utilities/openapi.utils.ts +497 -0
- package/source/framework/view-model.ts +4 -0
- package/source/types/access-guards.ts +4 -0
- package/source/types/artifact.ts +42 -1
- package/source/types/builder.ts +6 -1
- package/source/types/http-responses.ts +8 -0
- package/source/types/runtime.ts +4 -2
- package/source/types/scheme.ts +153 -0
- package/source/types/thread.ts +3 -1
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {MiddlewareCallable, ParamMetaInterface, RouteMetaInterface} from "../../types";
|
|
2
|
+
import {middlewareCompose} from "../../core";
|
|
3
|
+
import {validate} from "class-validator";
|
|
4
|
+
import {parseCookie} from "@/framework/utilities/cookie.util";
|
|
5
|
+
import {DataTransferObject, METADATA_KEYS, Parametrable, Throwable, ViewModel} from "@/framework";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Helper function for collecting arguments from route parameters
|
|
9
|
+
* @param instance
|
|
10
|
+
* @param routeMeta
|
|
11
|
+
* @param ctx
|
|
12
|
+
*/
|
|
13
|
+
export function collectRouteArguments(instance: any, routeMeta: RouteMetaInterface, ctx: any): any[] {
|
|
14
|
+
const args: any[] = [];
|
|
15
|
+
const params: ParamMetaInterface[] = Reflect.getMetadata(METADATA_KEYS.ROUTE_PARAMETERS, instance.constructor)?.[routeMeta.propertyKey] || [];
|
|
16
|
+
|
|
17
|
+
for (const p of params) {
|
|
18
|
+
switch (p.type) {
|
|
19
|
+
case Parametrable.QUERY:
|
|
20
|
+
args[p.index] = ctx.req.query?.[p.key!]
|
|
21
|
+
break;
|
|
22
|
+
case Parametrable.PARAM:
|
|
23
|
+
args[p.index] = ctx.req.params?.[p.key!] ?? ctx.params?.[p.key!]
|
|
24
|
+
break;
|
|
25
|
+
case Parametrable.BODY:
|
|
26
|
+
if (p.metatype && 'prototype' in p.metatype) {
|
|
27
|
+
ctx.req.body = new p.metatype(ctx.req.body)
|
|
28
|
+
}
|
|
29
|
+
args[p.index] = ctx.req.body
|
|
30
|
+
break;
|
|
31
|
+
case Parametrable.HEADER:
|
|
32
|
+
args[p.index] = ctx.req.headers[p.key!.toLowerCase()] as any;
|
|
33
|
+
break;
|
|
34
|
+
case Parametrable.COOKIE:
|
|
35
|
+
const cookieHeader = ctx.req.headers.get('cookie');
|
|
36
|
+
const cookies = parseCookie(cookieHeader);
|
|
37
|
+
args[p.index] = p.key ? cookies[p.key!] : cookies;
|
|
38
|
+
break;
|
|
39
|
+
case Parametrable.REQ:
|
|
40
|
+
args[p.index] = ctx.req
|
|
41
|
+
break;
|
|
42
|
+
case Parametrable.REPLY:
|
|
43
|
+
args[p.index] = ctx.REPLY
|
|
44
|
+
break;
|
|
45
|
+
case Parametrable.UPLOAD_FILE:
|
|
46
|
+
args[p.index] = ctx.req.file || ctx.req.files?.[p.key!]
|
|
47
|
+
break;
|
|
48
|
+
case Parametrable.CUSTOM:
|
|
49
|
+
args[p.index] = p.callable?.(ctx) ?? null;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return args;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Helper function for validating DTO arguments
|
|
59
|
+
* @param args
|
|
60
|
+
*/
|
|
61
|
+
export async function validateDtoArguments(args: any[]): Promise<void> {
|
|
62
|
+
for (const input of args) {
|
|
63
|
+
if (input instanceof DataTransferObject) {
|
|
64
|
+
const errors = await input.validation(false);
|
|
65
|
+
if (errors && errors.length) {
|
|
66
|
+
const stack = Object.values(errors[0].constraints || {});
|
|
67
|
+
throw new Throwable(stack.join(';'), 500);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
*
|
|
75
|
+
* @param responses
|
|
76
|
+
* @param instance
|
|
77
|
+
* @param routeMeta
|
|
78
|
+
*/
|
|
79
|
+
export async function validateResponse(responses: any, instance: any, routeMeta: RouteMetaInterface): Promise<void> {
|
|
80
|
+
// Special handling for ViewModel response wrapping: { data: viewModelInstance }
|
|
81
|
+
if (responses && typeof responses === 'object' && 'data' in responses) {
|
|
82
|
+
const data = (responses as any).data;
|
|
83
|
+
if (data instanceof ViewModel) {
|
|
84
|
+
const ViewModelClass = data.constructor as new (init: any) => ViewModel;
|
|
85
|
+
const validationTarget = new ViewModelClass(data['__initial__']);
|
|
86
|
+
const errors = await validate(validationTarget);
|
|
87
|
+
if (errors.length > 0) {
|
|
88
|
+
const errorMessages = errors.map(err =>
|
|
89
|
+
Object.values(err.constraints || {}).join(', ')
|
|
90
|
+
).join('; ');
|
|
91
|
+
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Validate response based on @ApiResponse metadata
|
|
97
|
+
const apiResponses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, instance.constructor)?.[routeMeta.propertyKey] || [];
|
|
98
|
+
const successResponse = apiResponses.find((r: any) => r.status === 200) || apiResponses[0]; // Prefer 200, fallback to first
|
|
99
|
+
|
|
100
|
+
if (successResponse && successResponse.type) {
|
|
101
|
+
// If it's supposed to be an array, validate each element
|
|
102
|
+
if (successResponse.isArray && Array.isArray(responses)) {
|
|
103
|
+
for (const item of responses) {
|
|
104
|
+
const validationTarget = typeof successResponse.type === 'function' && 'prototype' in successResponse.type
|
|
105
|
+
? new successResponse.type(item)
|
|
106
|
+
: item;
|
|
107
|
+
const errors = await validate(validationTarget);
|
|
108
|
+
if (errors.length > 0) {
|
|
109
|
+
const errorMessages = errors.map(err =>
|
|
110
|
+
Object.values(err.constraints || {}).join(', ')
|
|
111
|
+
).join('; ');
|
|
112
|
+
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
const validationTarget = typeof successResponse.type === 'function' && 'prototype' in successResponse.type
|
|
117
|
+
? new successResponse.type(responses)
|
|
118
|
+
: responses;
|
|
119
|
+
const errors = await validate(validationTarget);
|
|
120
|
+
if (errors.length > 0) {
|
|
121
|
+
const errorMessages = errors.map(err =>
|
|
122
|
+
Object.values(err.constraints || {}).join(', ')
|
|
123
|
+
).join('; ');
|
|
124
|
+
throw new Throwable(`Response validation failed: ${errorMessages}`, 500);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Helper function for running middlewares
|
|
131
|
+
export async function runMiddlewares(middlewares: MiddlewareCallable[], ctx: any): Promise<void> {
|
|
132
|
+
if (middlewares.length > 0) {
|
|
133
|
+
await middlewareCompose(middlewares)(ctx);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
@@ -1,51 +1,55 @@
|
|
|
1
1
|
import {Route} from '.'
|
|
2
2
|
|
|
3
3
|
export class RouteMatcher {
|
|
4
|
-
protected
|
|
4
|
+
protected _routes = new Map<string, Route>()
|
|
5
5
|
|
|
6
6
|
protected getKey(route: Route): string {
|
|
7
7
|
return `${route.method}:${route.path}`
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
get routes(): Map<string, Route> {
|
|
11
|
+
return this._routes
|
|
12
|
+
}
|
|
13
|
+
|
|
10
14
|
add(route: Route): this {
|
|
11
|
-
this.
|
|
15
|
+
this._routes.set(this.getKey(route), route)
|
|
12
16
|
return this;
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
has(route: Route): boolean {
|
|
16
|
-
return this.
|
|
20
|
+
return this._routes.has(this.getKey(route))
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
find(route: Route): Route | undefined {
|
|
20
|
-
return this.
|
|
24
|
+
return this._routes.get(this.getKey(route))
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
replace(route: Route): this {
|
|
24
28
|
if (this.has(route)) {
|
|
25
|
-
this.
|
|
29
|
+
this._routes.set(this.getKey(route), route)
|
|
26
30
|
}
|
|
27
31
|
return this;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
upsert(route: Route): this {
|
|
31
|
-
this.
|
|
35
|
+
this._routes.set(this.getKey(route), route)
|
|
32
36
|
return this
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
remove(route: Route): this {
|
|
36
|
-
this.
|
|
40
|
+
this._routes.delete(this.getKey(route))
|
|
37
41
|
return this;
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
match(method: string, url: string): Route | null {
|
|
41
|
-
for (const route of this.
|
|
45
|
+
for (const route of this._routes.values())
|
|
42
46
|
if (route.match(method, url))
|
|
43
47
|
return route.extractParams(url)
|
|
44
48
|
return null
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
clear(): this {
|
|
48
|
-
this.
|
|
52
|
+
this._routes.clear()
|
|
49
53
|
return this;
|
|
50
54
|
}
|
|
51
55
|
}
|
package/source/core/thread.ts
CHANGED
|
@@ -81,33 +81,36 @@ export class RaitonThread implements ThreadInterface {
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
async run(): Promise<this> {
|
|
84
|
-
|
|
85
84
|
if (!this.application)
|
|
86
85
|
throw new Throwable('Application not defined');
|
|
87
86
|
|
|
88
87
|
if (!this.runtime)
|
|
89
88
|
throw new Throwable('Runtime not defined');
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
await this.stop();
|
|
93
|
-
});
|
|
90
|
+
if (this.builder.source) await ControllerBuilder.scan(this.builder.source)
|
|
94
91
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
92
|
+
if (this._options.serve) {
|
|
93
|
+
process.on('SIGINT', async () => {
|
|
94
|
+
await this.stop();
|
|
95
|
+
});
|
|
98
96
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const prefix = this.application.config.prefix
|
|
97
|
+
process.on('SIGTERM', async () => {
|
|
98
|
+
await this.stop();
|
|
99
|
+
});
|
|
103
100
|
|
|
104
|
-
|
|
101
|
+
const port = this.application.config.port || 5712;
|
|
102
|
+
const hostname = this.application.config.hostname || '0.0.0.0';
|
|
103
|
+
const displayHostname = (hostname === '0.0.0.0') ? (this.getNetworkIp() || 'localhost') : hostname;
|
|
104
|
+
const prefix = this.application.config.prefix
|
|
105
105
|
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
this.runtimeServer = this.runtime.createServer(this.application.handle.bind(this.application))
|
|
107
|
+
|
|
108
|
+
await this.runtimeServer.listen(port, hostname)
|
|
109
|
+
|
|
110
|
+
Logger.log(LBadge.info('Local access:'), `http://localhost:${port}${prefix ?? ''}`,)
|
|
111
|
+
Logger.log(LBadge.info('LAN access:'), `http://${displayHostname}:${port}${prefix ?? ''}`,)
|
|
112
|
+
}
|
|
108
113
|
|
|
109
|
-
Logger.log(LBadge.info('Local access:'), `http://localhost:${port}${prefix ?? ''}`,)
|
|
110
|
-
Logger.log(LBadge.info('LAN access:'), `http://${displayHostname}:${port}${prefix ?? ''}`,)
|
|
111
114
|
return this;
|
|
112
115
|
}
|
|
113
116
|
}
|
|
@@ -12,6 +12,7 @@ export class Artifacts {
|
|
|
12
12
|
'service',
|
|
13
13
|
'provider',
|
|
14
14
|
'type',
|
|
15
|
+
'interface',
|
|
15
16
|
'repository',
|
|
16
17
|
'database',
|
|
17
18
|
'db',
|
|
@@ -28,6 +29,11 @@ export class Artifacts {
|
|
|
28
29
|
'strategy',
|
|
29
30
|
'strategy-provider',
|
|
30
31
|
'strategy-type',
|
|
32
|
+
'use-case',
|
|
33
|
+
'dto',
|
|
34
|
+
'entity',
|
|
35
|
+
'model',
|
|
36
|
+
'schema',
|
|
31
37
|
]
|
|
32
38
|
|
|
33
39
|
static register(type: string) {
|
|
@@ -48,7 +54,7 @@ export class Artifacts {
|
|
|
48
54
|
|
|
49
55
|
static reload(modulo: any, filename?: string) {
|
|
50
56
|
|
|
51
|
-
if (Raiton.thread?.builder.options.
|
|
57
|
+
if (Raiton.thread?.builder.options.serve === false)
|
|
52
58
|
return Logger.warn(
|
|
53
59
|
'Artifact reload is only available in development mode'
|
|
54
60
|
)
|
|
@@ -7,4 +7,11 @@ export const METADATA_KEYS = {
|
|
|
7
7
|
CONTAINER: Symbol('container:meta'),
|
|
8
8
|
INJECT_PARAMETERS: Symbol('inject:parameters'),
|
|
9
9
|
INJECT_PROPERTIES: Symbol('inject:properties'),
|
|
10
|
+
// OpenAPI metadata keys
|
|
11
|
+
API_RESPONSES: Symbol('api:responses'),
|
|
12
|
+
API_REQUEST_BODY: Symbol('api:requestBody'),
|
|
13
|
+
API_PARAMETERS: Symbol('api:parameters'),
|
|
14
|
+
API_TAGS: Symbol('api:tags'),
|
|
15
|
+
API_OPERATION: Symbol('api:operation'),
|
|
16
|
+
API_SECURITY: Symbol('api:security'),
|
|
10
17
|
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {METADATA_KEYS} from "../constants/decorators.constant";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Response definition for OpenAPI
|
|
6
|
+
*/
|
|
7
|
+
export interface ApiResponseOptions {
|
|
8
|
+
/**
|
|
9
|
+
* HTTP status code
|
|
10
|
+
*/
|
|
11
|
+
statusCode: number;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Description of the response
|
|
15
|
+
*/
|
|
16
|
+
description?: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type/schema of the response (optional)
|
|
20
|
+
*/
|
|
21
|
+
type?: any;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Whether this is the default response
|
|
25
|
+
*/
|
|
26
|
+
isArray?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Decorator to add API response metadata to a route handler
|
|
31
|
+
* @param options Response options (status, description, type, etc.)
|
|
32
|
+
*/
|
|
33
|
+
export function ApiResponse(options: ApiResponseOptions) {
|
|
34
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
35
|
+
const responses = Reflect.getMetadata(METADATA_KEYS.API_RESPONSES, target) || {};
|
|
36
|
+
if (!responses[propertyKey]) {
|
|
37
|
+
responses[propertyKey] = [];
|
|
38
|
+
}
|
|
39
|
+
responses[propertyKey].push(options);
|
|
40
|
+
Reflect.defineMetadata(METADATA_KEYS.API_RESPONSES, responses, target);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Shorthand decorator for 200 OK response
|
|
46
|
+
* @param description Response description
|
|
47
|
+
* @param type Response type (optional)
|
|
48
|
+
* @param isArray Whether the response is an array (optional)
|
|
49
|
+
*/
|
|
50
|
+
export function ApiOkResponse(description?: string, type?: any, isArray?: boolean) {
|
|
51
|
+
return ApiResponse({
|
|
52
|
+
statusCode: 200,
|
|
53
|
+
description,
|
|
54
|
+
type,
|
|
55
|
+
isArray
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Shorthand decorator for 201 Created response
|
|
61
|
+
* @param description Response description
|
|
62
|
+
* @param type Response type (optional)
|
|
63
|
+
* @param isArray Whether the response is an array (optional)
|
|
64
|
+
*/
|
|
65
|
+
export function ApiCreatedResponse(description?: string, type?: any, isArray?: boolean) {
|
|
66
|
+
return ApiResponse({
|
|
67
|
+
statusCode: 201,
|
|
68
|
+
description,
|
|
69
|
+
type,
|
|
70
|
+
isArray
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Shorthand decorator for 400 Bad Request response
|
|
76
|
+
* @param description Response description
|
|
77
|
+
*/
|
|
78
|
+
export function ApiBadRequestResponse(description?: string) {
|
|
79
|
+
return ApiResponse({
|
|
80
|
+
statusCode: 400,
|
|
81
|
+
description
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Shorthand decorator for 401 Unauthorized response
|
|
87
|
+
* @param description Response description
|
|
88
|
+
*/
|
|
89
|
+
export function ApiUnauthorizedResponse(description?: string) {
|
|
90
|
+
return ApiResponse({
|
|
91
|
+
statusCode: 401,
|
|
92
|
+
description
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Shorthand decorator for 403 Forbidden response
|
|
98
|
+
* @param description Response description
|
|
99
|
+
*/
|
|
100
|
+
export function ApiForbiddenResponse(description?: string) {
|
|
101
|
+
return ApiResponse({
|
|
102
|
+
statusCode: 403,
|
|
103
|
+
description
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Shorthand decorator for 404 Not Found response
|
|
109
|
+
* @param description Response description
|
|
110
|
+
*/
|
|
111
|
+
export function ApiNotFoundResponse(description?: string) {
|
|
112
|
+
return ApiResponse({
|
|
113
|
+
statusCode: 404,
|
|
114
|
+
description
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Shorthand decorator for 500 Internal Server Error response
|
|
120
|
+
* @param description Response description
|
|
121
|
+
*/
|
|
122
|
+
export function ApiInternalServerErrorResponse(description?: string) {
|
|
123
|
+
return ApiResponse({
|
|
124
|
+
statusCode: 500,
|
|
125
|
+
description
|
|
126
|
+
});
|
|
127
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {METADATA_KEYS} from "../constants/decorators.constant";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Tag metadata for OpenAPI grouping
|
|
6
|
+
*/
|
|
7
|
+
export interface ApiTagOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Tag name
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Tag description (optional)
|
|
15
|
+
*/
|
|
16
|
+
description?: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* External documentation URL (optional)
|
|
20
|
+
*/
|
|
21
|
+
externalDocs?: {
|
|
22
|
+
description?: string;
|
|
23
|
+
url: string;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Decorator to add API tags to a controller or method
|
|
29
|
+
* @param tags Tag(s) to apply
|
|
30
|
+
*/
|
|
31
|
+
export function ApiTags(...tags: (string | ApiTagOptions)[]) {
|
|
32
|
+
return function (target: any, propertyKey?: string | symbol) {
|
|
33
|
+
const tagsMetadata = Reflect.getMetadata(METADATA_KEYS.API_TAGS, target) || [];
|
|
34
|
+
|
|
35
|
+
// Convert string tags to objects
|
|
36
|
+
const processedTags = tags.map(tag =>
|
|
37
|
+
typeof tag === 'string' ? { name: tag } : tag
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (propertyKey) {
|
|
41
|
+
// Method-level decorator
|
|
42
|
+
const methodTags = Reflect.getMetadata(METADATA_KEYS.API_TAGS, target, propertyKey) || [];
|
|
43
|
+
Reflect.defineMetadata(METADATA_KEYS.API_TAGS, [...methodTags, ...processedTags], target, propertyKey);
|
|
44
|
+
} else {
|
|
45
|
+
// Class-level decorator
|
|
46
|
+
Reflect.defineMetadata(METADATA_KEYS.API_TAGS, [...tagsMetadata, ...processedTags], target);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Operation metadata for OpenAPI
|
|
53
|
+
*/
|
|
54
|
+
export interface ApiOperationOptions {
|
|
55
|
+
/**
|
|
56
|
+
* Operation summary
|
|
57
|
+
*/
|
|
58
|
+
summary?: string;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Operation description
|
|
62
|
+
*/
|
|
63
|
+
description?: string;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Operation ID (unique identifier)
|
|
67
|
+
*/
|
|
68
|
+
operationId?: string;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Whether the operation is deprecated
|
|
72
|
+
*/
|
|
73
|
+
deprecated?: boolean;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* External documentation
|
|
77
|
+
*/
|
|
78
|
+
externalDocs?: {
|
|
79
|
+
description?: string;
|
|
80
|
+
url: string;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Decorator to add operation metadata to a method
|
|
86
|
+
* @param options Operation options
|
|
87
|
+
*/
|
|
88
|
+
export function ApiOperation(options: ApiOperationOptions = {}) {
|
|
89
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
90
|
+
const existing = Reflect.getMetadata(METADATA_KEYS.API_OPERATION, target, propertyKey) || {};
|
|
91
|
+
Reflect.defineMetadata(METADATA_KEYS.API_OPERATION, {...existing, ...options}, target, propertyKey);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Security requirements for OpenAPI
|
|
97
|
+
*/
|
|
98
|
+
export interface ApiSecurityOptions {
|
|
99
|
+
/**
|
|
100
|
+
* Security scheme name
|
|
101
|
+
*/
|
|
102
|
+
[key: string]: string[] | undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Decorator to add security requirements to a controller or method
|
|
107
|
+
* @param security Security requirements (scheme name -> scopes[])
|
|
108
|
+
*/
|
|
109
|
+
export function ApiSecurity(...security: ApiSecurityOptions[]) {
|
|
110
|
+
return function (target: any, propertyKey?: string | symbol) {
|
|
111
|
+
const securities = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, target) || [];
|
|
112
|
+
|
|
113
|
+
if (propertyKey) {
|
|
114
|
+
// Method-level decorator
|
|
115
|
+
const methodSecurities = Reflect.getMetadata(METADATA_KEYS.API_SECURITY, target, propertyKey) || [];
|
|
116
|
+
Reflect.defineMetadata(METADATA_KEYS.API_SECURITY, [...methodSecurities, ...security], target, propertyKey);
|
|
117
|
+
} else {
|
|
118
|
+
// Class-level decorator
|
|
119
|
+
Reflect.defineMetadata(METADATA_KEYS.API_SECURITY, [...securities, ...security], target);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Decorator to define request body for a method (alternative to using type detection)
|
|
126
|
+
* @param type The type/schema of the request body
|
|
127
|
+
* @param description Description of the request body (optional)
|
|
128
|
+
* @param required Whether the request body is required (default: true)
|
|
129
|
+
*/
|
|
130
|
+
export function ApiBody(type: any, description?: string, required: boolean = true) {
|
|
131
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
132
|
+
Reflect.defineMetadata(METADATA_KEYS.API_REQUEST_BODY, {
|
|
133
|
+
type,
|
|
134
|
+
description,
|
|
135
|
+
required
|
|
136
|
+
}, target, propertyKey);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Decorator to add additional parameters (complements Parametrable decorators)
|
|
142
|
+
* @param name Parameter name
|
|
143
|
+
* @param options Parameter options
|
|
144
|
+
*/
|
|
145
|
+
export function ApiParam(name: string, options: {
|
|
146
|
+
/**
|
|
147
|
+
* Parameter location (path, query, header, cookie)
|
|
148
|
+
*/
|
|
149
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parameter description
|
|
153
|
+
*/
|
|
154
|
+
description?: string;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Whether the parameter is required
|
|
158
|
+
*/
|
|
159
|
+
required?: boolean;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Parameter type (for schema generation)
|
|
163
|
+
*/
|
|
164
|
+
type?: any;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Default value (optional)
|
|
168
|
+
*/
|
|
169
|
+
default?: any;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Whether this parameter should be excluded from documentation (deprecated)
|
|
173
|
+
* @deprecated
|
|
174
|
+
*/
|
|
175
|
+
deprecated?: boolean;
|
|
176
|
+
}) {
|
|
177
|
+
return function (target: any, propertyKey: string | symbol) {
|
|
178
|
+
const params = Reflect.getMetadata(METADATA_KEYS.API_PARAMETERS, target) || {};
|
|
179
|
+
if (!params[propertyKey]) {
|
|
180
|
+
params[propertyKey] = [];
|
|
181
|
+
}
|
|
182
|
+
params[propertyKey].push({name, ...options});
|
|
183
|
+
Reflect.defineMetadata(METADATA_KEYS.API_PARAMETERS, params, target, propertyKey);
|
|
184
|
+
};
|
|
185
|
+
}
|
|
@@ -2,6 +2,8 @@ export * from "./controllable.decorator"
|
|
|
2
2
|
export * from "./socket.decorator"
|
|
3
3
|
export * from "./middleware.decorator"
|
|
4
4
|
export * from "./routable.decorator"
|
|
5
|
-
export * from "./parametrable"
|
|
5
|
+
export * from "./parametrable.decorator"
|
|
6
6
|
export * from "./injection.decorator"
|
|
7
|
-
export * from "./guard.decorator"
|
|
7
|
+
export * from "./guard.decorator"
|
|
8
|
+
export * from "./api-response.decorator"
|
|
9
|
+
export * from "./api.decorator"
|
|
@@ -23,6 +23,6 @@ export const Param = createRouteParametrableDecorator(Parametrable.PARAM)
|
|
|
23
23
|
export const Body = createRouteParametrableDecorator(Parametrable.BODY)
|
|
24
24
|
export const UploadedFile = createRouteParametrableDecorator(Parametrable.UPLOAD_FILE)
|
|
25
25
|
export const Headers = createRouteParametrableDecorator(Parametrable.HEADER)
|
|
26
|
+
export const Cookie = createRouteParametrableDecorator(Parametrable.COOKIE);
|
|
26
27
|
export const Req = createRouteParametrableDecorator(Parametrable.REQ)
|
|
27
28
|
export const Reply = createRouteParametrableDecorator(Parametrable.REPLY)
|
|
28
|
-
|
|
@@ -21,7 +21,8 @@ export class Encryption {
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
async compare(value: string, hash: string): Promise<boolean> {
|
|
24
|
-
|
|
24
|
+
const result = await this.make(value)
|
|
25
|
+
return result === hash;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
async make(value: string, options?: DerivationOptionsInterface | ScryptOptionsInterface): Promise<EncryptionResultType> {
|