quantum-flow 1.0.5 → 1.1.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 +38 -24
- package/dist/app/aws/decorators.d.ts +8 -0
- package/dist/app/aws/decorators.js +14 -0
- package/dist/app/aws/index.d.ts +1 -0
- package/dist/app/aws/index.js +1 -0
- package/dist/app/aws/lambda.d.ts +2 -2
- package/dist/app/aws/lambda.js +4 -1
- package/dist/app/http/Application.d.ts +0 -1
- package/dist/app/http/Application.js +28 -32
- package/dist/app/http/decorators.d.ts +2 -5
- package/dist/app/http/decorators.js +2 -40
- package/dist/constants.d.ts +9 -3
- package/dist/constants.js +16 -4
- package/dist/core/Controller.d.ts +17 -29
- package/dist/core/Controller.js +139 -83
- package/dist/core/Endpoint.d.ts +19 -10
- package/dist/core/Endpoint.js +41 -11
- package/dist/core/index.d.ts +1 -1
- package/dist/core/utils/extractors.d.ts +0 -7
- package/dist/core/utils/extractors.js +9 -18
- package/dist/core/utils/index.d.ts +1 -0
- package/dist/core/utils/index.js +1 -0
- package/dist/core/utils/middlewares.d.ts +3 -0
- package/dist/core/utils/middlewares.js +22 -0
- package/dist/examples/controllers/socket.d.ts +0 -16
- package/dist/examples/controllers/socket.js +5 -46
- package/dist/examples/controllers/user.d.ts +13 -1
- package/dist/examples/controllers/user.js +27 -13
- package/dist/examples/controllers/userMetadata.d.ts +3 -0
- package/dist/examples/controllers/userMetadata.js +32 -0
- package/dist/examples/server.js +6 -22
- package/dist/types/common.d.ts +31 -7
- package/dist/types/common.js +12 -0
- package/dist/types/controller.d.ts +33 -0
- package/dist/types/http.d.ts +5 -9
- package/dist/types/lambda.d.ts +8 -1
- package/dist/utils/controller.d.ts +4 -9
- package/dist/utils/controller.js +8 -7
- package/dist/utils/endpoint.d.ts +2 -0
- package/dist/utils/endpoint.js +13 -0
- package/dist/utils/helper.d.ts +5 -1
- package/dist/utils/helper.js +54 -17
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/server.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,11 +23,14 @@ Use the `@Controller` decorator to define controllers with options such as prefi
|
|
|
23
23
|
```typescript
|
|
24
24
|
import { Controller } from 'quantum-flow/core';
|
|
25
25
|
|
|
26
|
+
@Controller(api, [...middlewares])
|
|
26
27
|
@Controller({
|
|
27
28
|
prefix: 'api',
|
|
28
29
|
controllers: [UserController, SocketController],
|
|
29
|
-
|
|
30
|
+
middelwares: [...middlewares],
|
|
31
|
+
interceptor: (parsedRequest, httpRequest, httpResponse) => parsedRequest,
|
|
30
32
|
})
|
|
33
|
+
@Catch((error) => ({ status: 400, error }))
|
|
31
34
|
class RootController {}
|
|
32
35
|
```
|
|
33
36
|
|
|
@@ -36,16 +39,14 @@ class RootController {}
|
|
|
36
39
|
Use the `@Server` decorator with configuration options like port, host, controllers, and WebSocket enablement.
|
|
37
40
|
|
|
38
41
|
```typescript
|
|
39
|
-
import { Server, Port, Host, Use,
|
|
42
|
+
import { Server, Port, Host, Use, Catch, HttpServer } from 'quantum-flow/http';
|
|
40
43
|
|
|
41
|
-
@Server({
|
|
42
|
-
controllers: [RootController],
|
|
43
|
-
})
|
|
44
|
+
@Server({ controllers: [RootController] })
|
|
44
45
|
@Port(3000)
|
|
45
46
|
@Host('localhost')
|
|
46
|
-
@Use((
|
|
47
|
-
@
|
|
48
|
-
@Catch((error) => error)
|
|
47
|
+
@Use((data) => data)
|
|
48
|
+
@Use((data) => data)
|
|
49
|
+
@Catch((error) => ({ status: 400, error }))
|
|
49
50
|
class App {}
|
|
50
51
|
|
|
51
52
|
const server = new HttpServer(App);
|
|
@@ -56,7 +57,6 @@ server.listen().catch(console.error);
|
|
|
56
57
|
## Middlewares, Interceptors, and Error Handlers
|
|
57
58
|
|
|
58
59
|
- Use `@Use` to apply middlewares.
|
|
59
|
-
- Use `@Intercept` to apply interceptors.
|
|
60
60
|
- Use `@Catch` to handle errors.
|
|
61
61
|
- Use `@Port` and `@Host` to configure server port and host.
|
|
62
62
|
|
|
@@ -66,11 +66,10 @@ server.listen().catch(console.error);
|
|
|
66
66
|
- Use `@Headers` to access request headers.
|
|
67
67
|
- Use `@Query` to handle query parameters.
|
|
68
68
|
- Use `@Params` to access route parameters.
|
|
69
|
-
- Use `@Host` to configure the server host.
|
|
70
|
-
- Use `@Port` to configure the server port.
|
|
71
69
|
- Use `@Multipart` for handling multipart/form-data requests.
|
|
72
|
-
- Use `@Request` to access the
|
|
73
|
-
- Use `@Response` to access the
|
|
70
|
+
- Use `@Request` to access the original request object.
|
|
71
|
+
- Use `@Response` to access the original object.
|
|
72
|
+
- Use `@InjectWS` to access the WebsocketService.
|
|
74
73
|
|
|
75
74
|
# AWS Lambda Support
|
|
76
75
|
|
|
@@ -79,6 +78,20 @@ Use `LambdaAdapter` to convert API Gateway events to requests and responses. Cre
|
|
|
79
78
|
```typescript
|
|
80
79
|
Example Lambda handler creation
|
|
81
80
|
import { LambdaAdapter } from 'quantum-flow/aws';
|
|
81
|
+
|
|
82
|
+
let dbConnection = null;
|
|
83
|
+
|
|
84
|
+
@Controller({
|
|
85
|
+
prefix: 'api',
|
|
86
|
+
controllers: [UserController, SocketController],
|
|
87
|
+
})
|
|
88
|
+
class RootController {
|
|
89
|
+
async beforeStart(){
|
|
90
|
+
if(!dbConnection){
|
|
91
|
+
connection = await connect()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
82
95
|
export const handler = LambdaAdapter.createHandler(RootController);
|
|
83
96
|
```
|
|
84
97
|
|
|
@@ -165,11 +178,20 @@ export class Socket {
|
|
|
165
178
|
);
|
|
166
179
|
}
|
|
167
180
|
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
# Http server configuration
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
168
186
|
@Server({
|
|
169
|
-
controllers: [
|
|
187
|
+
controllers: [Root],
|
|
170
188
|
websocket: { enabled: true },
|
|
189
|
+
interceptor: (data) => data,
|
|
171
190
|
})
|
|
172
|
-
@
|
|
191
|
+
@Port(3000)
|
|
192
|
+
@Use((data) => data)
|
|
193
|
+
@Use((data) => data)
|
|
194
|
+
@Catch((error) => ({ status: 400, error }))
|
|
173
195
|
class App {}
|
|
174
196
|
```
|
|
175
197
|
|
|
@@ -205,15 +227,6 @@ Class decorator to add global middlewares to the server.
|
|
|
205
227
|
class App {}
|
|
206
228
|
```
|
|
207
229
|
|
|
208
|
-
### Intercept
|
|
209
|
-
|
|
210
|
-
Class decorator to add global interceptors to the server.
|
|
211
|
-
|
|
212
|
-
```typescript
|
|
213
|
-
@Intercept(interceptor)
|
|
214
|
-
class App {}
|
|
215
|
-
```
|
|
216
|
-
|
|
217
230
|
### Catch
|
|
218
231
|
|
|
219
232
|
Class decorator to set a global error handler for the server.
|
|
@@ -248,6 +261,7 @@ class App {}
|
|
|
248
261
|
Method or class decorator to validate request parameters (query, body, params, headers) against a DTO class using class-validator.
|
|
249
262
|
|
|
250
263
|
```typescript
|
|
264
|
+
import { IsEmail } from 'class-validator';
|
|
251
265
|
class UserDTO {
|
|
252
266
|
@IsEmail()
|
|
253
267
|
email: string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parameter decorator to inject AWS lambda event.
|
|
3
|
+
*/
|
|
4
|
+
export declare const LambdaEvent: () => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
5
|
+
/**
|
|
6
|
+
* Parameter decorator to inject AWS lambda context.
|
|
7
|
+
*/
|
|
8
|
+
export declare const LambdaContext: () => (target: any, propertyKey: string | symbol, parameterIndex: number) => void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LambdaContext = exports.LambdaEvent = void 0;
|
|
4
|
+
const _utils_1 = require("../../utils/index.js");
|
|
5
|
+
/**
|
|
6
|
+
* Parameter decorator to inject AWS lambda event.
|
|
7
|
+
*/
|
|
8
|
+
const LambdaEvent = () => (0, _utils_1.createParamDecorator)('event');
|
|
9
|
+
exports.LambdaEvent = LambdaEvent;
|
|
10
|
+
/**
|
|
11
|
+
* Parameter decorator to inject AWS lambda context.
|
|
12
|
+
*/
|
|
13
|
+
const LambdaContext = () => (0, _utils_1.createParamDecorator)('context');
|
|
14
|
+
exports.LambdaContext = LambdaContext;
|
package/dist/app/aws/index.d.ts
CHANGED
package/dist/app/aws/index.js
CHANGED
package/dist/app/aws/lambda.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { APIGatewayProxyEvent, APIGatewayProxyResult, Context, Handler } from 'aws-lambda';
|
|
2
|
-
import { LambdaRequest, LambdaResponse } from '../../types/index.js';
|
|
2
|
+
import { LambdaRequest, LambdaResponse, LambdaApp } from '../../types/index.js';
|
|
3
3
|
export declare class LambdaAdapter {
|
|
4
4
|
private static getHeaderValue;
|
|
5
5
|
static toRequest(event: APIGatewayProxyEvent, context: Context): LambdaRequest;
|
|
6
6
|
static createResponseBody(response: any): any;
|
|
7
7
|
static toLambdaResponse(response: any, request?: LambdaRequest): LambdaResponse;
|
|
8
|
-
static createHandler(Contoller: any): Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
|
|
8
|
+
static createHandler(Contoller: new (...args: any[]) => LambdaApp): Handler<APIGatewayProxyEvent, APIGatewayProxyResult>;
|
|
9
9
|
private static safeHeaders;
|
|
10
10
|
private static safeParams;
|
|
11
11
|
private static getSourceIp;
|
package/dist/app/aws/lambda.js
CHANGED
|
@@ -63,7 +63,7 @@ class LambdaAdapter {
|
|
|
63
63
|
body,
|
|
64
64
|
params: this.safeParams(event.pathParameters),
|
|
65
65
|
cookies,
|
|
66
|
-
|
|
66
|
+
event,
|
|
67
67
|
context,
|
|
68
68
|
isBase64Encoded: event.isBase64Encoded || false,
|
|
69
69
|
requestId: context.awsRequestId,
|
|
@@ -156,6 +156,9 @@ class LambdaAdapter {
|
|
|
156
156
|
static createHandler(Contoller) {
|
|
157
157
|
const handler = async (event, context) => {
|
|
158
158
|
const instance = new Contoller();
|
|
159
|
+
if (Object.hasOwn(instance, 'beforeStart')) {
|
|
160
|
+
await instance.beforeStart?.();
|
|
161
|
+
}
|
|
159
162
|
try {
|
|
160
163
|
const request = LambdaAdapter.toRequest(event, context);
|
|
161
164
|
if (typeof instance.handleRequest === 'function') {
|
|
@@ -35,9 +35,11 @@ class HttpServer extends Socket_1.Socket {
|
|
|
35
35
|
║ 📍 Host: ${this.config.host}
|
|
36
36
|
║ 🔌 Port: ${this.config.port}
|
|
37
37
|
║ 🔌 Websocket: ${!!this.config.websocket}
|
|
38
|
-
║ 🔧 Middlewares: ${this.config.
|
|
39
|
-
║
|
|
40
|
-
║
|
|
38
|
+
║ 🔧 Global Middlewares: ${this.config.middlewares?.length || 0}
|
|
39
|
+
║ 🔧 Error middlewares: ${this.config.errorHandler?.length || 0}
|
|
40
|
+
║ 🎯 Global Interceptors: ${!!this.config.interceptor?.length}
|
|
41
|
+
║ 📦 Controllers: ${_constants_1.STATISTIC.controllers}
|
|
42
|
+
║ 📦 Routes: ${_constants_1.STATISTIC.routes}
|
|
41
43
|
╚════════════════════════════════════════╝
|
|
42
44
|
`);
|
|
43
45
|
}
|
|
@@ -99,10 +101,12 @@ class HttpServer extends Socket_1.Socket {
|
|
|
99
101
|
const startTime = Date.now();
|
|
100
102
|
try {
|
|
101
103
|
const request = await this.createRequest(req);
|
|
102
|
-
let
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
let appRequest = await this.applyMiddlewares(request, req, res);
|
|
105
|
+
let data = await this.findController(appRequest, req, res);
|
|
106
|
+
if (this.config.interceptor) {
|
|
107
|
+
data = await this.config.interceptor(data, req, res);
|
|
108
|
+
}
|
|
109
|
+
await this.sendResponse(res, data, startTime);
|
|
106
110
|
}
|
|
107
111
|
catch (error) {
|
|
108
112
|
await this.handleError(error, req, res, startTime);
|
|
@@ -121,9 +125,8 @@ class HttpServer extends Socket_1.Socket {
|
|
|
121
125
|
const fullUrl = `${protocol}://${host}${req.url}`;
|
|
122
126
|
const whatwgUrl = new URL(fullUrl);
|
|
123
127
|
return {
|
|
124
|
-
method: req.method,
|
|
128
|
+
method: req.method?.toUpperCase(),
|
|
125
129
|
url: whatwgUrl,
|
|
126
|
-
path: whatwgUrl.pathname,
|
|
127
130
|
headers: req.headers,
|
|
128
131
|
body: parsedBody,
|
|
129
132
|
rawBody: rawBody,
|
|
@@ -134,21 +137,21 @@ class HttpServer extends Socket_1.Socket {
|
|
|
134
137
|
_startTime: Date.now(),
|
|
135
138
|
};
|
|
136
139
|
}
|
|
137
|
-
async applyMiddlewares(request,
|
|
138
|
-
let processed =
|
|
139
|
-
for (const middleware of this.config.
|
|
140
|
-
const result = await middleware(processed,
|
|
140
|
+
async applyMiddlewares(appRequest, request, response) {
|
|
141
|
+
let processed = appRequest;
|
|
142
|
+
for (const middleware of this.config.middlewares?.reverse() || []) {
|
|
143
|
+
const result = await middleware(processed, request, response);
|
|
141
144
|
if (result) {
|
|
142
|
-
processed =
|
|
145
|
+
processed = result;
|
|
143
146
|
}
|
|
144
147
|
}
|
|
145
148
|
return processed;
|
|
146
149
|
}
|
|
147
|
-
async findController(request, response) {
|
|
150
|
+
async findController(appRequest, request, response) {
|
|
148
151
|
for (const ControllerClass of this.config.controllers || []) {
|
|
149
152
|
const instance = new ControllerClass();
|
|
150
153
|
if (typeof instance.handleRequest === 'function') {
|
|
151
|
-
const data = await instance.handleRequest(request, response);
|
|
154
|
+
const data = await instance.handleRequest(appRequest, request, response);
|
|
152
155
|
if (data && data.status !== 404) {
|
|
153
156
|
return data;
|
|
154
157
|
}
|
|
@@ -156,18 +159,11 @@ class HttpServer extends Socket_1.Socket {
|
|
|
156
159
|
}
|
|
157
160
|
return {
|
|
158
161
|
status: 404,
|
|
159
|
-
data: { message: `Route ${
|
|
162
|
+
data: { message: `Route ${appRequest.method} ${appRequest.url.pathname} not found` },
|
|
160
163
|
};
|
|
161
164
|
}
|
|
162
|
-
async applyInterceptors(data, request, response) {
|
|
163
|
-
let processed = data;
|
|
164
|
-
for (const interceptor of this.config.globalInterceptors || []) {
|
|
165
|
-
processed = await interceptor(processed, request, response);
|
|
166
|
-
}
|
|
167
|
-
return processed;
|
|
168
|
-
}
|
|
169
165
|
async sendResponse(res, data, startTime) {
|
|
170
|
-
const
|
|
166
|
+
const response = data?.data !== undefined ? data.data : data;
|
|
171
167
|
if (!res.headersSent) {
|
|
172
168
|
if (!res.getHeader('Content-Type')) {
|
|
173
169
|
res.setHeader('Content-Type', 'application/json');
|
|
@@ -182,9 +178,9 @@ class HttpServer extends Socket_1.Socket {
|
|
|
182
178
|
}
|
|
183
179
|
res.setHeader('X-Response-Time', `${Date.now() - startTime}ms`);
|
|
184
180
|
res.statusCode = data.status ?? 200;
|
|
185
|
-
res.end(JSON.stringify(
|
|
181
|
+
res.end(JSON.stringify(response));
|
|
186
182
|
}
|
|
187
|
-
async handleError(error,
|
|
183
|
+
async handleError(error, request, response, startTime) {
|
|
188
184
|
let errorResponse = {
|
|
189
185
|
status: error.status || 500,
|
|
190
186
|
data: {
|
|
@@ -192,17 +188,17 @@ class HttpServer extends Socket_1.Socket {
|
|
|
192
188
|
errors: error.errors || [],
|
|
193
189
|
},
|
|
194
190
|
};
|
|
195
|
-
if (!this.config.
|
|
196
|
-
return this.sendResponse(
|
|
191
|
+
if (!this.config.errorHandler) {
|
|
192
|
+
return this.sendResponse(response, errorResponse, startTime);
|
|
197
193
|
}
|
|
198
194
|
try {
|
|
199
|
-
const intercepted = await this.config.
|
|
200
|
-
errorResponse =
|
|
195
|
+
const intercepted = await this.config.errorHandler(error, request, response);
|
|
196
|
+
errorResponse = intercepted;
|
|
201
197
|
}
|
|
202
198
|
catch (cathed) {
|
|
203
199
|
Object.assign(errorResponse, cathed);
|
|
204
200
|
}
|
|
205
|
-
return this.sendResponse(
|
|
201
|
+
return this.sendResponse(response, errorResponse, startTime);
|
|
206
202
|
}
|
|
207
203
|
}
|
|
208
204
|
exports.HttpServer = HttpServer;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function Server(config?:
|
|
3
|
-
export declare function Use(middleware: any): (target: any) => any;
|
|
4
|
-
export declare function Intercept(interceptor: Interceptor): (target: any) => any;
|
|
5
|
-
export declare function Catch(handler: any): (target: any) => any;
|
|
1
|
+
import { ServerConfig } from '../../types/index.js';
|
|
2
|
+
export declare function Server(config?: ServerConfig): (target: any) => any;
|
|
6
3
|
export declare function Port(port: number): (target: any) => any;
|
|
7
4
|
export declare function Host(host: string): (target: any) => any;
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Server = Server;
|
|
4
|
-
exports.Use = Use;
|
|
5
|
-
exports.Intercept = Intercept;
|
|
6
|
-
exports.Catch = Catch;
|
|
7
4
|
exports.Port = Port;
|
|
8
5
|
exports.Host = Host;
|
|
9
6
|
const _constants_1 = require("../../constants.js");
|
|
@@ -14,8 +11,8 @@ function Server(config = {}) {
|
|
|
14
11
|
...existingConfig,
|
|
15
12
|
...config,
|
|
16
13
|
controllers: [...(existingConfig.controllers || []), ...(config.controllers || [])],
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
middlewares: [...(existingConfig.middlewares ?? []), ...(config.middlewares ?? [])],
|
|
15
|
+
interceptors: existingConfig.interceptor ?? config.interceptor,
|
|
19
16
|
};
|
|
20
17
|
Reflect.defineMetadata(_constants_1.SERVER_CONFIG_KEY, mergedConfig, target);
|
|
21
18
|
if (config.controllers) {
|
|
@@ -24,41 +21,6 @@ function Server(config = {}) {
|
|
|
24
21
|
return target;
|
|
25
22
|
};
|
|
26
23
|
}
|
|
27
|
-
function Use(middleware) {
|
|
28
|
-
return function (target) {
|
|
29
|
-
const existingConfig = Reflect.getMetadata(_constants_1.SERVER_CONFIG_KEY, target) || {};
|
|
30
|
-
const middlewares = existingConfig.globalMiddlewares || [];
|
|
31
|
-
middlewares.push(middleware);
|
|
32
|
-
console.log(middlewares);
|
|
33
|
-
Reflect.defineMetadata(_constants_1.SERVER_CONFIG_KEY, {
|
|
34
|
-
...existingConfig,
|
|
35
|
-
globalMiddlewares: middlewares,
|
|
36
|
-
}, target);
|
|
37
|
-
return target;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
function Intercept(interceptor) {
|
|
41
|
-
return function (target) {
|
|
42
|
-
const existingConfig = Reflect.getMetadata(_constants_1.SERVER_CONFIG_KEY, target) || {};
|
|
43
|
-
const interceptors = existingConfig.globalInterceptors || [];
|
|
44
|
-
interceptors.push(interceptor);
|
|
45
|
-
Reflect.defineMetadata(_constants_1.SERVER_CONFIG_KEY, {
|
|
46
|
-
...existingConfig,
|
|
47
|
-
globalInterceptors: interceptors,
|
|
48
|
-
}, target);
|
|
49
|
-
return target;
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function Catch(handler) {
|
|
53
|
-
return function (target) {
|
|
54
|
-
const existingConfig = Reflect.getMetadata(_constants_1.SERVER_CONFIG_KEY, target) || {};
|
|
55
|
-
Reflect.defineMetadata(_constants_1.SERVER_CONFIG_KEY, {
|
|
56
|
-
...existingConfig,
|
|
57
|
-
globalErrorHandler: handler,
|
|
58
|
-
}, target);
|
|
59
|
-
return target;
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
24
|
function Port(port) {
|
|
63
25
|
return function (target) {
|
|
64
26
|
const existingConfig = Reflect.getMetadata(_constants_1.SERVER_CONFIG_KEY, target) || {};
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
export declare const PARAM_METADATA_KEY = "design:parameters";
|
|
2
2
|
export declare const APP_METADATA_KEY = "app:configuration";
|
|
3
|
-
export declare const ROUTE_PREFIX = "route:
|
|
4
|
-
export declare const
|
|
3
|
+
export declare const ROUTE_PREFIX = "route:prefix";
|
|
4
|
+
export declare const ROUTE_MIDDLEWARES = "route:middlewares";
|
|
5
|
+
export declare const MIDDLEWARES = "controller:middlewares";
|
|
5
6
|
export declare const CONTROLLERS = "app:controllers";
|
|
6
|
-
export declare const
|
|
7
|
+
export declare const INTERCEPTOR = "app:interceptors";
|
|
7
8
|
export declare const ENDPOINT = "route:endpoints";
|
|
8
9
|
export declare const OK_METADATA_KEY = "custom:ok";
|
|
9
10
|
export declare const SERVER_CONFIG_KEY = "server:config";
|
|
10
11
|
export declare const SERVER_MODULES_KEY = "server:modules";
|
|
12
|
+
export declare const USE_MIDDLEWARE = "controller:usemiddleware";
|
|
11
13
|
export declare const WS_METADATA_KEY = "websocket:handler";
|
|
12
14
|
export declare const WS_TOPIC_KEY = "websocket:topic";
|
|
13
15
|
export declare const WS_SERVICE_KEY = "websocket:service";
|
|
16
|
+
export declare const INTECEPT = "server:intercept";
|
|
17
|
+
export declare const CATCH = "server:catch";
|
|
14
18
|
export declare const STOPPED = "\n\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\n\u2551 \uD83D\uDC4B Server stopped \u2551\n\u2551 \uD83D\uDCCA Status: STOPPED \u2551\n\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\n ";
|
|
15
19
|
export declare const OK_STATUSES: number[];
|
|
16
20
|
export declare const TO_VALIDATE: string[];
|
|
21
|
+
export declare const STATISTIC: Record<'controllers' | 'routes', number>;
|
|
22
|
+
export declare const INCREMENT_STATISTIC: (prop: "controllers" | "routes") => void;
|
package/dist/constants.js
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TO_VALIDATE = exports.OK_STATUSES = exports.STOPPED = exports.WS_SERVICE_KEY = exports.WS_TOPIC_KEY = exports.WS_METADATA_KEY = exports.SERVER_MODULES_KEY = exports.SERVER_CONFIG_KEY = exports.OK_METADATA_KEY = exports.ENDPOINT = exports.
|
|
3
|
+
exports.INCREMENT_STATISTIC = exports.STATISTIC = exports.TO_VALIDATE = exports.OK_STATUSES = exports.STOPPED = exports.CATCH = exports.INTECEPT = exports.WS_SERVICE_KEY = exports.WS_TOPIC_KEY = exports.WS_METADATA_KEY = exports.USE_MIDDLEWARE = exports.SERVER_MODULES_KEY = exports.SERVER_CONFIG_KEY = exports.OK_METADATA_KEY = exports.ENDPOINT = exports.INTERCEPTOR = exports.CONTROLLERS = exports.MIDDLEWARES = exports.ROUTE_MIDDLEWARES = exports.ROUTE_PREFIX = exports.APP_METADATA_KEY = exports.PARAM_METADATA_KEY = void 0;
|
|
4
4
|
exports.PARAM_METADATA_KEY = 'design:parameters';
|
|
5
5
|
exports.APP_METADATA_KEY = 'app:configuration';
|
|
6
|
-
exports.ROUTE_PREFIX = 'route:
|
|
7
|
-
exports.
|
|
6
|
+
exports.ROUTE_PREFIX = 'route:prefix';
|
|
7
|
+
exports.ROUTE_MIDDLEWARES = 'route:middlewares';
|
|
8
|
+
exports.MIDDLEWARES = 'controller:middlewares';
|
|
8
9
|
exports.CONTROLLERS = 'app:controllers';
|
|
9
|
-
exports.
|
|
10
|
+
exports.INTERCEPTOR = 'app:interceptors';
|
|
10
11
|
exports.ENDPOINT = 'route:endpoints';
|
|
11
12
|
exports.OK_METADATA_KEY = 'custom:ok';
|
|
12
13
|
exports.SERVER_CONFIG_KEY = 'server:config';
|
|
13
14
|
exports.SERVER_MODULES_KEY = 'server:modules';
|
|
15
|
+
exports.USE_MIDDLEWARE = 'controller:usemiddleware';
|
|
14
16
|
exports.WS_METADATA_KEY = 'websocket:handler';
|
|
15
17
|
exports.WS_TOPIC_KEY = 'websocket:topic';
|
|
16
18
|
exports.WS_SERVICE_KEY = 'websocket:service';
|
|
19
|
+
exports.INTECEPT = 'server:intercept';
|
|
20
|
+
exports.CATCH = 'server:catch';
|
|
17
21
|
exports.STOPPED = `
|
|
18
22
|
╔════════════════════════════════════════╗
|
|
19
23
|
║ 👋 Server stopped ║
|
|
@@ -22,3 +26,11 @@ exports.STOPPED = `
|
|
|
22
26
|
`;
|
|
23
27
|
exports.OK_STATUSES = [200, 201, 202, 203, 204, 205, 206, 207, 208, 226];
|
|
24
28
|
exports.TO_VALIDATE = ['headers', 'params', 'multipart', 'query', 'body'];
|
|
29
|
+
exports.STATISTIC = {
|
|
30
|
+
controllers: 0,
|
|
31
|
+
routes: 0,
|
|
32
|
+
};
|
|
33
|
+
const INCREMENT_STATISTIC = (prop) => {
|
|
34
|
+
exports.STATISTIC[prop] = exports.STATISTIC[prop] + 1;
|
|
35
|
+
};
|
|
36
|
+
exports.INCREMENT_STATISTIC = INCREMENT_STATISTIC;
|
|
@@ -1,16 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { ServerResponse } from 'http';
|
|
1
|
+
import { AppRequest, ControllerClass, ControllerConfig, ControllerInstance, InterceptorCB, RouteContext } from '../types/index.js';
|
|
2
|
+
import { IncomingMessage, ServerResponse } from 'http';
|
|
3
3
|
import 'reflect-metadata';
|
|
4
|
-
type ControllerClass = {
|
|
5
|
-
new (...args: any[]): any;
|
|
6
|
-
};
|
|
7
|
-
type ControllerInstance = InstanceType<ControllerClass>;
|
|
8
|
-
interface ControllerConfig {
|
|
9
|
-
prefix: string;
|
|
10
|
-
middlewares?: Array<Middleware>;
|
|
11
|
-
controllers?: ControllerClass[];
|
|
12
|
-
interceptors?: Array<(...args: any[]) => any> | ((...args: any[]) => any);
|
|
13
|
-
}
|
|
14
4
|
/**
|
|
15
5
|
* Class decorator to define a controller with optional configuration.
|
|
16
6
|
*
|
|
@@ -32,33 +22,31 @@ interface ControllerConfig {
|
|
|
32
22
|
*
|
|
33
23
|
* @returns A class decorator function that enhances the controller class.
|
|
34
24
|
*/
|
|
35
|
-
export declare function Controller(config: string | ControllerConfig, middlewares?: Array<
|
|
25
|
+
export declare function Controller(config: string | ControllerConfig, middlewares?: Array<InterceptorCB>): <T extends ControllerClass>(constructor: T) => {
|
|
36
26
|
new (...args: any[]): {
|
|
37
27
|
[x: string]: any;
|
|
38
|
-
executeControllerMethod: (controller:
|
|
39
|
-
getControllerMethods: (controller: import("../types/index.js").
|
|
40
|
-
name: string;
|
|
41
|
-
httpMethod: string;
|
|
42
|
-
pattern: string;
|
|
43
|
-
middlewares?: Array<(req: any, res?: ServerResponse) => any>;
|
|
44
|
-
}[];
|
|
28
|
+
executeControllerMethod: (controller: ControllerInstance, propertyName: string, payload: any, request?: IncomingMessage, response?: ServerResponse) => Promise<any>;
|
|
29
|
+
getControllerMethods: (controller: ControllerInstance) => import("../types/index.js").ControllerMethods;
|
|
45
30
|
getResponse(data: {
|
|
46
31
|
controllerInstance: ControllerInstance;
|
|
47
32
|
name: string;
|
|
48
33
|
payload: any;
|
|
49
|
-
interceptors:
|
|
34
|
+
interceptors: InterceptorCB[];
|
|
35
|
+
request?: IncomingMessage;
|
|
50
36
|
response?: ServerResponse;
|
|
51
37
|
}): Promise<{
|
|
52
38
|
status: any;
|
|
53
39
|
data: any;
|
|
54
40
|
}>;
|
|
55
|
-
handleRequest: (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
41
|
+
handleRequest: (appRequest: AppRequest, request?: IncomingMessage, response?: ServerResponse) => Promise<any>;
|
|
42
|
+
routeWalker(context: RouteContext): Promise<any>;
|
|
43
|
+
getAllMethods(obj: any): string[];
|
|
44
|
+
findRouteInController(instance: any, path: string, route: string, method: string): {
|
|
45
|
+
name: string;
|
|
46
|
+
pathParams: Record<string, string>;
|
|
47
|
+
priority: number;
|
|
48
|
+
methodMiddlewares: any[];
|
|
49
|
+
methodInterceptors: any[];
|
|
50
|
+
};
|
|
62
51
|
};
|
|
63
52
|
} & T;
|
|
64
|
-
export {};
|