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.
Files changed (46) hide show
  1. package/README.md +38 -24
  2. package/dist/app/aws/decorators.d.ts +8 -0
  3. package/dist/app/aws/decorators.js +14 -0
  4. package/dist/app/aws/index.d.ts +1 -0
  5. package/dist/app/aws/index.js +1 -0
  6. package/dist/app/aws/lambda.d.ts +2 -2
  7. package/dist/app/aws/lambda.js +4 -1
  8. package/dist/app/http/Application.d.ts +0 -1
  9. package/dist/app/http/Application.js +28 -32
  10. package/dist/app/http/decorators.d.ts +2 -5
  11. package/dist/app/http/decorators.js +2 -40
  12. package/dist/constants.d.ts +9 -3
  13. package/dist/constants.js +16 -4
  14. package/dist/core/Controller.d.ts +17 -29
  15. package/dist/core/Controller.js +139 -83
  16. package/dist/core/Endpoint.d.ts +19 -10
  17. package/dist/core/Endpoint.js +41 -11
  18. package/dist/core/index.d.ts +1 -1
  19. package/dist/core/utils/extractors.d.ts +0 -7
  20. package/dist/core/utils/extractors.js +9 -18
  21. package/dist/core/utils/index.d.ts +1 -0
  22. package/dist/core/utils/index.js +1 -0
  23. package/dist/core/utils/middlewares.d.ts +3 -0
  24. package/dist/core/utils/middlewares.js +22 -0
  25. package/dist/examples/controllers/socket.d.ts +0 -16
  26. package/dist/examples/controllers/socket.js +5 -46
  27. package/dist/examples/controllers/user.d.ts +13 -1
  28. package/dist/examples/controllers/user.js +27 -13
  29. package/dist/examples/controllers/userMetadata.d.ts +3 -0
  30. package/dist/examples/controllers/userMetadata.js +32 -0
  31. package/dist/examples/server.js +6 -22
  32. package/dist/types/common.d.ts +31 -7
  33. package/dist/types/common.js +12 -0
  34. package/dist/types/controller.d.ts +33 -0
  35. package/dist/types/http.d.ts +5 -9
  36. package/dist/types/lambda.d.ts +8 -1
  37. package/dist/utils/controller.d.ts +4 -9
  38. package/dist/utils/controller.js +8 -7
  39. package/dist/utils/endpoint.d.ts +2 -0
  40. package/dist/utils/endpoint.js +13 -0
  41. package/dist/utils/helper.d.ts +5 -1
  42. package/dist/utils/helper.js +54 -17
  43. package/dist/utils/index.d.ts +1 -0
  44. package/dist/utils/index.js +1 -0
  45. package/dist/utils/server.js +4 -0
  46. package/package.json +1 -1
