plutin 1.1.3 → 1.2.1
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/core/decorators/controller-http-decorator.cjs +1 -3
- package/dist/core/decorators/controller-http-decorator.cjs.map +1 -1
- package/dist/core/decorators/controller-http-decorator.js +1 -3
- package/dist/core/decorators/controller-http-decorator.js.map +1 -1
- package/dist/core/http/base-controller.cjs +1 -1
- package/dist/core/http/base-controller.cjs.map +1 -1
- package/dist/core/http/base-controller.js +1 -3
- package/dist/core/http/base-controller.js.map +1 -1
- package/dist/index.cjs +189 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +186 -4
- package/dist/index.js.map +1 -1
- package/dist/infra/adapters/http/express-adapter.cjs +5 -1
- package/dist/infra/adapters/http/express-adapter.cjs.map +1 -1
- package/dist/infra/adapters/http/express-adapter.d.cts +1 -1
- package/dist/infra/adapters/http/express-adapter.d.ts +1 -1
- package/dist/infra/adapters/http/express-adapter.js +1 -1
- package/dist/infra/adapters/http/express-adapter.js.map +1 -1
- package/dist/infra/adapters/http/fastify-adapter.cjs +5 -1
- package/dist/infra/adapters/http/fastify-adapter.cjs.map +1 -1
- package/dist/infra/adapters/http/fastify-adapter.d.cts +1 -1
- package/dist/infra/adapters/http/fastify-adapter.d.ts +1 -1
- package/dist/infra/adapters/http/fastify-adapter.js +1 -1
- package/dist/infra/adapters/http/fastify-adapter.js.map +1 -1
- package/dist/infra/adapters/http/validate-controller-metadata.cjs.map +1 -1
- package/dist/infra/adapters/http/validate-controller-metadata.js.map +1 -1
- package/dist/infra/adapters/notifications/notification-factory.cjs +219 -0
- package/dist/infra/adapters/notifications/notification-factory.cjs.map +1 -0
- package/dist/infra/adapters/notifications/notification-factory.d.cts +18 -0
- package/dist/infra/adapters/notifications/notification-factory.d.ts +18 -0
- package/dist/infra/adapters/notifications/notification-factory.js +184 -0
- package/dist/infra/adapters/notifications/notification-factory.js.map +1 -0
- package/dist/infra/adapters/validators/zod/index.cjs.map +1 -1
- package/dist/infra/adapters/validators/zod/index.js.map +1 -1
- package/dist/infra/adapters/validators/zod/zod-validator.cjs.map +1 -1
- package/dist/infra/adapters/validators/zod/zod-validator.js.map +1 -1
- package/dist/infra/env/index.cjs +1 -1
- package/dist/infra/env/index.cjs.map +1 -1
- package/dist/infra/env/index.js +3 -1
- package/dist/infra/env/index.js.map +1 -1
- package/package.json +2 -10
|
@@ -25,9 +25,6 @@ __export(controller_http_decorator_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(controller_http_decorator_exports);
|
|
27
27
|
|
|
28
|
-
// src/core/http/base-controller.ts
|
|
29
|
-
var import_reflect_metadata2 = require("reflect-metadata");
|
|
30
|
-
|
|
31
28
|
// src/core/decorators/dependency-container.ts
|
|
32
29
|
var import_reflect_metadata = require("reflect-metadata");
|
|
33
30
|
var DependencyContainer = class {
|
|
@@ -180,6 +177,7 @@ var ValidationError = class extends Error {
|
|
|
180
177
|
};
|
|
181
178
|
|
|
182
179
|
// src/core/http/base-controller.ts
|
|
180
|
+
var import_reflect_metadata2 = require("reflect-metadata");
|
|
183
181
|
var BaseController = class {
|
|
184
182
|
static {
|
|
185
183
|
__name(this, "BaseController");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/decorators/controller-http-decorator.ts","../../../src/core/http/base-controller.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts"],"sourcesContent":["import { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { BaseController } from '../http/base-controller'\nimport type { MethodType } from '../http/http'\n\ntype Props = {\n method: MethodType\n path: string\n middlewares?: MiddlewareFunction[]\n}\n\nexport function Controller({\n method,\n path,\n middlewares = [],\n}: Props): ClassDecorator {\n return (target: any) => {\n if (!(target.prototype instanceof BaseController)) {\n throw new Error(\n `The class ${target.name} should extends abstract class BaseController`\n )\n }\n\n Reflect.defineMetadata(\n 'route',\n { method, path: `/api/${path}`, middlewares },\n target\n )\n }\n}\n","import 'reflect-metadata'\n\nimport { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { IErrorNotifier } from './error-notifier'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AACA;;;;;;;ACDA,IAAAA,2BAAO;;;ACAP,8BAAO;AAQA,IAAMC,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EAEA,OAAOC,QAAWC,QAAqB;AACrC,UAAMC,aAAaC,QAAQC,YAAY,qBAAqBH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YAAM,IAAIN,WAAW,2BAAA;IACvB;AAEA,UAAMS,cAAoCH,cAAcG,eAAe,CAAA;AAEvE,QAAIC,mBAAmBhC;AAEvB,QAAI+B,YAAYE,QAAQ;AACtB,iBAAWC,cAAcH,aAAa;AACpCC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;;;AD5JO,SAASI,WAAW,EACzBC,QACAC,MACAC,cAAc,CAAA,EAAE,GACV;AACN,SAAO,CAACC,WAAAA;AACN,QAAI,EAAEA,OAAOC,qBAAqBC,iBAAiB;AACjD,YAAM,IAAIC,MACR,aAAaH,OAAOI,IAAI,+CAA+C;IAE3E;AAEAC,YAAQC,eACN,SACA;MAAET;MAAQC,MAAM,QAAQA,IAAAA;MAAQC;IAAY,GAC5CC,MAAAA;EAEJ;AACF;AAlBgBJ;","names":["import_reflect_metadata","DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle","Controller","method","path","middlewares","target","prototype","BaseController","Error","name","Reflect","defineMetadata"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/core/decorators/controller-http-decorator.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts","../../../src/core/http/base-controller.ts"],"sourcesContent":["import { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { BaseController } from '../http/base-controller'\nimport type { MethodType } from '../http/http'\n\ntype Props = {\n method: MethodType\n path: string\n middlewares?: MiddlewareFunction[]\n}\n\nexport function Controller({\n method,\n path,\n middlewares = [],\n}: Props): ClassDecorator {\n return (target: any) => {\n if (!(target.prototype instanceof BaseController)) {\n throw new Error(\n `The class ${target.name} should extends abstract class BaseController`\n )\n }\n\n Reflect.defineMetadata(\n 'route',\n { method, path: `/api/${path}`, middlewares },\n target\n )\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n","import { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\n\nimport { IErrorNotifier } from './error-notifier'\n\nimport 'reflect-metadata'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await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ACF;AAEA,QAAIN,iBAAiBc,iBAAiB;AACpC,aAAO;QACL9B,MAAMgB,MAAMG,MAAMnB;QAClBC,MAAM;UACJmB,SAASJ,MAAMG,MAAMC;UACrBC,WAAWL,MAAMG,MAAME;UACvBC,YAAYN,MAAMG,MAAMG;UACxBS,QAAQf,MAAMG,MAAMY;QACtB;MACF;IACF;AAEA,QAAItB,QAAQD,IAAIwB,qBAAqB;AACnC,YAAM,KAAKtC,cAAcuC,OAAOjB,OAAOC,OAAAA;IACzC;AAEA,WAAO;MACLjB,MAAM;MACNC,MAAM;QACJD,MAAM;QACNoB,SAAS;MACX;IACF;EACF;EAEA,MAAac,QAAQ3B,SAAqC;AACxD,UAAM4B,gBAAgBC,QAAQC,YAAY,SAAS,KAAK1C,WAAW;AAEnE,QAAI,CAACwC,eAAe;AAClB,YAAM,IAAIN,WAAW,2BAAA;IACvB;AAEA,UAAMS,cAAoCH,cAAcG,eAAe,CAAA;AAEvE,QAAIC,mBAAmBhC;AAEvB,QAAI+B,YAAYE,QAAQ;AACtB,iBAAWC,cAAcH,aAAa;AACpCC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;;;AR7JO,SAASI,WAAW,EACzBC,QACAC,MACAC,cAAc,CAAA,EAAE,GACV;AACN,SAAO,CAACC,WAAAA;AACN,QAAI,EAAEA,OAAOC,qBAAqBC,iBAAiB;AACjD,YAAM,IAAIC,MACR,aAAaH,OAAOI,IAAI,+CAA+C;IAE3E;AAEAC,YAAQC,eACN,SACA;MAAET;MAAQC,MAAM,QAAQA,IAAAA;MAAQC;IAAY,GAC5CC,MAAAA;EAEJ;AACF;AAlBgBJ;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","import_reflect_metadata","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle","Controller","method","path","middlewares","target","prototype","BaseController","Error","name","Reflect","defineMetadata"]}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/core/http/base-controller.ts
|
|
5
|
-
import "reflect-metadata";
|
|
6
|
-
|
|
7
4
|
// src/core/decorators/dependency-container.ts
|
|
8
5
|
import "reflect-metadata";
|
|
9
6
|
var DependencyContainer = class {
|
|
@@ -156,6 +153,7 @@ var ValidationError = class extends Error {
|
|
|
156
153
|
};
|
|
157
154
|
|
|
158
155
|
// src/core/http/base-controller.ts
|
|
156
|
+
import "reflect-metadata";
|
|
159
157
|
var BaseController = class {
|
|
160
158
|
static {
|
|
161
159
|
__name(this, "BaseController");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/http/base-controller.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts","../../../src/core/decorators/controller-http-decorator.ts"],"sourcesContent":["import 'reflect-metadata'\n\nimport { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { IErrorNotifier } from './error-notifier'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n","import { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { BaseController } from '../http/base-controller'\nimport type { MethodType } from '../http/http'\n\ntype Props = {\n method: MethodType\n path: string\n middlewares?: MiddlewareFunction[]\n}\n\nexport function Controller({\n method,\n path,\n middlewares = [],\n}: Props): ClassDecorator {\n return (target: any) => {\n if (!(target.prototype instanceof BaseController)) {\n throw new Error(\n `The class ${target.name} should extends abstract class BaseController`\n )\n }\n\n Reflect.defineMetadata(\n 'route',\n { method, path: `/api/${path}`, middlewares },\n target\n )\n }\n}\n"],"mappings":";;;;AAAA,OAAO;;;ACAP,OAAO;AAQA,IAAMA,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;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AAcuC,OAAOjB,OAAOC,OAAAA;IACzC;AAEA,WAAO;MACLjB,MAAM;MACNC,MAAM;QACJD,MAAM;QACNoB,SAAS;MACX;IACF;EACF;EAEA,MAAac,QAAQ3B,SAAqC;AACxD,UAAM4B,gBAAgBC,QAAQC,YAAY,SAAS,KAAK1C,WAAW;AAEnE,QAAI,CAACwC,eAAe;AAClB,YAAM,IAAIN,WAAW,2BAAA;IACvB;AAEA,UAAMS,cAAoCH,cAAcG,eAAe,CAAA;AAEvE,QAAIC,mBAAmBhC;AAEvB,QAAI+B,YAAYE,QAAQ;AACtB,iBAAWC,cAAcH,aAAa;AACpCC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;;;AQ5JO,SAASI,WAAW,EACzBC,QACAC,MACAC,cAAc,CAAA,EAAE,GACV;AACN,SAAO,CAACC,WAAAA;AACN,QAAI,EAAEA,OAAOC,qBAAqBC,iBAAiB;AACjD,YAAM,IAAIC,MACR,aAAaH,OAAOI,IAAI,+CAA+C;IAE3E;AAEAC,YAAQC,eACN,SACA;MAAET;MAAQC,MAAM,QAAQA,IAAAA;MAAQC;IAAY,GAC5CC,MAAAA;EAEJ;AACF;AAlBgBJ;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle","Controller","method","path","middlewares","target","prototype","BaseController","Error","name","Reflect","defineMetadata"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts","../../../src/core/http/base-controller.ts","../../../src/core/decorators/controller-http-decorator.ts"],"sourcesContent":["import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n","import { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\n\nimport { IErrorNotifier } from './error-notifier'\n\nimport 'reflect-metadata'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { BaseController } from '../http/base-controller'\nimport type { MethodType } from '../http/http'\n\ntype Props = {\n method: MethodType\n path: string\n middlewares?: MiddlewareFunction[]\n}\n\nexport function Controller({\n method,\n path,\n middlewares = [],\n}: Props): ClassDecorator {\n return (target: any) => {\n if (!(target.prototype instanceof BaseController)) {\n throw new Error(\n `The class ${target.name} should extends abstract class BaseController`\n )\n }\n\n Reflect.defineMetadata(\n 'route',\n { method, path: `/api/${path}`, middlewares },\n target\n )\n }\n}\n"],"mappings":";;;;AAAA,OAAO;AAQA,IAAMA,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EAEA,OAAOC,QAAWC,QAAqB;AACrC,UAAMC,aAAaC,QAAQC,YAAY,qBAAqBH,MAAAA,KAAW,CAAA;AAEvE,UAAMI,iBACJF,QAAQG,eAAe,iBAAiBL,MAAAA,KAAW,CAAC;AAEtD,UAAMM,SAASL,WAAWM,IAAI,CAACC,GAAQC,UAAAA;AACrC,YAAMlB,QAAQa,eAAeK,KAAAA;AAE7B,UAAI,CAAClB,OAAO;AACV,cAAM,IAAImB,MACR,6CAA6CD,KAAAA,OAAYT,OAAOW,IAAI,EAAE;MAE1E;AAEA,aAAO,KAAKC,aAAarB,KAAAA;IAC3B,CAAA;AAEA,WAAO,IAAIS,OAAAA,GAAUM,MAAAA;EACvB;EAEA,OAAOM,aAAarB,OAAoB;AACtC,UAAMsB,eAAe,KAAK1B,SAAS2B,IAAIvB,KAAAA;AAEvC,QAAI,CAACsB,cAAc;AACjB,YAAM,IAAIH,MACR,IAAInB,KAAAA,wDAA6D;IAErE;AAEA,QAAIsB,aAAalB,SAA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;AACF;AAlBgBJ;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle","Controller","method","path","middlewares","target","prototype","BaseController","Error","name","Reflect","defineMetadata"]}
|
|
@@ -24,7 +24,6 @@ __export(base_controller_exports, {
|
|
|
24
24
|
BaseController: () => BaseController
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(base_controller_exports);
|
|
27
|
-
var import_reflect_metadata2 = require("reflect-metadata");
|
|
28
27
|
|
|
29
28
|
// src/core/decorators/dependency-container.ts
|
|
30
29
|
var import_reflect_metadata = require("reflect-metadata");
|
|
@@ -178,6 +177,7 @@ var ValidationError = class extends Error {
|
|
|
178
177
|
};
|
|
179
178
|
|
|
180
179
|
// src/core/http/base-controller.ts
|
|
180
|
+
var import_reflect_metadata2 = require("reflect-metadata");
|
|
181
181
|
var BaseController = class {
|
|
182
182
|
static {
|
|
183
183
|
__name(this, "BaseController");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/http/base-controller.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts"],"sourcesContent":["import 'reflect-metadata'\n\nimport { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { IErrorNotifier } from './error-notifier'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;IAAAA,2BAAO;;;ACAP,8BAAO;AAQA,IAAMC,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EAEA,OAAOC,QAAW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AAS,KAAK1C,WAAW;AAEnE,QAAI,CAACwC,eAAe;AAClB,YAAM,IAAIN,WAAW,2BAAA;IACvB;AAEA,UAAMS,cAAoCH,cAAcG,eAAe,CAAA;AAEvE,QAAIC,mBAAmBhC;AAEvB,QAAI+B,YAAYE,QAAQ;AACtB,iBAAWC,cAAcH,aAAa;AACpCC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;","names":["import_reflect_metadata","DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/core/http/base-controller.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts"],"sourcesContent":["import { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\n\nimport { IErrorNotifier } from './error-notifier'\n\nimport 'reflect-metadata'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;ACAA,8BAAO;AAQA,IAAMA,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EAEA,OAAOC,QAAWC,QAAqB;AACrC,UAAMC,aAAaC,QAAQC,YAAY,qBAAqBH,MAAAA,KAAW,CAAA;AAEvE,UAAMI,iBACJF,QAAQG,eAAe,iBAAiBL,MAAAA,KAAW,CAAC;AAEtD,UAAMM,SAASL,WAAWM,IAAI,CAACC,GAAQC,UAAAA;AACrC,YAAMlB,QAAQa,eAAeK,KAAAA;AAE7B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CC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","import_reflect_metadata","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle"]}
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/core/http/base-controller.ts
|
|
5
|
-
import "reflect-metadata";
|
|
6
|
-
|
|
7
4
|
// src/core/decorators/dependency-container.ts
|
|
8
5
|
import "reflect-metadata";
|
|
9
6
|
var DependencyContainer = class {
|
|
@@ -156,6 +153,7 @@ var ValidationError = class extends Error {
|
|
|
156
153
|
};
|
|
157
154
|
|
|
158
155
|
// src/core/http/base-controller.ts
|
|
156
|
+
import "reflect-metadata";
|
|
159
157
|
var BaseController = class {
|
|
160
158
|
static {
|
|
161
159
|
__name(this, "BaseController");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/core/http/base-controller.ts","../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts"],"sourcesContent":["import 'reflect-metadata'\n\nimport { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\nimport { IErrorNotifier } from './error-notifier'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n"],"mappings":";;;;AAAA,OAAO;;;ACAP,OAAO;AAQA,IAAMA,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EA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,QAAQC,YAAY,SAAS,KAAK1C,WAAW;AAEnE,QAAI,CAACwC,eAAe;AAClB,YAAM,IAAIN,WAAW,2BAAA;IACvB;AAEA,UAAMS,cAAoCH,cAAcG,eAAe,CAAA;AAEvE,QAAIC,mBAAmBhC;AAEvB,QAAI+B,YAAYE,QAAQ;AACtB,iBAAWC,cAAcH,aAAa;AACpCC,2BAAmB,MAAME,WAAWF,gBAAAA;MACtC;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/core/decorators/dependency-container.ts","../../../src/core/errors/api-common-error.ts","../../../src/core/errors/application-error.ts","../../../src/core/errors/conflict-error.ts","../../../src/core/errors/domain-error.ts","../../../src/core/errors/infra-error.ts","../../../src/core/errors/validation-error.ts","../../../src/core/http/base-controller.ts"],"sourcesContent":["import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","export type PropertiesError = {\n receivedValue?: any\n type: string\n message: string\n property: string | number | undefined\n propertyType?: string\n path?: string\n}\n\nexport type CommonError = {\n location: string\n propertyErrors?: PropertiesError[]\n}\n\nexport type ApiCommonError = {\n code: number\n occurredAt: Date\n message: string\n errorCode: string\n errors?: CommonError[]\n}\n\nexport enum ApiErrorEnum {\n DOMAIN = 'ERR001',\n APPLICATION = 'ERR002',\n INFRA = 'ERR003',\n HTTP_CLIENT = 'ERR004',\n VALIDATOR = 'ERR005',\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class ApplicationError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.APPLICATION,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\ntype ConflictProps = { id: string; [key: string]: unknown }\n\nexport default class ConflictError<T extends ConflictProps> extends Error {\n props: ApiCommonError\n conflictProps: T | T[]\n\n constructor(conflictProps: T | T[]) {\n super('Resource already exists.')\n this.conflictProps = conflictProps\n this.props = {\n code: 409,\n errorCode: ApiErrorEnum.APPLICATION,\n message: this.message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class DomainError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.DOMAIN,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import { type ApiCommonError, ApiErrorEnum } from './api-common-error'\n\nexport default class InfraError extends Error {\n props: ApiCommonError\n\n constructor(message: string) {\n super(message)\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.INFRA,\n message,\n occurredAt: new Date(),\n }\n }\n}\n","import {\n type ApiCommonError,\n ApiErrorEnum,\n type CommonError,\n} from './api-common-error'\n\nexport default class ValidationError extends Error {\n props: ApiCommonError\n\n constructor(errors: CommonError[]) {\n super('Validation Error')\n this.props = {\n code: 400,\n errorCode: ApiErrorEnum.VALIDATOR,\n message: 'Validation Error',\n occurredAt: new Date(),\n errors,\n }\n }\n}\n","import { DependencyContainer } from '../../core/decorators/dependency-container'\nimport ApplicationError from '../../core/errors/application-error'\nimport ConflictError from '../../core/errors/conflict-error'\nimport DomainError from '../../core/errors/domain-error'\nimport InfraError from '../../core/errors/infra-error'\nimport ValidationError from '../../core/errors/validation-error'\nimport { MiddlewareFunction } from '../../infra/adapters/validators/zod/zod-validator'\n\nimport { IErrorNotifier } from './error-notifier'\n\nimport 'reflect-metadata'\n\ntype AnyObject = Record<string, any>\n\nexport type Request = {\n body: AnyObject\n params: AnyObject\n headers: AnyObject\n query: AnyObject\n}\n\nexport type Response = {\n code: number\n data: any\n}\n\nexport type ContextError = {\n env: string\n user?: {\n id?: string\n name?: string\n email?: string\n }\n request?: {\n method?: string\n requestId?: string\n url?: string\n headers?: any\n query?: any\n body?: any\n params?: any\n }\n}\n\nexport abstract class BaseController {\n protected readonly errorNotifier: IErrorNotifier\n\n abstract handle<T>(request: T | Request): Promise<Response>\n\n constructor() {\n this.errorNotifier = DependencyContainer.resolveToken('IErrorNotifier')\n }\n\n protected success<T>(dto?: T): Response {\n return {\n code: 200,\n data: { data: dto },\n }\n }\n\n protected noContent(): Response {\n return {\n code: 204,\n data: undefined,\n }\n }\n\n protected created<T>(dto?: T): Response {\n return {\n code: 201,\n data: dto ? { data: dto } : undefined,\n }\n }\n\n protected paginated<T>(dto?: T): Response {\n return {\n code: 200,\n data: dto,\n }\n }\n\n protected buildContextError(request: Request): ContextError {\n return {\n env: process.env.ENVIRONMENT as string,\n request: {\n body: request.body,\n headers: request.headers,\n params: request.params,\n query: request.query,\n },\n }\n }\n\n public async failure(error: Error, context: ContextError): Promise<Response> {\n if (error instanceof ConflictError) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n items: Array.isArray(error.conflictProps)\n ? error.conflictProps\n : [error.conflictProps],\n },\n }\n }\n\n if (\n error instanceof DomainError ||\n error instanceof ApplicationError ||\n error instanceof InfraError\n ) {\n return {\n code: error.props.code,\n data: {\n message: error.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n },\n }\n }\n\n if (error instanceof ValidationError) {\n return {\n code: error.props.code,\n data: {\n message: error.props.message,\n errorCode: error.props.errorCode,\n occurredAt: error.props.occurredAt,\n errors: error.props.errors,\n },\n }\n }\n\n if (process.env.SHOULD_NOTIFY_ERROR) {\n await this.errorNotifier.notify(error, context)\n }\n\n return {\n code: 500,\n data: {\n code: 500,\n message: 'Server failed. Contact the administrator!',\n },\n }\n }\n\n public async execute(request: Request): Promise<Response> {\n const routeMetadata = Reflect.getMetadata('route', this.constructor)\n\n if (!routeMetadata) {\n throw new InfraError('Route metadata not found.')\n }\n\n const middlewares: MiddlewareFunction[] = routeMetadata.middlewares || []\n\n let processedRequest = request\n\n if (middlewares.length) {\n for (const middleware of middlewares) {\n processedRequest = await middleware(processedRequest)\n }\n }\n\n return await this.handle(processedRequest)\n }\n}\n"],"mappings":";;;;AAAA,OAAO;AAQA,IAAMA,sBAAN,MAAMA;EARb,OAQaA;;;EACX,OAAOC,WAAW,oBAAIC,IAAAA;EACtB,OAAOC,aAAa,oBAAID,IAAAA;EAExB,OAAOE,SACLC,OACAC,SACAC,SACA;AACA,SAAKN,SAASO,IAAIH,OAAO;MACvBI,MAAM;MACNH;MACAI,WAAWH,QAAQG;IACrB,CAAA;EACF;EAEA,OAAOC,cAAiBN,OAAeO,OAAU;AAC/C,SAAKX,SAASO,IAAIH,OAAO;MAAEI,MAAM;MAASG;IAAM,CAAA;EAClD;EAEA,OAAOC,QAAWC,QAAqB;AACrC,UAAMC,aAAaC,QAAQC,YAAY,qBAAqBH,MAAAA,KAAW,CAAA;AAEvE,UAAMI,iBACJF,QAAQG,eAAe,iBAAiBL,MAAAA,KAAW,CAAC;AAEtD,UAAMM,SAASL,WAAWM,IAAI,CAACC,GAAQC,UAAAA;AACrC,YAAMlB,QAAQa,eAAeK,KAAAA;AAE7B,UAAI,CAAClB,OAAO;AACV,cAAM,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;IACF;AAEA,WAAO,MAAM,KAAKG,OAAOH,gBAAAA;EAC3B;AACF;","names":["DependencyContainer","registry","Map","singletons","register","token","myClass","options","set","type","singleton","registerValue","value","resolve","target","paramTypes","Reflect","getMetadata","injectMetadata","getOwnMetadata","params","map","_","index","Error","name","resolveToken","registration","get","has","instance","ApiErrorEnum","ApplicationError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","APPLICATION","occurredAt","Date","ConflictError","Error","props","conflictProps","constructor","code","errorCode","ApiErrorEnum","APPLICATION","message","occurredAt","Date","DomainError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","DOMAIN","occurredAt","Date","InfraError","Error","props","constructor","message","code","errorCode","ApiErrorEnum","INFRA","occurredAt","Date","ValidationError","Error","props","constructor","errors","code","errorCode","ApiErrorEnum","VALIDATOR","message","occurredAt","Date","BaseController","errorNotifier","constructor","DependencyContainer","resolveToken","success","dto","code","data","noContent","undefined","created","paginated","buildContextError","request","env","process","ENVIRONMENT","body","headers","params","query","failure","error","context","ConflictError","props","message","errorCode","occurredAt","items","Array","isArray","conflictProps","DomainError","ApplicationError","InfraError","ValidationError","errors","SHOULD_NOTIFY_ERROR","notify","execute","routeMetadata","Reflect","getMetadata","middlewares","processedRequest","length","middleware","handle"]}
|