diff --git a/.eslintrc.js b/.eslintrc.js index f2a0ff9..bbef400 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -30,6 +30,7 @@ module.exports = { ], rules: { 'no-console': 'error', + '@typescript-eslint/no-explicit-any': 'off', '@typescript-eslint/no-inferrable-types': 'error', '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', diff --git a/lib/rest/decorators.ts b/lib/rest/decorators.ts new file mode 100644 index 0000000..6d0bf68 --- /dev/null +++ b/lib/rest/decorators.ts @@ -0,0 +1,15 @@ +import { ExecutionContext, createParamDecorator } from '@nestjs/common'; + +export const Req = createParamDecorator( + (data: string, ctx: ExecutionContext) => { + const request = ctx.switchToHttp().getRequest(); + return request.intent.req(); + }, +); + +// export const Res = createParamDecorator( +// (data: string, ctx: ExecutionContext) => { +// const request = ctx.switchToHttp().getRequest(); +// return request.intent.res(); +// }, +// ); diff --git a/lib/rest/decoratorts.ts b/lib/rest/decoratorts.ts deleted file mode 100644 index 038e6ed..0000000 --- a/lib/rest/decoratorts.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { ExecutionContext, createParamDecorator } from '@nestjs/common'; - -export const IReq = createParamDecorator( - (data: string, ctx: ExecutionContext) => { - const request = ctx.switchToHttp().getRequest(); - return request['$intent']['req']; - }, -); - -export const IRes = createParamDecorator( - (data: string, ctx: ExecutionContext) => { - const request = ctx.switchToHttp().getRequest(); - return request['$intent']['res']; - }, -); diff --git a/lib/rest/index.ts b/lib/rest/index.ts index 8d5ba8d..2b83438 100644 --- a/lib/rest/index.ts +++ b/lib/rest/index.ts @@ -2,6 +2,7 @@ export * from './restController'; export * from './restServer'; export * from './interceptors/timeout'; export * from './interfaces'; -export * from './decoratorts'; +export * from './decorators'; +export { Res } from '@nestjs/common'; export { Response } from 'express'; export * from './request'; diff --git a/lib/rest/request.ts b/lib/rest/request.ts index 2716155..164463e 100644 --- a/lib/rest/request.ts +++ b/lib/rest/request.ts @@ -16,15 +16,19 @@ export class Request { constructor(private request: ERequest) { this.$payload = {}; this.$headers = {}; - this.initiate(); + this.initiate(request); this.id = ulid(); + this.$query = request.query; + this.$body = request.body; + this.$pathParams = request.params; + this.$headers = request.headers; } - private initiate() { - this.$query = this.request.query; - this.$body = this.request.body; - this.$pathParams = this.request.params; - this.$headers = this.request.headers; + private initiate(request: ERequest) { + this.$query = request.query; + this.$body = request.body; + this.$pathParams = request.params; + this.$headers = request.headers; this.$payload = { ...this.$query, ...this.$pathParams, ...this.$body }; } @@ -151,22 +155,18 @@ export class Request { } only(...keys: string[]): Record { - console.log(keys); return {}; } except(...keys: string[]): Record { - console.log(keys); return {}; } is(pathPattern: string): boolean { - console.log(pathPattern); return false; } has(...keys: string[]): boolean { - console.log('kjeys ===> ', keys); for (const key of keys) { if (!(key in this.$payload)) return false; } diff --git a/lib/rest/restServer.ts b/lib/rest/restServer.ts index 3b76f04..958085b 100644 --- a/lib/rest/restServer.ts +++ b/lib/rest/restServer.ts @@ -13,7 +13,9 @@ export class RestServer { */ static async make(module: any, options?: ServerOptions): Promise { - const app = await NestFactory.create(module); + const app = await NestFactory.create(module, { + bodyParser: true, + }); if (options?.addValidationContainer) { useContainer(app.select(module), { fallbackOnErrors: true }); @@ -25,6 +27,13 @@ export class RestServer { app.enable(corsRule); } + /** + * Explicitly enable body parsers, so that they are available in all of the middlewares. + */ + app.useBodyParser('json'); + app.useBodyParser('raw'); + app.useBodyParser('urlencoded'); + app.use(requestMiddleware); this.configureErrorReporter(config.get('app.sentry')); @@ -34,7 +43,9 @@ export class RestServer { app.useGlobalFilters(options.exceptionFilter(httpAdapter)); } - options?.globalPrefix && app.setGlobalPrefix(options.globalPrefix); + if (options?.globalPrefix) { + app.setGlobalPrefix(options.globalPrefix); + } await app.listen(options?.port || config.get('app.port')); } diff --git a/lib/validator/validationGuard.ts b/lib/validator/validationGuard.ts index 3cce378..2cc37e7 100644 --- a/lib/validator/validationGuard.ts +++ b/lib/validator/validationGuard.ts @@ -1,5 +1,6 @@ import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; +import { Request as ERequest } from 'express'; import { Request } from '../rest'; @Injectable() @@ -7,10 +8,10 @@ export class IntentValidationGuard implements CanActivate { constructor(private reflector: Reflector) {} async canActivate(context: ExecutionContext): Promise { - const request = context.switchToHttp().getRequest() as Request; - const iRequest = request['intent'].req(); + const expressRequest = context.switchToHttp().getRequest() as ERequest; + const request = expressRequest['intent'].req() as Request; const schema = this.reflector.get('dtoSchema', context.getHandler()); - iRequest.addDto(await iRequest.validate(schema)); + request.addDto(await request.validate(schema)); return true; } }