framework-do-dede 1.0.23 → 1.0.25

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,4 +1,9 @@
1
1
  export declare function Restrict(): (target: any, propertyKey: string) => void;
2
+ export type ExposeConfig = {
3
+ mapping?: string | Record<string, string>;
4
+ deserialize?: (value: any) => any | Promise<any>;
5
+ };
6
+ export declare function Expose(configOrMapping: ExposeConfig | string): PropertyDecorator;
2
7
  export declare function VirtualProperty(propertyName: string): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
3
8
  type DbColumnConfig = {
4
9
  mapping?: string | Record<string, string>;
@@ -6,13 +6,37 @@ export function Restrict() {
6
6
  target.constructor._restrictedProperties.add(propertyKey);
7
7
  };
8
8
  }
9
+ export function Expose(configOrMapping) {
10
+ return function (target, propertyKey) {
11
+ const ctor = target.constructor;
12
+ const configs = ctor._exposeConfigs || (ctor._exposeConfigs = new Map());
13
+ if (typeof configOrMapping === "string") {
14
+ configs.set(propertyKey, [
15
+ ...(configs.get(propertyKey) || []),
16
+ {
17
+ mapping: configOrMapping,
18
+ deserialize: (value) => value
19
+ }
20
+ ]);
21
+ }
22
+ else {
23
+ configs.set(propertyKey, [
24
+ ...(configs.get(propertyKey) || []),
25
+ {
26
+ mapping: configOrMapping.mapping,
27
+ deserialize: configOrMapping.deserialize || ((value) => value)
28
+ }
29
+ ]);
30
+ }
31
+ };
32
+ }
9
33
  export function VirtualProperty(propertyName) {
10
34
  return function (target, methodName, descriptor) {
11
35
  const ctor = target.constructor;
12
- if (!ctor._exposedProperties) {
13
- ctor._exposedProperties = new Map();
36
+ if (!ctor._virtualProperties) {
37
+ ctor._virtualProperties = new Map();
14
38
  }
15
- ctor._exposedProperties.set(propertyName, methodName);
39
+ ctor._virtualProperties.set(propertyName, methodName);
16
40
  };
17
41
  }
18
42
  export function DbColumn(config) {
@@ -1,6 +1,6 @@
1
1
  import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Metrics, OffConsoleLog } from './controller';
2
2
  import { Context } from './usecase';
3
3
  import { Inject } from './di';
4
- import { Restrict, DbColumn, VirtualProperty } from './entity';
4
+ import { Restrict, DbColumn, VirtualProperty, Expose } from './entity';
5
5
  import { Storage } from './services';
6
- export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, VirtualProperty };
6
+ export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, Expose, VirtualProperty };
@@ -1,6 +1,6 @@
1
1
  import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Metrics, OffConsoleLog } from './controller';
2
2
  import { Context } from './usecase';
3
3
  import { Inject } from './di';
4
- import { Restrict, DbColumn, VirtualProperty } from './entity';
4
+ import { Restrict, DbColumn, VirtualProperty, Expose } from './entity';
5
5
  import { Storage } from './services';
6
- export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, VirtualProperty };
6
+ export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, Expose, VirtualProperty };
@@ -1,7 +1,13 @@
1
1
  import { Registry } from "../di/registry";
2
2
  import { StorageGateway } from "../protocols/StorageGateway";
