@nathapp/nestjs-common 1.0.1-rc.1 → 1.0.1-rc.3

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.
@@ -2,5 +2,5 @@ import DbConfig from "./db.config";
2
2
  import MongoConfig from "./mongodb.config";
3
3
  import RedisConfig from "./redis.config";
4
4
  import RabbitMQConfig from "./rabbitmq.config";
5
- import JoiUtil, { JoiConfig } from "./joi-util";
5
+ import JoiUtil, { JoiConfig } from "./utils/joi-util";
6
6
  export { DbConfig, MongoConfig, RedisConfig, RabbitMQConfig, JoiUtil, JoiConfig, };
@@ -9,5 +9,5 @@ const redis_config_1 = require("./redis.config");
9
9
  exports.RedisConfig = redis_config_1.default;
10
10
  const rabbitmq_config_1 = require("./rabbitmq.config");
11
11
  exports.RabbitMQConfig = rabbitmq_config_1.default;
12
- const joi_util_1 = require("./joi-util");
12
+ const joi_util_1 = require("./utils/joi-util");
13
13
  exports.JoiUtil = joi_util_1.default;
@@ -0,0 +1,9 @@
1
+ export declare enum CommonExceptionCode {
2
+ UNKNOWN_ERROR = -1,
3
+ NOT_FOUND = 404,
4
+ INTERNAL_ERROR = 501,
5
+ USER_NOT_FOUND = 2018,
6
+ FORBIDDEN_IP_NOT_MATCH = 40001,
7
+ FORBIDDEN_BLACKLISTED = 40002,
8
+ FORBIDDEN = 40003
9
+ }
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommonExceptionCode = void 0;
4
+ var CommonExceptionCode;
5
+ (function (CommonExceptionCode) {
6
+ CommonExceptionCode[CommonExceptionCode["UNKNOWN_ERROR"] = -1] = "UNKNOWN_ERROR";
7
+ CommonExceptionCode[CommonExceptionCode["NOT_FOUND"] = 404] = "NOT_FOUND";
8
+ CommonExceptionCode[CommonExceptionCode["INTERNAL_ERROR"] = 501] = "INTERNAL_ERROR";
9
+ CommonExceptionCode[CommonExceptionCode["USER_NOT_FOUND"] = 2018] = "USER_NOT_FOUND";
10
+ CommonExceptionCode[CommonExceptionCode["FORBIDDEN_IP_NOT_MATCH"] = 40001] = "FORBIDDEN_IP_NOT_MATCH";
11
+ CommonExceptionCode[CommonExceptionCode["FORBIDDEN_BLACKLISTED"] = 40002] = "FORBIDDEN_BLACKLISTED";
12
+ CommonExceptionCode[CommonExceptionCode["FORBIDDEN"] = 40003] = "FORBIDDEN";
13
+ })(CommonExceptionCode = exports.CommonExceptionCode || (exports.CommonExceptionCode = {}));
@@ -0,0 +1,40 @@
1
+ import { ExceptionFilter, ArgumentsHost, ForbiddenException, NotFoundException } from "@nestjs/common";
2
+ import { HttpAdapterHost } from "@nestjs/core";
3
+ import { I18nService } from "nestjs-i18n";
4
+ import { CommonExceptionCode } from "./app-exception-code.enum";
5
+ import { AppException } from "./app.exception";
6
+ import { UsernameNotFoundException } from "./user-not-found.exception";
7
+ export declare class GlobalExceptionsFilter implements ExceptionFilter {
8
+ private readonly httpAdapterHost;
9
+ private readonly i18nService;
10
+ private readonly logger;
11
+ private readonly exceptionPrefix;
12
+ constructor(httpAdapterHost: HttpAdapterHost, i18nService: I18nService);
13
+ catch(exception: unknown, host: ArgumentsHost): Promise<void>;
14
+ handleAppException(exception: AppException, lang: string): Promise<{
15
+ httpStatus: number;
16
+ errorCode: number;
17
+ errMessage: string;
18
+ }>;
19
+ handleForbiddenException(exception: ForbiddenException, lang: string): Promise<{
20
+ httpStatus: number;
21
+ errorCode: CommonExceptionCode;
22
+ errMessage: string;
23
+ }>;
24
+ handleUserNotFoundException(exception: UsernameNotFoundException, lang: string): Promise<{
25
+ httpStatus: number;
26
+ errorCode: CommonExceptionCode;
27
+ errMessage: string;
28
+ }>;
29
+ handleNotFoundException(exception: NotFoundException, request: any): Promise<{
30
+ httpStatus: number;
31
+ errorCode: CommonExceptionCode;
32
+ errMessage: string;
33
+ }>;
34
+ handleUnknownException(exception: any, lang: string, responseBody?: any): Promise<{
35
+ httpStatus: number;
36
+ errorCode: CommonExceptionCode;
37
+ errMessage: string;
38
+ }>;
39
+ translate(code: number, lang: string, args?: any, prefix?: string): Promise<string>;
40
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var GlobalExceptionsFilter_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.GlobalExceptionsFilter = void 0;
14
+ const common_1 = require("@nathapp/common");
15
+ const common_2 = require("@nestjs/common");
16
+ const core_1 = require("@nestjs/core");
17
+ const nestjs_i18n_1 = require("nestjs-i18n");
18
+ const app_exception_code_enum_1 = require("./app-exception-code.enum");
19
+ const app_exception_1 = require("./app.exception");
20
+ const user_not_found_exception_1 = require("./user-not-found.exception");
21
+ let GlobalExceptionsFilter = GlobalExceptionsFilter_1 = class GlobalExceptionsFilter {
22
+ constructor(httpAdapterHost, i18nService) {
23
+ this.httpAdapterHost = httpAdapterHost;
24
+ this.i18nService = i18nService;
25
+ this.logger = new common_2.Logger(GlobalExceptionsFilter_1.name);
26
+ this.exceptionPrefix = "exception";
27
+ }
28
+ async catch(exception, host) {
29
+ const { httpAdapter } = this.httpAdapterHost;
30
+ const ctx = host.switchToHttp();
31
+ const response = ctx.getResponse();
32
+ const request = ctx.getRequest();
33
+ const lang = ctx.getRequest().i18nLang;
34
+ let es;
35
+ if (exception instanceof app_exception_1.AppException) {
36
+ es = await this.handleAppException(exception, lang);
37
+ }
38
+ else if (exception instanceof common_2.ForbiddenException) {
39
+ es = await this.handleForbiddenException(exception, lang);
40
+ }
41
+ else if (exception instanceof common_2.NotFoundException) {
42
+ es = await this.handleNotFoundException(exception, request);
43
+ }
44
+ else if (exception instanceof user_not_found_exception_1.UsernameNotFoundException) {
45
+ es = await this.handleUserNotFoundException(exception, lang);
46
+ }
47
+ else {
48
+ const httpStatus = exception instanceof common_2.HttpException
49
+ ? exception.getStatus()
50
+ : common_2.HttpStatus.INTERNAL_SERVER_ERROR;
51
+ const responseBody = {
52
+ statusCode: httpStatus,
53
+ timestamp: new Date().toISOString(),
54
+ path: httpAdapter.getRequestUrl(ctx.getRequest()),
55
+ };
56
+ es = await this.handleUnknownException(exception, lang, responseBody);
57
+ }
58
+ httpAdapter.reply(response, common_1.JsonResponse.Error(es.errorCode, es.errMessage), es.httpStatus);
59
+ }
60
+ async handleAppException(exception, lang) {
61
+ const httpStatus = 200;
62
+ const errorCode = exception.code;
63
+ const errMessage = await this.translate(errorCode, lang, exception.args, exception.prefix);
64
+ return {
65
+ httpStatus,
66
+ errorCode,
67
+ errMessage,
68
+ };
69
+ }
70
+ async handleForbiddenException(exception, lang) {
71
+ const httpStatus = 200;
72
+ const errorCode = app_exception_code_enum_1.CommonExceptionCode.FORBIDDEN;
73
+ const errMessage = await this.translate(errorCode, lang);
74
+ return {
75
+ httpStatus,
76
+ errorCode,
77
+ errMessage,
78
+ };
79
+ }
80
+ async handleUserNotFoundException(exception, lang) {
81
+ const httpStatus = 200;
82
+ const errorCode = app_exception_code_enum_1.CommonExceptionCode.USER_NOT_FOUND;
83
+ this.logger.warn(exception.message, user_not_found_exception_1.UsernameNotFoundException.name);
84
+ const errMessage = await this.translate(errorCode, lang);
85
+ return {
86
+ httpStatus,
87
+ errorCode,
88
+ errMessage,
89
+ };
90
+ }
91
+ async handleNotFoundException(exception, request) {
92
+ const httpStatus = exception.getStatus();
93
+ const errorCode = app_exception_code_enum_1.CommonExceptionCode.NOT_FOUND;
94
+ this.logger.warn(`Method: ${request.method}, url: ${request.url}`, exception.name);
95
+ const errMessage = exception.message;
96
+ return {
97
+ httpStatus,
98
+ errorCode,
99
+ errMessage,
100
+ };
101
+ }
102
+ async handleUnknownException(exception, lang, responseBody) {
103
+ const httpStatus = 500;
104
+ const errorCode = app_exception_code_enum_1.CommonExceptionCode.UNKNOWN_ERROR;
105
+ const { message } = (0, common_1.describeError)(exception);
106
+ this.logger.error(`Unknown Error occur. msg: ${message}, data: ${JSON.stringify(responseBody)}`, exception.stack);
107
+ const errMessage = await this.translate(errorCode, lang);
108
+ return {
109
+ httpStatus,
110
+ errorCode,
111
+ errMessage,
112
+ };
113
+ }
114
+ async translate(code, lang, args, prefix) {
115
+ return this.i18nService.translate(`${prefix || this.exceptionPrefix}.${code}`, {
116
+ lang,
117
+ args,
118
+ });
119
+ }
120
+ };
121
+ GlobalExceptionsFilter = GlobalExceptionsFilter_1 = __decorate([
122
+ (0, common_2.Catch)(),
123
+ __metadata("design:paramtypes", [core_1.HttpAdapterHost,
124
+ nestjs_i18n_1.I18nService])
125
+ ], GlobalExceptionsFilter);
126
+ exports.GlobalExceptionsFilter = GlobalExceptionsFilter;
@@ -1,4 +1,7 @@
1
+ import { CommonExceptionCode } from "./app-exception-code.enum";
1
2
  import { createAppExceptionFilterProvider } from "./app-exception-filter.provider";
2
3
  import { AppExceptionFilter } from "./app-exception.filter";
3
4
  import { AppException } from "./app.exception";
4
- export { AppException, AppExceptionFilter, createAppExceptionFilterProvider };
5
+ import { GlobalExceptionsFilter } from "./global-exception.filter";
6
+ import { UsernameNotFoundException } from "./user-not-found.exception";
7
+ export { AppException, AppExceptionFilter, createAppExceptionFilterProvider, GlobalExceptionsFilter, UsernameNotFoundException, CommonExceptionCode };
@@ -1,9 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAppExceptionFilterProvider = exports.AppExceptionFilter = exports.AppException = void 0;
3
+ exports.CommonExceptionCode = exports.UsernameNotFoundException = exports.GlobalExceptionsFilter = exports.createAppExceptionFilterProvider = exports.AppExceptionFilter = exports.AppException = void 0;
4
+ const app_exception_code_enum_1 = require("./app-exception-code.enum");
5
+ Object.defineProperty(exports, "CommonExceptionCode", { enumerable: true, get: function () { return app_exception_code_enum_1.CommonExceptionCode; } });
4
6
  const app_exception_filter_provider_1 = require("./app-exception-filter.provider");
5
7
  Object.defineProperty(exports, "createAppExceptionFilterProvider", { enumerable: true, get: function () { return app_exception_filter_provider_1.createAppExceptionFilterProvider; } });
6
8
  const app_exception_filter_1 = require("./app-exception.filter");
7
9
  Object.defineProperty(exports, "AppExceptionFilter", { enumerable: true, get: function () { return app_exception_filter_1.AppExceptionFilter; } });
8
10
  const app_exception_1 = require("./app.exception");
9
11
  Object.defineProperty(exports, "AppException", { enumerable: true, get: function () { return app_exception_1.AppException; } });
12
+ const global_exception_filter_1 = require("./global-exception.filter");
13
+ Object.defineProperty(exports, "GlobalExceptionsFilter", { enumerable: true, get: function () { return global_exception_filter_1.GlobalExceptionsFilter; } });
14
+ const user_not_found_exception_1 = require("./user-not-found.exception");
15
+ Object.defineProperty(exports, "UsernameNotFoundException", { enumerable: true, get: function () { return user_not_found_exception_1.UsernameNotFoundException; } });
@@ -0,0 +1,4 @@
1
+ import { HttpException } from "@nestjs/common";
2
+ export declare class UsernameNotFoundException extends HttpException {
3
+ constructor(message: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UsernameNotFoundException = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ class UsernameNotFoundException extends common_1.HttpException {
6
+ constructor(message) {
7
+ super(message, 500);
8
+ }
9
+ }
10
+ exports.UsernameNotFoundException = UsernameNotFoundException;
@@ -0,0 +1,10 @@
1
+ import { ClassConstructor } from "class-transformer";
2
+ import { JsonResponse as JResponse } from "@nathapp/common";
3
+ export declare class JsonResponse<T> extends JResponse {
4
+ ret: number;
5
+ data: T;
6
+ message?: string;
7
+ constructor(ret: number, data: any, message?: string);
8
+ static Ok<T>(data?: T, cls?: ClassConstructor<T>): JsonResponse<T>;
9
+ static Error<T>(errcode: number, message: string): JsonResponse<T>;
10
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonResponse = void 0;
4
+ const class_transformer_1 = require("class-transformer");
5
+ const common_1 = require("@nathapp/common");
6
+ class JsonResponse extends common_1.JsonResponse {
7
+ constructor(ret, data, message) {
8
+ super(ret, data, message);
9
+ this.ret = ret;
10
+ this.data = data;
11
+ this.message = message;
12
+ }
13
+ static Ok(data, cls) {
14
+ let responseData;
15
+ if (data && cls) {
16
+ responseData = (0, class_transformer_1.plainToInstance)(cls, data);
17
+ }
18
+ else {
19
+ responseData = data;
20
+ }
21
+ const response = new JsonResponse(0, responseData, "ok");
22
+ return response;
23
+ }
24
+ static Error(errcode, message) {
25
+ const response = new JsonResponse(errcode, null, message);
26
+ delete response.data;
27
+ return response;
28
+ }
29
+ }
30
+ exports.JsonResponse = JsonResponse;
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ ## Using Joi Utils and Validate Utils to validate config
2
+
3
+ 1. create an config interface
4
+
5
+ 2. create a validation class to schema validation
6
+
7
+ https://dev.to/rrgt19/ways-to-validate-environment-configuration-in-a-forfeature-config-in-nestjs-2ehp
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nathapp/nestjs-common",
3
- "version": "1.0.1-rc.1",
3
+ "version": "1.0.1-rc.3",
4
4
  "description": "NestJs Common Library",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && tsc -p tsconfig.json",
@@ -16,7 +16,6 @@
16
16
  "devDependencies": {
17
17
  "@nestjs/platform-express": "^9.0.0",
18
18
  "@nestjs/platform-fastify": "^9.0.0",
19
- "@types/js-yaml": "^4.0.5",
20
19
  "@types/node": "^17.0.22",
21
20
  "@typescript-eslint/eslint-plugin": "^4",
22
21
  "@typescript-eslint/parser": "^4",
@@ -34,7 +33,6 @@
34
33
  "dependencies": {
35
34
  "@nathapp/common": "^0.1.10",
36
35
  "joi": "^17.6.0",
37
- "js-yaml": "^4.1.0",
38
36
  "nestjs-i18n": "^10.0.0",
39
37
  "request-ip": "^3.2.0"
40
38
  },
File without changes