framework-do-dede 3.1.0 → 3.3.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 {};
@@ -30,11 +30,17 @@ export function flushControllers() {
30
30
  });
31
31
  controllers = [];
32
32
  }
33
+ function isClass(fn) {
34
+ return /^\s*class\s/.test(Function.prototype.toString.call(fn));
35
+ }
33
36
  export function UseMiddleware(middlewareClass) {
34
37
  return function (target, propertyKey, descriptor) {
35
38
  if (typeof middlewareClass !== 'function' && !middlewareClass?.execute) {
36
39
  throw new FrameworkError('Middleware must implement execute()');
37
40
  }
41
+ if (typeof middlewareClass === 'function' && isClass(middlewareClass) && !middlewareClass.prototype?.execute) {
42
+ throw new FrameworkError('Middleware must implement execute()');
43
+ }
38
44
  if (typeof middlewareClass === 'function' && middlewareClass.prototype?.execute) {
39
45
  const middlewares = Reflect.getMetadata('middlewares', target, propertyKey) || [];
40
46
  middlewares.push(middlewareClass);
@@ -52,6 +58,9 @@ export function UseMiddlewares(middlewareClasses) {
52
58
  if (typeof middlewareClass !== 'function' && !middlewareClass?.execute) {
53
59
  throw new FrameworkError('Middleware must implement execute()');
54
60
  }
61
+ if (typeof middlewareClass === 'function' && isClass(middlewareClass) && !middlewareClass.prototype?.execute) {
62
+ throw new FrameworkError('Middleware must implement execute()');
63
+ }
55
64
  }
56
65
  const existingMiddlewares = Reflect.getMetadata('middlewares', target, propertyKey) || [];
57
66
  existingMiddlewares.push(...middlewareClasses);
@@ -66,6 +75,8 @@ export function Post(config = {}) {
66
75
  params: config.params,
67
76
  query: config.query,
68
77
  headers: config.headers,
78
+ body: config.body,
79
+ bodyFilter: config.bodyFilter || 'none',
69
80
  statusCode: config.statusCode || 200,
70
81
  responseType: config.responseType || 'json'
71
82
  }, target, propertyKey);
@@ -92,6 +103,8 @@ export function Put(config = {}) {
92
103
  params: config.params,
93
104
  query: config.query,
94
105
  headers: config.headers,
106
+ body: config.body,
107
+ bodyFilter: config.bodyFilter || 'none',
95
108
  statusCode: config.statusCode || 200,
96
109
  responseType: config.responseType || 'json'
97
110
  }, target, propertyKey);
@@ -105,6 +118,8 @@ export function Patch(config = {}) {
105
118
  params: config.params,
106
119
  query: config.query,
107
120
  headers: config.headers,
121
+ body: config.body,
122
+ bodyFilter: config.bodyFilter || 'none',
108
123
  statusCode: config.statusCode || 200,
109
124
  responseType: config.responseType || 'json'
110
125
  }, target, propertyKey);
@@ -118,6 +133,8 @@ export function Delete(config = {}) {
118
133
  params: config.params,
119
134
  query: config.query,
120
135
  headers: config.headers,
136
+ body: config.body,
137
+ bodyFilter: config.bodyFilter || 'none',
121
138
  statusCode: config.statusCode || 200,
122
139
  responseType: config.responseType || 'json'
123
140
  }, target, propertyKey);
@@ -1,7 +1,5 @@
1
1
  export const USE_CASE_DECORATORS = Symbol('USE_CASE_DECORATORS');