3
+ import { Log } from "../utils/Log";
4
+ import { Testing } from "../utils/Testing";
3
5
  export function Storage(gatewayName) {
4
6
  return function (target, propertyKey) {
7
+ if (Testing.isEnabled()) {
8
+ Log.info(`@Storage ${gatewayName} is ignored in testing mode`);
9
+ return;
10
+ }
5
11
  // Verifica se a classe está registrada
6
12
  if (!Registry.has(gatewayName)) {
7
13
  throw new Error(`StorageGateway ${gatewayName} not registered`);
@@ -2,7 +2,8 @@ export class Entity {
2
2
  async get() {
3
3
  const ctor = this.constructor;
4
4
  const restrictedProps = ctor._restrictedProperties || new Set();
5
- const exposedProps = ctor._exposedProperties || new Map();
5
+ const virtualProperties = ctor._virtualProperties || new Map();
6
+ const exposeConfigs = ctor._exposeConfigs || new Map();
6
7
  const attributes = {};
7
8
  await this.beforeGet();
8
9
  for (const [key, value] of Object.entries(this)) {
@@ -10,11 +11,43 @@ export class Entity {
10
11
  attributes[key] = value;
11
12
  }
12
13
  }
13
- for (const [propName, methodName] of exposedProps) {
14
+ for await (const [propName, methodName] of virtualProperties) {
14
15
  // @ts-ignore
15
16
  if (!restrictedProps.has(propName) && typeof this[methodName] === "function") {
16
17
  // @ts-ignore
17
- attributes[propName] = this[methodName]();
18
+ attributes[propName] = await this[methodName]();
19
+ }
20
+ }
21
+ for (const [propertyKey, configs] of exposeConfigs) {
22
+ if (restrictedProps.has(propertyKey))
23
+ continue;
24
+ const rawValue = this[propertyKey];
25
+ for (const config of configs) {
26
+ try {
27
+ let value = config.deserialize ? await config.deserialize(rawValue) : rawValue;
28
+ if (config.mapping) {
29
+ if (typeof config.mapping === "string") {
30
+ attributes[config.mapping] = value;
31
+ }
32
+ else {
33
+ Object.entries(config.mapping).forEach(([srcKey, destKey]) => {
34
+ // @ts-ignore
35
+ attributes[destKey] = value[srcKey];
36
+ });
37
+ }
38
+ }
39
+ else {
40
+ if (typeof value === "object" && value !== null) {
41
+ Object.assign(attributes, value);
42
+ }
43
+ else {
44
+ attributes[propertyKey] = value;
45
+ }
46
+ }
47
+ }
48
+ catch (error) {
49
+ console.error(`Error in @Expose for ${String(propertyKey)}:`, error);
50
+ }
18
51
  }
19
52
  }
20
53
  return attributes;
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Dede, Register as DedeRegister, Options as DedeOptions } from './dede';
2
- import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage } from './decorators';
2
+ import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage, Expose } from './decorators';
3
3
  import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
4
4
  import { Validation, HttpMiddleware, UseCase, CreateRepository, ExistsById, DeleteRepository, UpdateRepository, RestoreRepository, RestoreManyRepository, RequestMetricsHandler, RequestData, RequestMetrics, HttpServerError, StorageGateway } from './protocols';
5
5
  import { Entity } from './domain/Entity';
6
+ import { Testing } from './utils/Testing';
6
7
  declare class UseCaseHandler {
7
8
  static load<T extends UseCase<any, any>>(useCaseClass: new (...args: any[]) => T, request?: RequestData): T;
8
9
  }
9
- export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, RequestMetrics, HttpServerError, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, ExistsById, RestoreManyRepository, RequestData, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Entity, Restrict, DbColumn, VirtualProperty, Metrics, OffConsoleLog, StorageGateway, Storage };
10
+ export { UseCase, HttpMiddleware, Validation, RequestMetricsHandler, RequestMetrics, HttpServerError, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, ExistsById, RestoreManyRepository, RequestData, Dede, DedeRegister, DedeOptions, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Entity, Restrict, DbColumn, VirtualProperty, Metrics, OffConsoleLog, StorageGateway, Storage, Expose, Testing };
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Dede } from './dede';
2
- import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage } from './decorators';
2
+ import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage, Expose } from './decorators';
3
3
  import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
4
4
  import { Registry } from './di/registry';
5
5
  import { Entity } from './domain/Entity';
6
+ import { Testing } from './utils/Testing';
6
7
  class UseCaseHandler {
7
8
  static load(useCaseClass, request) {
8
9
  const instance = Registry.classLoader(useCaseClass);
@@ -16,4 +17,4 @@ class UseCaseHandler {
16
17
  return instance;
17
18
  }
18
19
  }
19
- export { Dede, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Entity, Restrict, DbColumn, VirtualProperty, Metrics, OffConsoleLog, Storage };
20
+ export { Dede, UseCaseHandler, ServerError, BadRequest, Conflict, Forbidden, HttpServer, NotFound, Unauthorized, UnprocessableEntity, Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Entity, Restrict, DbColumn, VirtualProperty, Metrics, OffConsoleLog, Storage, Expose, Testing };
@@ -0,0 +1,6 @@
1
+ export declare class Testing {
2
+ private static enabled;
3
+ static enable(): void;
4
+ static disable(): void;
5
+ static isEnabled(): typeof Testing.isEnabled;
6
+ }
@@ -0,0 +1,12 @@
1
+ export class Testing {
2
+ static enabled = false;
3
+ static enable() {
4
+ this.enabled = true;
5
+ }
6
+ static disable() {
7
+ this.enabled = false;
8
+ }
9
+ static isEnabled() {
10
+ return this.isEnabled;
11
+ }
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",