raiton 3.1.1 → 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 +38 -3
- package/build/bin/index.mjs +822 -524
- package/examples/user-controller.example.ts +53 -0
- package/package.json +8 -10
- package/scripts/update-version.ts +1 -1
- package/source/bin/bootstrapper.ts +1 -1
- package/source/bin/cli-tools.ts +2 -0
- package/source/commands/build.command.ts +2 -1
- package/source/commands/develop.command.ts +2 -1
- package/source/commands/start.command.ts +15 -10
- package/source/core/application.ts +42 -33
- 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 +44 -15
- package/source/core/config/define.ts +1 -1
- package/source/core/controller/builder.ts +1 -1
- package/source/core/controller/metadata.ts +1 -1
- package/source/core/hooks.ts +0 -8
- package/source/core/index.ts +15 -14
- package/source/core/injection/injection.ts +9 -6
- package/source/core/middleware/compose.ts +1 -1
- package/source/core/middleware/pipeline.ts +2 -8
- package/source/core/plugins/scope.ts +18 -6
- package/source/core/router/handler.ts +26 -80
- package/source/core/router/handler.utils.ts +135 -0
- package/source/core/router/matcher.ts +13 -9
- package/source/core/router/router.ts +6 -2
- package/source/core/socket/index.ts +1 -0
- package/source/core/socket/metadata.ts +13 -0
- package/source/core/thread.ts +23 -20
- package/source/{sdk → framework}/artifacts.ts +8 -1
- package/source/framework/constants/decorators.constant.ts +17 -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 +9 -0
- package/source/{sdk/decorators/parametrable.ts → framework/decorators/parametrable.decorator.ts} +1 -1
- package/source/framework/decorators/socket.decorator.ts +41 -0
- package/source/{sdk → framework}/encryption.ts +2 -1
- package/source/{sdk → framework}/enums/encrypted.enum.ts +2 -2
- package/source/{sdk → framework}/enums/http-parameters.enum.ts +1 -0
- package/source/{sdk → framework}/enums/runtime.enum.ts +2 -2
- package/source/{sdk → framework}/env.ts +2 -2
- package/source/{sdk → framework}/exceptions/http-exception.ts +1 -1
- package/source/{sdk → framework}/index.ts +3 -2
- package/source/{sdk → framework}/plugins/body-parser.plugin.ts +28 -11
- package/source/framework/plugins/index.ts +3 -0
- package/source/framework/plugins/openapi.plugin.ts +98 -0
- package/source/framework/plugins/security/cors.ts +78 -0
- package/source/{sdk → framework}/responses/helpers.ts +4 -3
- package/source/{sdk → framework}/responses/http-throwable.ts +2 -2
- package/source/{sdk → framework}/responses/http.ts +3 -3
- package/source/{sdk → framework}/runtime/bun/server.ts +6 -2
- package/source/framework/runtime/deno/server.ts +104 -0
- package/source/{sdk → 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 +32 -0
- package/source/framework/runtime/web/server.ts +73 -0
- package/source/{sdk → framework}/utilities/artifact.util.ts +3 -0
- package/source/framework/utilities/cookie.util.ts +11 -0
- package/source/{sdk → 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/application.ts +1 -1
- package/source/types/artifact.ts +42 -1
- package/source/types/builder.ts +6 -1
- package/source/types/controller.ts +1 -1
- package/source/types/http-responses.ts +8 -0
- package/source/types/index.ts +1 -0
- package/source/types/responses.ts +1 -1
- package/source/types/router.ts +1 -1
- package/source/types/runtime.ts +5 -3
- package/source/types/scheme.ts +153 -0
- package/source/types/socket.ts +15 -0
- package/source/types/thread.ts +4 -2
- package/source/sdk/constants/decorators.constant.ts +0 -8
- package/source/sdk/decorators/index.ts +0 -6
- package/source/sdk/plugins/index.ts +0 -2
- package/source/sdk/plugins/security/cors.ts +0 -44
- package/source/sdk/runtime/deno/server.ts +0 -53
- package/source/sdk/runtime/node/reply.ts +0 -0
- package/source/sdk/runtime/node/request.ts +0 -0
- package/source/sdk/runtime/node/server.ts +0 -60
- package/source/sdk/runtime/web/server.ts +0 -57
- /package/source/{sdk → framework}/constants/index.ts +0 -0
- /package/source/{sdk → framework}/constants/microservices.constant.ts +0 -0
- /package/source/{sdk → framework}/controllers.ts +0 -0
- /package/source/{sdk → framework}/data-transfer-object.ts +0 -0
- /package/source/{sdk → framework}/decorators/access-guard.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/controllable.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/guard.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/injection.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/middleware.decorator.ts +0 -0
- /package/source/{sdk → framework}/decorators/routable.decorator.ts +0 -0
- /package/source/{sdk → framework}/enums/event.message.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/http-method.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/http-status.enum.ts +0 -0
- /package/source/{sdk → framework}/enums/index.ts +0 -0
- /package/source/{sdk → framework}/enums/timestamp.enum.ts +0 -0
- /package/source/{sdk → framework}/exceptions/index.ts +0 -0
- /package/source/{sdk → framework}/exceptions/throwable.ts +0 -0
- /package/source/{sdk → framework}/parameter-bag.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/body-limit.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/headers.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/index.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/method-guard.ts +0 -0
- /package/source/{sdk → framework}/plugins/security/rate-limit.ts +0 -0
- /package/source/{sdk → framework}/repositories.ts +0 -0
- /package/source/{sdk → framework}/responses/error.ts +0 -0
- /package/source/{sdk → framework}/responses/index.ts +0 -0
- /package/source/{sdk → framework}/services.ts +0 -0
- /package/source/{sdk → framework}/utilities/alias-path.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/callable.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/json.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/path.util.ts +0 -0
- /package/source/{sdk → framework}/utilities/url.ts +0 -0
- /package/source/{sdk → framework}/utilities/utilities.util.ts +0 -0
|
@@ -1,89 +1,27 @@
|
|
|
1
|
-
import {ControllerMetaInterface,
|
|
2
|
-
import {METADATA_KEYS, Parametrable} from "../../sdk";
|
|
1
|
+
import {ControllerMetaInterface, RouteMetaInterface} from "../../types";
|
|
3
2
|
import {Logger} from "@protorians/logger";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {ThrowableResponse} from "../../sdk/responses/http-throwable";
|
|
3
|
+
import {Raiton} from "..";
|
|
4
|
+
import {HttpException} from "../../framework/exceptions";
|
|
5
|
+
import {ThrowableResponse} from "../../framework/responses/http-throwable";
|
|
6
|
+
import {collectRouteArguments, validateDtoArguments, validateResponse, runMiddlewares} from "./handler.utils";
|
|
9
7
|
|
|
10
8
|
export function createHandler(
|
|
11
9
|
instance: any,
|
|
12
10
|
routeMeta: RouteMetaInterface,
|
|
13
11
|
controllerMeta: ControllerMetaInterface,
|
|
14
12
|
) {
|
|
15
|
-
|
|
16
|
-
const isDevelopment = Raiton.thread?.builder?.options?.
|
|
13
|
+
const handler = async (ctx: any) => {
|
|
14
|
+
const isDevelopment = Raiton.thread?.builder?.options?.serve || false;
|
|
17
15
|
const handlerName = `${instance.constructor.name}.${routeMeta.propertyKey}`
|
|
18
16
|
|
|
19
17
|
try {
|
|
20
|
-
const args
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
args[p.index] =
|
|
28
|
-
ctx.req.query?.[p.key!]
|
|
29
|
-
break
|
|
30
|
-
case Parametrable.PARAM:
|
|
31
|
-
args[p.index] =
|
|
32
|
-
ctx.req.params?.[p.key!] ?? ctx.params?.[p.key!]
|
|
33
|
-
break
|
|
34
|
-
case Parametrable.BODY:
|
|
35
|
-
if (p.metatype && 'prototype' in p.metatype) {
|
|
36
|
-
ctx.req.body = new p.metatype(ctx.req.body)
|
|
37
|
-
}
|
|
38
|
-
args[p.index] = ctx.req.body
|
|
39
|
-
break
|
|
40
|
-
case Parametrable.HEADER:
|
|
41
|
-
args[p.index] = ctx.req.headers[p.key!.toLowerCase()] as any;
|
|
42
|
-
break
|
|
43
|
-
case Parametrable.REQ:
|
|
44
|
-
args[p.index] = ctx.req
|
|
45
|
-
break
|
|
46
|
-
case Parametrable.REPLY:
|
|
47
|
-
args[p.index] = ctx.REPLY
|
|
48
|
-
break
|
|
49
|
-
case Parametrable.UPLOAD_FILE:
|
|
50
|
-
args[p.index] = ctx.req.file || ctx.req.files?.[p.key!]
|
|
51
|
-
break
|
|
52
|
-
case Parametrable.CUSTOM:
|
|
53
|
-
args[p.index] = p.callable?.(ctx) ?? null;
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (!(routeMeta.propertyKey in instance))
|
|
59
|
-
throw new Throwable(`${routeMeta.propertyKey} does not exist`)
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Middlewares running order
|
|
63
|
-
*/
|
|
64
|
-
const middlewares: MiddlewareCallable[] = [
|
|
65
|
-
...controllerMeta.middlewares['@'] || [],
|
|
66
|
-
...controllerMeta.middlewares[routeMeta.propertyKey] || []
|
|
67
|
-
]
|
|
68
|
-
if (middlewares.length > 0) await middlewareCompose(middlewares)(ctx)
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Vérification des arguments
|
|
72
|
-
*/
|
|
73
|
-
for (const input of args) {
|
|
74
|
-
/** DTO validation */
|
|
75
|
-
if (input instanceof DataTransferObject) {
|
|
76
|
-
const errors = await input.validation(false);
|
|
77
|
-
if (errors && errors.length) {
|
|
78
|
-
const stack = Object.values(errors[0].constraints || {})
|
|
79
|
-
throw new Throwable(stack.join(';'), 500)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let responses = instance[routeMeta.propertyKey](...args)
|
|
85
|
-
if (responses instanceof Promise) responses = await responses
|
|
86
|
-
|
|
18
|
+
const args = collectRouteArguments(instance, routeMeta, ctx);
|
|
19
|
+
const middlewares = [...controllerMeta.middlewares['@'] || [], ...controllerMeta.middlewares[routeMeta.propertyKey] || []];
|
|
20
|
+
await runMiddlewares(middlewares, ctx);
|
|
21
|
+
await validateDtoArguments(args);
|
|
22
|
+
let responses = instance[routeMeta.propertyKey](...args);
|
|
23
|
+
if (responses instanceof Promise) responses = await responses;
|
|
24
|
+
await validateResponse(responses, instance, routeMeta);
|
|
87
25
|
return responses;
|
|
88
26
|
} catch (err: any) {
|
|
89
27
|
|
|
@@ -108,8 +46,16 @@ export function createHandler(
|
|
|
108
46
|
? (typeof err.stack === 'string' ? err.stack.split('\n')
|
|
109
47
|
: [String(err.stack || err.toString() || err.message || err.name || 'Unknown error')])
|
|
110
48
|
.map((l: any) => typeof l === 'string' ? l.trim() : String(l)) : undefined
|
|
111
|
-
|
|
112
|
-
}
|
|
49
|
+
};
|
|
113
50
|
}
|
|
114
|
-
}
|
|
115
|
-
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Attach metadata for OpenAPI generation
|
|
54
|
+
(handler as any)._raitonMeta = {
|
|
55
|
+
routeMeta,
|
|
56
|
+
controllerMeta,
|
|
57
|
+
controllerClass: instance.constructor
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
return handler;
|
|
61
|
+
}
|
|
@@ -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
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {RouteHandlerCallable} from '../../types'
|
|
2
|
-
import {HttpMethod} from "../../
|
|
2
|
+
import {HttpMethod} from "../../framework/enums";
|
|
3
3
|
import {Route} from './route'
|
|
4
4
|
import {RouteMatcher} from './matcher'
|
|
5
5
|
|
|
@@ -28,4 +28,8 @@ export class Router {
|
|
|
28
28
|
reset() {
|
|
29
29
|
this.matcher.clear()
|
|
30
30
|
}
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
getRoutes(): Route[] {
|
|
33
|
+
return Array.from(this.matcher.routes.values())
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./metadata"
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {SocketMetaInterface} from "../../types";
|
|
2
|
+
import {METADATA_KEYS} from "../../framework";
|
|
3
|
+
import "reflect-metadata";
|
|
4
|
+
|
|
5
|
+
export function getSocketMetadata(target: any): SocketMetaInterface {
|
|
6
|
+
let metadata = Reflect.getMetadata(METADATA_KEYS.SOCKETS, target);
|
|
7
|
+
if (!metadata) {
|
|
8
|
+
metadata = { namespace: '/', events: [] };
|
|
9
|
+
Reflect.defineMetadata(METADATA_KEYS.SOCKETS, metadata, target);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return metadata;
|
|
13
|
+
}
|
package/source/core/thread.ts
CHANGED
|
@@ -7,16 +7,16 @@ import type {
|
|
|
7
7
|
ThreadSetupOptionsInterface,
|
|
8
8
|
ThreadWaitCallable,
|
|
9
9
|
} from "../types";
|
|
10
|
-
import {EventMessageEnum, RuntimeType} from "../
|
|
10
|
+
import {EventMessageEnum, RuntimeType} from "../framework/enums";
|
|
11
11
|
import {ProcessUtility} from "@protorians/core";
|
|
12
12
|
import {until} from "./process.util";
|
|
13
13
|
import {ApplicationInterface} from "../types/application";
|
|
14
|
-
import {Runtime} from "../
|
|
14
|
+
import {Runtime} from "../framework/runtime";
|
|
15
15
|
import {LBadge, Logger} from "@protorians/logger";
|
|
16
16
|
import {ControllerBuilder} from "./controller";
|
|
17
|
-
import {bodyParserPlugin} from "../
|
|
17
|
+
import {bodyParserPlugin} from "../framework/plugins/body-parser.plugin";
|
|
18
18
|
import {Injection} from "./injection/injection";
|
|
19
|
-
import {Throwable} from "../
|
|
19
|
+
import {Throwable} from "../framework/exceptions";
|
|
20
20
|
import os from "os";
|
|
21
21
|
|
|
22
22
|
|
|
@@ -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',
|
|
@@ -19,6 +20,7 @@ export class Artifacts {
|
|
|
19
20
|
'utility',
|
|
20
21
|
'source',
|
|
21
22
|
'controller',
|
|
23
|
+
'socket',
|
|
22
24
|
'middleware',
|
|
23
25
|
'hook',
|
|
24
26
|
'event',
|
|
@@ -27,6 +29,11 @@ export class Artifacts {
|
|
|
27
29
|
'strategy',
|
|
28
30
|
'strategy-provider',
|
|
29
31
|
'strategy-type',
|
|
32
|
+
'use-case',
|
|
33
|
+
'dto',
|
|
34
|
+
'entity',
|
|
35
|
+
'model',
|
|
36
|
+
'schema',
|
|
30
37
|
]
|
|
31
38
|
|
|
32
39
|
static register(type: string) {
|
|
@@ -47,7 +54,7 @@ export class Artifacts {
|
|
|
47
54
|
|
|
48
55
|
static reload(modulo: any, filename?: string) {
|
|
49
56
|
|
|
50
|
-
if (Raiton.thread?.builder.options.
|
|
57
|
+
if (Raiton.thread?.builder.options.serve === false)
|
|
51
58
|
return Logger.warn(
|
|
52
59
|
'Artifact reload is only available in development mode'
|
|
53
60
|
)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const METADATA_KEYS = {
|
|
2
|
+
CONTROLLERS: Symbol('controller:meta'),
|
|
3
|
+
SOCKETS: Symbol('socket:meta'),
|
|
4
|
+
SOCKET_EVENTS: Symbol('socket:event:meta'),
|
|
5
|
+
ROUTE_PARAMETERS: Symbol('route:meta'),
|
|
6
|
+
GRAFTS: Symbol('graft:meta'),
|
|
7
|
+
CONTAINER: Symbol('container:meta'),
|
|
8
|
+
INJECT_PARAMETERS: Symbol('inject:parameters'),
|
|
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'),
|
|
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
|
+
}
|