UNPKG

@softkit/i18n

Version:

This library is a simple wrapper based on [nestjs-i18n](https://nestjs-i18n.com/)

359 lines 15.2 kB
"use strict"; var HelloController_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.HelloController = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const microservices_1 = require("@nestjs/microservices"); const __1 = require("../../../../"); const create_user_dto_1 = require("../dto/create-user.dto"); const example_functions_1 = require("../examples/example.functions"); const test_filter_1 = require("../filter/test.filter"); const test_guard_1 = require("../guards/test.guard"); const test_interceptor_1 = require("../interceptors/test.interceptor"); let HelloController = HelloController_1 = class HelloController { constructor(i18n) { this.i18n = i18n; } hello(lang) { return this.i18n.translate(HelloController_1.TEST_HELLO_KEY, { lang }); } helloTyped(lang) { return this.i18n.translate(HelloController_1.TEST_HELLO_KEY, { lang }); } index() { return { count: 1 }; } index2() { } index3() { } helloShort(lang) { return this.i18n.t(HelloController_1.TEST_HELLO_KEY, { lang }); } helloShortTyped(lang) { return this.i18n.t(HelloController_1.TEST_HELLO_KEY, { lang }); } helloContext(i18n) { return i18n.translate(HelloController_1.TEST_HELLO_KEY); } helloContextTyped(i18n) { return i18n.translate(HelloController_1.TEST_HELLO_KEY); } helloShortContext(i18n) { return i18n.t(HelloController_1.TEST_HELLO_KEY); } helloShortContextTyped(i18n) { return i18n.t(HelloController_1.TEST_HELLO_KEY); } helloRequestScope() { const current = __1.I18nContext.current(); return current?.translate(HelloController_1.TEST_HELLO_KEY); } helloRequestScopeAdditionalInterceptor() { return __1.I18nContext.current()?.translate(HelloController_1.TEST_HELLO_KEY); } helloRequestScopeTyped() { return __1.I18nContext.current()?.translate(HelloController_1.TEST_HELLO_KEY); } helloShortRequestScope() { return __1.I18nContext.current()?.translate(HelloController_1.TEST_HELLO_KEY); } helloShortRequestScopeTyped() { return __1.I18nContext.current()?.translate(HelloController_1.TEST_HELLO_KEY); } object() { return this.i18n.translate('test.set-up-password', { args: { username: 'KirillCherkalov' }, }); } array() { return this.i18n.translate('test.ARRAY'); } plurarization(count) { return __1.I18nContext.current()?.translate('test.day_interval', { args: { count: Number.parseInt(count) }, }); } nested(username) { return __1.I18nContext.current()?.translate('test.nested', { args: { username }, }); } nestedNoArgs() { return __1.I18nContext.current()?.translate('test.nested-no-args'); } deeplyNested(count) { return __1.I18nContext.current()?.translate('test.nest1.nest2.nest3', { args: { count }, }); } guard() { return 'NO'; } exception() { throw new test_filter_1.TestException(); } validation(_) { return HelloController_1.THIS_ACTION_ADDS_NEW_USER; } validationWithoutDetails(_) { return HelloController_1.THIS_ACTION_ADDS_NEW_USER; } validationWithCustomHttpCode(_) { return HelloController_1.THIS_ACTION_ADDS_NEW_USER; } validationCustomFormatter(_) { return HelloController_1.THIS_ACTION_ADDS_NEW_USER; } validationResponseBodyFormatter(_) { return HelloController_1.THIS_ACTION_ADDS_NEW_USER; } customValidation(i18n) { const createUserDto = new create_user_dto_1.CreateUserDto(); return i18n.validate(createUserDto); } findOne(data, i18n) { const items = [ { id: 1, name: i18n.t('test.set-up-password.heading', { args: { username: 'John' }, }), }, { id: 2, name: 'Doe' }, ]; const result = items.find(({ id }) => id === data.id); if (!result) { throw new Error('Hero did not found'); } return result; } }; exports.HelloController = HelloController; HelloController.TEST_HELLO_KEY = 'test.HELLO'; HelloController.THIS_ACTION_ADDS_NEW_USER = 'This action adds a new user'; tslib_1.__decorate([ (0, common_1.Get)(), tslib_1.__param(0, (0, __1.I18nLang)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "hello", null); tslib_1.__decorate([ (0, common_1.Get)('/typed'), tslib_1.__param(0, (0, __1.I18nLang)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", String) ], HelloController.prototype, "helloTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/index'), (0, common_1.Render)('index'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "index", null); tslib_1.__decorate([ (0, common_1.Get)('/index2'), (0, common_1.Render)('index2'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "index2", null); tslib_1.__decorate([ (0, common_1.Get)('/index3'), (0, common_1.Render)('index3'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "index3", null); tslib_1.__decorate([ (0, common_1.Get)('/short'), tslib_1.__param(0, (0, __1.I18nLang)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloShort", null); tslib_1.__decorate([ (0, common_1.Get)('/short/typed'), tslib_1.__param(0, (0, __1.I18nLang)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", String) ], HelloController.prototype, "helloShortTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/context'), tslib_1.__param(0, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [__1.I18nContext]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloContext", null); tslib_1.__decorate([ (0, common_1.Get)('/context/typed'), tslib_1.__param(0, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [__1.I18nContext]), tslib_1.__metadata("design:returntype", String) ], HelloController.prototype, "helloContextTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/short/context'), tslib_1.__param(0, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [__1.I18nContext]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloShortContext", null); tslib_1.__decorate([ (0, common_1.Get)('/short/context/typed'), tslib_1.__param(0, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [__1.I18nContext]), tslib_1.__metadata("design:returntype", String) ], HelloController.prototype, "helloShortContextTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/request-scope'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloRequestScope", null); tslib_1.__decorate([ (0, common_1.Get)('/request-scope/additional-interceptor'), (0, common_1.UseInterceptors)(test_interceptor_1.TestInterceptor), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloRequestScopeAdditionalInterceptor", null); tslib_1.__decorate([ (0, common_1.Get)('/request-scope/typed'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloRequestScopeTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/short/request-scope'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloShortRequestScope", null); tslib_1.__decorate([ (0, common_1.Get)('/short/request-scope/typed'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "helloShortRequestScopeTyped", null); tslib_1.__decorate([ (0, common_1.Get)('/object'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "object", null); tslib_1.__decorate([ (0, common_1.Get)('/array'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "array", null); tslib_1.__decorate([ (0, common_1.Get)('/plurarization'), tslib_1.__param(0, (0, common_1.Query)('count')), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "plurarization", null); tslib_1.__decorate([ (0, common_1.Get)('/nested'), tslib_1.__param(0, (0, common_1.Query)('username')), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "nested", null); tslib_1.__decorate([ (0, common_1.Get)('/nested-no-args'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "nestedNoArgs", null); tslib_1.__decorate([ (0, common_1.Get)('/deeply-nested'), tslib_1.__param(0, (0, common_1.Query)('count')), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [Number]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "deeplyNested", null); tslib_1.__decorate([ (0, common_1.Get)('/guard'), (0, common_1.UseGuards)(test_guard_1.TestGuard), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "guard", null); tslib_1.__decorate([ (0, common_1.Get)('/exception'), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "exception", null); tslib_1.__decorate([ (0, common_1.Post)('/validation'), (0, common_1.UseFilters)(new __1.I18nValidationExceptionFilter()), tslib_1.__param(0, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [create_user_dto_1.CreateUserDto]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "validation", null); tslib_1.__decorate([ (0, common_1.Post)('/validation-without-details'), (0, common_1.UseFilters)(new __1.I18nValidationExceptionFilter({ detailedErrors: false })), tslib_1.__param(0, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [create_user_dto_1.CreateUserDto]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "validationWithoutDetails", null); tslib_1.__decorate([ (0, common_1.Post)('/validation-with-custom-http-code'), (0, common_1.UseFilters)(new __1.I18nValidationExceptionFilter({ errorHttpStatusCode: 422 })), tslib_1.__param(0, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [create_user_dto_1.CreateUserDto]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "validationWithCustomHttpCode", null); tslib_1.__decorate([ (0, common_1.Post)('/validation-custom-formatter'), (0, common_1.UseFilters)(new __1.I18nValidationExceptionFilter({ errorFormatter: example_functions_1.exampleErrorFormatter, })), tslib_1.__param(0, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [create_user_dto_1.CreateUserDto]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "validationCustomFormatter", null); tslib_1.__decorate([ (0, common_1.Post)('/validation-custom-response-body-formatter'), (0, common_1.UseFilters)(new __1.I18nValidationExceptionFilter({ responseBodyFormatter: example_functions_1.exampleResponseBodyFormatter, errorFormatter: example_functions_1.exampleErrorFormatter, })), tslib_1.__param(0, (0, common_1.Body)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [create_user_dto_1.CreateUserDto]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "validationResponseBodyFormatter", null); tslib_1.__decorate([ (0, common_1.Post)('/custom-validation'), tslib_1.__param(0, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [__1.I18nContext]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "customValidation", null); tslib_1.__decorate([ (0, microservices_1.GrpcMethod)('HeroesService', 'FindOne'), tslib_1.__param(0, (0, microservices_1.Payload)()), tslib_1.__param(1, (0, __1.I18n)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [Object, __1.I18nContext]), tslib_1.__metadata("design:returntype", Object) ], HelloController.prototype, "findOne", null); exports.HelloController = HelloController = HelloController_1 = tslib_1.__decorate([ (0, common_1.Controller)('hello'), (0, common_1.UseFilters)(new test_filter_1.TestExceptionFilter()), tslib_1.__metadata("design:paramtypes", [__1.I18nService]) ], HelloController); //# sourceMappingURL=hello.controller.js.map