framework-do-dede 4.0.1 → 4.0.3
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/dist/application/controller.d.ts +6 -6
- package/dist/http/controller.handler.js +16 -4
- package/dist/http/http-server.d.ts +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/interface/http/request-mapper.js +4 -4
- package/dist/interface/validation/class-validator.d.ts +5 -5
- package/dist/interface/validation/class-validator.js +8 -11
- package/dist/interface/validation/validator.d.ts +5 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import type {
|
|
2
|
+
import type { ValidatorDefinition } from "../interface/validation/validator";
|
|
3
3
|
export interface Middleware {
|
|
4
4
|
execute(input: Input<any>): Promise<any>;
|
|
5
5
|
}
|
|
@@ -40,7 +40,7 @@ export declare function Post(config?: {
|
|
|
40
40
|
body?: string[];
|
|
41
41
|
bodyFilter?: BodyFilter;
|
|
42
42
|
responseType?: 'json' | 'text' | 'html';
|
|
43
|
-
validator?:
|
|
43
|
+
validator?: ValidatorDefinition;
|
|
44
44
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
45
45
|
export declare function Get(config?: {
|
|
46
46
|
path?: string;
|
|
@@ -49,7 +49,7 @@ export declare function Get(config?: {
|
|
|
49
49
|
query?: string[];
|
|
50
50
|
headers?: string[];
|
|
51
51
|
responseType?: 'json' | 'text' | 'html';
|
|
52
|
-
validator?:
|
|
52
|
+
validator?: ValidatorDefinition;
|
|
53
53
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
54
54
|
export declare function Put(config?: {
|
|
55
55
|
path?: string;
|
|
@@ -60,7 +60,7 @@ export declare function Put(config?: {
|
|
|
60
60
|
body?: string[];
|
|
61
61
|
bodyFilter?: BodyFilter;
|
|
62
62
|
responseType?: 'json' | 'text' | 'html';
|
|
63
|
-
validator?:
|
|
63
|
+
validator?: ValidatorDefinition;
|
|
64
64
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
65
65
|
export declare function Patch(config?: {
|
|
66
66
|
path?: string;
|
|
@@ -71,7 +71,7 @@ export declare function Patch(config?: {
|
|
|
71
71
|
body?: string[];
|
|
72
72
|
bodyFilter?: BodyFilter;
|
|
73
73
|
responseType?: 'json' | 'text' | 'html';
|
|
74
|
-
validator?:
|
|
74
|
+
validator?: ValidatorDefinition;
|
|
75
75
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
76
76
|
export declare function Delete(config?: {
|
|
77
77
|
path?: string;
|
|
@@ -82,6 +82,6 @@ export declare function Delete(config?: {
|
|
|
82
82
|
body?: string[];
|
|
83
83
|
bodyFilter?: BodyFilter;
|
|
84
84
|
responseType?: 'json' | 'text' | 'html';
|
|
85
|
-
validator?:
|
|
85
|
+
validator?: ValidatorDefinition;
|
|
86
86
|
}): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
|
|
87
87
|
export {};
|
|
@@ -32,11 +32,20 @@ export default class ControllerHandler {
|
|
|
32
32
|
startTime = performance.now();
|
|
33
33
|
request = this.requestMapper.map(input, { params, query, headers, body, bodyFilter });
|
|
34
34
|
if (validator) {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
let validatorLike;
|
|
36
|
+
let options;
|
|
37
|
+
if (isValidatorDefinition(validator)) {
|
|
38
|
+
validatorLike = validator.validator;
|
|
39
|
+
options = validator.error;
|
|
37
40
|
}
|
|
38
|
-
else
|
|
39
|
-
|
|
41
|
+
else {
|
|
42
|
+
validatorLike = validator;
|
|
43
|
+
}
|
|
44
|
+
if (typeof validatorLike === 'function') {
|
|
45
|
+
await validateWithClassValidator(validatorLike, request.data, options);
|
|
46
|
+
}
|
|
47
|
+
else if (typeof validatorLike.validate === 'function') {
|
|
48
|
+
await validatorLike.validate(request.data);
|
|
40
49
|
}
|
|
41
50
|
else {
|
|
42
51
|
throw new FrameworkError('Validator must be a class or implement validate()');
|
|
@@ -135,3 +144,6 @@ export default class ControllerHandler {
|
|
|
135
144
|
return middleware;
|
|
136
145
|
}
|
|
137
146
|
}
|
|
147
|
+
function isValidatorDefinition(value) {
|
|
148
|
+
return !!value && typeof value === 'object' && 'validator' in value;
|
|
149
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Middleware, Tracer } from "../application/controller";
|
|
2
|
-
import type {
|
|
2
|
+
import type { ValidatorDefinition } from "../interface/validation/validator";
|
|
3
3
|
export type Request = {
|
|
4
4
|
data: any;
|
|
5
5
|
context: any;
|
|
@@ -16,7 +16,7 @@ export type HttpServerParams = {
|
|
|
16
16
|
};
|
|
17
17
|
responseType: 'json' | 'text' | 'html';
|
|
18
18
|
middlewares?: Middleware[];
|
|
19
|
-
validator?:
|
|
19
|
+
validator?: ValidatorDefinition;
|
|
20
20
|
statusCode?: number;
|
|
21
21
|
params?: string[];
|
|
22
22
|
query?: string[];
|
package/dist/index.d.ts
CHANGED
|
@@ -3,5 +3,7 @@ import { Container, DefaultContainer, Inject, setDefaultContainer } from './infr
|
|
|
3
3
|
import { Dede, type Options, Register } from './dede';
|
|
4
4
|
import { ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError, CustomServerError } from './http/errors/server';
|
|
5
5
|
import { AppError } from './domain/errors/app-error';
|
|
6
|
+
import type { ValidatorDefinition } from './interface/validation/validator';
|
|
6
7
|
import type { RepositoryCreate, RepositoryUpdate, RepositoryRemove, RepositoryRemoveBy, RepositoryExistsBy, RepositoryRestore, RepositoryRestoreBy, RepositoryNotExistsBy, RepositoryPagination } from './protocols/repository';
|
|
7
8
|
export { Controller, Post, Get, Put, Delete, Patch, Input, Middleware, UseMiddleware, UseMiddlewares, Tracer, Tracing, TracerData, Entity, Restrict, VirtualProperty, GetterPrefix, Serialize, UseCase, DecorateUseCase, Storage, StorageGateway, Inject, Container, DefaultContainer, setDefaultContainer, Dede, Options, Register, ServerError, NotFound, Forbidden, Conflict, Unauthorized, UnprocessableEntity, BadRequest, InternalServerError, CustomServerError, AppError, RepositoryCreate, RepositoryUpdate, RepositoryRemove, RepositoryRemoveBy, RepositoryRestore, RepositoryExistsBy, RepositoryRestoreBy, RepositoryNotExistsBy, RepositoryPagination };
|
|
9
|
+
export type { ValidatorDefinition };
|
|
@@ -18,16 +18,16 @@ export class HttpRequestMapper {
|
|
|
18
18
|
let value = params[paramName] ?? params[paramNameFiltered];
|
|
19
19
|
if (value === undefined || value === null)
|
|
20
20
|
continue;
|
|
21
|
-
if (type === 'boolean')
|
|
21
|
+
if (type === 'boolean' && typeof value !== 'boolean')
|
|
22
22
|
value = value === 'true';
|
|
23
|
-
if (type === 'integer') {
|
|
23
|
+
if (type === 'integer' && typeof value === 'string' && [/^0-9$/.test(value)]) {
|
|
24
24
|
value = value.replace(/[^0-9]/g, '');
|
|
25
25
|
value = value ? parseInt(value) : 0;
|
|
26
26
|
}
|
|
27
|
+
if (type === 'number' && typeof value === 'string' && [/^0-9$/.test(value)])
|
|
28
|
+
value = parseFloat(value);
|
|
27
29
|
if (type === 'string')
|
|
28
30
|
value = value.toString();
|
|
29
|
-
if (type === 'number')
|
|
30
|
-
value = parseFloat(value);
|
|
31
31
|
filter[paramNameFiltered] = value;
|
|
32
32
|
}
|
|
33
33
|
return filter;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export type
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export type ValidationErrorMap = Record<string, string[]>;
|
|
2
|
+
export type ValidationErrorOptions = {
|
|
3
|
+
statusCode?: number;
|
|
4
|
+
errorName?: string;
|
|
5
5
|
};
|
|
6
|
-
export declare function validateWithClassValidator<T extends object>(dtoClass: new () => T, input: T): Promise<void>;
|
|
6
|
+
export declare function validateWithClassValidator<T extends object>(dtoClass: new () => T, input: T, options?: ValidationErrorOptions): Promise<void>;
|
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CustomServerError } from '../../http/errors/server';
|
|
2
2
|
import { validate } from 'class-validator';
|
|
3
|
-
export async function validateWithClassValidator(dtoClass, input) {
|
|
3
|
+
export async function validateWithClassValidator(dtoClass, input, options = {}) {
|
|
4
4
|
const instance = Object.assign(new dtoClass(), input);
|
|
5
5
|
const errors = await validate(instance);
|
|
6
6
|
if (errors.length === 0)
|
|
7
7
|
return;
|
|
8
8
|
const details = flattenErrors(errors);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
});
|
|
9
|
+
const statusCode = options.statusCode ?? 400;
|
|
10
|
+
const errorName = options.errorName ?? 'BadRequest';
|
|
11
|
+
throw new CustomServerError({ errors: details }, statusCode, errorName);
|
|
13
12
|
}
|
|
14
13
|
function flattenErrors(errors, parentPath = '') {
|
|
15
|
-
const output =
|
|
14
|
+
const output = {};
|
|
16
15
|
for (const error of errors) {
|
|
17
16
|
const path = parentPath ? `${parentPath}.${error.property}` : error.property;
|
|
18
17
|
if (error.constraints) {
|
|
19
|
-
|
|
20
|
-
output.push({ path, rule, message });
|
|
21
|
-
}
|
|
18
|
+
output[path] = Object.values(error.constraints);
|
|
22
19
|
}
|
|
23
20
|
if (error.children && error.children.length > 0) {
|
|
24
|
-
|
|
21
|
+
Object.assign(output, flattenErrors(error.children, path));
|
|
25
22
|
}
|
|
26
23
|
}
|
|
27
24
|
return output;
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
+
import type { ValidationErrorOptions } from '../../interface/validation/class-validator';
|
|
1
2
|
export interface Validator<T = any> {
|
|
2
3
|
validate(input: T): void | Promise<void>;
|
|
3
4
|
}
|
|
4
5
|
export type ValidatorClass<T = any> = new () => T;
|
|
5
6
|
export type ValidatorLike<T = any> = Validator<T> | ValidatorClass<T>;
|
|
7
|
+
export type ValidatorDefinition<T = any> = ValidatorLike<T> | {
|
|
8
|
+
validator: ValidatorLike<T>;
|
|
9
|
+
error?: ValidationErrorOptions;
|
|
10
|
+
};
|