framework-do-dede 3.0.40 → 3.2.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.
|
@@ -25,6 +25,7 @@ export interface Input<T, K = any> {
|
|
|
25
25
|
data: T;
|
|
26
26
|
context?: K;
|
|
27
27
|
}
|
|
28
|
+
type BodyFilter = "restrict" | "none";
|
|
28
29
|
export declare function Controller(basePath?: string): (target: any) => void;
|
|
29
30
|
export declare function Tracing<R>(tracer: Tracer<R>): (target: any, propertyKey?: string) => void;
|
|
30
31
|
export declare function getControllers(): any[];
|
|
@@ -37,6 +38,8 @@ export declare function Post(config?: {
|
|
|
37
38
|
params?: string[];
|
|
38
39
|
query?: string[];
|
|
39
40
|
headers?: string[];
|
|
41
|
+
body?: string[];
|
|
42
|
+
bodyFilter?: BodyFilter;
|
|
40
43
|
responseType?: 'json' | 'text' | 'html';
|
|
41
44
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
42
45
|
export declare function Get(config?: {
|
|
@@ -53,6 +56,8 @@ export declare function Put(config?: {
|
|
|
53
56
|
params?: string[];
|
|
54
57
|
query?: string[];
|
|
55
58
|
headers?: string[];
|
|
59
|
+
body?: string[];
|
|
60
|
+
bodyFilter?: BodyFilter;
|
|
56
61
|
responseType?: 'json' | 'text' | 'html';
|
|
57
62
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
58
63
|
export declare function Patch(config?: {
|
|
@@ -61,6 +66,8 @@ export declare function Patch(config?: {
|
|
|
61
66
|
params?: string[];
|
|
62
67
|
query?: string[];
|
|
63
68
|
headers?: string[];
|
|
69
|
+
body?: string[];
|
|
70
|
+
bodyFilter?: BodyFilter;
|
|
64
71
|
responseType?: 'json' | 'text' | 'html';
|
|
65
72
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
66
73
|
export declare function Delete(config?: {
|
|
@@ -69,5 +76,8 @@ export declare function Delete(config?: {
|
|
|
69
76
|
params?: string[];
|
|
70
77
|
query?: string[];
|
|
71
78
|
headers?: string[];
|
|
79
|
+
body?: string[];
|
|
80
|
+
bodyFilter?: BodyFilter;
|
|
72
81
|
responseType?: 'json' | 'text' | 'html';
|
|
73
82
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
83
|
+
export {};
|
|
@@ -66,6 +66,8 @@ export function Post(config = {}) {
|
|
|
66
66
|
params: config.params,
|
|
67
67
|
query: config.query,
|
|
68
68
|
headers: config.headers,
|
|
69
|
+
body: config.body,
|
|
70
|
+
bodyFilter: config.bodyFilter || 'none',
|
|
69
71
|
statusCode: config.statusCode || 200,
|
|
70
72
|
responseType: config.responseType || 'json'
|
|
71
73
|
}, target, propertyKey);
|
|
@@ -92,6 +94,8 @@ export function Put(config = {}) {
|
|
|
92
94
|
params: config.params,
|
|
93
95
|
query: config.query,
|
|
94
96
|
headers: config.headers,
|
|
97
|
+
body: config.body,
|
|
98
|
+
bodyFilter: config.bodyFilter || 'none',
|
|
95
99
|
statusCode: config.statusCode || 200,
|
|
96
100
|
responseType: config.responseType || 'json'
|
|
97
101
|
}, target, propertyKey);
|
|
@@ -105,6 +109,8 @@ export function Patch(config = {}) {
|
|
|
105
109
|
params: config.params,
|
|
106
110
|
query: config.query,
|
|
107
111
|
headers: config.headers,
|
|
112
|
+
body: config.body,
|
|
113
|
+
bodyFilter: config.bodyFilter || 'none',
|
|
108
114
|
statusCode: config.statusCode || 200,
|
|
109
115
|
responseType: config.responseType || 'json'
|
|
110
116
|
}, target, propertyKey);
|
|
@@ -118,6 +124,8 @@ export function Delete(config = {}) {
|
|
|
118
124
|
params: config.params,
|
|
119
125
|
query: config.query,
|
|
120
126
|
headers: config.headers,
|
|
127
|
+
body: config.body,
|
|
128
|
+
bodyFilter: config.bodyFilter || 'none',
|
|
121
129
|
statusCode: config.statusCode || 200,
|
|
122
130
|
responseType: config.responseType || 'json'
|
|
123
131
|
}, target, propertyKey);
|
|
@@ -4,7 +4,7 @@ import { FrameworkError } from "../http/errors/framework";
|
|
|
4
4
|
import { CustomServerError } from "./errors/server";
|
|
5
5
|
export default class ControllerHandler {
|
|
6
6
|
constructor(httpServer, port) {
|
|
7
|
-
for (const { handler, middlewares, method, route, statusCode, params, query, headers, responseType } of this.registryControllers()) {
|
|
7
|
+
for (const { handler, middlewares, method, route, statusCode, params, query, headers, body, bodyFilter, responseType } of this.registryControllers()) {
|
|
8
8
|
httpServer.register({
|
|
9
9
|
method,
|
|
10
10
|
route,
|
|
@@ -28,7 +28,12 @@ export default class ControllerHandler {
|
|
|
28
28
|
const filterParams = this.filter(input.params, params);
|
|
29
29
|
const filterQueryParams = this.filter(input.query, query);
|
|
30
30
|
const filterHeaders = this.filter(input.headers, headers);
|
|
31
|
-
|
|
31
|
+
const normalizeBody = this.normalizeBracketNotation(body);
|
|
32
|
+
let filterBody = this.filter(normalizeBody, body);
|
|
33
|
+
if (bodyFilter === 'none') {
|
|
34
|
+
filterBody = { ...normalizeBody, ...filterBody };
|
|
35
|
+
}
|
|
36
|
+
mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...filterBody };
|
|
32
37
|
request = { data: mergedParams, context: {} };
|
|
33
38
|
middlewaresExecuted = await this.executeMiddlewares(middlewares, request);
|
|
34
39
|
const response = await handler.instance[handler.methodName](request);
|
|
@@ -116,6 +121,8 @@ export default class ControllerHandler {
|
|
|
116
121
|
params: routeConfig.params,
|
|
117
122
|
query: routeConfig.query,
|
|
118
123
|
headers: routeConfig.headers,
|
|
124
|
+
body: routeConfig.body,
|
|
125
|
+
bodyFilter: routeConfig.bodyFilter,
|
|
119
126
|
statusCode: routeConfig.statusCode,
|
|
120
127
|
handler: {
|
|
121
128
|
instance,
|
|
@@ -197,4 +204,24 @@ export default class ControllerHandler {
|
|
|
197
204
|
...debugError
|
|
198
205
|
};
|
|
199
206
|
}
|
|
207
|
+
normalizeBracketNotation(data) {
|
|
208
|
+
if (!data || typeof data !== "object")
|
|
209
|
+
return data;
|
|
210
|
+
const normalized = {};
|
|
211
|
+
for (const [rawKey, value] of Object.entries(data)) {
|
|
212
|
+
const key = String(rawKey);
|
|
213
|
+
const match = key.match(/^([^\[\]]+)\[([^\[\]]+)\]$/);
|
|
214
|
+
if (match) {
|
|
215
|
+
const parent = match[1];
|
|
216
|
+
const child = match[2];
|
|
217
|
+
if (!normalized[parent] || typeof normalized[parent] !== "object") {
|
|
218
|
+
normalized[parent] = {};
|
|
219
|
+
}
|
|
220
|
+
normalized[parent][child] = value;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
normalized[key] = value;
|
|
224
|
+
}
|
|
225
|
+
return normalized;
|
|
226
|
+
}
|
|
200
227
|
}
|