framework-do-dede 3.0.39 → 3.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.
|
@@ -2,6 +2,9 @@ import 'reflect-metadata';
|
|
|
2
2
|
export interface Middleware {
|
|
3
3
|
execute(input: Input<any>): Promise<any>;
|
|
4
4
|
}
|
|
5
|
+
export type MiddlewareClass = new (...args: any[]) => Middleware;
|
|
6
|
+
export type MiddlewareFactory = () => Middleware;
|
|
7
|
+
export type MiddlewareDefinition = Middleware | MiddlewareClass | MiddlewareFactory;
|
|
5
8
|
export interface TracerData {
|
|
6
9
|
requestedAt: Date;
|
|
7
10
|
elapsedTime: string;
|
|
@@ -26,8 +29,8 @@ export declare function Controller(basePath?: string): (target: any) => void;
|
|
|
26
29
|
export declare function Tracing<R>(tracer: Tracer<R>): (target: any, propertyKey?: string) => void;
|
|
27
30
|
export declare function getControllers(): any[];
|
|
28
31
|
export declare function flushControllers(): void;
|
|
29
|
-
export declare function UseMiddleware(middlewareClass:
|
|
30
|
-
export declare function UseMiddlewares(middlewareClasses:
|
|
32
|
+
export declare function UseMiddleware(middlewareClass: MiddlewareDefinition): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
33
|
+
export declare function UseMiddlewares(middlewareClasses: MiddlewareDefinition[]): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
31
34
|
export declare function Post(config?: {
|
|
32
35
|
path?: string;
|
|
33
36
|
statusCode?: number;
|
|
@@ -32,9 +32,15 @@ export function flushControllers() {
|
|
|
32
32
|
}
|
|
33
33
|
export function UseMiddleware(middlewareClass) {
|
|
34
34
|
return function (target, propertyKey, descriptor) {
|
|
35
|
-
if (!middlewareClass
|
|
35
|
+
if (typeof middlewareClass !== 'function' && !middlewareClass?.execute) {
|
|
36
36
|
throw new FrameworkError('Middleware must implement execute()');
|
|
37
37
|
}
|
|
38
|
+
if (typeof middlewareClass === 'function' && middlewareClass.prototype?.execute) {
|
|
39
|
+
const middlewares = Reflect.getMetadata('middlewares', target, propertyKey) || [];
|
|
40
|
+
middlewares.push(middlewareClass);
|
|
41
|
+
Reflect.defineMetadata('middlewares', middlewares, target, propertyKey);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
38
44
|
const middlewares = Reflect.getMetadata('middlewares', target, propertyKey) || [];
|
|
39
45
|
middlewares.push(middlewareClass);
|
|
40
46
|
Reflect.defineMetadata('middlewares', middlewares, target, propertyKey);
|
|
@@ -43,7 +49,7 @@ export function UseMiddleware(middlewareClass) {
|
|
|
43
49
|
export function UseMiddlewares(middlewareClasses) {
|
|
44
50
|
return function (target, propertyKey, descriptor) {
|
|
45
51
|
for (const middlewareClass of middlewareClasses) {
|
|
46
|
-
if (!middlewareClass
|
|
52
|
+
if (typeof middlewareClass !== 'function' && !middlewareClass?.execute) {
|
|
47
53
|
throw new FrameworkError('Middleware must implement execute()');
|
|
48
54
|
}
|
|
49
55
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { InternalServerError, ServerError } from "../http";
|
|
2
2
|
import { flushControllers, getControllers } from "../application/controller";
|
|
3
|
+
import { FrameworkError } from "../http/errors/framework";
|
|
3
4
|
import { CustomServerError } from "./errors/server";
|
|
4
5
|
export default class ControllerHandler {
|
|
5
6
|
constructor(httpServer, port) {
|
|
@@ -27,7 +28,11 @@ export default class ControllerHandler {
|
|
|
27
28
|
const filterParams = this.filter(input.params, params);
|
|
28
29
|
const filterQueryParams = this.filter(input.query, query);
|
|
29
30
|
const filterHeaders = this.filter(input.headers, headers);
|
|
30
|
-
|
|
31
|
+
let body = input.body || {};
|
|
32
|
+
if (Object.keys(body).length > 0) {
|
|
33
|
+
body = this.normalizeBracketNotation(body);
|
|
34
|
+
}
|
|
35
|
+
mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...body };
|
|
31
36
|
request = { data: mergedParams, context: {} };
|
|
32
37
|
middlewaresExecuted = await this.executeMiddlewares(middlewares, request);
|
|
33
38
|
const response = await handler.instance[handler.methodName](request);
|
|
@@ -122,13 +127,34 @@ export default class ControllerHandler {
|
|
|
122
127
|
tracer
|
|
123
128
|
},
|
|
124
129
|
responseType,
|
|
125
|
-
middlewares: middlewares ? middlewares.map(middleware =>
|
|
130
|
+
middlewares: middlewares ? middlewares.map(middleware => this.resolveMiddleware(middleware)) : [],
|
|
126
131
|
});
|
|
127
132
|
}
|
|
128
133
|
}
|
|
129
134
|
flushControllers();
|
|
130
135
|
return controllers;
|
|
131
136
|
}
|
|
137
|
+
resolveMiddleware(middleware) {
|
|
138
|
+
if (typeof middleware === 'function') {
|
|
139
|
+
if (middleware.prototype?.execute) {
|
|
140
|
+
return new middleware();
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
const instance = middleware();
|
|
144
|
+
if (!instance?.execute) {
|
|
145
|
+
throw new FrameworkError('Middleware must implement execute()');
|
|
146
|
+
}
|
|
147
|
+
return instance;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new FrameworkError('Middleware must implement execute()');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (!middleware?.execute) {
|
|
154
|
+
throw new FrameworkError('Middleware must implement execute()');
|
|
155
|
+
}
|
|
156
|
+
return middleware;
|
|
157
|
+
}
|
|
132
158
|
filter(params, filterParams) {
|
|
133
159
|
const filter = {};
|
|
134
160
|
for (const paramName of filterParams || []) {
|
|
@@ -175,4 +201,24 @@ export default class ControllerHandler {
|
|
|
175
201
|
...debugError
|
|
176
202
|
};
|
|
177
203
|
}
|
|
204
|
+
normalizeBracketNotation(data) {
|
|
205
|
+
if (!data || typeof data !== "object")
|
|
206
|
+
return data;
|
|
207
|
+
const normalized = {};
|
|
208
|
+
for (const [rawKey, value] of Object.entries(data)) {
|
|
209
|
+
const key = String(rawKey);
|
|
210
|
+
const match = key.match(/^([^\[\]]+)\[([^\[\]]+)\]$/);
|
|
211
|
+
if (match) {
|
|
212
|
+
const parent = match[1];
|
|
213
|
+
const child = match[2];
|
|
214
|
+
if (!normalized[parent] || typeof normalized[parent] !== "object") {
|
|
215
|
+
normalized[parent] = {};
|
|
216
|
+
}
|
|
217
|
+
normalized[parent][child] = value;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
normalized[key] = value;
|
|
221
|
+
}
|
|
222
|
+
return normalized;
|
|
223
|
+
}
|
|
178
224
|
}
|