From 20f242e41d9f6efb52f3fa9caf4c31d38c62e818 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 11 Aug 2024 00:29:09 +0530 Subject: [PATCH 1/4] remove unnecessary logs --- lib/exceptions/intentExceptionFilter.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/exceptions/intentExceptionFilter.ts b/lib/exceptions/intentExceptionFilter.ts index 6245c40..d105245 100644 --- a/lib/exceptions/intentExceptionFilter.ts +++ b/lib/exceptions/intentExceptionFilter.ts @@ -30,7 +30,6 @@ export abstract class IntentExceptionFilter extends BaseExceptionFilter { reportToSentry(exception: any): void { const sentryConfig = IntentConfig.get('app.sentry'); - console.log(sentryConfig); if (!sentryConfig?.dsn) return; const exceptionConstructor = exception?.constructor; @@ -39,7 +38,6 @@ export abstract class IntentExceptionFilter extends BaseExceptionFilter { exceptionConstructor && !this.doNotReport().includes(exceptionConstructor) ) { - console.log(exceptionConstructor); sentry.captureException(exception); } } From 83fe62b2b2bddb29e7a1f2bb0a104b49e87a9ea6 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 11 Aug 2024 00:30:45 +0530 Subject: [PATCH 2/4] Sync main branch with release/v0.1.3 (#10) * import bugs * Release/v0.1.21 (#7) * wip github workflow on PR * change the pull request workflow * add support for prettier and eslint * fix prettier command targetting wrong files * formatted files as per the prettier rules * update gitignore * fix some eslint issues * change eslint rules * import bugs * bump version * bump version * Bugfix/v0.1.22 (#8) * correct file name * fix validation bugs * version bump to v0.1.23 --- .eslintrc.js | 1 + lib/index.ts | 2 ++ lib/rest/decorators.ts | 15 +++++++++++++++ lib/rest/decoratorts.ts | 15 --------------- lib/rest/index.ts | 3 ++- lib/rest/request.ts | 20 ++++++++++---------- lib/rest/restServer.ts | 15 +++++++++++++-- lib/validator/validationGuard.ts | 7 ++++--- package.json | 2 +- 9 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 lib/rest/decorators.ts delete mode 100644 lib/rest/decoratorts.ts 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/index.ts b/lib/index.ts index 6b382a9..37684e9 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -15,3 +15,5 @@ export * from './database'; export * from './serializers/validationErrorSerializer'; export * from './mailer'; export * from './events'; +export * from './validator'; +export * from './config/service'; 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; } } diff --git a/package.json b/package.json index 1813611..78f4825 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@intentjs/core", - "version": "0.1.2", + "version": "0.1.23", "description": "Core module for Intent", "main": "dist/lib/index.js", "types": "dist/lib/index.d.ts", From b05bf1d7da24dd999da51bff73492a2091bfe649 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 11 Aug 2024 00:37:03 +0530 Subject: [PATCH 3/4] fix bearerToken method in request class --- lib/rest/request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rest/request.ts b/lib/rest/request.ts index 164463e..1bb944e 100644 --- a/lib/rest/request.ts +++ b/lib/rest/request.ts @@ -88,7 +88,7 @@ export class Request { bearerToken(): string { const authHeader = this.$headers['authorization']; const asArray = authHeader?.split(' '); - return !isEmpty(asArray) && asArray(' ')[1]; + return !isEmpty(asArray) && asArray[1]; } host(): string { From 9d93dc7427fafcd3f5223b78c8eaeccdc0ba8b28 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 11 Aug 2024 00:38:48 +0530 Subject: [PATCH 4/4] add setUser and user method in request class --- lib/rest/request.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/rest/request.ts b/lib/rest/request.ts index 1bb944e..63f1405 100644 --- a/lib/rest/request.ts +++ b/lib/rest/request.ts @@ -12,12 +12,14 @@ export class Request { private $body: Record; private $dto: any; private id: string; + private $user: Record; constructor(private request: ERequest) { this.$payload = {}; this.$headers = {}; this.initiate(request); this.id = ulid(); + this.$user = null; this.$query = request.query; this.$body = request.body; this.$pathParams = request.params; @@ -154,6 +156,14 @@ export class Request { .validate({ ...this.all() }); } + setUser(user: any): void { + this.$user = user; + } + + user(): T { + return this.$user as T; + } + only(...keys: string[]): Record { return {}; }