From 6c15e7cc9d9bf21ad020d641b75358b94934e22c Mon Sep 17 00:00:00 2001 From: moesjarraf Date: Wed, 22 Feb 2023 16:40:24 +0100 Subject: [PATCH] feat: initial release --- package.json | 3 + src/api/base.controller.ts | 2 +- src/app/app.providers.ts | 2 +- src/common.module.ts | 2 +- src/index.ts | 14 +- src/modules/captcha/captcha.guard.ts | 5 +- .../exception}/common-exception.filter.ts | 4 +- src/modules/exception/enums/code.enum.ts | 3 - .../exception}/exception.module.ts | 4 +- .../exception}/http-exception.filter.ts | 13 +- src/modules/exception/i18n/code.i18n.ts | 195 ------------------ .../exception}/index-exception.filter.ts | 6 +- .../exception}/internal-exception.filter.ts | 15 +- 13 files changed, 24 insertions(+), 244 deletions(-) rename src/{filters => modules/exception}/common-exception.filter.ts (87%) delete mode 100644 src/modules/exception/enums/code.enum.ts rename src/{filters => modules/exception}/exception.module.ts (85%) rename src/{filters => modules/exception}/http-exception.filter.ts (72%) delete mode 100644 src/modules/exception/i18n/code.i18n.ts rename src/{filters => modules/exception}/index-exception.filter.ts (84%) rename src/{filters => modules/exception}/internal-exception.filter.ts (63%) diff --git a/package.json b/package.json index 1b7db85..682de9e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "Nest.js common libraries", "author": "Moesjarraf J", "license": "MIT", + "publishConfig": { + "access": "public" + }, "main": "dist/index.js", "files": [ "dist" diff --git a/src/api/base.controller.ts b/src/api/base.controller.ts index a4b1829..99db2eb 100644 --- a/src/api/base.controller.ts +++ b/src/api/base.controller.ts @@ -1,5 +1,5 @@ import { UseFilters, UsePipes, ValidationPipe } from '@nestjs/common'; -import { CommonExceptionFilter } from '../filters/common-exception.filter'; +import { CommonExceptionFilter } from '../modules/exception/common-exception.filter'; @UsePipes(new ValidationPipe({ whitelist: true })) @UseFilters(CommonExceptionFilter) diff --git a/src/app/app.providers.ts b/src/app/app.providers.ts index 993e3eb..04508e3 100644 --- a/src/app/app.providers.ts +++ b/src/app/app.providers.ts @@ -1,5 +1,5 @@ import { APP_FILTER } from '@nestjs/core'; -import { IndexExceptionFilter } from '../filters/index-exception.filter'; +import { IndexExceptionFilter } from '../modules/exception/index-exception.filter'; export const commonAppProviders = [ { diff --git a/src/common.module.ts b/src/common.module.ts index a333259..67ae59d 100644 --- a/src/common.module.ts +++ b/src/common.module.ts @@ -1,9 +1,9 @@ import { Global, Module } from '@nestjs/common'; -import { ExceptionModule } from './filters/exception.module'; import { CaptchaModule } from './modules/captcha/captcha.module'; import { ConfigModule } from './modules/config/config.module'; import { DatabaseModule } from './modules/database/database.module'; import { EmitterModule } from './modules/emitter/emitter.module'; +import { ExceptionModule } from './modules/exception/exception.module'; import { HttpAuthModule } from './modules/http-auth/http-auth.module'; import { LoggerModule } from './modules/logger/logger.module'; import { MailerModule } from './modules/mailer/mailer.module'; diff --git a/src/index.ts b/src/index.ts index b1848d7..88b53f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,13 +6,6 @@ export * from './app/classes/main.class'; export * from './app/app.controller'; export * from './app/app.module'; -// filters -export * from './filters/exception.module'; -export * from './filters/common-exception.filter'; -export * from './filters/http-exception.filter'; -export * from './filters/index-exception.filter'; -export * from './filters/internal-exception.filter'; - // sub modules export * from './modules/captcha/captcha.module'; export * from './modules/captcha/captcha.guard'; @@ -34,8 +27,11 @@ export * from './modules/database/classes/soft-delete-entity-service.class'; export * from './modules/emitter/emitter.service'; export * from './modules/emitter/emitter.module'; -export * from './modules/exception/enums/code.enum'; -export * from './modules/exception/i18n/code.i18n'; +export * from './modules/exception/exception.module'; +export * from './modules/exception/common-exception.filter'; +export * from './modules/exception/http-exception.filter'; +export * from './modules/exception/index-exception.filter'; +export * from './modules/exception/internal-exception.filter'; export * from './modules/http-auth/interfaces/auth-result.interface'; export * from './modules/http-auth/http-auth.decorator'; diff --git a/src/modules/captcha/captcha.guard.ts b/src/modules/captcha/captcha.guard.ts index b84422b..8e0f94e 100644 --- a/src/modules/captcha/captcha.guard.ts +++ b/src/modules/captcha/captcha.guard.ts @@ -5,7 +5,6 @@ import { Injectable, } from '@nestjs/common'; import { Request } from 'express'; -import { ExceptionCodeEnum } from '../exception/enums/code.enum'; @Injectable() export class CaptchaGuard implements CanActivate { @@ -17,9 +16,7 @@ export class CaptchaGuard implements CanActivate { } if (!(request as any).captchaValid) { - throw new BadRequestException( - ExceptionCodeEnum.InvalidCaptchaResponseGiven, - ); + throw new BadRequestException('invalid_captcha'); } return true; diff --git a/src/filters/common-exception.filter.ts b/src/modules/exception/common-exception.filter.ts similarity index 87% rename from src/filters/common-exception.filter.ts rename to src/modules/exception/common-exception.filter.ts index f07d7ae..9f0fd62 100644 --- a/src/filters/common-exception.filter.ts +++ b/src/modules/exception/common-exception.filter.ts @@ -1,5 +1,5 @@ -import { ConfigService } from '../modules/config/config.service'; -import { LoggerService } from '../modules/logger/logger.service'; +import { ConfigService } from '../config/config.service'; +import { LoggerService } from '../logger/logger.service'; import { ArgumentsHost, Catch, diff --git a/src/modules/exception/enums/code.enum.ts b/src/modules/exception/enums/code.enum.ts deleted file mode 100644 index d0a7bd4..0000000 --- a/src/modules/exception/enums/code.enum.ts +++ /dev/null @@ -1,3 +0,0 @@ -export enum ExceptionCodeEnum { - InvalidCaptchaResponseGiven = 'invalid_captcha_response_given', -} diff --git a/src/filters/exception.module.ts b/src/modules/exception/exception.module.ts similarity index 85% rename from src/filters/exception.module.ts rename to src/modules/exception/exception.module.ts index 07e708b..f91f512 100644 --- a/src/filters/exception.module.ts +++ b/src/modules/exception/exception.module.ts @@ -3,8 +3,8 @@ import { InternalExceptionFilter } from './internal-exception.filter'; import { IndexExceptionFilter } from './index-exception.filter'; import { CommonExceptionFilter } from './common-exception.filter'; import { Module } from '@nestjs/common'; -import { ConfigModule } from '../modules/config/config.module'; -import { LoggerModule } from '../modules/logger/logger.module'; +import { ConfigModule } from '../config/config.module'; +import { LoggerModule } from '../logger/logger.module'; export const ExceptionModuleConfig = { imports: [ConfigModule, LoggerModule], diff --git a/src/filters/http-exception.filter.ts b/src/modules/exception/http-exception.filter.ts similarity index 72% rename from src/filters/http-exception.filter.ts rename to src/modules/exception/http-exception.filter.ts index b48c2e4..2b4b9ed 100644 --- a/src/filters/http-exception.filter.ts +++ b/src/modules/exception/http-exception.filter.ts @@ -1,12 +1,9 @@ -import { snakeCase } from 'snake-case'; -import { ExceptionCodeI18n } from './../modules/exception/i18n/code.i18n'; -import { LoggerService } from './../modules/logger/logger.service'; +import { LoggerService } from './../logger/logger.service'; import { ArgumentsHost, Catch, ExceptionFilter, HttpException, - HttpStatus, Injectable, } from '@nestjs/common'; import { InternalExceptionFilter } from './internal-exception.filter'; @@ -39,11 +36,6 @@ export class HttpExceptionFilter implements ExceptionFilter { ? exResponse : exResponse.message || exResponse.error; const stack = exception.stack; - const locale = request.locale; - const type = typeof message === 'string' ? snakeCase(message) : undefined; - const translation = - ExceptionCodeI18n[type]?.[locale] || - ExceptionCodeI18n[HttpStatus[type?.toUpperCase?.()]]?.[locale]; this.logger.debug( `'[${method}] ${path} (${status})' failed with ${message}`, @@ -56,9 +48,8 @@ export class HttpExceptionFilter implements ExceptionFilter { path, method, code: status, - message: typeof message !== 'string' ? undefined : translation || message, + message, error: typeof message !== 'string' ? message : undefined, - type: translation ? type : undefined, time, }); } diff --git a/src/modules/exception/i18n/code.i18n.ts b/src/modules/exception/i18n/code.i18n.ts deleted file mode 100644 index a7c531a..0000000 --- a/src/modules/exception/i18n/code.i18n.ts +++ /dev/null @@ -1,195 +0,0 @@ -import { HttpStatus } from '@nestjs/common'; -import { Locale } from 'locale-enum'; -import { ExceptionCodeEnum } from './../enums/code.enum'; - -// @todo: see if we can make a provider for this that can be globally injected -// so that it can be modified based on the applications needs (extend this as base + add custom errors through injection) -export const ExceptionCodeI18n = { - // @note: custom error messages - [ExceptionCodeEnum.InvalidCaptchaResponseGiven]: { - [Locale.en_GB]: 'Invalid captcha response', - [Locale.nl_NL]: 'Ongeldige captcha response', - }, - - // @note: default http error messages - [HttpStatus.CONTINUE]: { - [Locale.en_GB]: 'Continue', - [Locale.nl_NL]: 'Doorgaan', - }, - [HttpStatus.SWITCHING_PROTOCOLS]: { - [Locale.en_GB]: 'Switching protocols', - [Locale.nl_NL]: 'Protocolwissel', - }, - [HttpStatus.PROCESSING]: { - [Locale.en_GB]: 'Processing', - [Locale.nl_NL]: 'Processing', - }, - [HttpStatus.OK]: { - [Locale.en_GB]: 'Ok', - [Locale.nl_NL]: 'Ok', - }, - [HttpStatus.CREATED]: { - [Locale.en_GB]: 'Created', - [Locale.nl_NL]: 'Aangemaakt', - }, - [HttpStatus.ACCEPTED]: { - [Locale.en_GB]: 'Accepted', - [Locale.nl_NL]: 'Aanvaard', - }, - [HttpStatus.NON_AUTHORITATIVE_INFORMATION]: { - [Locale.en_GB]: 'Non authorative information', - [Locale.nl_NL]: 'Niet-gemachtigde informatie', - }, - [HttpStatus.NO_CONTENT]: { - [Locale.en_GB]: 'No content', - [Locale.nl_NL]: 'Geen inhoud', - }, - [HttpStatus.RESET_CONTENT]: { - [Locale.en_GB]: 'Reset content', - [Locale.nl_NL]: 'Inhoud opnieuw instellen', - }, - [HttpStatus.PARTIAL_CONTENT]: { - [Locale.en_GB]: 'Partial content', - [Locale.nl_NL]: 'Gedeeltelijke inhoud', - }, - [HttpStatus.AMBIGUOUS]: { - [Locale.en_GB]: 'Ambiguous', - [Locale.nl_NL]: 'Meerkeuze', - }, - [HttpStatus.MOVED_PERMANENTLY]: { - [Locale.en_GB]: 'Moved permanently', - [Locale.nl_NL]: 'Definitief verplaatst', - }, - [HttpStatus.FOUND]: { - [Locale.en_GB]: 'Found', - [Locale.nl_NL]: 'Gevonden', - }, - [HttpStatus.SEE_OTHER]: { - [Locale.en_GB]: 'See other', - [Locale.nl_NL]: 'Zie andere', - }, - [HttpStatus.NOT_MODIFIED]: { - [Locale.en_GB]: 'Not modified', - [Locale.nl_NL]: 'Niet gewijzigd', - }, - [HttpStatus.TEMPORARY_REDIRECT]: { - [Locale.en_GB]: 'Temporary redirect', - [Locale.nl_NL]: 'Tijdelijke omleiding', - }, - [HttpStatus.PERMANENT_REDIRECT]: { - [Locale.en_GB]: 'Permanent redirect', - [Locale.nl_NL]: 'Definitieve omleiding', - }, - [HttpStatus.BAD_REQUEST]: { - [Locale.en_GB]: 'Bad request', - [Locale.nl_NL]: 'Foute aanvraag', - }, - [HttpStatus.UNAUTHORIZED]: { - [Locale.en_GB]: 'Unauthorized', - [Locale.nl_NL]: 'Niet geautoriseerd', - }, - [HttpStatus.PAYMENT_REQUIRED]: { - [Locale.en_GB]: 'Payment required', - [Locale.nl_NL]: 'Betaalde toegang', - }, - [HttpStatus.FORBIDDEN]: { - [Locale.en_GB]: 'Forbidden', - [Locale.nl_NL]: 'Verboden toegang', - }, - [HttpStatus.NOT_FOUND]: { - [Locale.en_GB]: 'Not found', - [Locale.nl_NL]: 'Niet gevonden', - }, - [HttpStatus.METHOD_NOT_ALLOWED]: { - [Locale.en_GB]: 'Method not allowed', - [Locale.nl_NL]: 'Methode niet toegestaan', - }, - [HttpStatus.NOT_ACCEPTABLE]: { - [Locale.en_GB]: 'Not acceptable', - [Locale.nl_NL]: 'Niet aanvaardbaar', - }, - [HttpStatus.PROXY_AUTHENTICATION_REQUIRED]: { - [Locale.en_GB]: 'Proxy authentication required', - [Locale.nl_NL]: 'Authenticatie op de proxyserver verplicht', - }, - [HttpStatus.REQUEST_TIMEOUT]: { - [Locale.en_GB]: 'Request timeout', - [Locale.nl_NL]: 'Aanvraagtijd verstreken', - }, - [HttpStatus.CONFLICT]: { - [Locale.en_GB]: 'Conflict', - [Locale.nl_NL]: 'Conflict', - }, - [HttpStatus.GONE]: { - [Locale.en_GB]: 'Gone', - [Locale.nl_NL]: 'Verdwenen', - }, - [HttpStatus.LENGTH_REQUIRED]: { - [Locale.en_GB]: 'Length required', - [Locale.nl_NL]: 'Lengte benodigd', - }, - [HttpStatus.PRECONDITION_FAILED]: { - [Locale.en_GB]: 'Precondition failed', - [Locale.nl_NL]: 'Niet voldaan aan vooraf gestelde voorwaarde', - }, - [HttpStatus.PAYLOAD_TOO_LARGE]: { - [Locale.en_GB]: 'Payload too large', - [Locale.nl_NL]: 'Aanvraag te groot', - }, - [HttpStatus.URI_TOO_LONG]: { - [Locale.en_GB]: 'URI too long', - [Locale.nl_NL]: 'Aanvraag-URL te lang', - }, - [HttpStatus.UNSUPPORTED_MEDIA_TYPE]: { - [Locale.en_GB]: 'Unsupported media type', - [Locale.nl_NL]: 'Media-type niet ondersteund', - }, - [HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE]: { - [Locale.en_GB]: 'Requested range not satisfiable', - [Locale.nl_NL]: 'Aangevraagd gedeelte niet opvraagbaar', - }, - [HttpStatus.EXPECTATION_FAILED]: { - [Locale.en_GB]: 'Expectation failed', - [Locale.nl_NL]: 'Niet voldaan aan verwachting', - }, - [HttpStatus.I_AM_A_TEAPOT]: { - [Locale.en_GB]: 'I am a teapot', - [Locale.nl_NL]: 'I am a teapot', - }, - [HttpStatus.UNPROCESSABLE_ENTITY]: { - [Locale.en_GB]: 'Unprocessable entity', - [Locale.nl_NL]: 'Aanvraag kan niet verwerkt worden', - }, - [HttpStatus.FAILED_DEPENDENCY]: { - [Locale.en_GB]: 'Failed dependency', - [Locale.nl_NL]: 'Gefaalde afhankelijkheid', - }, - [HttpStatus.TOO_MANY_REQUESTS]: { - [Locale.en_GB]: 'Too many requests', - [Locale.nl_NL]: 'Te veel requests', - }, - [HttpStatus.INTERNAL_SERVER_ERROR]: { - [Locale.en_GB]: 'Internal server error', - [Locale.nl_NL]: 'Interne serverfout', - }, - [HttpStatus.NOT_IMPLEMENTED]: { - [Locale.en_GB]: 'Not implemented', - [Locale.nl_NL]: 'Niet geïmplementeerd', - }, - [HttpStatus.BAD_GATEWAY]: { - [Locale.en_GB]: 'Bad gateway', - [Locale.nl_NL]: 'Bad gateway', - }, - [HttpStatus.SERVICE_UNAVAILABLE]: { - [Locale.en_GB]: 'Service unavailable', - [Locale.nl_NL]: 'Dienst niet beschikbaar', - }, - [HttpStatus.GATEWAY_TIMEOUT]: { - [Locale.en_GB]: 'Gateway timeout', - [Locale.nl_NL]: 'Gateway timeout', - }, - [HttpStatus.HTTP_VERSION_NOT_SUPPORTED]: { - [Locale.en_GB]: 'HTTP version not supported', - [Locale.nl_NL]: 'HTTP-versie wordt niet ondersteund', - }, -}; diff --git a/src/filters/index-exception.filter.ts b/src/modules/exception/index-exception.filter.ts similarity index 84% rename from src/filters/index-exception.filter.ts rename to src/modules/exception/index-exception.filter.ts index 945128e..7844f6c 100644 --- a/src/filters/index-exception.filter.ts +++ b/src/modules/exception/index-exception.filter.ts @@ -1,4 +1,4 @@ -import { ConfigService } from './../modules/config/config.service'; +import { ConfigService } from './../config/config.service'; import { ArgumentsHost, Catch, @@ -7,8 +7,8 @@ import { Injectable, } from '@nestjs/common'; import { HttpExceptionFilter } from './http-exception.filter'; -import { LoggerService } from '../modules/logger/logger.service'; -import { fileExists } from '../utils/file-exists.util'; +import { LoggerService } from '../logger/logger.service'; +import { fileExists } from '../../utils/file-exists.util'; @Injectable() @Catch(HttpException) diff --git a/src/filters/internal-exception.filter.ts b/src/modules/exception/internal-exception.filter.ts similarity index 63% rename from src/filters/internal-exception.filter.ts rename to src/modules/exception/internal-exception.filter.ts index 8aa02f0..cbea61f 100644 --- a/src/filters/internal-exception.filter.ts +++ b/src/modules/exception/internal-exception.filter.ts @@ -1,13 +1,10 @@ -import { ExceptionCodeI18n } from './../modules/exception/i18n/code.i18n'; -import { snakeCase } from 'snake-case'; -import { ConfigService } from './../modules/config/config.service'; -import { LoggerService } from './../modules/logger/logger.service'; +import { ConfigService } from './../config/config.service'; +import { LoggerService } from './../logger/logger.service'; import { ExceptionFilter, Catch, ArgumentsHost, Injectable, - HttpStatus, } from '@nestjs/common'; @Injectable() @@ -32,11 +29,6 @@ export class InternalExceptionFilter implements ExceptionFilter { ? exception.message : 'internal server error'; const stack = exception.stack; - const locale = request.locale; - const type = typeof message === 'string' ? snakeCase(message) : undefined; - const translation = - ExceptionCodeI18n[type]?.[locale] || - ExceptionCodeI18n[HttpStatus[type?.toUpperCase?.()]]?.[locale]; this.logger.error( `'[${method}] ${path} (${status})' failed with ${message}`, @@ -49,9 +41,8 @@ export class InternalExceptionFilter implements ExceptionFilter { path, method, code: status, - message: typeof message !== 'string' ? undefined : translation || message, + message, error: typeof message !== 'string' ? message : undefined, - type: translation ? type : undefined, time, }); }