framework-do-dede 4.0.0 → 4.0.2

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.
@@ -1,5 +1,5 @@
1
1
  import 'reflect-metadata';
2
- import type { ValidatorLike } from "../interface/validation/validator";
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?: ValidatorLike;
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?: ValidatorLike;
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?: ValidatorLike;
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?: ValidatorLike;
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?: ValidatorLike;
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
- if (typeof validator === 'function') {
36
- await validateWithClassValidator(validator, request.data);
35
+ let validatorLike;
36
+ let options;
37
+ if (isValidatorDefinition(validator)) {
38
+ validatorLike = validator.validator;
39
+ options = validator.error;
37
40
  }
38
- else if (typeof validator.validate === 'function') {
39
- await validator.validate(request.data);
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 { ValidatorLike } from "../interface/validation/validator";
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?: ValidatorLike;
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,11 +18,12 @@ export let DefaultContainer = new Container();
18
18
  export function setDefaultContainer(container) {
19
19
  DefaultContainer = container;
20
20
  }
21
- export function Inject(name, container = DefaultContainer) {
21
+ export function Inject(name, container) {
22
22
  return function (target, propertyKey) {
23
23
  target[propertyKey] = new Proxy({}, {
24
24
  get(_, propertyKey) {
25
- const dependency = container.inject(name);
25
+ const resolvedContainer = container ?? DefaultContainer;
26
+ const dependency = resolvedContainer.inject(name);
26
27
  return dependency[propertyKey];
27
28
  }
28
29
  });
@@ -1,6 +1,6 @@
1
- export type ValidationErrorDetail = {
2
- path: string;
3
- message: string;
4
- rule: string;
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 { UnprocessableEntity } from '../../domain/errors/http-errors';
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
- throw new UnprocessableEntity('Validation error', {
10
- code: 'validation_error',
11
- details
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
- for (const [rule, message] of Object.entries(error.constraints)) {
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
- output.push(...flattenErrors(error.children, path));
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",