framework-do-dede 1.0.20 → 1.0.22

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,3 +1,8 @@
1
1
  export declare function Restrict(): (target: any, propertyKey: string) => void;
2
2
  export declare function VirtualProperty(propertyName: string): (target: any, methodName: string, descriptor: PropertyDescriptor) => void;
3
- export declare function DbColumn(mapping: string | Record<string, string>): (target: Object, propertyKey: string) => void;
3
+ type DbColumnConfig = {
4
+ mapping?: string | Record<string, string>;
5
+ serialize?: (value: any) => any | Promise<any>;
6
+ };
7
+ export declare function DbColumn(config: string | Record<string, string> | DbColumnConfig): (target: Object, propertyKey: string) => void;
8
+ export {};
@@ -15,15 +15,27 @@ export function VirtualProperty(propertyName) {
15
15
  ctor._exposedProperties.set(propertyName, methodName);
16
16
  };
17
17
  }
18
- export function DbColumn(mapping) {
18
+ export function DbColumn(config) {
19
19
  return function (target, propertyKey) {
20
20
  const ctor = target.constructor;
21
- if (!Object.prototype.hasOwnProperty.call(ctor, '_dbColumns')) {
22
- ctor._dbColumns = new Set();
21
+ let actualMapping;
22
+ let serialize;
23
+ if (typeof config === 'string') {
24
+ actualMapping = config;
25
+ }
26
+ else if (typeof config === 'object') {
27
+ if ('serialize' in config || 'mapping' in config) {
28
+ actualMapping = config.mapping ?? propertyKey;
29
+ // @ts-ignore
30
+ serialize = config.serialize;
31
+ }
32
+ else {
33
+ // @ts-ignore
34
+ actualMapping = config;
35
+ }
36
+ }
37
+ else {
38
+ throw new Error('Configuração inválida para @DbColumn');
23
39
  }
24
- ctor._dbColumns.add({
25
- property: propertyKey,
26
- mapping: mapping
27
- });
28
40
  };
29
41
  }
@@ -2,4 +2,5 @@ import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Metri
2
2
  import { Context } from './usecase';
3
3
  import { Inject } from './di';
4
4
  import { Restrict, DbColumn, VirtualProperty } from './entity';
5
- export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, VirtualProperty };
5
+ import { Storage } from './services';
6
+ export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, VirtualProperty };
@@ -2,4 +2,5 @@ import { Controller, Post, Get, Put, Delete, Patch, Validator, Middleware, Metri
2
2
  import { Context } from './usecase';
3
3
  import { Inject } from './di';
4
4
  import { Restrict, DbColumn, VirtualProperty } from './entity';
5
- export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, VirtualProperty };
5
+ import { Storage } from './services';
6
+ export { Controller, Middleware, Validator, Metrics, OffConsoleLog, Post, Get, Put, Delete, Patch, Context, Inject, Restrict, DbColumn, Storage, VirtualProperty };
@@ -0,0 +1 @@
1
+ export declare function Storage(): (target: any, propertyKey: string) => void;
@@ -0,0 +1,29 @@
1
+ import { Registry } from "../di/registry";
2
+ import { StorageGateway } from "../protocols/StorageGateway";
3
+ export function Storage() {
4
+ return function (target, propertyKey) {
5
+ const designType = Reflect.getMetadata('design:type', target, propertyKey);
6
+ if (!(designType?.prototype instanceof StorageGateway)) {
7
+ throw new Error(`@Storage() can only be used with StorageGateway subclasses`);
8
+ }
9
+ const gatewayName = designType.name;
10
+ if (!Registry.has(gatewayName)) {
11
+ throw new Error(`StorageGateway ${gatewayName} not registered`);
12
+ }
13
+ const instanceSymbol = Symbol();
14
+ Object.defineProperty(target, propertyKey, {
15
+ get: function () {
16
+ if (!this[instanceSymbol]) {
17
+ const GatewayClass = Registry.resolve(gatewayName);
18
+ this[instanceSymbol] = GatewayClass;
19
+ }
20
+ return this[instanceSymbol];
21
+ },
22
+ set: () => {
23
+ throw new Error('Cannot assign new value to @Storage() property');
24
+ },
25
+ enumerable: true,
26
+ configurable: true
27
+ });
28
+ };
29
+ }
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
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 } from './decorators';
2
+ import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage } from './decorators';
3
3
  import { BadRequest, Conflict, Forbidden, HttpServer, NotFound, ServerError, Unauthorized, UnprocessableEntity } from './http';
4
- import { Validation, HttpMiddleware, UseCase, CreateRepository, ExistsById, DeleteRepository, UpdateRepository, RestoreRepository, RestoreManyRepository, RequestMetricsHandler, RequestData, RequestMetrics, HttpServerError } from './protocols';
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
6
  declare class UseCaseHandler {
7
7
  static load<T extends UseCase<any, any>>(useCaseClass: new (...args: any[]) => T, request?: RequestData): T;
8
8
  }
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 };
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 };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Dede } from './dede';
2
- import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog } from './decorators';
2
+ import { Controller, Post, Put, Get, Delete, Patch, Validator, Middleware, Context, Inject, Restrict, Metrics, DbColumn, VirtualProperty, OffConsoleLog, Storage } 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';
@@ -16,4 +16,4 @@ class UseCaseHandler {
16
16
  return instance;
17
17
  }
18
18
  }
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 };
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 };
@@ -0,0 +1,5 @@
1
+ export declare abstract class StorageGateway {
2
+ save(file: File, path: string): Promise<void>;
3
+ get(key: string): Promise<void>;
4
+ delete(key: string): Promise<void>;
5
+ }
@@ -0,0 +1,11 @@
1
+ export class StorageGateway {
2
+ save(file, path) {
3
+ throw new Error(this.constructor.name + ' save method not implemented');
4
+ }
5
+ get(key) {
6
+ throw new Error(this.constructor.name + ' get method not implemented');
7
+ }
8
+ delete(key) {
9
+ throw new Error(this.constructor.name + ' delete method not implemented');
10
+ }
11
+ }
@@ -11,4 +11,5 @@ import type { RequestMetricsHandler } from './RequestMetricsHandler';
11
11
  import type { RequestData } from './RequestData';
12
12
  import type { RequestMetrics } from './RequestMetrics';
13
13
  import type { HttpServerError } from './HttpServerError';
14
- export type { RequestData, RequestMetrics, HttpMiddleware, UseCase, Validation, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RestoreManyRepository, RequestMetricsHandler, HttpServerError, ExistsById };
14
+ import type { StorageGateway } from './StorageGateway';
15
+ export type { RequestData, RequestMetrics, HttpMiddleware, UseCase, Validation, CreateRepository, DeleteRepository, UpdateRepository, RestoreRepository, RestoreManyRepository, RequestMetricsHandler, HttpServerError, StorageGateway, ExistsById };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",