2
2
  export class UseCase {
3
- data;
4
- context;
5
3
  constructor(input) {
6
4
  this.data = input?.data || undefined;
7
5
  if (input?.context) {
package/dist/dede.js CHANGED
@@ -3,9 +3,6 @@ import { ElysiaServerAdapter } from "./http/elysia-server.adapter";
3
3
  import { ExpressServerAdapter } from "./http/express-server.adapter";
4
4
  import { Registry } from "./infra/di/registry";
5
5
  export class Dede {
6
- framework;
7
- defaultServerError;
8
- httpServer;
9
6
  constructor(framework, defaultServerError) {
10
7
  this.framework = framework;
11
8
  this.defaultServerError = defaultServerError;
@@ -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,11 +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
- let body = input.body || {};
32
- if (Object.keys(body).length > 0) {
33
- body = this.normalizeBracketNotation(body);
31
+ const normalizeBody = this.normalizeBracketNotation(input.body);
32
+ let filterBody = this.filter(normalizeBody, body);
33
+ if (bodyFilter !== 'restrict') {
34
+ filterBody = { ...normalizeBody, ...filterBody };
34
35
  }
35
- mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...body };
36
+ mergedParams = { ...filterHeaders, ...filterParams, ...filterQueryParams, ...filterBody };
36
37
  request = { data: mergedParams, context: {} };
37
38
  middlewaresExecuted = await this.executeMiddlewares(middlewares, request);
38
39
  const response = await handler.instance[handler.methodName](request);
@@ -120,6 +121,8 @@ export default class ControllerHandler {
120
121
  params: routeConfig.params,
121
122
  query: routeConfig.query,
122
123
  headers: routeConfig.headers,
124
+ body: routeConfig.body,
125
+ bodyFilter: routeConfig.bodyFilter,
123
126
  statusCode: routeConfig.statusCode,
124
127
  handler: {
125
128
  instance,
@@ -1,5 +1,4 @@
1
1
  export class ServerError extends Error {
2
- statusCode;
3
2
  constructor(message, statusCode) {
4
3
  super(message);
5
4
  this.name = this.constructor.name;
@@ -10,8 +9,6 @@ export class ServerError extends Error {
10
9
  }
11
10
  }
12
11
  export class CustomServerError extends Error {
13
- statusCode;
14
- custom;
15
12
  constructor(custom, statusCode, nameError = '') {
16
13
  super();
17
14
  this.name = nameError || this.constructor.name;
@@ -56,7 +53,6 @@ export class BadRequest extends ServerError {
56
53
  }
57
54
  }
58
55
  export class InternalServerError extends ServerError {
59
- unexpectedError;
60
56
  constructor(unexpectedError, defaultMessage = 'Ops, An unexpected error occurred') {
61
57
  super(defaultMessage, 500);
62
58
  this.unexpectedError = unexpectedError;
@@ -18,6 +18,8 @@ export type HttpServerParams = {
18
18
  statusCode?: number;
19
19
  params?: string[];
20
20
  query?: string[];
21
+ body?: string[];
22
+ bodyFilter?: 'none' | 'restrict';
21
23
  headers?: string[];
22
24
  };
23
25
  type FrameworkWeb = {
@@ -1,8 +1,5 @@
1
1
  import { FrameworkError } from "./errors/framework";
2
2
  export default class HttpServer {
3
- framework;
4
- frameworkName;
5
- defaultMessageError;
6
3
  constructor(framework, frameworkName) {
7
4
  if (frameworkName !== 'elysia' && frameworkName !== 'express')
8
5
  throw new FrameworkError('Framework not supported');
@@ -1,6 +1,7 @@
1
1
  class ComponentRegistry {
2
- static instance;
3
- dependencies = new Map();
2
+ constructor() {
3
+ this.dependencies = new Map();
4
+ }
4
5
  static getInstance() {
5
6
  if (!this.instance) {
6
7
  this.instance = new ComponentRegistry();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "3.1.0",
3
+ "version": "3.3.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -12,7 +12,7 @@
12
12
  "require": "./dist/index.cjs"
13
13
  },
14
14
  "scripts": {
15
- "test": "jest && jest ./example/test",
15
+ "test": "jest",
16
16
  "test:watch": "jest --watch",
17
17
  "clean": "rimraf dist",
18
18
  "build": "npm run clean && tsc -p tsconfig.build.json && tsc-alias",