framework-do-dede 3.4.0 → 4.0.1
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 +77 -14
- package/dist/application/controller.d.ts +6 -2
- package/dist/application/controller.js +10 -18
- package/dist/application/index.d.ts +1 -1
- package/dist/application/index.js +1 -1
- package/dist/application/services.d.ts +2 -1
- package/dist/application/services.js +3 -3
- package/dist/dede.d.ts +10 -1
- package/dist/dede.js +30 -9
- package/dist/domain/entity.d.ts +4 -0
- package/dist/domain/entity.js +25 -0
- package/dist/domain/errors/app-error.d.ts +12 -0
- package/dist/domain/errors/app-error.js +14 -0
- package/dist/domain/errors/http-errors.d.ts +42 -0
- package/dist/domain/errors/http-errors.js +40 -0
- package/dist/domain/index.d.ts +2 -0
- package/dist/domain/index.js +2 -0
- package/dist/http/controller.handler.d.ts +4 -5
- package/dist/http/controller.handler.js +27 -119
- package/dist/http/errors/server.d.ts +2 -28
- package/dist/http/errors/server.js +2 -49
- package/dist/http/http-server.d.ts +2 -0
- package/dist/http/http-server.js +1 -1
- package/dist/http/index.d.ts +2 -2
- package/dist/http/index.js +2 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/infra/di/registry.d.ts +4 -6
- package/dist/infra/di/registry.js +8 -10
- package/dist/{application → infra/serialization}/entity.d.ts +3 -1
- package/dist/{application → infra/serialization}/entity.js +7 -25
- package/dist/interface/errors/http-error-mapper.d.ts +10 -0
- package/dist/interface/errors/http-error-mapper.js +31 -0
- package/dist/interface/http/middleware-executor.d.ts +10 -0
- package/dist/interface/http/middleware-executor.js +33 -0
- package/dist/interface/http/request-mapper.d.ts +21 -0
- package/dist/interface/http/request-mapper.js +55 -0
- package/dist/interface/validation/class-validator.d.ts +6 -0
- package/dist/interface/validation/class-validator.js +28 -0
- package/dist/interface/validation/validator.d.ts +5 -0
- package/dist/interface/validation/validator.js +1 -0
- package/dist/protocols/repository.d.ts +1 -1
- package/package.json +5 -2
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export class HttpRequestMapper {
|
|
2
|
+
map(input, config) {
|
|
3
|
+
const filterParams = this.filter(input.params, config.params);
|
|
4
|
+
const filterQueryParams = this.filter(input.query, config.query);
|
|
5
|
+
const filterHeaders = this.filter(input.headers, config.headers);
|
|
6
|
+
const normalizeBody = this.normalizeBracketNotation(input.body);
|
|
7
|
+
let filterBody = this.filter(normalizeBody, config.body);
|
|
8
|
+
if (config.bodyFilter !== 'restrict') {
|
|
9
|
+
filterBody = { ...normalizeBody, ...filterBody };
|
|
10
|
+
}
|
|
11
|
+
const mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...filterBody };
|
|
12
|
+
return { data: mergedParams, context: {} };
|
|
13
|
+
}
|
|
14
|
+
filter(params, filterParams) {
|
|
15
|
+
const filter = {};
|
|
16
|
+
for (const paramName of filterParams || []) {
|
|
17
|
+
const [paramNameFiltered, type] = paramName.split('|');
|
|
18
|
+
let value = params[paramName] ?? params[paramNameFiltered];
|
|
19
|
+
if (value === undefined || value === null)
|
|
20
|
+
continue;
|
|
21
|
+
if (type === 'boolean')
|
|
22
|
+
value = value === 'true';
|
|
23
|
+
if (type === 'integer') {
|
|
24
|
+
value = value.replace(/[^0-9]/g, '');
|
|
25
|
+
value = value ? parseInt(value) : 0;
|
|
26
|
+
}
|
|
27
|
+
if (type === 'string')
|
|
28
|
+
value = value.toString();
|
|
29
|
+
if (type === 'number')
|
|
30
|
+
value = parseFloat(value);
|
|
31
|
+
filter[paramNameFiltered] = value;
|
|
32
|
+
}
|
|
33
|
+
return filter;
|
|
34
|
+
}
|
|
35
|
+
normalizeBracketNotation(data) {
|
|
36
|
+
if (!data || typeof data !== "object")
|
|
37
|
+
return data;
|
|
38
|
+
const normalized = {};
|
|
39
|
+
for (const [rawKey, value] of Object.entries(data)) {
|
|
40
|
+
const key = String(rawKey);
|
|
41
|
+
const match = key.match(/^([^\[\]]+)\[([^\[\]]+)\]$/);
|
|
42
|
+
if (match) {
|
|
43
|
+
const parent = match[1];
|
|
44
|
+
const child = match[2];
|
|
45
|
+
if (!normalized[parent] || typeof normalized[parent] !== "object") {
|
|
46
|
+
normalized[parent] = {};
|
|
47
|
+
}
|
|
48
|
+
normalized[parent][child] = value;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
normalized[key] = value;
|
|
52
|
+
}
|
|
53
|
+
return normalized;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { UnprocessableEntity } from '../../domain/errors/http-errors';
|
|
2
|
+
import { validate } from 'class-validator';
|
|
3
|
+
export async function validateWithClassValidator(dtoClass, input) {
|
|
4
|
+
const instance = Object.assign(new dtoClass(), input);
|
|
5
|
+
const errors = await validate(instance);
|
|
6
|
+
if (errors.length === 0)
|
|
7
|
+
return;
|
|
8
|
+
const details = flattenErrors(errors);
|
|
9
|
+
throw new UnprocessableEntity('Validation error', {
|
|
10
|
+
code: 'validation_error',
|
|
11
|
+
details
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function flattenErrors(errors, parentPath = '') {
|
|
15
|
+
const output = [];
|
|
16
|
+
for (const error of errors) {
|
|
17
|
+
const path = parentPath ? `${parentPath}.${error.property}` : error.property;
|
|
18
|
+
if (error.constraints) {
|
|
19
|
+
for (const [rule, message] of Object.entries(error.constraints)) {
|
|
20
|
+
output.push({ path, rule, message });
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (error.children && error.children.length > 0) {
|
|
24
|
+
output.push(...flattenErrors(error.children, path));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return output;
|
|
28
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "framework-do-dede",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
"test:watch": "jest --watch",
|
|
17
17
|
"clean": "rimraf dist",
|
|
18
18
|
"build": "npm run clean && tsc -p tsconfig.build.json && tsc-alias",
|
|
19
|
-
"prepare": "npm run build"
|
|
19
|
+
"prepare": "npm run build",
|
|
20
|
+
"bench": "node bench/benchmark.mjs",
|
|
21
|
+
"bench:compare": "node bench/run.mjs"
|
|
20
22
|
},
|
|
21
23
|
"files": [
|
|
22
24
|
"dist"
|
|
@@ -46,6 +48,7 @@
|
|
|
46
48
|
"typescript": "^5.8.2"
|
|
47
49
|
},
|
|
48
50
|
"peerDependencies": {
|
|
51
|
+
"class-validator": "^0.14.3",
|
|
49
52
|
"elysia": "^1.3.5",
|
|
50
53
|
"express": "^5.1.0",
|
|
51
54
|
"typescript": "^5.8.2"
|