@nest-omni/core 1.0.56 → 2.0.1-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/.history/src/common/boilerplate.polyfill_20250521135408.ts +202 -0
- package/.history/src/common/boilerplate.polyfill_20250712165919.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712165924.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712165927.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712170044.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170049.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170053.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170610.ts +280 -0
- package/.history/src/common/boilerplate.polyfill_20250712170614.ts +280 -0
- package/.history/src/common/boilerplate.polyfill_20250712170739.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170801.ts +291 -0
- package/.history/src/common/boilerplate.polyfill_20250712170809.ts +317 -0
- package/.history/src/common/boilerplate.polyfill_20250712170816.ts +319 -0
- package/.history/src/common/boilerplate.polyfill_20250712170820.ts +318 -0
- package/.history/src/common/boilerplate.polyfill_20250712170824.ts +318 -0
- package/.history/src/common/boilerplate.polyfill_20250712171040.ts +289 -0
- package/README.md +16 -0
- package/build.sh +45 -0
- package/{common → dist/common}/abstract.entity.d.ts +1 -0
- package/{common → dist/common}/abstract.entity.js +13 -0
- package/{decorators → dist/decorators}/field.decorators.d.ts +1 -1
- package/dist/package.json +71 -0
- package/{providers → dist/providers}/context.provider.d.ts +3 -3
- package/dist/setup/bootstrap.setup.d.ts +2 -0
- package/{setup → dist/setup}/bootstrap.setup.js +45 -27
- package/{shared → dist/shared}/serviceRegistryModule.js +4 -11
- package/{shared → dist/shared}/services/api-config.service.d.ts +3 -15
- package/{shared → dist/shared}/services/api-config.service.js +15 -42
- package/{shared → dist/shared}/services/index.d.ts +0 -1
- package/{shared → dist/shared}/services/index.js +0 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/yarn.lock +644 -0
- package/package.json +1 -3
- package/src/.hygen.js +116 -0
- package/src/common/abstract-client.service.ts +56 -0
- package/src/common/abstract.entity.ts +82 -0
- package/src/common/boilerplate.polyfill.ts +202 -0
- package/src/common/dto/abstract.dto.ts +61 -0
- package/src/common/dto/bulk-id.dto.ts +13 -0
- package/src/common/dto/create-translation.dto.ts +10 -0
- package/src/common/dto/index.ts +6 -0
- package/src/common/dto/page-meta.dto.ts +37 -0
- package/src/common/dto/page-options.dto.ts +35 -0
- package/src/common/dto/page.dto.ts +36 -0
- package/src/common/index.ts +7 -0
- package/src/common/snake-naming.strategy.ts +64 -0
- package/src/common/types.ts +44 -0
- package/src/common/utils.ts +45 -0
- package/src/constants/index.ts +2 -0
- package/src/constants/language-code.ts +7 -0
- package/src/constants/order.ts +4 -0
- package/src/decorators/api-page-ok-response.decorator.ts +32 -0
- package/src/decorators/auth-user.decorator.ts +16 -0
- package/src/decorators/controller.decorator.ts +8 -0
- package/src/decorators/field.decorators.ts +856 -0
- package/src/decorators/http.decorators.ts +10 -0
- package/src/decorators/index.ts +12 -0
- package/src/decorators/operation.decorator.ts +4 -0
- package/src/decorators/property.decorators.ts +62 -0
- package/src/decorators/public-route.decorator.ts +8 -0
- package/src/decorators/swagger.schema.ts +117 -0
- package/src/decorators/timestamp-column.decorator.ts +29 -0
- package/src/decorators/transform.decorators.ts +159 -0
- package/src/decorators/translate.decorator.ts +19 -0
- package/src/decorators/use-dto.decorator.ts +11 -0
- package/src/decorators/user-check.decorator.ts +30 -0
- package/src/decorators/user.decorator.ts +59 -0
- package/src/decorators/user_auth.decorator.ts +15 -0
- package/src/decorators/validator.decorators.ts +98 -0
- package/src/exceptions/file-not-image.exception.ts +7 -0
- package/src/exceptions/index.ts +2 -0
- package/src/exceptions/page-type.exception.ts +7 -0
- package/src/exceptions/user-not-found.exception.ts +7 -0
- package/src/filters/bad-request.filter.ts +77 -0
- package/src/filters/constraint-errors.ts +5 -0
- package/src/filters/index.ts +3 -0
- package/src/filters/query-failed.filter.ts +32 -0
- package/src/health-checker/health-checker.controller.ts +51 -0
- package/src/health-checker/health-checker.module.ts +13 -0
- package/src/health-checker/health-indicators/service.indicator.ts +44 -0
- package/src/helpers/common.helper.ts +49 -0
- package/src/helpers/date.helper.ts +64 -0
- package/src/helpers/index.ts +2 -0
- package/src/i18n/en_US/validation.json +41 -0
- package/src/i18n/zh_CN/validation.json +41 -0
- package/src/index.ts +11 -0
- package/src/interceptors/index.ts +2 -0
- package/src/interceptors/language-interceptor.service.ts +28 -0
- package/src/interceptors/translation-interceptor.service.ts +43 -0
- package/src/interfaces/IApiFile.ts +4 -0
- package/src/interfaces/IFile.ts +8 -0
- package/src/interfaces/ITranslationDecoratorInterface.ts +3 -0
- package/src/interfaces/index.ts +3 -0
- package/src/interfaces/response.ts +37 -0
- package/src/middlewares/index.ts +4 -0
- package/src/middlewares/omni-auth.middleware.ts +10 -0
- package/src/middlewares/powerby.middleware.ts +6 -0
- package/src/middlewares/request-id.middleware.ts +28 -0
- package/src/middlewares/xml.middleware.ts +14 -0
- package/src/providers/context.provider.ts +109 -0
- package/src/providers/generator.provider.ts +66 -0
- package/src/providers/index.ts +2 -0
- package/src/setup/bootstrap.setup.ts +254 -0
- package/src/setup/index.ts +1 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/serviceRegistryModule.ts +135 -0
- package/src/shared/services/api-config.service.ts +336 -0
- package/src/shared/services/generator.service.ts +13 -0
- package/src/shared/services/index.ts +4 -0
- package/src/shared/services/translation.service.ts +59 -0
- package/src/shared/services/validator.service.ts +10 -0
- package/src/validator-json/decorators.ts +63 -0
- package/src/validator-json/default.ts +257 -0
- package/src/validator-json/defaultConverters.ts +402 -0
- package/src/validator-json/index.ts +4 -0
- package/src/validator-json/options.ts +50 -0
- package/src/validators/file-mimetype.validator.ts +34 -0
- package/src/validators/index.ts +6 -0
- package/src/validators/is-exists.validator.ts +60 -0
- package/src/validators/is-unique.validator.ts +93 -0
- package/src/validators/phone-country-code.validator.ts +36 -0
- package/src/validators/same-as.validator.ts +27 -0
- package/src/validators/skip-empty.validator.ts +25 -0
- package/tsconfig.json +31 -0
- package/tsconfig.lib.json +16 -0
- package/setup/bootstrap.setup.d.ts +0 -2
- package/shared/services/aws-s3.service.d.ts +0 -10
- package/shared/services/aws-s3.service.js +0 -56
- package/tsconfig.tsbuildinfo +0 -1
- /package/{.hygen → dist/.hygen}/new/crud/controller.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/create.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/get.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/index-dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/index-response.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/module.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/prompt.js +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/service.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/update.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/controller.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/create-command.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/create.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/get-query.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/module.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/not-found.exception.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/page-options.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/prompt.js +0 -0
- /package/{.hygen → dist/.hygen}/new/module/repository.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/service.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-repository.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/update.dto.ejs.t +0 -0
- /package/{.hygen.js → dist/.hygen.js} +0 -0
- /package/{common → dist/common}/abstract-client.service.d.ts +0 -0
- /package/{common → dist/common}/abstract-client.service.js +0 -0
- /package/{common → dist/common}/boilerplate.polyfill.d.ts +0 -0
- /package/{common → dist/common}/boilerplate.polyfill.js +0 -0
- /package/{common → dist/common}/dto/abstract.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/abstract.dto.js +0 -0
- /package/{common → dist/common}/dto/bulk-id.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/bulk-id.dto.js +0 -0
- /package/{common → dist/common}/dto/create-translation.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/create-translation.dto.js +0 -0
- /package/{common → dist/common}/dto/index.d.ts +0 -0
- /package/{common → dist/common}/dto/index.js +0 -0
- /package/{common → dist/common}/dto/page-meta.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page-meta.dto.js +0 -0
- /package/{common → dist/common}/dto/page-options.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page-options.dto.js +0 -0
- /package/{common → dist/common}/dto/page.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page.dto.js +0 -0
- /package/{common → dist/common}/index.d.ts +0 -0
- /package/{common → dist/common}/index.js +0 -0
- /package/{common → dist/common}/snake-naming.strategy.d.ts +0 -0
- /package/{common → dist/common}/snake-naming.strategy.js +0 -0
- /package/{common → dist/common}/types.d.ts +0 -0
- /package/{common → dist/common}/types.js +0 -0
- /package/{common → dist/common}/utils.d.ts +0 -0
- /package/{common → dist/common}/utils.js +0 -0
- /package/{constants → dist/constants}/index.d.ts +0 -0
- /package/{constants → dist/constants}/index.js +0 -0
- /package/{constants → dist/constants}/language-code.d.ts +0 -0
- /package/{constants → dist/constants}/language-code.js +0 -0
- /package/{constants → dist/constants}/order.d.ts +0 -0
- /package/{constants → dist/constants}/order.js +0 -0
- /package/{decorators → dist/decorators}/api-page-ok-response.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/api-page-ok-response.decorator.js +0 -0
- /package/{decorators → dist/decorators}/auth-user.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/auth-user.decorator.js +0 -0
- /package/{decorators → dist/decorators}/controller.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/controller.decorator.js +0 -0
- /package/{decorators → dist/decorators}/field.decorators.js +0 -0
- /package/{decorators → dist/decorators}/http.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/http.decorators.js +0 -0
- /package/{decorators → dist/decorators}/index.d.ts +0 -0
- /package/{decorators → dist/decorators}/index.js +0 -0
- /package/{decorators → dist/decorators}/operation.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/operation.decorator.js +0 -0
- /package/{decorators → dist/decorators}/property.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/property.decorators.js +0 -0
- /package/{decorators → dist/decorators}/public-route.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/public-route.decorator.js +0 -0
- /package/{decorators → dist/decorators}/swagger.schema.d.ts +0 -0
- /package/{decorators → dist/decorators}/swagger.schema.js +0 -0
- /package/{decorators → dist/decorators}/timestamp-column.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/timestamp-column.decorator.js +0 -0
- /package/{decorators → dist/decorators}/transform.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/transform.decorators.js +0 -0
- /package/{decorators → dist/decorators}/translate.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/translate.decorator.js +0 -0
- /package/{decorators → dist/decorators}/use-dto.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/use-dto.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user-check.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user-check.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user_auth.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user_auth.decorator.js +0 -0
- /package/{decorators → dist/decorators}/validator.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/validator.decorators.js +0 -0
- /package/{exceptions → dist/exceptions}/file-not-image.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/file-not-image.exception.js +0 -0
- /package/{exceptions → dist/exceptions}/index.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/index.js +0 -0
- /package/{exceptions → dist/exceptions}/page-type.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/page-type.exception.js +0 -0
- /package/{exceptions → dist/exceptions}/user-not-found.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/user-not-found.exception.js +0 -0
- /package/{filters → dist/filters}/bad-request.filter.d.ts +0 -0
- /package/{filters → dist/filters}/bad-request.filter.js +0 -0
- /package/{filters → dist/filters}/constraint-errors.d.ts +0 -0
- /package/{filters → dist/filters}/constraint-errors.js +0 -0
- /package/{filters → dist/filters}/index.d.ts +0 -0
- /package/{filters → dist/filters}/index.js +0 -0
- /package/{filters → dist/filters}/query-failed.filter.d.ts +0 -0
- /package/{filters → dist/filters}/query-failed.filter.js +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.controller.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.controller.js +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.module.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.module.js +0 -0
- /package/{health-checker → dist/health-checker}/health-indicators/service.indicator.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-indicators/service.indicator.js +0 -0
- /package/{helpers → dist/helpers}/common.helper.d.ts +0 -0
- /package/{helpers → dist/helpers}/common.helper.js +0 -0
- /package/{helpers → dist/helpers}/date.helper.d.ts +0 -0
- /package/{helpers → dist/helpers}/date.helper.js +0 -0
- /package/{helpers → dist/helpers}/index.d.ts +0 -0
- /package/{helpers → dist/helpers}/index.js +0 -0
- /package/{i18n → dist/i18n}/en_US/validation.json +0 -0
- /package/{i18n → dist/i18n}/zh_CN/validation.json +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{interceptors → dist/interceptors}/index.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/index.js +0 -0
- /package/{interceptors → dist/interceptors}/language-interceptor.service.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/language-interceptor.service.js +0 -0
- /package/{interceptors → dist/interceptors}/translation-interceptor.service.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/translation-interceptor.service.js +0 -0
- /package/{interfaces → dist/interfaces}/IApiFile.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/IApiFile.js +0 -0
- /package/{interfaces → dist/interfaces}/IFile.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/IFile.js +0 -0
- /package/{interfaces → dist/interfaces}/ITranslationDecoratorInterface.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/ITranslationDecoratorInterface.js +0 -0
- /package/{interfaces → dist/interfaces}/index.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/index.js +0 -0
- /package/{interfaces → dist/interfaces}/response.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/response.js +0 -0
- /package/{middlewares → dist/middlewares}/index.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/index.js +0 -0
- /package/{middlewares → dist/middlewares}/omni-auth.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/omni-auth.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/powerby.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/powerby.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/request-id.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/request-id.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/xml.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/xml.middleware.js +0 -0
- /package/{providers → dist/providers}/context.provider.js +0 -0
- /package/{providers → dist/providers}/generator.provider.d.ts +0 -0
- /package/{providers → dist/providers}/generator.provider.js +0 -0
- /package/{providers → dist/providers}/index.d.ts +0 -0
- /package/{providers → dist/providers}/index.js +0 -0
- /package/{setup → dist/setup}/index.d.ts +0 -0
- /package/{setup → dist/setup}/index.js +0 -0
- /package/{shared → dist/shared}/index.d.ts +0 -0
- /package/{shared → dist/shared}/index.js +0 -0
- /package/{shared → dist/shared}/serviceRegistryModule.d.ts +0 -0
- /package/{shared → dist/shared}/services/generator.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/generator.service.js +0 -0
- /package/{shared → dist/shared}/services/translation.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/translation.service.js +0 -0
- /package/{shared → dist/shared}/services/validator.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/validator.service.js +0 -0
- /package/{validator-json → dist/validator-json}/decorators.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/decorators.js +0 -0
- /package/{validator-json → dist/validator-json}/default.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/default.js +0 -0
- /package/{validator-json → dist/validator-json}/defaultConverters.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/defaultConverters.js +0 -0
- /package/{validator-json → dist/validator-json}/index.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/index.js +0 -0
- /package/{validator-json → dist/validator-json}/options.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/options.js +0 -0
- /package/{validators → dist/validators}/file-mimetype.validator.d.ts +0 -0
- /package/{validators → dist/validators}/file-mimetype.validator.js +0 -0
- /package/{validators → dist/validators}/index.d.ts +0 -0
- /package/{validators → dist/validators}/index.js +0 -0
- /package/{validators → dist/validators}/is-exists.validator.d.ts +0 -0
- /package/{validators → dist/validators}/is-exists.validator.js +0 -0
- /package/{validators → dist/validators}/is-unique.validator.d.ts +0 -0
- /package/{validators → dist/validators}/is-unique.validator.js +0 -0
- /package/{validators → dist/validators}/phone-country-code.validator.d.ts +0 -0
- /package/{validators → dist/validators}/phone-country-code.validator.js +0 -0
- /package/{validators → dist/validators}/same-as.validator.d.ts +0 -0
- /package/{validators → dist/validators}/same-as.validator.js +0 -0
- /package/{validators → dist/validators}/skip-empty.validator.d.ts +0 -0
- /package/{validators → dist/validators}/skip-empty.validator.js +0 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import { Catch, HttpException } from '@nestjs/common';
|
|
3
|
+
import { Request, Response } from 'express';
|
|
4
|
+
import { EntityNotFoundError } from 'typeorm';
|
|
5
|
+
import { I18nContext, I18nValidationException } from 'nestjs-i18n';
|
|
6
|
+
|
|
7
|
+
@Catch()
|
|
8
|
+
export class HttpExceptionFilter implements ExceptionFilter {
|
|
9
|
+
catch(exception, host: ArgumentsHost) {
|
|
10
|
+
const i18n = I18nContext.current(host);
|
|
11
|
+
const ctx = host.switchToHttp();
|
|
12
|
+
const response: Response = ctx.getResponse();
|
|
13
|
+
const request: Request = ctx.getRequest();
|
|
14
|
+
let statusCode =
|
|
15
|
+
exception instanceof HttpException ? exception.getStatus() : 500;
|
|
16
|
+
let error =
|
|
17
|
+
exception instanceof HttpException
|
|
18
|
+
? exception.message
|
|
19
|
+
: 'Internal server error';
|
|
20
|
+
|
|
21
|
+
if (exception instanceof EntityNotFoundError) {
|
|
22
|
+
statusCode = 404;
|
|
23
|
+
error = 'Entity not found';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (statusCode === 500) {
|
|
27
|
+
console.error({
|
|
28
|
+
message: exception.message,
|
|
29
|
+
stack: exception.stack,
|
|
30
|
+
connection: {
|
|
31
|
+
localAddress: request.socket?.localAddress,
|
|
32
|
+
remoteAddress: request.socket?.remoteAddress,
|
|
33
|
+
bytesRead: request.socket?.bytesRead,
|
|
34
|
+
bytesWritten: request.socket?.bytesWritten,
|
|
35
|
+
},
|
|
36
|
+
headers: request.headers,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const getFirstError = (error, property = '') => {
|
|
41
|
+
if (error?.children?.length > 0) {
|
|
42
|
+
const firstError = error.children[0];
|
|
43
|
+
firstError['property'] = `${property}.${firstError['property']}`;
|
|
44
|
+
return getFirstError(firstError, firstError.property);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return error;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (exception instanceof I18nValidationException) {
|
|
51
|
+
const firstErros = getFirstError(
|
|
52
|
+
exception.errors[0],
|
|
53
|
+
exception.errors[0].property,
|
|
54
|
+
);
|
|
55
|
+
const constraint = Object.values(firstErros.constraints)[0] as string;
|
|
56
|
+
const [translationKey, argsString] = constraint.split('|');
|
|
57
|
+
const args = !!argsString ? JSON.parse(argsString) : {};
|
|
58
|
+
error = i18n.translate(translationKey, {
|
|
59
|
+
args: {
|
|
60
|
+
property: firstErros.property,
|
|
61
|
+
value: firstErros.value,
|
|
62
|
+
constraints: firstErros.constraints,
|
|
63
|
+
...args,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const parseJson = {
|
|
69
|
+
code: statusCode,
|
|
70
|
+
msg: error,
|
|
71
|
+
requestId: request.id,
|
|
72
|
+
timestamp: new Date().toISOString(),
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return response.status(200).json(parseJson);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
import { Catch, HttpStatus } from '@nestjs/common';
|
|
3
|
+
import { Reflector } from '@nestjs/core';
|
|
4
|
+
import { STATUS_CODES } from 'http';
|
|
5
|
+
import { QueryFailedError } from 'typeorm';
|
|
6
|
+
|
|
7
|
+
import { constraintErrors } from './constraint-errors';
|
|
8
|
+
|
|
9
|
+
@Catch(QueryFailedError)
|
|
10
|
+
export class QueryFailedFilter implements ExceptionFilter<QueryFailedError> {
|
|
11
|
+
constructor(public reflector: Reflector) {}
|
|
12
|
+
|
|
13
|
+
catch(
|
|
14
|
+
exception: QueryFailedError & { constraint?: string },
|
|
15
|
+
host: ArgumentsHost,
|
|
16
|
+
) {
|
|
17
|
+
const ctx = host.switchToHttp();
|
|
18
|
+
const response = ctx.getResponse();
|
|
19
|
+
|
|
20
|
+
const status = exception.constraint?.startsWith('UQ')
|
|
21
|
+
? HttpStatus.CONFLICT
|
|
22
|
+
: HttpStatus.INTERNAL_SERVER_ERROR;
|
|
23
|
+
|
|
24
|
+
response.status(status).json({
|
|
25
|
+
code: status,
|
|
26
|
+
error: STATUS_CODES[status],
|
|
27
|
+
msg: exception.constraint
|
|
28
|
+
? constraintErrors[exception.constraint]
|
|
29
|
+
: undefined,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
import { Controller, Get } from '@nestjs/common';
|
|
3
|
+
import type { HealthCheckResult } from '@nestjs/terminus';
|
|
4
|
+
import {
|
|
5
|
+
HealthCheck,
|
|
6
|
+
HealthCheckService,
|
|
7
|
+
TypeOrmHealthIndicator,
|
|
8
|
+
} from '@nestjs/terminus';
|
|
9
|
+
|
|
10
|
+
import { ServiceHealthIndicator } from './health-indicators/service.indicator';
|
|
11
|
+
import { ApiOperation } from '@nestjs/swagger';
|
|
12
|
+
import { RedisHealthIndicator } from '@mvmdev/nestjs-redis-health';
|
|
13
|
+
import { ApiConfigService } from '../shared/services';
|
|
14
|
+
|
|
15
|
+
@Controller('health')
|
|
16
|
+
export class HealthCheckerController {
|
|
17
|
+
private readonly redis: Redis;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
private configService: ApiConfigService,
|
|
21
|
+
private healthCheckService: HealthCheckService,
|
|
22
|
+
private ormIndicator: TypeOrmHealthIndicator,
|
|
23
|
+
private serviceIndicator: ServiceHealthIndicator,
|
|
24
|
+
private redisIndicator: RedisHealthIndicator,
|
|
25
|
+
) {
|
|
26
|
+
if (this.configService.isRedisEnabled) {
|
|
27
|
+
this.redis = new Redis(this.configService.ioRedisConfig);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
@Get()
|
|
32
|
+
@HealthCheck()
|
|
33
|
+
@ApiOperation({ summary: 'Service health check' })
|
|
34
|
+
async check(): Promise<HealthCheckResult> {
|
|
35
|
+
const indicator = [
|
|
36
|
+
() => this.ormIndicator.pingCheck('database', { timeout: 1500 }),
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
if (this.configService.isRedisEnabled) {
|
|
40
|
+
indicator.push(() =>
|
|
41
|
+
this.redisIndicator.checkHealth('redis', {
|
|
42
|
+
type: 'redis',
|
|
43
|
+
client: this.redis,
|
|
44
|
+
timeout: 1500,
|
|
45
|
+
}),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return this.healthCheckService.check(indicator);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { TerminusModule } from '@nestjs/terminus';
|
|
3
|
+
import { RedisHealthModule } from '@mvmdev/nestjs-redis-health';
|
|
4
|
+
import { HealthCheckerController } from './health-checker.controller';
|
|
5
|
+
import { ServiceHealthIndicator } from './health-indicators/service.indicator';
|
|
6
|
+
import { ApiConfigService } from '../shared/services';
|
|
7
|
+
|
|
8
|
+
@Module({
|
|
9
|
+
imports: [TerminusModule, RedisHealthModule],
|
|
10
|
+
controllers: [HealthCheckerController],
|
|
11
|
+
providers: [ApiConfigService, ServiceHealthIndicator],
|
|
12
|
+
})
|
|
13
|
+
export class HealthCheckerModule {}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
+
import { ClientProxy } from '@nestjs/microservices';
|
|
3
|
+
import type { HealthIndicatorResult } from '@nestjs/terminus';
|
|
4
|
+
import { HealthCheckError, HealthIndicator } from '@nestjs/terminus';
|
|
5
|
+
import { firstValueFrom } from 'rxjs';
|
|
6
|
+
import { timeout } from 'rxjs/operators';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class ServiceHealthIndicator extends HealthIndicator {
|
|
10
|
+
constructor(
|
|
11
|
+
@Optional()
|
|
12
|
+
@Inject('NATS_SERVICE')
|
|
13
|
+
private readonly clientProxy?: ClientProxy,
|
|
14
|
+
) {
|
|
15
|
+
super();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async isHealthy(eventName: string): Promise<HealthIndicatorResult> {
|
|
19
|
+
try {
|
|
20
|
+
if (!this.clientProxy) {
|
|
21
|
+
return {
|
|
22
|
+
[eventName]: {
|
|
23
|
+
status: 'down',
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const result = await firstValueFrom(
|
|
29
|
+
this.clientProxy.send(eventName, { check: true }).pipe(timeout(10000)),
|
|
30
|
+
{
|
|
31
|
+
defaultValue: undefined,
|
|
32
|
+
},
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
[eventName]: result,
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
throw new HealthCheckError(`${eventName} failed`, {
|
|
40
|
+
[eventName]: error,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { networkInterfaces } from 'os';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 将枚举值转换成数组
|
|
5
|
+
* @param Enum 枚举
|
|
6
|
+
*/
|
|
7
|
+
export function enumToArray<E>(Enum: any): E[] {
|
|
8
|
+
return Object.keys(Enum)
|
|
9
|
+
.filter((key) => typeof Enum[key as any] === 'number')
|
|
10
|
+
.map((key) => Enum[key]);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 生成随机字符串
|
|
15
|
+
* @param {number} length 生成长度
|
|
16
|
+
* @param {string} charSet 指定字符集
|
|
17
|
+
* @returns {string} 生成字符串
|
|
18
|
+
*/
|
|
19
|
+
export const randomString = (length: number = 8, charSet?: string): string => {
|
|
20
|
+
charSet =
|
|
21
|
+
charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
22
|
+
let randomString = '';
|
|
23
|
+
for (let i = 0; i < length; i++) {
|
|
24
|
+
const randomPoz = Math.floor(Math.random() * charSet.length);
|
|
25
|
+
randomString += charSet.substring(randomPoz, randomPoz + 1);
|
|
26
|
+
}
|
|
27
|
+
return randomString;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 获取服务部署 IP 地址
|
|
32
|
+
*/
|
|
33
|
+
export function getIPAdress(): string {
|
|
34
|
+
const interfaces = networkInterfaces();
|
|
35
|
+
|
|
36
|
+
for (const devName in interfaces) {
|
|
37
|
+
const iface = interfaces[devName];
|
|
38
|
+
for (let i = 0; i < iface.length; i++) {
|
|
39
|
+
const alias = iface[i];
|
|
40
|
+
if (
|
|
41
|
+
alias.family === 'IPv4' &&
|
|
42
|
+
alias.address !== '127.0.0.1' &&
|
|
43
|
+
!alias.internal
|
|
44
|
+
) {
|
|
45
|
+
return alias.address;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { ColumnOptions } from 'typeorm/decorator/options/ColumnOptions';
|
|
2
|
+
import * as moment from 'moment';
|
|
3
|
+
import * as _ from 'lodash';
|
|
4
|
+
|
|
5
|
+
export function timestamp(value?: number) {
|
|
6
|
+
if (!value) {
|
|
7
|
+
value = moment.now();
|
|
8
|
+
}
|
|
9
|
+
return Math.round(value / 1000);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function strToTimeStamp(str: string | Date) {
|
|
13
|
+
if (_.isEmpty(str)) {
|
|
14
|
+
return 0;
|
|
15
|
+
}
|
|
16
|
+
return moment(str).unix();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getTodayTimeStamp() {
|
|
20
|
+
const startTime = strToTimeStamp(moment().format('YYYY-MM-DD 00:00:00'));
|
|
21
|
+
const endTime = strToTimeStamp(moment().format('YYYY-MM-DD 23:59:59'));
|
|
22
|
+
return {
|
|
23
|
+
startTime,
|
|
24
|
+
endTime,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function unixtimestampToDate(value: number): Date {
|
|
29
|
+
return moment(value * 1000).toDate();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function formatDateTime(
|
|
33
|
+
value: number | string,
|
|
34
|
+
format: string = 'YYYY-MM-DD HH:mm:ss',
|
|
35
|
+
holder: string | null = '',
|
|
36
|
+
): string {
|
|
37
|
+
if (value === 'now') {
|
|
38
|
+
return moment().format(format);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (typeof value === 'string') {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (value > 0) {
|
|
46
|
+
return moment(value * 1000).format(format);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return holder;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function timestampColumn(name: string): ColumnOptions {
|
|
53
|
+
return {
|
|
54
|
+
type: 'int',
|
|
55
|
+
name,
|
|
56
|
+
nullable: false,
|
|
57
|
+
default: 0,
|
|
58
|
+
transformer: {
|
|
59
|
+
to: (value) =>
|
|
60
|
+
value === 0 || /\d{10}/.test(value) ? value : strToTimeStamp(value),
|
|
61
|
+
from: (value?: number) => (!value ? value : formatDateTime),
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"NOT_EMPTY": "{property} cannot be empty",
|
|
3
|
+
"NOT_EQUALS": "{property} should not be equal to {constraints.1}",
|
|
4
|
+
"LENGTH": "{property} needs to be at least {constraints.0} and at most {constraints.1} characters long",
|
|
5
|
+
"MIN_LENGTH": "{property} needs to be at least {constraints.0} characters long",
|
|
6
|
+
"MAX_LENGTH": "{property} needs to be at most {constraints.0} characters long",
|
|
7
|
+
"INVALID_EMAIL": " {property} is invalid email",
|
|
8
|
+
"INVALID_BOOLEAN": "{property} is not a boolean",
|
|
9
|
+
"MIN": "{property} with value: \"{value}\" needs to be at least {constraints.0}",
|
|
10
|
+
"MAX": "{property} with value: \"{value}\" needs to be less than {constraints.0}",
|
|
11
|
+
"NOT_FOUND": "Not Found",
|
|
12
|
+
"DELETE_FAILED": "Gagal menghapus data",
|
|
13
|
+
"FILE_NOT_EMPTY": "File tidak boleh kosong",
|
|
14
|
+
"FILE_ONLY_EXCEL": "Hanya menerima extensi xlsx, xls",
|
|
15
|
+
"EMAIL_EXISTS": "email already exits",
|
|
16
|
+
"CONFIRM_PASSWORD_NOT_MATCH": "confirm password not match",
|
|
17
|
+
"INVALID_STRING": "{property} must be a string",
|
|
18
|
+
"INVALID_DATETIME": "{property} is not a valid date",
|
|
19
|
+
"IS_NUMBER": "{property} must be a number",
|
|
20
|
+
"IS_ENUM": "{property} must be one of the following values: {constraints.1}",
|
|
21
|
+
"IS_STRING": "{property} must be a string",
|
|
22
|
+
"IS_ARRAY": "{property} must be an array",
|
|
23
|
+
"IS_URL": "{property} must be a URL",
|
|
24
|
+
"IS_UUID": "{property} must be a UUID",
|
|
25
|
+
"IS_DATE": "{property} must be a Date instance",
|
|
26
|
+
"MIN_DATE": "{property} should be later than {constraints.0}",
|
|
27
|
+
"MIN_DATE_NOW": "{property} should be later than the current time",
|
|
28
|
+
"MAX_DATE": "{property} should be earlier than {constraints.0}",
|
|
29
|
+
"MAX_DATE_NOW": "{property} should be earlier than current time",
|
|
30
|
+
"IS_IN": "{property} must be one of the following values: {constraints.1}",
|
|
31
|
+
"IS_INT": "{property} must be an integer number",
|
|
32
|
+
"IS_POSITIVE": "{property} must be a positive number",
|
|
33
|
+
"IS_NOT_EMPTY": "{property} should not be empty",
|
|
34
|
+
"IS_UNIQUE": "{property} with the same value `{value}` exists",
|
|
35
|
+
"IS_EXISTS": "{property} value `{value}` does not exists",
|
|
36
|
+
"IS_FQDN": "{property} value `{value}` not a valid FQDN",
|
|
37
|
+
"IS_IP": "{property} value `{value}` not a valid IP",
|
|
38
|
+
"IS_OBJECT": "{property} value `{value}` not a valid Object",
|
|
39
|
+
"IS_TIME_ZONE": "{property} value `{value}` must be a valid IANA time-zone",
|
|
40
|
+
"IS_LOCALE": "{property} value `{value}` must be a valid locale"
|
|
41
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"NOT_EMPTY": "{property}不能为空",
|
|
3
|
+
"NOT_EQUALS": "{property}不能等于{constraints.1}",
|
|
4
|
+
"LENGTH": "{property}长度必须在{constraints.0}到{constraints.1}个字符之间",
|
|
5
|
+
"MIN_LENGTH": "{property}长度至少为{constraints.0}个字符",
|
|
6
|
+
"MAX_LENGTH": "{property}长度不能超过{constraints.0}个字符",
|
|
7
|
+
"INVALID_EMAIL": "{property}不是有效电子邮件",
|
|
8
|
+
"INVALID_BOOLEAN": "{property}不是布尔值",
|
|
9
|
+
"MIN": "{property}必须大于{constraints.0}",
|
|
10
|
+
"MAX": "{property}必须小于{constraints.0}",
|
|
11
|
+
"NOT_FOUND": "未找到",
|
|
12
|
+
"DELETE_FAILED": "删除数据失败",
|
|
13
|
+
"FILE_NOT_EMPTY": "文件不能为空",
|
|
14
|
+
"FILE_ONLY_EXCEL": "只接受xlsx、xls文件",
|
|
15
|
+
"EMAIL_EXISTS": "电子邮件已存在",
|
|
16
|
+
"CONFIRM_PASSWORD_NOT_MATCH": "确认密码不匹配",
|
|
17
|
+
"INVALID_STRING": "{property}必须是一个字符串",
|
|
18
|
+
"INVALID_DATETIME": "{property}不是一个有效日期",
|
|
19
|
+
"IS_NUMBER": "{property}必须是一个数字",
|
|
20
|
+
"IS_ENUM": "{property}必须是以下值之一:{constraints.1}",
|
|
21
|
+
"IS_STRING": "{property}必须是一个字符串",
|
|
22
|
+
"IS_ARRAY": "{property}必须是一个数组",
|
|
23
|
+
"IS_URL": "{property}必须是一个URL",
|
|
24
|
+
"IS_UUID": "{property}必须是一个UUID",
|
|
25
|
+
"IS_DATE": "{property}必须是一个日期实例",
|
|
26
|
+
"MIN_DATE": "{property}日期不早于{constraints.0}",
|
|
27
|
+
"MAX_DATE": "{property}日期不晚于{constraints.0}",
|
|
28
|
+
"MIN_DATE_NOW": "{property}日期不早于当前时间",
|
|
29
|
+
"MAX_DATE_NOW": "{property}日期不晚于当前时间",
|
|
30
|
+
"IS_IN": "{property}必须是以下值之一:{constraints}",
|
|
31
|
+
"IS_INT": "{property}必须是一个整数",
|
|
32
|
+
"IS_POSITIVE": "{property}必须是一个正数",
|
|
33
|
+
"IS_NOT_EMPTY": "{property}不能为空",
|
|
34
|
+
"IS_UNIQUE": "存在相同值为`{value}`的{property}",
|
|
35
|
+
"IS_EXISTS": "{property}值`{value}`不存在",
|
|
36
|
+
"IS_FQDN": "{property} 值 `{value}` 必须是有效域名",
|
|
37
|
+
"IS_IP": "{property} 值 `{value}` 必须是有效IP",
|
|
38
|
+
"IS_OBJECT": "{property} 值 `{value}` 必须是有效对象",
|
|
39
|
+
"IS_TIME_ZONE": "{property} 值 `{value}` 必须是有效时区",
|
|
40
|
+
"IS_LOCALE": "{property} 值 `{value}` 必须是有效语言"
|
|
41
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './constants';
|
|
2
|
+
export * from './decorators';
|
|
3
|
+
export * from './exceptions';
|
|
4
|
+
export * from './filters';
|
|
5
|
+
export * from './interfaces';
|
|
6
|
+
export * from './interceptors';
|
|
7
|
+
export * from './shared';
|
|
8
|
+
export * from './middlewares';
|
|
9
|
+
export * from './validators';
|
|
10
|
+
export * from './common';
|
|
11
|
+
export * from './validator-json';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallHandler,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
NestInterceptor,
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import { Injectable, UseInterceptors } from '@nestjs/common';
|
|
7
|
+
import type { Observable } from 'rxjs';
|
|
8
|
+
|
|
9
|
+
import { LanguageCode } from '../constants';
|
|
10
|
+
import { ContextProvider } from '../providers';
|
|
11
|
+
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class LanguageInterceptor implements NestInterceptor {
|
|
14
|
+
intercept(
|
|
15
|
+
context: ExecutionContext,
|
|
16
|
+
next: CallHandler,
|
|
17
|
+
): Observable<undefined> {
|
|
18
|
+
const request = context.switchToHttp().getRequest();
|
|
19
|
+
const lang = request.headers['accept-language'] ?? LanguageCode.en_US;
|
|
20
|
+
ContextProvider.setLanguage(lang);
|
|
21
|
+
|
|
22
|
+
return next.handle();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function UseLanguageInterceptor() {
|
|
27
|
+
return UseInterceptors(LanguageInterceptor);
|
|
28
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallHandler,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
NestInterceptor,
|
|
5
|
+
} from '@nestjs/common';
|
|
6
|
+
import { HttpStatus, Injectable } from '@nestjs/common';
|
|
7
|
+
import type { Observable } from 'rxjs';
|
|
8
|
+
import { map } from 'rxjs/operators';
|
|
9
|
+
import { Request } from 'express';
|
|
10
|
+
// import { TranslationService } from '../shared/services/translation.service';
|
|
11
|
+
|
|
12
|
+
// FIXME: add implementation
|
|
13
|
+
@Injectable()
|
|
14
|
+
export class TranslationInterceptor implements NestInterceptor {
|
|
15
|
+
// constructor(private readonly translationService: TranslationService) {}
|
|
16
|
+
|
|
17
|
+
public intercept(
|
|
18
|
+
context: ExecutionContext,
|
|
19
|
+
next: CallHandler,
|
|
20
|
+
): Observable<any> {
|
|
21
|
+
const ctx = context.switchToHttp();
|
|
22
|
+
const req: Request = ctx.getRequest();
|
|
23
|
+
const res = ctx.getResponse();
|
|
24
|
+
return next.handle().pipe(
|
|
25
|
+
map((data) => {
|
|
26
|
+
// const newData = this.translationService.translateNecessaryKeys(data);
|
|
27
|
+
|
|
28
|
+
// status 201 => 200
|
|
29
|
+
if (res.statusCode === HttpStatus.CREATED) {
|
|
30
|
+
res.status(HttpStatus.OK);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
code: 200,
|
|
35
|
+
data,
|
|
36
|
+
msg: 'success',
|
|
37
|
+
requestId: req.id,
|
|
38
|
+
timestamp: new Date().toISOString(),
|
|
39
|
+
};
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type TExceptionOption =
|
|
2
|
+
| string
|
|
3
|
+
| {
|
|
4
|
+
code: number;
|
|
5
|
+
msg: string;
|
|
6
|
+
error?: any;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
// 分页数据
|
|
10
|
+
export interface IHttpResultPaginate<T> {
|
|
11
|
+
list: T;
|
|
12
|
+
total: number;
|
|
13
|
+
page: number;
|
|
14
|
+
pageCount: number;
|
|
15
|
+
count: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// HTTP 状态返回
|
|
19
|
+
export interface IHttpResponseBase {
|
|
20
|
+
code: number;
|
|
21
|
+
msg: string;
|
|
22
|
+
timestamp: string;
|
|
23
|
+
requestId: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// HTTP error
|
|
27
|
+
export type THttpErrorResponse = IHttpResponseBase & {
|
|
28
|
+
error: any;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// HTTP success
|
|
32
|
+
export type THttpSuccessResponse<T> = IHttpResponseBase & {
|
|
33
|
+
data: T | IHttpResultPaginate<T>;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// HTTP Response
|
|
37
|
+
export type THttpResponse<T> = THttpErrorResponse | THttpSuccessResponse<T>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { getOmniAuthData } from '../decorators/user.decorator';
|
|
2
|
+
import { ContextProvider } from '../providers';
|
|
3
|
+
|
|
4
|
+
export const OmniAuthMiddleware = () => {
|
|
5
|
+
return (req, res, next) => {
|
|
6
|
+
req.user = getOmniAuthData(req);
|
|
7
|
+
ContextProvider.setAuthUser(req.user);
|
|
8
|
+
next();
|
|
9
|
+
};
|
|
10
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
+
import { ContextProvider } from '../providers';
|
|
4
|
+
|
|
5
|
+
export interface RequestIdConfig {
|
|
6
|
+
header: string;
|
|
7
|
+
idGenerator: () => string;
|
|
8
|
+
setHeader: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const RequestIdMiddleware = (
|
|
12
|
+
config: RequestIdConfig = {
|
|
13
|
+
header: 'x-request-id',
|
|
14
|
+
idGenerator: uuidv4,
|
|
15
|
+
setHeader: true,
|
|
16
|
+
},
|
|
17
|
+
) => {
|
|
18
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
19
|
+
const requestId = req.header(config.header) || config.idGenerator();
|
|
20
|
+
req.headers[config.header] = requestId;
|
|
21
|
+
req.id = requestId;
|
|
22
|
+
ContextProvider.setRequestId(requestId);
|
|
23
|
+
if (config.setHeader) {
|
|
24
|
+
res.set(config.header, requestId);
|
|
25
|
+
}
|
|
26
|
+
next();
|
|
27
|
+
};
|
|
28
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Injectable, NestMiddleware } from '@nestjs/common';
|
|
2
|
+
import * as bodyParser from 'body-parser';
|
|
3
|
+
|
|
4
|
+
const bodyParserMiddleware = bodyParser.text({
|
|
5
|
+
limit: '1024kb',
|
|
6
|
+
type: 'application/xml',
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class XMLMiddleware implements NestMiddleware {
|
|
11
|
+
use(req: any, res: any, next: () => void) {
|
|
12
|
+
bodyParserMiddleware(req, res, next);
|
|
13
|
+
}
|
|
14
|
+
}
|