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/dist/core/Controller.js
CHANGED
|
@@ -27,21 +27,19 @@ require("reflect-metadata");
|
|
|
27
27
|
* @returns A class decorator function that enhances the controller class.
|
|
28
28
|
*/
|
|
29
29
|
function Controller(config, middlewares = []) {
|
|
30
|
+
(0, _constants_1.INCREMENT_STATISTIC)('controllers');
|
|
30
31
|
// Handle both string and config object
|
|
31
32
|
const routePrefix = typeof config === 'string' ? config : config.prefix;
|
|
32
33
|
const controllers = typeof config === 'object' ? config.controllers : undefined;
|
|
33
34
|
const controllerMiddlewares = typeof config === 'object' ? [...(config.middlewares || []), ...middlewares] : middlewares;
|
|
34
|
-
|
|
35
|
-
? config.interceptors
|
|
36
|
-
? [].concat(config.interceptors)
|
|
37
|
-
: []
|
|
38
|
-
: [];
|
|
35
|
+
let interceptor = typeof config === 'object' && typeof config.interceptor === 'function' && config.interceptor;
|
|
39
36
|
return function (constructor) {
|
|
40
37
|
const proto = constructor.prototype;
|
|
38
|
+
Reflect.defineMetadata('controller:name', constructor.name, proto);
|
|
41
39
|
Reflect.defineMetadata(_constants_1.ROUTE_PREFIX, routePrefix, proto);
|
|
42
40
|
Reflect.defineMetadata(_constants_1.MIDDLEWARES, controllerMiddlewares, proto);
|
|
43
41
|
Reflect.defineMetadata(_constants_1.CONTROLLERS, controllers || [], proto);
|
|
44
|
-
Reflect.defineMetadata(_constants_1.
|
|
42
|
+
Reflect.defineMetadata(_constants_1.INTERCEPTOR, interceptor, proto);
|
|
45
43
|
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
46
44
|
if (key === 'constructor')
|
|
47
45
|
continue;
|
|
@@ -73,12 +71,13 @@ function Controller(config, middlewares = []) {
|
|
|
73
71
|
}
|
|
74
72
|
async getResponse(data) {
|
|
75
73
|
try {
|
|
76
|
-
let
|
|
77
|
-
let status =
|
|
74
|
+
let appResponse = await this.executeControllerMethod(data.controllerInstance, data.name, data.payload, data.request, data.response);
|
|
75
|
+
let status = appResponse.status ?? 200;
|
|
78
76
|
const isError = !_constants_1.OK_STATUSES.includes(status);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
77
|
+
const interceptors = data.interceptors.reverse();
|
|
78
|
+
for (let index = 0; index < interceptors?.length && !isError; index++) {
|
|
79
|
+
const interceptor = interceptors[index];
|
|
80
|
+
appResponse = await interceptor(appResponse, data.request, data.response);
|
|
82
81
|
}
|
|
83
82
|
const propertyName = data.name;
|
|
84
83
|
const prototype = Object.getPrototypeOf(data.controllerInstance);
|
|
@@ -90,91 +89,148 @@ function Controller(config, middlewares = []) {
|
|
|
90
89
|
const classOkStatus = Reflect.getMetadata(_constants_1.OK_METADATA_KEY, prototype);
|
|
91
90
|
!isError && classOkStatus && (status = classOkStatus);
|
|
92
91
|
}
|
|
93
|
-
return { status, data:
|
|
92
|
+
return { status, data: appResponse };
|
|
94
93
|
}
|
|
95
94
|
catch (err) {
|
|
96
95
|
console.error(err);
|
|
97
96
|
throw err;
|
|
98
97
|
}
|
|
99
98
|
}
|
|
100
|
-
handleRequest = async (request, response) => {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
99
|
+
handleRequest = async (appRequest, request, response) => {
|
|
100
|
+
const context = {
|
|
101
|
+
controllerInstance: this,
|
|
102
|
+
controllerMeta: {
|
|
103
|
+
routePrefix: Reflect.getMetadata(_constants_1.ROUTE_PREFIX, proto) || '',
|
|
104
|
+
middlewares: Reflect.getMetadata(_constants_1.MIDDLEWARES, proto) || [],
|
|
105
|
+
interceptor: Reflect.getMetadata(_constants_1.INTERCEPTOR, proto),
|
|
106
|
+
subControllers: Reflect.getMetadata(_constants_1.CONTROLLERS, proto) || [],
|
|
107
|
+
errorHandler: Reflect.getMetadata(_constants_1.CATCH, proto),
|
|
108
|
+
},
|
|
109
|
+
path: (appRequest.url.pathname ?? '').replace(/^\/+/g, ''),
|
|
110
|
+
method: appRequest.method.toUpperCase(),
|
|
111
|
+
appRequest,
|
|
112
|
+
request,
|
|
113
|
+
response,
|
|
114
|
+
middlewareChain: [],
|
|
115
|
+
interceptorChain: [],
|
|
116
|
+
subPath: Reflect.getMetadata(_constants_1.ROUTE_PREFIX, proto) || '',
|
|
117
|
+
};
|
|
118
|
+
const result = await this.routeWalker(context);
|
|
119
|
+
return result || { status: 404, message: 'Method Not Found' };
|
|
120
|
+
};
|
|
121
|
+
async routeWalker(context) {
|
|
122
|
+
const { controllerInstance, controllerMeta, path, method, subPath } = context;
|
|
123
|
+
for (const SubController of controllerMeta.subControllers) {
|
|
124
|
+
const subInstance = new SubController();
|
|
125
|
+
const subMeta = {
|
|
126
|
+
routePrefix: Reflect.getMetadata(_constants_1.ROUTE_PREFIX, SubController.prototype) || '',
|
|
127
|
+
middlewares: Reflect.getMetadata(_constants_1.MIDDLEWARES, SubController.prototype) || [],
|
|
128
|
+
interceptor: Reflect.getMetadata(_constants_1.INTERCEPTOR, SubController.prototype),
|
|
129
|
+
errorHandler: Reflect.getMetadata(_constants_1.CATCH, SubController.prototype),
|
|
130
|
+
subControllers: Reflect.getMetadata(_constants_1.CONTROLLERS, SubController.prototype) || [],
|
|
131
|
+
};
|
|
132
|
+
const fullSubPath = [subPath, subMeta.routePrefix]
|
|
133
|
+
.filter(Boolean)
|
|
134
|
+
.join('/')
|
|
135
|
+
.replace(/\/+/g, '/');
|
|
136
|
+
if (path.startsWith(fullSubPath)) {
|
|
137
|
+
const subResult = await this.routeWalker({
|
|
138
|
+
...context,
|
|
139
|
+
subPath: fullSubPath,
|
|
140
|
+
controllerInstance: subInstance,
|
|
141
|
+
controllerMeta: subMeta,
|
|
142
|
+
path,
|
|
143
|
+
middlewareChain: [...context.middlewareChain, ...controllerMeta.middlewares],
|
|
144
|
+
interceptorChain: [...context.interceptorChain, controllerMeta.interceptor].filter((el) => !!el),
|
|
145
|
+
});
|
|
146
|
+
if (subResult && subResult.status !== 404) {
|
|
147
|
+
return subResult;
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
}
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
const
|
|
149
|
-
|
|
151
|
+
const routeMatch = this.findRouteInController(controllerInstance, subPath, path, method);
|
|
152
|
+
if (routeMatch) {
|
|
153
|
+
const { name, pathParams, methodMiddlewares, methodInterceptors } = routeMatch;
|
|
154
|
+
const allMiddlewares = [
|
|
155
|
+
...context.middlewareChain,
|
|
156
|
+
...controllerMeta.middlewares,
|
|
157
|
+
...methodMiddlewares,
|
|
158
|
+
];
|
|
159
|
+
let payload = { ...context.appRequest, params: pathParams };
|
|
160
|
+
for (const mw of allMiddlewares) {
|
|
161
|
+
const mwResult = await mw(payload, context.request, context.response);
|
|
162
|
+
payload = { ...payload, ...mwResult };
|
|
163
|
+
}
|
|
164
|
+
return this.getResponse({
|
|
165
|
+
interceptors: [...context.interceptorChain, controllerMeta.interceptor].filter((el) => !!el),
|
|
166
|
+
controllerInstance,
|
|
167
|
+
name,
|
|
168
|
+
payload,
|
|
169
|
+
response: context.response,
|
|
170
|
+
request: context.request,
|
|
171
|
+
}).catch((error) => {
|
|
172
|
+
if (controllerMeta.errorHandler) {
|
|
173
|
+
return controllerMeta.errorHandler(error, context.request, context.response);
|
|
174
|
+
}
|
|
175
|
+
return error;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
getAllMethods(obj) {
|
|
181
|
+
let methods = new Set();
|
|
182
|
+
let current = Object.getPrototypeOf(obj);
|
|
183
|
+
while (current && current !== Object.prototype) {
|
|
184
|
+
Object.getOwnPropertyNames(current).forEach((name) => {
|
|
185
|
+
if (name !== 'constructor' && typeof current[name] === 'function') {
|
|
186
|
+
methods.add(name);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
current = Object.getPrototypeOf(current);
|
|
190
|
+
}
|
|
191
|
+
return Array.from(methods);
|
|
192
|
+
}
|
|
193
|
+
findRouteInController(instance, path, route, method) {
|
|
194
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
195
|
+
const propertyNames = this.getAllMethods(instance);
|
|
196
|
+
const matches = [];
|
|
197
|
+
for (const name of propertyNames) {
|
|
198
|
+
if ([
|
|
199
|
+
'constructor',
|
|
200
|
+
'getResponse',
|
|
201
|
+
'routeWalker',
|
|
202
|
+
'getAllMethods',
|
|
203
|
+
'findRouteInController',
|
|
204
|
+
].includes(name))
|
|
205
|
+
continue;
|
|
206
|
+
const endpointMeta = Reflect.getMetadata(_constants_1.ENDPOINT, prototype, name) || [];
|
|
207
|
+
if (endpointMeta.length === 0)
|
|
150
208
|
continue;
|
|
151
|
-
const endpointMeta = Reflect.getMetadata(_constants_1.ENDPOINT, proto, propertyName) || [];
|
|
152
209
|
const [httpMethod, routePattern] = endpointMeta;
|
|
153
|
-
if (httpMethod
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
}
|
|
210
|
+
if (httpMethod !== method && httpMethod !== 'USE') {
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (httpMethod === 'USE') {
|
|
214
|
+
let useRoute = route.split('/');
|
|
215
|
+
useRoute.pop();
|
|
216
|
+
route = useRoute.join('/');
|
|
217
|
+
}
|
|
218
|
+
const current = [path, routePattern].join('/').replace(/\/+/g, '/');
|
|
219
|
+
const pathParams = (0, _utils_1.matchRoute)(current, route);
|
|
220
|
+
if (pathParams) {
|
|
221
|
+
const priority = httpMethod === 'USE' ? 0 : Object.keys(pathParams).length > 0 ? 1 : 2;
|
|
222
|
+
matches.push({
|
|
223
|
+
name,
|
|
224
|
+
pathParams,
|
|
225
|
+
priority,
|
|
226
|
+
methodMiddlewares: Reflect.getMetadata(_constants_1.MIDDLEWARES, prototype, name) || [],
|
|
227
|
+
methodInterceptors: Reflect.getMetadata(_constants_1.INTERCEPTOR, prototype, name) || [],
|
|
228
|
+
});
|
|
174
229
|
}
|
|
175
230
|
}
|
|
176
|
-
|
|
177
|
-
|
|
231
|
+
matches.sort((a, b) => b.priority - a.priority);
|
|
232
|
+
return matches[0] || null;
|
|
233
|
+
}
|
|
178
234
|
};
|
|
179
235
|
};
|
|
180
236
|
}
|
package/dist/core/Endpoint.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { HTTP_METHODS, MiddlewareCB } from '../types/index.js';
|
|
2
2
|
/**
|
|
3
3
|
* Method decorator to define HTTP method and route pattern metadata on controller methods.
|
|
4
4
|
*
|
|
@@ -13,7 +13,7 @@ import { Middleware } from '../types/index.js';
|
|
|
13
13
|
*
|
|
14
14
|
* @returns A method decorator function.
|
|
15
15
|
*/
|
|
16
|
-
export declare function Endpoint(method:
|
|
16
|
+
export declare function Endpoint(method: HTTP_METHODS, pathPattern?: string, middlewares?: MiddlewareCB[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
17
17
|
/**
|
|
18
18
|
* Shortcut decorator for HTTP GET method.
|
|
19
19
|
*
|
|
@@ -21,7 +21,7 @@ export declare function Endpoint(method: string, pathPattern?: string, middlewar
|
|
|
21
21
|
* @param middlewares - Optional array of middlewares.
|
|
22
22
|
* @returns Method decorator for GET endpoint.
|
|
23
23
|
*/
|
|
24
|
-
export declare const GET: (pathPattern?: string, middlewares?:
|
|
24
|
+
export declare const GET: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
25
25
|
/**
|
|
26
26
|
* Shortcut decorator for HTTP POST method.
|
|
27
27
|
*
|
|
@@ -29,7 +29,7 @@ export declare const GET: (pathPattern?: string, middlewares?: Middleware[]) =>
|
|
|
29
29
|
* @param middlewares - Optional array of middlewares.
|
|
30
30
|
* @returns Method decorator for POST endpoint.
|
|
31
31
|
*/
|
|
32
|
-
export declare const POST: (pathPattern?: string, middlewares?:
|
|
32
|
+
export declare const POST: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
33
33
|
/**
|
|
34
34
|
* Shortcut decorator for HTTP PUT method.
|
|
35
35
|
*
|
|
@@ -37,7 +37,7 @@ export declare const POST: (pathPattern?: string, middlewares?: Middleware[]) =>
|
|
|
37
37
|
* @param middlewares - Optional array of middlewares.
|
|
38
38
|
* @returns Method decorator for PUT endpoint.
|
|
39
39
|
*/
|
|
40
|
-
export declare const PUT: (pathPattern?: string, middlewares?:
|
|
40
|
+
export declare const PUT: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
41
41
|
/**
|
|
42
42
|
* Shortcut decorator for HTTP PATCH method.
|
|
43
43
|
*
|
|
@@ -45,7 +45,7 @@ export declare const PUT: (pathPattern?: string, middlewares?: Middleware[]) =>
|
|
|
45
45
|
* @param middlewares - Optional array of middlewares.
|
|
46
46
|
* @returns Method decorator for PATCH endpoint.
|
|
47
47
|
*/
|
|
48
|
-
export declare const PATCH: (pathPattern?: string, middlewares?:
|
|
48
|
+
export declare const PATCH: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
49
49
|
/**
|
|
50
50
|
* Shortcut decorator for HTTP DELETE method.
|
|
51
51
|
*
|
|
@@ -53,12 +53,21 @@ export declare const PATCH: (pathPattern?: string, middlewares?: Middleware[]) =
|
|
|
53
53
|
* @param middlewares - Optional array of middlewares.
|
|
54
54
|
* @returns Method decorator for DELETE endpoint.
|
|
55
55
|
*/
|
|
56
|
-
export declare const DELETE: (pathPattern?: string, middlewares?:
|
|
56
|
+
export declare const DELETE: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
57
57
|
/**
|
|
58
|
-
* Shortcut decorator for
|
|
58
|
+
* Shortcut decorator for HTTP OPTIONS method.
|
|
59
59
|
*
|
|
60
60
|
* @param pathPattern - Optional route pattern string.
|
|
61
61
|
* @param middlewares - Optional array of middlewares.
|
|
62
|
-
* @returns Method decorator for
|
|
62
|
+
* @returns Method decorator for DELETE endpoint.
|
|
63
|
+
*/
|
|
64
|
+
export declare const OPTIONS: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
65
|
+
/**
|
|
66
|
+
* Shortcut decorator for HTTP HEAD method.
|
|
67
|
+
*
|
|
68
|
+
* @param pathPattern - Optional route pattern string.
|
|
69
|
+
* @param middlewares - Optional array of middlewares.
|
|
70
|
+
* @returns Method decorator for DELETE endpoint.
|
|
63
71
|
*/
|
|
64
|
-
export declare const
|
|
72
|
+
export declare const HEAD: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
73
|
+
export declare function USE(middlewares?: MiddlewareCB[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
package/dist/core/Endpoint.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.HEAD = exports.OPTIONS = exports.DELETE = exports.PATCH = exports.PUT = exports.POST = exports.GET = void 0;
|
|
4
4
|
exports.Endpoint = Endpoint;
|
|
5
|
+
exports.USE = USE;
|
|
5
6
|
const _constants_1 = require("../constants.js");
|
|
7
|
+
const _types_1 = require("../types/index.js");
|
|
6
8
|
/**
|
|
7
9
|
* Method decorator to define HTTP method and route pattern metadata on controller methods.
|
|
8
10
|
*
|
|
@@ -24,10 +26,12 @@ function Endpoint(method, pathPattern, middlewares) {
|
|
|
24
26
|
console.warn('❌ originalMethod is undefined!');
|
|
25
27
|
return descriptor;
|
|
26
28
|
}
|
|
29
|
+
console.log(pathPattern);
|
|
27
30
|
if (method && pathPattern) {
|
|
28
31
|
Reflect.defineMetadata(_constants_1.ENDPOINT, [method, pathPattern], target, propertyKey);
|
|
29
32
|
Reflect.defineMetadata('middlewares', middlewares || [], target, propertyKey);
|
|
30
33
|
}
|
|
34
|
+
(0, _constants_1.INCREMENT_STATISTIC)('routes');
|
|
31
35
|
return descriptor;
|
|
32
36
|
};
|
|
33
37
|
}
|
|
@@ -39,7 +43,7 @@ function Endpoint(method, pathPattern, middlewares) {
|
|
|
39
43
|
* @returns Method decorator for GET endpoint.
|
|
40
44
|
*/
|
|
41
45
|
const GET = (pathPattern, middlewares) => {
|
|
42
|
-
return Endpoint(
|
|
46
|
+
return Endpoint(_types_1.HTTP_METHODS.GET, pathPattern, middlewares);
|
|
43
47
|
};
|
|
44
48
|
exports.GET = GET;
|
|
45
49
|
/**
|
|
@@ -50,7 +54,7 @@ exports.GET = GET;
|
|
|
50
54
|
* @returns Method decorator for POST endpoint.
|
|
51
55
|
*/
|
|
52
56
|
const POST = (pathPattern, middlewares) => {
|
|
53
|
-
return Endpoint(
|
|
57
|
+
return Endpoint(_types_1.HTTP_METHODS.POST, pathPattern, middlewares);
|
|
54
58
|
};
|
|
55
59
|
exports.POST = POST;
|
|
56
60
|
/**
|
|
@@ -61,7 +65,7 @@ exports.POST = POST;
|
|
|
61
65
|
* @returns Method decorator for PUT endpoint.
|
|
62
66
|
*/
|
|
63
67
|
const PUT = (pathPattern, middlewares) => {
|
|
64
|
-
return Endpoint(
|
|
68
|
+
return Endpoint(_types_1.HTTP_METHODS.PUT, pathPattern, middlewares);
|
|
65
69
|
};
|
|
66
70
|
exports.PUT = PUT;
|
|
67
71
|
/**
|
|
@@ -72,7 +76,7 @@ exports.PUT = PUT;
|
|
|
72
76
|
* @returns Method decorator for PATCH endpoint.
|
|
73
77
|
*/
|
|
74
78
|
const PATCH = (pathPattern, middlewares) => {
|
|
75
|
-
return Endpoint(
|
|
79
|
+
return Endpoint(_types_1.HTTP_METHODS.PATCH, pathPattern, middlewares);
|
|
76
80
|
};
|
|
77
81
|
exports.PATCH = PATCH;
|
|
78
82
|
/**
|
|
@@ -83,17 +87,43 @@ exports.PATCH = PATCH;
|
|
|
83
87
|
* @returns Method decorator for DELETE endpoint.
|
|
84
88
|
*/
|
|
85
89
|
const DELETE = (pathPattern, middlewares) => {
|
|
86
|
-
return Endpoint(
|
|
90
|
+
return Endpoint(_types_1.HTTP_METHODS.DELETE, pathPattern, middlewares);
|
|
87
91
|
};
|
|
88
92
|
exports.DELETE = DELETE;
|
|
89
93
|
/**
|
|
90
|
-
* Shortcut decorator for
|
|
94
|
+
* Shortcut decorator for HTTP OPTIONS method.
|
|
91
95
|
*
|
|
92
96
|
* @param pathPattern - Optional route pattern string.
|
|
93
97
|
* @param middlewares - Optional array of middlewares.
|
|
94
|
-
* @returns Method decorator for
|
|
98
|
+
* @returns Method decorator for DELETE endpoint.
|
|
95
99
|
*/
|
|
96
|
-
const
|
|
97
|
-
return Endpoint(
|
|
100
|
+
const OPTIONS = (pathPattern, middlewares) => {
|
|
101
|
+
return Endpoint(_types_1.HTTP_METHODS.OPTIONS, pathPattern, middlewares);
|
|
98
102
|
};
|
|
99
|
-
exports.
|
|
103
|
+
exports.OPTIONS = OPTIONS;
|
|
104
|
+
/**
|
|
105
|
+
* Shortcut decorator for HTTP HEAD method.
|
|
106
|
+
*
|
|
107
|
+
* @param pathPattern - Optional route pattern string.
|
|
108
|
+
* @param middlewares - Optional array of middlewares.
|
|
109
|
+
* @returns Method decorator for DELETE endpoint.
|
|
110
|
+
*/
|
|
111
|
+
const HEAD = (pathPattern, middlewares) => {
|
|
112
|
+
return Endpoint(_types_1.HTTP_METHODS.HEAD, pathPattern, middlewares);
|
|
113
|
+
};
|
|
114
|
+
exports.HEAD = HEAD;
|
|
115
|
+
// /**
|
|
116
|
+
// * Shortcut decorator for middleware usage on routes.
|
|
117
|
+
// *
|
|
118
|
+
// * @param pathPattern - Optional route pattern string.
|
|
119
|
+
// * @param middlewares - Optional array of middlewares.
|
|
120
|
+
// * @returns Method decorator for middleware usage.
|
|
121
|
+
// */
|
|
122
|
+
function USE(middlewares) {
|
|
123
|
+
return function (target, propertyKey, descriptor) {
|
|
124
|
+
Reflect.defineMetadata(_constants_1.ENDPOINT, ['USE', '/'], target, propertyKey);
|
|
125
|
+
Reflect.defineMetadata(_constants_1.MIDDLEWARES, middlewares || [], target, propertyKey);
|
|
126
|
+
(0, _constants_1.INCREMENT_STATISTIC)('routes');
|
|
127
|
+
return descriptor;
|
|
128
|
+
};
|
|
129
|
+
}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This module provides centralized exports for controller and endpoint decorators,
|
|
5
5
|
* as well as related types and utility functions used throughout the core framework.
|
|
6
6
|
*/
|
|
7
|
-
export { EndpointResponse, IController,
|
|
7
|
+
export { AppRequest, EndpointResponse, ErrorCB, IController, InterceptorCB, IWebSocketService, MiddlewareCB, ResponseWithStatus, Router, WebSocketClient, WebSocketEvent, WebSocketMessage, } from '../types/index.js';
|
|
8
8
|
export * from './Controller';
|
|
9
9
|
export * from './Endpoint';
|
|
10
10
|
export * from './utils';
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import { ParamDecoratorType } from '../../types/index.js';
|
|
2
|
-
export interface ParamMetadata {
|
|
3
|
-
index: number;
|
|
4
|
-
type: ParamDecoratorType;
|
|
5
|
-
dto?: any;
|
|
6
|
-
name?: string;
|
|
7
|
-
}
|
|
8
1
|
/**
|
|
9
2
|
* Parameter decorator to extract and validate the request body.
|
|
10
3
|
* @param dto Optional DTO class for validation and transformation.
|
|
@@ -1,59 +1,50 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Response = exports.Multipart = exports.Cookies = exports.Headers = exports.Request = exports.Query = exports.Params = exports.Body = void 0;
|
|
4
|
-
const
|
|
5
|
-
function createParamDecorator(type, dto, name) {
|
|
6
|
-
return function (target, propertyKey, parameterIndex) {
|
|
7
|
-
const existingParams = Reflect.getMetadata(_constants_1.PARAM_METADATA_KEY, target, propertyKey) || [];
|
|
8
|
-
existingParams.push({ index: parameterIndex, type, dto, name });
|
|
9
|
-
existingParams.sort((a, b) => a.index - b.index);
|
|
10
|
-
Reflect.defineMetadata(_constants_1.PARAM_METADATA_KEY, existingParams, target, propertyKey);
|
|
11
|
-
const saved = Reflect.getMetadata(_constants_1.PARAM_METADATA_KEY, target, propertyKey);
|
|
12
|
-
};
|
|
13
|
-
}
|
|
4
|
+
const _utils_1 = require("../../utils/index.js");
|
|
14
5
|
/**
|
|
15
6
|
* Parameter decorator to extract and validate the request body.
|
|
16
7
|
* @param dto Optional DTO class for validation and transformation.
|
|
17
8
|
*/
|
|
18
|
-
const Body = (dto) => createParamDecorator('body', dto);
|
|
9
|
+
const Body = (dto) => (0, _utils_1.createParamDecorator)('body', dto);
|
|
19
10
|
exports.Body = Body;
|
|
20
11
|
/**
|
|
21
12
|
* Parameter decorator to extract route parameters.
|
|
22
13
|
* @param name Optional name of the parameter to extract.
|
|
23
14
|
*/
|
|
24
|
-
const Params = (name) => createParamDecorator('params', undefined, name);
|
|
15
|
+
const Params = (name) => (0, _utils_1.createParamDecorator)('params', undefined, name);
|
|
25
16
|
exports.Params = Params;
|
|
26
17
|
/**
|
|
27
18
|
* Parameter decorator to extract query parameters.
|
|
28
19
|
* @param name Optional name of the query parameter to extract.
|
|
29
20
|
*/
|
|
30
|
-
const Query = (name) => createParamDecorator('query', undefined, name);
|
|
21
|
+
const Query = (name) => (0, _utils_1.createParamDecorator)('query', undefined, name);
|
|
31
22
|
exports.Query = Query;
|
|
32
23
|
/**
|
|
33
24
|
* Parameter decorator to inject the entire request object.
|
|
34
25
|
*/
|
|
35
|
-
const Request = () => createParamDecorator('request');
|
|
26
|
+
const Request = () => (0, _utils_1.createParamDecorator)('request');
|
|
36
27
|
exports.Request = Request;
|
|
37
28
|
/**
|
|
38
29
|
* Parameter decorator to extract headers from the request.
|
|
39
30
|
* @param name Optional name of the header to extract.
|
|
40
31
|
*/
|
|
41
|
-
const Headers = (name) => createParamDecorator('headers', undefined, name);
|
|
32
|
+
const Headers = (name) => (0, _utils_1.createParamDecorator)('headers', undefined, name);
|
|
42
33
|
exports.Headers = Headers;
|
|
43
34
|
/**
|
|
44
35
|
* Parameter decorator to extract cookies from the request.
|
|
45
36
|
* @param name Optional name of the cookie to extract.
|
|
46
37
|
*/
|
|
47
|
-
const Cookies = (name) => createParamDecorator('cookies', undefined, name);
|
|
38
|
+
const Cookies = (name) => (0, _utils_1.createParamDecorator)('cookies', undefined, name);
|
|
48
39
|
exports.Cookies = Cookies;
|
|
49
40
|
/**
|
|
50
41
|
* Parameter decorator to extract multipart form data.
|
|
51
42
|
* @param name Optional name of the multipart field to extract.
|
|
52
43
|
*/
|
|
53
|
-
const Multipart = (name) => createParamDecorator('multipart', undefined, name);
|
|
44
|
+
const Multipart = (name) => (0, _utils_1.createParamDecorator)('multipart', undefined, name);
|
|
54
45
|
exports.Multipart = Multipart;
|
|
55
46
|
/**
|
|
56
47
|
* Parameter decorator to inject the response object.
|
|
57
48
|
*/
|
|
58
|
-
const Response = () => createParamDecorator('response');
|
|
49
|
+
const Response = () => (0, _utils_1.createParamDecorator)('response');
|
|
59
50
|
exports.Response = Response;
|
package/dist/core/utils/index.js
CHANGED
|
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./extractors"), exports);
|
|
18
18
|
__exportStar(require("./helpers"), exports);
|
|
19
|
+
__exportStar(require("./middlewares"), exports);
|
|
19
20
|
__exportStar(require("./websocket"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Use = Use;
|
|
4
|
+
exports.Catch = Catch;
|
|
5
|
+
const _constants_1 = require("../../constants.js");
|
|
6
|
+
function Use(middleware) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
const config = Reflect.getMetadata(_constants_1.SERVER_CONFIG_KEY, target) || {};
|
|
9
|
+
const mergedConfig = {
|
|
10
|
+
...config,
|
|
11
|
+
middlewares: [...(config?.middlewares ?? []), middleware].filter((el) => !!el),
|
|
12
|
+
};
|
|
13
|
+
Reflect.defineMetadata(_constants_1.SERVER_CONFIG_KEY, mergedConfig, target);
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function Catch(handler) {
|
|
18
|
+
return function (target) {
|
|
19
|
+
Reflect.defineMetadata(_constants_1.CATCH, handler, target);
|
|
20
|
+
return target;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
import { WebSocketEvent } from 'quantum-flow/core';
|
|
2
2
|
export declare class Socket {
|
|
3
|
-
/**
|
|
4
|
-
* 1. Приветствие при подключении
|
|
5
|
-
*/
|
|
6
3
|
onConnection(event: WebSocketEvent): void;
|
|
7
|
-
/**
|
|
8
|
-
* 2. @Subscribe - АВТОМАТИЧЕСКАЯ рассылка всем подписчикам
|
|
9
|
-
* Не нужно использовать WebSocketService!
|
|
10
|
-
*/
|
|
11
4
|
onChatMessage(event: WebSocketEvent): void;
|
|
12
|
-
/**
|
|
13
|
-
* 3. @Subscribe для другой комнаты
|
|
14
|
-
*/
|
|
15
5
|
onNewsMessage(event: WebSocketEvent): void;
|
|
16
|
-
/**
|
|
17
|
-
* 4. @OnMessage для команд (без WebSocketService)
|
|
18
|
-
*/
|
|
19
6
|
onPing(event: WebSocketEvent): void;
|
|
20
|
-
/**
|
|
21
|
-
* 5. @OnMessage для подписки
|
|
22
|
-
*/
|
|
23
7
|
onSubscribe(event: WebSocketEvent): void;
|
|
24
8
|
}
|