@@ -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
- const interceptors = typeof config === 'object'
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.INTERCEPTORS, interceptors, proto);
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 response = await this.executeControllerMethod(data.controllerInstance, data.name, data.payload, data.response);
77
- let status = response.status ?? 200;
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
- for (let index = 0; index < data.interceptors?.length && !isError; index++) {
80
- const interceptor = data.interceptors[index];
81
- response = await interceptor(response);
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: response };
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 method = request.method;
102
- const path = (request.url.path ?? request.url.pathname ?? '').replace(/^\/+/g, '');
103
- const baseInterceptors = Reflect.getMetadata(_constants_1.INTERCEPTORS, proto);
104
- const routePrefix = Reflect.getMetadata(_constants_1.ROUTE_PREFIX, proto) || '';
105
- const middlewares = Reflect.getMetadata(_constants_1.MIDDLEWARES, proto) || [];
106
- const subControllers = Reflect.getMetadata(_constants_1.CONTROLLERS, proto) || [];
107
- // Try sub-controllers
108
- for (const SubController of subControllers) {
109
- const controllerInstance = new SubController(SubController);
110
- if (!controllerInstance)
111
- continue;
112
- const subInterceptors = Reflect.getMetadata(_constants_1.INTERCEPTORS, controllerInstance);
113
- const controllerPrefix = Reflect.getMetadata(_constants_1.ROUTE_PREFIX, SubController.prototype) || '';
114
- const controllerMiddlewares = Reflect.getMetadata(_constants_1.MIDDLEWARES, SubController.prototype) || [];
115
- const methods = this.getControllerMethods(controllerInstance);
116
- for (const methodInfo of methods) {
117
- if (methodInfo.httpMethod === method || methodInfo.httpMethod === 'USE') {
118
- // Build full pattern: main prefix + controller prefix + method pattern
119
- const fullPattern = [routePrefix, controllerPrefix, methodInfo.pattern]
120
- .filter(Boolean)
121
- .join('/')
122
- .replace(/\/+ /g, '/');
123
- const pathParams = (0, _utils_1.matchRoute)(fullPattern, path);
124
- if (pathParams) {
125
- let payload = { ...request, params: pathParams };
126
- // Apply all middlewares in order: main controller -> sub-controller -> method
127
- for (const middleware of [
128
- ...middlewares,
129
- ...controllerMiddlewares,
130
- ...(methodInfo.middlewares || []),
131
- ]) {
132
- const middlawareResponde = await middleware(payload, response);
133
- payload = { ...payload, ...middlawareResponde };
134
- }
135
- return this.getResponse({
136
- interceptors: [...subInterceptors, ...baseInterceptors],
137
- controllerInstance,
138
- name: methodInfo.name,
139
- payload,
140
- response,
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 propertyNames = Object.getOwnPropertyNames(proto);
147
- for (const propertyName of propertyNames) {
148
- const fn = this[propertyName];
149
- if (typeof fn !== 'function')
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 === method || httpMethod === 'USE') {
154
- const fullPattern = [routePrefix, routePattern]
155
- .filter(Boolean)
156
- .join('/')
157
- .replace(/\/+ /g, '/');
158
- const pathParams = (0, _utils_1.matchRoute)(fullPattern, path);
159
- if (pathParams) {
160
- let payload = { ...request, params: pathParams };
161
- // Apply controller-level middlewares
162
- for (const middleware of middlewares) {
163
- const middlewareResponse = await middleware(payload);
164
- payload = { ...payload, middlewareResponse };
165
- }
166
- return this.getResponse({
167
- interceptors: baseInterceptors,
168
- controllerInstance: this,
169
- name: propertyName,
170
- payload,
171
- response,
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
- return { status: 404, message: 'Method Not Found' };
177
- };
231
+ matches.sort((a, b) => b.priority - a.priority);
232
+ return matches[0] || null;
233
+ }
178
234
  };
179
235
  };
180
236
  }
@@ -1,4 +1,4 @@
1
- import { Middleware } from '../types/index.js';
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: string, pathPattern?: string, middlewares?: Middleware[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
56
+ export declare const DELETE: (pathPattern?: string, middlewares?: MiddlewareCB[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
57
57
  /**
58
- * Shortcut decorator for middleware usage on routes.
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 middleware usage.
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 USE: (pathPattern?: string, middlewares?: Middleware[]) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
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;
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.USE = exports.DELETE = exports.PATCH = exports.PUT = exports.POST = exports.GET = void 0;
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('GET', pathPattern, middlewares);
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('POST', pathPattern, middlewares);
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('PUT', pathPattern, middlewares);
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('PATCH', pathPattern, middlewares);
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('DELETE', pathPattern, middlewares);
90
+ return Endpoint(_types_1.HTTP_METHODS.DELETE, pathPattern, middlewares);
87
91
  };
88
92
  exports.DELETE = DELETE;
89
93
  /**
90
- * Shortcut decorator for middleware usage on routes.
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 middleware usage.
98
+ * @returns Method decorator for DELETE endpoint.
95
99
  */
96
- const USE = (pathPattern, middlewares) => {
97
- return Endpoint('USE', pathPattern, middlewares);
100
+ const OPTIONS = (pathPattern, middlewares) => {
101
+ return Endpoint(_types_1.HTTP_METHODS.OPTIONS, pathPattern, middlewares);
98
102
  };
99
- exports.USE = USE;
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
+ }
@@ -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, Interceptor, Middleware, Request, Router, WebSocketClient, WebSocketEvent, WebSocketMessage, } from '../types/index.js';
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 _constants_1 = require("../../constants.js");
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;
@@ -1,3 +1,4 @@
1
1
  export * from './extractors';
2
2
  export * from './helpers';
3
+ export * from './middlewares';
3
4
  export * from './websocket';
@@ -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,3 @@
1
+ import { ErrorCB, MiddlewareCB } from '../../types/index.js';
2
+ export declare function Use(middleware: MiddlewareCB): (target: any) => any;
3
+ export declare function Catch(handler: ErrorCB): (target: any) => any;
@@ -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
  }