@nathapp/nestjs-common 1.0.1-rc.0
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.
- package/.eslintrc.js +24 -0
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/config/db.config.d.ts +14 -0
- package/dist/config/db.config.js +17 -0
- package/dist/config/index.d.ts +6 -0
- package/dist/config/index.js +13 -0
- package/dist/config/joi-util.d.ts +11 -0
- package/dist/config/joi-util.js +25 -0
- package/dist/config/mongodb.config.d.ts +10 -0
- package/dist/config/mongodb.config.js +14 -0
- package/dist/config/rabbitmq.config.d.ts +10 -0
- package/dist/config/rabbitmq.config.js +14 -0
- package/dist/config/redis.config.d.ts +10 -0
- package/dist/config/redis.config.js +14 -0
- package/dist/config/validate-util.d.ts +2 -0
- package/dist/config/validate-util.js +18 -0
- package/dist/decorator/client-info.decorator.d.ts +1 -0
- package/dist/decorator/client-info.decorator.js +37 -0
- package/dist/decorator/index.d.ts +5 -0
- package/dist/decorator/index.js +11 -0
- package/dist/decorator/is-public.decorator.d.ts +2 -0
- package/dist/decorator/is-public.decorator.js +7 -0
- package/dist/decorator/permission.decorator.d.ts +2 -0
- package/dist/decorator/permission.decorator.js +7 -0
- package/dist/decorator/real-ip.decorator.d.ts +1 -0
- package/dist/decorator/real-ip.decorator.js +11 -0
- package/dist/exception/app-exception-filter.provider.d.ts +2 -0
- package/dist/exception/app-exception-filter.provider.js +9 -0
- package/dist/exception/app-exception.filter.d.ts +9 -0
- package/dist/exception/app-exception.filter.js +51 -0
- package/dist/exception/app.exception.d.ts +10 -0
- package/dist/exception/app.exception.js +22 -0
- package/dist/exception/index.d.ts +4 -0
- package/dist/exception/index.js +9 -0
- package/dist/exception/translator.d.ts +12 -0
- package/dist/exception/translator.js +9 -0
- package/dist/i18n/i18n-core.module.d.ts +8 -0
- package/dist/i18n/i18n-core.module.js +76 -0
- package/dist/i18n/i18n.constants.d.ts +0 -0
- package/dist/i18n/i18n.constants.js +0 -0
- package/dist/i18n/index.d.ts +2 -0
- package/dist/i18n/index.js +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +21 -0
- package/dist/serialize/index.d.ts +2 -0
- package/dist/serialize/index.js +5 -0
- package/dist/serialize/localize.string.d.ts +3 -0
- package/dist/serialize/localize.string.js +6 -0
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/package.json +49 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
parserOptions: {
|
|
4
|
+
project: 'tsconfig.json',
|
|
5
|
+
sourceType: 'module',
|
|
6
|
+
},
|
|
7
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
|
8
|
+
extends: [
|
|
9
|
+
'plugin:@typescript-eslint/recommended',
|
|
10
|
+
'plugin:prettier/recommended',
|
|
11
|
+
],
|
|
12
|
+
root: true,
|
|
13
|
+
env: {
|
|
14
|
+
node: true,
|
|
15
|
+
jest: true,
|
|
16
|
+
},
|
|
17
|
+
ignorePatterns: ['.eslintrc.js'],
|
|
18
|
+
rules: {
|
|
19
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
|
20
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
21
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
22
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
|
23
|
+
},
|
|
24
|
+
};
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 KnowYourself
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Common NestJs Library
|
|
2
|
+
|
|
3
|
+
# sample Default predefine Configuration on dotenv
|
|
4
|
+
|
|
5
|
+
1.) Database
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
DB_URL=mysql://root:password@localhost/db?useUnicode=true&characterEncoding=UTF-8
|
|
9
|
+
DB_ENTITY_PATH=/**/*.entity.ts
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
2.) Redis
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
REDIS_URL=redis://root@password@localhost:6379/0
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
3.) MongoDb
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
MONGO_URL=mongodb://mongodb0.example.com:27017
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
4.) RabbitMQ
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
RABBITMQ_URL=amqp://user:pass@host:10000/vhost
|
|
28
|
+
```
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare const _default: (() => {
|
|
2
|
+
isPresent: boolean;
|
|
3
|
+
name: string;
|
|
4
|
+
url: string;
|
|
5
|
+
type: "mysql" | "mariadb" | "postgres" | "mssql" | "oracle" | "sqlite";
|
|
6
|
+
entityPath: string;
|
|
7
|
+
}) & import("@nestjs/config").ConfigFactoryKeyHost<{
|
|
8
|
+
isPresent: boolean;
|
|
9
|
+
name: string;
|
|
10
|
+
url: string;
|
|
11
|
+
type: "mysql" | "mariadb" | "postgres" | "mssql" | "oracle" | "sqlite";
|
|
12
|
+
entityPath: string;
|
|
13
|
+
}>;
|
|
14
|
+
export default _default;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@nathapp/common");
|
|
4
|
+
const config_1 = require("@nestjs/config");
|
|
5
|
+
exports.default = (0, config_1.registerAs)("db", () => {
|
|
6
|
+
const isPresent = !(0, common_1.isUndefinedOrNullOrEmpty)(process.env.DB_URL);
|
|
7
|
+
const name = process.env.DB_NAME || "default";
|
|
8
|
+
const url = process.env.DB_URL;
|
|
9
|
+
const type = (isPresent ? url.split(":").shift() : null);
|
|
10
|
+
return {
|
|
11
|
+
isPresent,
|
|
12
|
+
name,
|
|
13
|
+
url,
|
|
14
|
+
type,
|
|
15
|
+
entityPath: process.env.DB_ENTITY_PATH || "/**/*.entity.ts",
|
|
16
|
+
};
|
|
17
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import DbConfig from "./db.config";
|
|
2
|
+
import MongoConfig from "./mongodb.config";
|
|
3
|
+
import RedisConfig from "./redis.config";
|
|
4
|
+
import RabbitMQConfig from "./rabbitmq.config";
|
|
5
|
+
import JoiUtil, { JoiConfig } from "./joi-util";
|
|
6
|
+
export { DbConfig, MongoConfig, RedisConfig, RabbitMQConfig, JoiUtil, JoiConfig, };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JoiUtil = exports.RabbitMQConfig = exports.RedisConfig = exports.MongoConfig = exports.DbConfig = void 0;
|
|
4
|
+
const db_config_1 = require("./db.config");
|
|
5
|
+
exports.DbConfig = db_config_1.default;
|
|
6
|
+
const mongodb_config_1 = require("./mongodb.config");
|
|
7
|
+
exports.MongoConfig = mongodb_config_1.default;
|
|
8
|
+
const redis_config_1 = require("./redis.config");
|
|
9
|
+
exports.RedisConfig = redis_config_1.default;
|
|
10
|
+
const rabbitmq_config_1 = require("./rabbitmq.config");
|
|
11
|
+
exports.RabbitMQConfig = rabbitmq_config_1.default;
|
|
12
|
+
const joi_util_1 = require("./joi-util");
|
|
13
|
+
exports.JoiUtil = joi_util_1.default;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Schema, SchemaMap } from "joi";
|
|
2
|
+
interface ConfigProps {
|
|
3
|
+
value: unknown;
|
|
4
|
+
joi: Schema;
|
|
5
|
+
}
|
|
6
|
+
export declare type JoiConfig<T> = Record<keyof T, ConfigProps>;
|
|
7
|
+
export default class JoiUtil {
|
|
8
|
+
static validate<T>(config: JoiConfig<T>): T;
|
|
9
|
+
static extractByPropName<T>(config: JoiConfig<T>, propName: keyof ConfigProps): T | SchemaMap<T>;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const Joi = require("joi");
|
|
4
|
+
class JoiUtil {
|
|
5
|
+
static validate(config) {
|
|
6
|
+
const schemaObj = JoiUtil.extractByPropName(config, "joi");
|
|
7
|
+
const schema = Joi.object(schemaObj);
|
|
8
|
+
const values = JoiUtil.extractByPropName(config, "value");
|
|
9
|
+
const { error } = schema.validate(values, { abortEarly: false });
|
|
10
|
+
if (error) {
|
|
11
|
+
throw new Error(`Validation failed - Is there an environment variable missing?
|
|
12
|
+
${error.message}`);
|
|
13
|
+
}
|
|
14
|
+
return values;
|
|
15
|
+
}
|
|
16
|
+
static extractByPropName(config, propName) {
|
|
17
|
+
const arr = Object.keys(config).map(key => {
|
|
18
|
+
return {
|
|
19
|
+
[key]: config[key][propName],
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
return Object.assign({}, ...arr);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.default = JoiUtil;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@nathapp/common");
|
|
4
|
+
const config_1 = require("@nestjs/config");
|
|
5
|
+
exports.default = (0, config_1.registerAs)("mongodb", () => {
|
|
6
|
+
const isPresent = !(0, common_1.isUndefinedOrNullOrEmpty)(process.env.MONGO_URL);
|
|
7
|
+
const name = process.env.MONGO_NAME || "default";
|
|
8
|
+
const url = process.env.MONGO_URL;
|
|
9
|
+
return {
|
|
10
|
+
isPresent,
|
|
11
|
+
name,
|
|
12
|
+
url,
|
|
13
|
+
};
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@nathapp/common");
|
|
4
|
+
const config_1 = require("@nestjs/config");
|
|
5
|
+
exports.default = (0, config_1.registerAs)("rabbitmq", () => {
|
|
6
|
+
const isPresent = !(0, common_1.isUndefinedOrNullOrEmpty)(process.env.RABBITMQ_URL);
|
|
7
|
+
const name = process.env.RABBITMQ_NAME || "default";
|
|
8
|
+
const url = process.env.RABBITMQ_URL;
|
|
9
|
+
return {
|
|
10
|
+
isPresent,
|
|
11
|
+
name,
|
|
12
|
+
url,
|
|
13
|
+
};
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const common_1 = require("@nathapp/common");
|
|
4
|
+
const config_1 = require("@nestjs/config");
|
|
5
|
+
exports.default = (0, config_1.registerAs)("redis", () => {
|
|
6
|
+
const isPresent = !(0, common_1.isUndefinedOrNullOrEmpty)(process.env.REDIS_URL);
|
|
7
|
+
const name = process.env.REDIS_NAME || "default";
|
|
8
|
+
const url = process.env.REDIS_URL;
|
|
9
|
+
return {
|
|
10
|
+
isPresent,
|
|
11
|
+
name,
|
|
12
|
+
url,
|
|
13
|
+
};
|
|
14
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateUtil = void 0;
|
|
4
|
+
const class_transformer_1 = require("class-transformer");
|
|
5
|
+
const class_validator_1 = require("class-validator");
|
|
6
|
+
function validateUtil(config, envVariablesClass) {
|
|
7
|
+
const validatedConfig = (0, class_transformer_1.plainToClass)(envVariablesClass, config, {
|
|
8
|
+
enableImplicitConversion: true,
|
|
9
|
+
});
|
|
10
|
+
const errors = (0, class_validator_1.validateSync)(validatedConfig, {
|
|
11
|
+
skipMissingProperties: false,
|
|
12
|
+
});
|
|
13
|
+
if (errors.length > 0) {
|
|
14
|
+
throw new Error(errors.toString());
|
|
15
|
+
}
|
|
16
|
+
return validatedConfig;
|
|
17
|
+
}
|
|
18
|
+
exports.validateUtil = validateUtil;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const ClientInfo: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClientInfo = void 0;
|
|
4
|
+
const requestIp = require("request-ip");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const common_2 = require("@nathapp/common");
|
|
7
|
+
exports.ClientInfo = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
8
|
+
const request = ctx.switchToHttp().getRequest();
|
|
9
|
+
const clientInfo = {
|
|
10
|
+
country: "",
|
|
11
|
+
userAgent: "",
|
|
12
|
+
lang: "en_US",
|
|
13
|
+
os: "",
|
|
14
|
+
appId: "",
|
|
15
|
+
deviceId: "",
|
|
16
|
+
ip: requestIp.getClientIp(request),
|
|
17
|
+
};
|
|
18
|
+
if (request.headers) {
|
|
19
|
+
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["user-agent"])) {
|
|
20
|
+
clientInfo.userAgent = request.headers["user-agent"];
|
|
21
|
+
}
|
|
22
|
+
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["lang"])) {
|
|
23
|
+
clientInfo.lang = request.headers["lang"];
|
|
24
|
+
}
|
|
25
|
+
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["country"])) {
|
|
26
|
+
clientInfo.country = request.headers["country"];
|
|
27
|
+
}
|
|
28
|
+
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["os"])) {
|
|
29
|
+
clientInfo.os = request.headers["os"];
|
|
30
|
+
}
|
|
31
|
+
if (!(0, common_2.isUndefinedOrNullOrEmpty)(request.headers["app-id"]) &&
|
|
32
|
+
!isNaN(+request.headers["app-id"])) {
|
|
33
|
+
clientInfo.appId = request.headers["app-id"];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return clientInfo;
|
|
37
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { ClientInfo } from "./client-info.decorator";
|
|
2
|
+
import { RealIP } from "./real-ip.decorator";
|
|
3
|
+
import { Public } from "./is-public.decorator";
|
|
4
|
+
import { RequiredPermission } from "./permission.decorator";
|
|
5
|
+
export { ClientInfo, RealIP, Public, RequiredPermission };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequiredPermission = exports.Public = exports.RealIP = exports.ClientInfo = void 0;
|
|
4
|
+
const client_info_decorator_1 = require("./client-info.decorator");
|
|
5
|
+
Object.defineProperty(exports, "ClientInfo", { enumerable: true, get: function () { return client_info_decorator_1.ClientInfo; } });
|
|
6
|
+
const real_ip_decorator_1 = require("./real-ip.decorator");
|
|
7
|
+
Object.defineProperty(exports, "RealIP", { enumerable: true, get: function () { return real_ip_decorator_1.RealIP; } });
|
|
8
|
+
const is_public_decorator_1 = require("./is-public.decorator");
|
|
9
|
+
Object.defineProperty(exports, "Public", { enumerable: true, get: function () { return is_public_decorator_1.Public; } });
|
|
10
|
+
const permission_decorator_1 = require("./permission.decorator");
|
|
11
|
+
Object.defineProperty(exports, "RequiredPermission", { enumerable: true, get: function () { return permission_decorator_1.RequiredPermission; } });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Public = exports.IS_PUBLIC_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.IS_PUBLIC_KEY = "isPublic";
|
|
6
|
+
const Public = () => (0, common_1.SetMetadata)(exports.IS_PUBLIC_KEY, true);
|
|
7
|
+
exports.Public = Public;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequiredPermission = exports.PERMISSION_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.PERMISSION_KEY = "PERMISSION";
|
|
6
|
+
const RequiredPermission = (permission) => (0, common_1.SetMetadata)(exports.PERMISSION_KEY, permission);
|
|
7
|
+
exports.RequiredPermission = RequiredPermission;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const RealIP: (...dataOrPipes: unknown[]) => ParameterDecorator;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RealIP = void 0;
|
|
4
|
+
const requestIp = require("request-ip");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
exports.RealIP = (0, common_1.createParamDecorator)((data, ctx) => {
|
|
7
|
+
const request = ctx.switchToHttp().getRequest();
|
|
8
|
+
if (request.clientIp)
|
|
9
|
+
return request.clientIp;
|
|
10
|
+
return requestIp.getClientIp(request);
|
|
11
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAppExceptionFilterProvider = void 0;
|
|
4
|
+
const core_1 = require("@nestjs/core");
|
|
5
|
+
const app_exception_filter_1 = require("./app-exception.filter");
|
|
6
|
+
exports.createAppExceptionFilterProvider = {
|
|
7
|
+
provide: core_1.APP_FILTER,
|
|
8
|
+
useClass: app_exception_filter_1.AppExceptionFilter,
|
|
9
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ExceptionFilter, ArgumentsHost } from "@nestjs/common";
|
|
2
|
+
import { I18nService } from "nestjs-i18n";
|
|
3
|
+
import { AppException } from "./app.exception";
|
|
4
|
+
export declare class AppExceptionFilter implements ExceptionFilter {
|
|
5
|
+
private readonly i18n;
|
|
6
|
+
private logger;
|
|
7
|
+
constructor(i18n: I18nService);
|
|
8
|
+
catch(exception: AppException, host: ArgumentsHost): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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 __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var AppExceptionFilter_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.AppExceptionFilter = void 0;
|
|
17
|
+
const common_1 = require("@nathapp/common");
|
|
18
|
+
const common_2 = require("@nestjs/common");
|
|
19
|
+
const nestjs_i18n_1 = require("nestjs-i18n");
|
|
20
|
+
const app_exception_1 = require("./app.exception");
|
|
21
|
+
let AppExceptionFilter = AppExceptionFilter_1 = class AppExceptionFilter {
|
|
22
|
+
constructor(i18n) {
|
|
23
|
+
this.i18n = i18n;
|
|
24
|
+
this.logger = new common_2.Logger(AppExceptionFilter_1.name);
|
|
25
|
+
}
|
|
26
|
+
async catch(exception, host) {
|
|
27
|
+
const ctx = host.switchToHttp();
|
|
28
|
+
const response = ctx.getResponse();
|
|
29
|
+
const statusCode = exception.getStatus();
|
|
30
|
+
const prefix = exception.prefix || "exception";
|
|
31
|
+
let message = exception.code + "";
|
|
32
|
+
if (this.i18n) {
|
|
33
|
+
message = await this.i18n.translate(`${prefix}.${exception.code}`, {
|
|
34
|
+
lang: ctx.getRequest().i18nLang,
|
|
35
|
+
args: exception.args,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
this.logger.warn("I18nModule was not register. will to resolve AppException Message");
|
|
40
|
+
}
|
|
41
|
+
response
|
|
42
|
+
.status(statusCode)
|
|
43
|
+
.json(common_1.JsonResponse.Error(exception.code, message));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
AppExceptionFilter = AppExceptionFilter_1 = __decorate([
|
|
47
|
+
(0, common_2.Catch)(app_exception_1.AppException),
|
|
48
|
+
__param(0, (0, common_2.Optional)()),
|
|
49
|
+
__metadata("design:paramtypes", [nestjs_i18n_1.I18nService])
|
|
50
|
+
], AppExceptionFilter);
|
|
51
|
+
exports.AppExceptionFilter = AppExceptionFilter;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HttpException } from "@nestjs/common";
|
|
2
|
+
export declare class AppException extends HttpException {
|
|
3
|
+
private _code;
|
|
4
|
+
private _args?;
|
|
5
|
+
private _prefix?;
|
|
6
|
+
constructor(code: number, args?: any, prefix?: string);
|
|
7
|
+
get code(): number;
|
|
8
|
+
get args(): any;
|
|
9
|
+
get prefix(): string;
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppException = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
class AppException extends common_1.HttpException {
|
|
6
|
+
constructor(code, args, prefix) {
|
|
7
|
+
super({ code, args, prefix }, 200);
|
|
8
|
+
this._code = code;
|
|
9
|
+
this._args = args;
|
|
10
|
+
this._prefix = prefix;
|
|
11
|
+
}
|
|
12
|
+
get code() {
|
|
13
|
+
return this._code;
|
|
14
|
+
}
|
|
15
|
+
get args() {
|
|
16
|
+
return this._args;
|
|
17
|
+
}
|
|
18
|
+
get prefix() {
|
|
19
|
+
return this._prefix;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.AppException = AppException;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { createAppExceptionFilterProvider } from "./app-exception-filter.provider";
|
|
2
|
+
import { AppExceptionFilter } from "./app-exception.filter";
|
|
3
|
+
import { AppException } from "./app.exception";
|
|
4
|
+
export { AppException, AppExceptionFilter, createAppExceptionFilterProvider };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createAppExceptionFilterProvider = exports.AppExceptionFilter = exports.AppException = void 0;
|
|
4
|
+
const app_exception_filter_provider_1 = require("./app-exception-filter.provider");
|
|
5
|
+
Object.defineProperty(exports, "createAppExceptionFilterProvider", { enumerable: true, get: function () { return app_exception_filter_provider_1.createAppExceptionFilterProvider; } });
|
|
6
|
+
const app_exception_filter_1 = require("./app-exception.filter");
|
|
7
|
+
Object.defineProperty(exports, "AppExceptionFilter", { enumerable: true, get: function () { return app_exception_filter_1.AppExceptionFilter; } });
|
|
8
|
+
const app_exception_1 = require("./app.exception");
|
|
9
|
+
Object.defineProperty(exports, "AppException", { enumerable: true, get: function () { return app_exception_1.AppException; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface Translator {
|
|
2
|
+
translate(key: string, params: {
|
|
3
|
+
lang: string;
|
|
4
|
+
args?: any;
|
|
5
|
+
}): string | Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
export declare class NullTranslator implements Translator {
|
|
8
|
+
translate(key: string, params: {
|
|
9
|
+
lang: string;
|
|
10
|
+
args?: any;
|
|
11
|
+
}): string | Promise<string>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { DynamicModule } from "@nestjs/common";
|
|
2
|
+
import { I18nAsyncOptions, I18nOptions } from "nestjs-i18n";
|
|
3
|
+
export declare class I18nCoreModule {
|
|
4
|
+
static forRoot(options?: I18nOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options?: I18nAsyncOptions): DynamicModule;
|
|
6
|
+
private static sanitizeI18nOptions;
|
|
7
|
+
private static sanitizeI18nAsyncOptions;
|
|
8
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
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 I18nCoreModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.I18nCoreModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const nestjs_i18n_1 = require("nestjs-i18n");
|
|
13
|
+
const path_1 = require("path");
|
|
14
|
+
let I18nCoreModule = I18nCoreModule_1 = class I18nCoreModule {
|
|
15
|
+
static forRoot(options) {
|
|
16
|
+
return {
|
|
17
|
+
module: I18nCoreModule_1,
|
|
18
|
+
imports: [nestjs_i18n_1.I18nModule.forRoot(this.sanitizeI18nOptions(options))],
|
|
19
|
+
exports: [nestjs_i18n_1.I18nModule],
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
static forRootAsync(options) {
|
|
23
|
+
return {
|
|
24
|
+
module: I18nCoreModule_1,
|
|
25
|
+
imports: [
|
|
26
|
+
nestjs_i18n_1.I18nModule.forRootAsync(this.sanitizeI18nAsyncOptions(options)),
|
|
27
|
+
],
|
|
28
|
+
exports: [nestjs_i18n_1.I18nModule],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
static sanitizeI18nOptions(options) {
|
|
32
|
+
options = { ...defaultOptions, ...options };
|
|
33
|
+
return options;
|
|
34
|
+
}
|
|
35
|
+
static sanitizeI18nAsyncOptions(options) {
|
|
36
|
+
options = { ...defaultAsyncOptions, ...options };
|
|
37
|
+
return options;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
I18nCoreModule = I18nCoreModule_1 = __decorate([
|
|
41
|
+
(0, common_1.Module)({})
|
|
42
|
+
], I18nCoreModule);
|
|
43
|
+
exports.I18nCoreModule = I18nCoreModule;
|
|
44
|
+
const defaultResolvers = [
|
|
45
|
+
{
|
|
46
|
+
use: nestjs_i18n_1.QueryResolver,
|
|
47
|
+
options: ["lang"],
|
|
48
|
+
},
|
|
49
|
+
new nestjs_i18n_1.HeaderResolver(["lang"]),
|
|
50
|
+
nestjs_i18n_1.AcceptLanguageResolver,
|
|
51
|
+
new nestjs_i18n_1.CookieResolver(["lang"]),
|
|
52
|
+
];
|
|
53
|
+
const defaultLoader = nestjs_i18n_1.I18nJsonLoader;
|
|
54
|
+
const defaultPath = process.env.NODE_ENV == "development" ? "src/i18n/" : "dist/i18n/";
|
|
55
|
+
const defaultOptions = {
|
|
56
|
+
fallbackLanguage: process.env.LOCALES_FALLBACK_LANGUAGE || "en_US",
|
|
57
|
+
resolvers: defaultResolvers,
|
|
58
|
+
loader: defaultLoader,
|
|
59
|
+
loaderOptions: {
|
|
60
|
+
path: (0, path_1.join)(process.cwd(), process.env.LOCALES_MAP_PATH || defaultPath),
|
|
61
|
+
watch: process.env.LOCALES_LIVE_RELOAD == "true",
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
const defaultAsyncOptions = {
|
|
65
|
+
resolvers: defaultResolvers,
|
|
66
|
+
loader: defaultLoader,
|
|
67
|
+
useFactory: () => {
|
|
68
|
+
return {
|
|
69
|
+
fallbackLanguage: defaultOptions.fallbackLanguage,
|
|
70
|
+
loaderOptions: {
|
|
71
|
+
path: (0, path_1.join)(process.cwd(), process.env.LOCALES_MAP_PATH || defaultPath),
|
|
72
|
+
watch: process.env.LOCALES_LIVE_RELOAD == "true",
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
};
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.I18nCoreModule = void 0;
|
|
4
|
+
const i18n_core_module_1 = require("./i18n-core.module");
|
|
5
|
+
Object.defineProperty(exports, "I18nCoreModule", { enumerable: true, get: function () { return i18n_core_module_1.I18nCoreModule; } });
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./decorator"), exports);
|
|
18
|
+
__exportStar(require("./exception"), exports);
|
|
19
|
+
__exportStar(require("./i18n"), exports);
|
|
20
|
+
__exportStar(require("./config"), exports);
|
|
21
|
+
__exportStar(require("./serialize"), exports);
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LocaleString = void 0;
|
|
4
|
+
const localize_string_1 = require("./localize.string");
|
|
5
|
+
Object.defineProperty(exports, "LocaleString", { enumerable: true, get: function () { return localize_string_1.LocaleString; } });
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./dist";
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nathapp/nestjs-common",
|
|
3
|
+
"version": "1.0.1-rc.0",
|
|
4
|
+
"description": "NestJs Common Library",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
7
|
+
"precommit": "lint-staged",
|
|
8
|
+
"test": "jest",
|
|
9
|
+
"test:watch": "jest --watch",
|
|
10
|
+
"test:cov": "jest --coverage",
|
|
11
|
+
"test:e2e": "jest --config ./test/jest-e2e.json",
|
|
12
|
+
"lint": "eslint -c .eslintrc.js"
|
|
13
|
+
},
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@nestjs/platform-express": "^9.0.0",
|
|
18
|
+
"@nestjs/platform-fastify": "^9.0.0",
|
|
19
|
+
"@types/js-yaml": "^4.0.5",
|
|
20
|
+
"@types/node": "^17.0.22",
|
|
21
|
+
"@typescript-eslint/eslint-plugin": "^4",
|
|
22
|
+
"@typescript-eslint/parser": "^4",
|
|
23
|
+
"eslint": "*",
|
|
24
|
+
"eslint-config-prettier": "^6",
|
|
25
|
+
"eslint-plugin-prettier": "^3",
|
|
26
|
+
"prettier": "^2",
|
|
27
|
+
"typescript": "^4"
|
|
28
|
+
},
|
|
29
|
+
"types": "./index.d.ts",
|
|
30
|
+
"main": "index.js",
|
|
31
|
+
"directories": {
|
|
32
|
+
"lib": "lib"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@nathapp/common": "^0.1.10",
|
|
36
|
+
"joi": "^17.6.0",
|
|
37
|
+
"js-yaml": "^4.1.0",
|
|
38
|
+
"nestjs-i18n": "^10.0.0",
|
|
39
|
+
"request-ip": "^3.2.0"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@nestjs/common": "*",
|
|
43
|
+
"@nestjs/config": "*",
|
|
44
|
+
"@nestjs/core": "*",
|
|
45
|
+
"class-transformer": "*",
|
|
46
|
+
"class-validator": "*",
|
|
47
|
+
"rxjs": "*"
|
|
48
|
+
}
|
|
49
|
+
}
|