raiton 4.0.0 → 5.5.0-beta.3
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 +54 -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 +219 -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,53 @@
|
|
|
1
|
+
import { Controller, Get } from '@protorians/framework';
|
|
2
|
+
import { ApiOkResponse, ApiCreatedResponse, ApiBadRequestResponse, ApiResponse } from '@protorians/framework/decorators';
|
|
3
|
+
|
|
4
|
+
@Controller('/users')
|
|
5
|
+
export class UserController {
|
|
6
|
+
// Example 1: Using ApiOkResponse to specify a 200 response with a specific type
|
|
7
|
+
@Get()
|
|
8
|
+
@ApiOkResponse({
|
|
9
|
+
description='Returns a list of users',
|
|
10
|
+
type: Array,
|
|
11
|
+
isArray: true
|
|
12
|
+
})
|
|
13
|
+
async getUsers() {
|
|
14
|
+
return [{ id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Smith' }];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Example 2: Using ApiCreatedResponse for a POST endpoint (would need @Post decorator)
|
|
18
|
+
// @Post()
|
|
19
|
+
// @ApiCreatedResponse({
|
|
20
|
+
// description='User created successfully',
|
|
21
|
+
// type: Object
|
|
22
|
+
// })
|
|
23
|
+
// async createUser() {
|
|
24
|
+
// // ... implementation
|
|
25
|
+
// }
|
|
26
|
+
|
|
27
|
+
// Example 3: Using ApiResponse for multiple response types
|
|
28
|
+
@Get('/:id')
|
|
29
|
+
@ApiResponse({
|
|
30
|
+
status: 200,
|
|
31
|
+
description='Returns the user if found',
|
|
32
|
+
type: Object
|
|
33
|
+
})
|
|
34
|
+
@ApiResponse({
|
|
35
|
+
status: 404,
|
|
36
|
+
description='User not found'
|
|
37
|
+
})
|
|
38
|
+
@ApiResponse({
|
|
39
|
+
status: 500,
|
|
40
|
+
description='Internal server error'
|
|
41
|
+
})
|
|
42
|
+
async getUserById() {
|
|
43
|
+
// ... implementation
|
|
44
|
+
return { id: 1, name: 'John Doe' };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Example 4: Using the shorthand decorators
|
|
48
|
+
@Get('/health')
|
|
49
|
+
@ApiOkResponse({ description='API is healthy' })
|
|
50
|
+
async healthCheck() {
|
|
51
|
+
return { status: 'OK' };
|
|
52
|
+
}
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "raiton",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.5.0-beta.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Modular TypeScript Backend Framework with Dependency Injection, Plugins, Controllers, Middleware and Hot Reloading.",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"exports": {
|
|
30
30
|
".": "./source/core/index.ts",
|
|
31
|
+
"./core": "./source/core/index.ts",
|
|
31
32
|
"./core/*": "./source/core/*.ts",
|
|
32
33
|
"./commands/*": "./source/commands/*.ts",
|
|
33
34
|
"./framework": "./source/framework/index.ts",
|
|
@@ -55,22 +56,19 @@
|
|
|
55
56
|
"class-validator": "^0.14.3",
|
|
56
57
|
"commander": "^14.0.2",
|
|
57
58
|
"dotenv": "^17.2.3",
|
|
59
|
+
"esbuild": "^0.28.0",
|
|
58
60
|
"reflect-metadata": "^0.2.2"
|
|
59
61
|
},
|
|
60
62
|
"devDependencies": {
|
|
61
63
|
"@types/bcrypt": "^6.0.0",
|
|
62
64
|
"@types/bun": "^1.3.5",
|
|
63
65
|
"@types/deno": "^2.5.0",
|
|
64
|
-
"typescript": "^
|
|
65
|
-
},
|
|
66
|
-
"config": {
|
|
67
|
-
"commitizen": {
|
|
68
|
-
"path": "./node_modules/cz-conventional-changelog"
|
|
69
|
-
}
|
|
66
|
+
"typescript": "^6.0.3"
|
|
70
67
|
},
|
|
71
68
|
"compilerOptions": {
|
|
72
69
|
"types": [
|
|
73
|
-
"bun"
|
|
70
|
+
"bun",
|
|
71
|
+
"deno"
|
|
74
72
|
]
|
|
75
73
|
},
|
|
76
74
|
"publishConfig": {
|
package/source/bin/cli-tools.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/// <reference types="deno" />
|
|
1
2
|
import {spawn} from 'node:child_process';
|
|
2
3
|
import {isBunUsed, isDenoUsed} from "./constants";
|
|
3
4
|
|
|
@@ -50,6 +51,7 @@ export class CliTools {
|
|
|
50
51
|
}).spawn();
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
|
|
53
55
|
return new Deno.Command(cmd, {
|
|
54
56
|
args: cmdArgs,
|
|
55
57
|
...options
|
|
@@ -21,7 +21,8 @@ export default class BuildCommand extends RaitonCommand {
|
|
|
21
21
|
if (options.develop) Logger.warn(LBadge.log("Dev Mode"),);
|
|
22
22
|
|
|
23
23
|
const builder = new RaitonBuilder(this.workdir, {
|
|
24
|
-
|
|
24
|
+
serve: options.develop,
|
|
25
|
+
hmr: false
|
|
25
26
|
});
|
|
26
27
|
|
|
27
28
|
await builder.prepare()
|
|
@@ -1,28 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="deno" />
|
|
2
|
+
import {RaitonBuilder, RaitonCommand} from "../core";
|
|
2
3
|
import {ChildProcess} from 'node:child_process';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import path from "node:path";
|
|
4
|
+
import {LBadge, Logger} from "@protorians/logger";
|
|
5
|
+
import {StartCommandOptionsInterface} from "../types";
|
|
6
6
|
|
|
7
7
|
export default class StartCommand extends RaitonCommand {
|
|
8
8
|
public readonly name: string = 'start';
|
|
9
9
|
public readonly description: string = 'Run the application in production mode';
|
|
10
10
|
|
|
11
|
-
private child:
|
|
11
|
+
private child: Bun.Subprocess<"ignore", "pipe", "inherit"> | ChildProcess | Deno.ChildProcess | null = null;
|
|
12
12
|
|
|
13
13
|
public register(): void {
|
|
14
14
|
this.cli
|
|
15
15
|
.command(this.name)
|
|
16
16
|
.alias("run")
|
|
17
17
|
.description("Start the application in production mode")
|
|
18
|
+
.option("--develop, -d", "Build in development mode")
|
|
18
19
|
.action(this.run.bind(this));
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
protected async run(): Promise<void> {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
protected async run(options: StartCommandOptionsInterface): Promise<void> {
|
|
23
|
+
if (options.develop) Logger.warn(LBadge.log("Dev Mode"),);
|
|
24
|
+
|
|
25
|
+
const builder = new RaitonBuilder(this.workdir, {
|
|
26
|
+
serve: true,
|
|
27
|
+
hmr: options.develop
|
|
26
28
|
});
|
|
29
|
+
|
|
30
|
+
await builder.prepare()
|
|
31
|
+
await builder.boot()
|
|
27
32
|
}
|
|
28
33
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {Security, bodyParserPlugin} from "../framework/plugins";
|
|
1
2
|
import {PluginScope} from './plugins/scope'
|
|
2
3
|
import {RequestContext} from './context'
|
|
3
4
|
import {ApplicationConfigInterface, ApplicationInterface} from "../types/application";
|
|
@@ -13,7 +14,9 @@ export class Application implements ApplicationInterface {
|
|
|
13
14
|
|
|
14
15
|
readonly version: string = RaitonConfig.get('version') || '0.0.1'
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
static get container(): typeof Injection{
|
|
18
|
+
return Injection;
|
|
19
|
+
}
|
|
17
20
|
|
|
18
21
|
constructor(
|
|
19
22
|
readonly config: ApplicationConfigInterface
|
|
@@ -30,6 +33,10 @@ export class Application implements ApplicationInterface {
|
|
|
30
33
|
const artifactTypes = [...artifacts?.types || [], ...Artifacts.defaultTypes]
|
|
31
34
|
|
|
32
35
|
Artifacts.registerMany(...artifactTypes)
|
|
36
|
+
|
|
37
|
+
this.register(Security.headers)
|
|
38
|
+
this.register(bodyParserPlugin())
|
|
39
|
+
|
|
33
40
|
return this;
|
|
34
41
|
}
|
|
35
42
|
|
|
@@ -115,40 +122,42 @@ export class Application implements ApplicationInterface {
|
|
|
115
122
|
|
|
116
123
|
await this.root.hooks.run('onRequest', ctx)
|
|
117
124
|
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
const handler: any = async (param: any) => {
|
|
126
|
+
const context = param.context ?? param;
|
|
127
|
+
const req = context.req;
|
|
128
|
+
const reply = context.reply;
|
|
129
|
+
|
|
130
|
+
const url = new URL(req.url, this.hostname)
|
|
131
|
+
let pathname = url.pathname
|
|
132
|
+
|
|
133
|
+
if (this.config.pathname && this.config.pathname !== '/') {
|
|
134
|
+
const appPathname = this.config.pathname.endsWith('/') ? this.config.pathname : `${this.config.pathname}/`
|
|
135
|
+
if (pathname.startsWith(appPathname)) {
|
|
136
|
+
pathname = pathname.substring(appPathname.length - 1) || '/'
|
|
137
|
+
} else if (pathname === this.config.pathname) {
|
|
138
|
+
pathname = '/'
|
|
139
|
+
} else {
|
|
140
|
+
if (this.config.verbose) {
|
|
141
|
+
Logger.warn(`Request out of application pathname: ${pathname} (expected prefix: ${this.config.pathname})`)
|
|
142
|
+
}
|
|
143
|
+
reply.status(404)
|
|
144
|
+
return reply.send({error: false, statusCode: 404})
|
|
131
145
|
}
|
|
132
|
-
reply.status(404)
|
|
133
|
-
return reply.send({error: false, statusCode: 404})
|
|
134
146
|
}
|
|
135
|
-
}
|
|
136
147
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
148
|
+
const route = this.root.router.match(
|
|
149
|
+
req.method,
|
|
150
|
+
pathname
|
|
151
|
+
)
|
|
141
152
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
153
|
+
if (!route) {
|
|
154
|
+
if (this.config.verbose) {
|
|
155
|
+
Logger.warn(`Route not found: ${req.method} ${pathname}`)
|
|
156
|
+
}
|
|
157
|
+
reply.status(404)
|
|
158
|
+
return reply.send({error: false, statusCode: 404})
|
|
145
159
|
}
|
|
146
|
-
reply.status(404)
|
|
147
|
-
return reply.send({error: false, statusCode: 404})
|
|
148
|
-
}
|
|
149
160
|
|
|
150
|
-
const pipeline = this.root.middleware.clone()
|
|
151
|
-
pipeline.use(async ({context}) => {
|
|
152
161
|
try {
|
|
153
162
|
(context as any).params = route.parameters;
|
|
154
163
|
let responses = await route.handler(context)
|
|
@@ -160,9 +169,9 @@ export class Application implements ApplicationInterface {
|
|
|
160
169
|
console.error(e)
|
|
161
170
|
}
|
|
162
171
|
}
|
|
163
|
-
}
|
|
172
|
+
}
|
|
164
173
|
|
|
165
|
-
await
|
|
174
|
+
await this.root.middleware.run(ctx, handler)
|
|
166
175
|
await this.root.hooks.run('onResponse', ctx)
|
|
167
176
|
}
|
|
168
177
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./artifact";
|
package/source/core/builder.ts
CHANGED
|
@@ -2,16 +2,17 @@ import {RaitonConfig} from "./config";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import {RaitonDirectories} from "./directories";
|
|
4
4
|
import fs, {WatchEventType} from "node:fs";
|
|
5
|
-
import type {BuilderConfigInterface, BuilderInterface,} from "../types";
|
|
5
|
+
import type {BuilderConfigInterface, BuilderInterface, ThreadInterface,} from "../types";
|
|
6
6
|
import {RaitonThread} from "./thread";
|
|
7
7
|
import {Raiton} from "./raiton";
|
|
8
|
-
import {isControllerArtifact
|
|
8
|
+
import {isControllerArtifact} from "../framework";
|
|
9
9
|
import {ControllerBuilder} from "./controller";
|
|
10
10
|
import {watch} from "fs";
|
|
11
11
|
import {LBadge, Logger} from "@protorians/logger";
|
|
12
12
|
import {Throwable} from "../framework/exceptions";
|
|
13
|
-
import {Injection} from "./injection";
|
|
14
13
|
import {Artifacts} from "../framework/artifacts";
|
|
14
|
+
import {build} from "esbuild";
|
|
15
|
+
import {execSync, spawn, spawnSync} from "node:child_process";
|
|
15
16
|
|
|
16
17
|
export class RaitonBuilder implements BuilderInterface {
|
|
17
18
|
protected _source: string | null = null;
|
|
@@ -116,14 +117,38 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
116
117
|
|
|
117
118
|
public async prepare(): Promise<this> {
|
|
118
119
|
await this.initialize()
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
120
|
+
|
|
121
|
+
if (this.options.hmr && this.options.serve) {
|
|
122
|
+
Raiton.signals.listen(
|
|
123
|
+
'hmr:controller',
|
|
124
|
+
async ({filename, version, timestamp}) => {
|
|
125
|
+
await ControllerBuilder.build({filename, version, timestamp})
|
|
126
|
+
}
|
|
127
|
+
)
|
|
128
|
+
// this.parsing();
|
|
129
|
+
this.watching();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
protected async build(thread: ThreadInterface): Promise<this> {
|
|
136
|
+
if (!this._source) throw new Error('Application source not found');
|
|
137
|
+
if (!this._out) throw new Error('Application output not found');
|
|
138
|
+
if (!this.bootstrapper) throw new Error('Bootstrapper not found')
|
|
139
|
+
if (!this.bootstrapperFile) throw new Error('Bootstrapper file not found')
|
|
140
|
+
|
|
141
|
+
const source = path.relative(this.workdir, this._source)
|
|
142
|
+
const output = path.relative(this.workdir, this._out)
|
|
143
|
+
const tsconfig = 'tsconfig.json'
|
|
144
|
+
// const tsconfig = path.join(path.relative(this._source, this.workdir), 'tsconfig.json')
|
|
145
|
+
|
|
146
|
+
Logger.log(LBadge.notice('Building'), 'application');
|
|
147
|
+
Logger.log(LBadge.info('Source'), source);
|
|
148
|
+
Logger.log(LBadge.info('Output'), output);
|
|
149
|
+
|
|
150
|
+
execSync(`cd ${this.workdir} && npx tsc -p ${tsconfig} --outDir ${output} --noEmit`, {stdio: 'inherit'})
|
|
151
|
+
|
|
127
152
|
return this;
|
|
128
153
|
}
|
|
129
154
|
|
|
@@ -136,9 +161,13 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
136
161
|
if (!('default' in bootstrapper))
|
|
137
162
|
throw new Error('Bootstrapper not supported! Please export to "default"')
|
|
138
163
|
|
|
139
|
-
const thread = new RaitonThread(this, {})
|
|
164
|
+
const thread = new RaitonThread(this, {serve: this.options.serve})
|
|
140
165
|
Raiton.thread = thread;
|
|
141
|
-
|
|
166
|
+
const app = await bootstrapper.default(thread);
|
|
167
|
+
|
|
168
|
+
if (!this.options.serve) await this.build(thread)
|
|
169
|
+
|
|
170
|
+
return app;
|
|
142
171
|
}
|
|
143
172
|
|
|
144
173
|
}
|
package/source/core/hooks.ts
CHANGED
package/source/core/index.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
export * from "./config"
|
|
2
|
-
export * from "./process.util"
|
|
3
|
-
export * from "./builder"
|
|
4
|
-
export * from "./bytes.util"
|
|
5
|
-
export * from "./commands"
|
|
6
|
-
export * from "./command"
|
|
7
|
-
export * from "./directories"
|
|
8
|
-
export * from "./raiton"
|
|
9
|
-
export * from "./thread"
|
|
10
|
-
export * from "./application"
|
|
11
|
-
export * from "./controller"
|
|
12
|
-
export * from "./socket"
|
|
13
|
-
export * from "./plugins"
|
|
14
|
-
export * from "./middleware"
|
|
15
|
-
export * from "./router"
|
|
1
|
+
export * from "./config";
|
|
2
|
+
export * from "./process.util";
|
|
3
|
+
export * from "./builder";
|
|
4
|
+
export * from "./bytes.util";
|
|
5
|
+
export * from "./commands";
|
|
6
|
+
export * from "./command";
|
|
7
|
+
export * from "./directories";
|
|
8
|
+
export * from "./raiton";
|
|
9
|
+
export * from "./thread";
|
|
10
|
+
export * from "./application";
|
|
11
|
+
export * from "./controller";
|
|
12
|
+
export * from "./socket";
|
|
13
|
+
export * from "./plugins";
|
|
14
|
+
export * from "./middleware";
|
|
15
|
+
export * from "./router";
|
|
@@ -131,7 +131,7 @@ export class Injection {
|
|
|
131
131
|
static get<T>(name: string, scope?: Symbol): T | undefined {
|
|
132
132
|
const name_ = this.normalizeName(name);
|
|
133
133
|
const cls = this._classes.get(name_);
|
|
134
|
-
if (!cls) throw new Throwable(`Dependency ${
|
|
134
|
+
if (!cls) throw new Throwable(`Dependency ${name_} not registered`);
|
|
135
135
|
|
|
136
136
|
const effectiveScope = scope || cls.scope || this.defaultScope;
|
|
137
137
|
if (this._resolutionStack.includes(name_)) {
|
|
@@ -211,10 +211,13 @@ export class Injection {
|
|
|
211
211
|
|
|
212
212
|
static resolve<T>(construct: ConstructorType<T>): T {
|
|
213
213
|
const metadata: ContainerDefinitionInterface = Reflect.getMetadata(METADATA_KEYS.CONTAINER, construct);
|
|
214
|
+
const name = metadata?.name || ('constructor' in construct ? construct.constructor.name || false : false) || construct.name;
|
|
214
215
|
|
|
215
|
-
if (!metadata)
|
|
216
|
-
|
|
216
|
+
if (!metadata) {
|
|
217
|
+
if (typeof name === 'undefined') Logger.debug('Cannot resolve', construct);
|
|
218
|
+
throw new Throwable(`Cannot resolve ${name} as dependency`);
|
|
219
|
+
}
|
|
217
220
|
|
|
218
|
-
return this.get(
|
|
221
|
+
return this.get(name) as T;
|
|
219
222
|
}
|
|
220
223
|
}
|
|
@@ -14,14 +14,8 @@ export class MiddlewarePipeline {
|
|
|
14
14
|
return this;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
run(ctx: any) {
|
|
18
|
-
const fn = middlewareCompose(this.stack)
|
|
17
|
+
run(ctx: any, ...extra: MiddlewareType[]) {
|
|
18
|
+
const fn = middlewareCompose([...this.stack, ...extra])
|
|
19
19
|
return typeof fn == 'function' ? fn(ctx) : undefined;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
clone() {
|
|
23
|
-
const pipeline = new MiddlewarePipeline()
|
|
24
|
-
pipeline.stack = [...this.stack]
|
|
25
|
-
return pipeline
|
|
26
|
-
}
|
|
27
21
|
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import {HookStore} from '../hooks'
|
|
2
2
|
import {MiddlewarePipeline} from '../middleware'
|
|
3
3
|
import {Route, Router} from '../router'
|
|
4
|
+
import {HttpMethod} from "../../framework/enums";
|
|
4
5
|
|
|
5
6
|
export class PluginScope {
|
|
6
7
|
public hooks: HookStore
|
|
7
8
|
public middleware: MiddlewarePipeline
|
|
8
9
|
public router: Router
|
|
10
|
+
private parent?: PluginScope
|
|
9
11
|
|
|
10
12
|
constructor(parent?: PluginScope) {
|
|
11
|
-
this.
|
|
13
|
+
this.parent = parent
|
|
14
|
+
this.hooks = parent ? parent.hooks : new HookStore()
|
|
12
15
|
this.middleware = parent
|
|
13
|
-
? parent.middleware
|
|
16
|
+
? parent.middleware
|
|
14
17
|
: new MiddlewarePipeline()
|
|
15
18
|
this.router = parent ? parent.router : new Router()
|
|
16
19
|
}
|
|
@@ -20,11 +23,12 @@ export class PluginScope {
|
|
|
20
23
|
return this;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
use(mw: any):
|
|
26
|
+
use(mw: any): any {
|
|
24
27
|
if (typeof mw === 'object' && mw !== null && 'setup' in mw && typeof mw.setup === 'function' && mw.setup.length === 1) {
|
|
25
28
|
mw.setup(this)
|
|
26
29
|
return this
|
|
27
30
|
}
|
|
31
|
+
|
|
28
32
|
this.middleware.use(mw)
|
|
29
33
|
return this;
|
|
30
34
|
}
|
|
@@ -38,9 +42,17 @@ export class PluginScope {
|
|
|
38
42
|
return this.router.add(method, path, handler, version)
|
|
39
43
|
}
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
get(path: string, handler: any, version?: string): Route {
|
|
46
|
+
return this.route(HttpMethod.GET, path, handler, version)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
register(plugin: any): this {
|
|
50
|
+
if (typeof plugin === 'object' && plugin !== null && 'setup' in plugin && typeof plugin.setup === 'function' && plugin.setup.length === 1) {
|
|
51
|
+
plugin.setup(this)
|
|
52
|
+
return this
|
|
53
|
+
}
|
|
42
54
|
const child = new PluginScope(this)
|
|
43
55
|
plugin.setup(child)
|
|
44
|
-
return
|
|
56
|
+
return this
|
|
45
57
|
}
|
|
46
|
-
}
|
|
58
|
+
}
|
|
@@ -1,89 +1,27 @@
|
|
|
1
|
-
import {ControllerMetaInterface,
|
|
2
|
-
import {METADATA_KEYS, Parametrable} from "../../framework";
|
|
1
|
+
import {ControllerMetaInterface, RouteMetaInterface} from "../../types";
|
|
3
2
|
import {Logger} from "@protorians/logger";
|
|
4
|
-
import {
|
|
5
|
-
import {DataTransferObject} from "../../framework/data-transfer-object";
|
|
6
|
-
import {Throwable} from "../../framework/exceptions/throwable";
|
|
3
|
+
import {Raiton} from "..";
|
|
7
4
|
import {HttpException} from "../../framework/exceptions";
|
|
8
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
|
+
}
|