Skip to content

Commit

Permalink
feat: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
moesjarraf committed Feb 22, 2023
1 parent 793c4c3 commit 6c15e7c
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 244 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"description": "Nest.js common libraries",
"author": "Moesjarraf J",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"main": "dist/index.js",
"files": [
"dist"
Expand Down
2 changes: 1 addition & 1 deletion src/api/base.controller.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/app/app.providers.ts
Original file line number Diff line number Diff line change
@@ -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 = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/common.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
14 changes: 5 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
5 changes: 1 addition & 4 deletions src/modules/captcha/captcha.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 0 additions & 3 deletions src/modules/exception/enums/code.enum.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`,
Expand All @@ -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,
});
}
Expand Down
195 changes: 0 additions & 195 deletions src/modules/exception/i18n/code.i18n.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigService } from './../modules/config/config.service';
import { ConfigService } from './../config/config.service';
import {
ArgumentsHost,
Catch,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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}`,
Expand All @@ -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,
});
}
Expand Down

0 comments on commit 6c15e7c

Please sign in to comment.