Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
SchroederSteffen committed Oct 17, 2024
1 parent 8ffad41 commit ecce5cf
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/i18n.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export class I18nContext<K = Record<string, unknown>>
return this;
}

constructor(readonly lang: string, readonly service: I18nService<K>, readonly i18nOptions: I18nOptions,) {}
constructor(
readonly lang: string,
readonly service: I18nService<K>,
readonly i18nOptions: I18nOptions,
) {}

public translate<P extends Path<K> = any, R = PathValue<K, P>>(
key: P,
Expand Down Expand Up @@ -64,7 +68,7 @@ export class I18nContext<K = Record<string, unknown>>
const i18n = this.storage.getStore() as I18nContext<K> | undefined;

if (!i18n && !!context) {
return getContextObject(i18n.i18nOptions,context)?.i18nContext;
return getContextObject(i18n.i18nOptions, context)?.i18nContext;
}

return i18n;
Expand Down
8 changes: 6 additions & 2 deletions src/interceptors/i18n-language.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class I18nLanguageInterceptor implements NestInterceptor {
const i18nContext = I18nContext.current();
let language = null;

const ctx = getContextObject(this.i18nOptions,context);
const ctx = getContextObject(this.i18nOptions, context);

// Skip interceptor if language is already resolved (in case of http middleware) or when ctx is undefined (unsupported context)
if (ctx === undefined || !!ctx.i18nLang) {
Expand Down Expand Up @@ -68,7 +68,11 @@ export class I18nLanguageInterceptor implements NestInterceptor {
}

if (!i18nContext) {
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService, this.i18nOptions);
ctx.i18nContext = new I18nContext(
ctx.i18nLang,
this.i18nService,
this.i18nOptions,
);

if (!this.i18nOptions.skipAsyncHook) {
return new Observable((observer) => {
Expand Down
6 changes: 5 additions & 1 deletion src/middlewares/i18n.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class I18nMiddleware implements NestMiddleware {
req.app.locals.i18nLang = req.i18nLang;
}

req.i18nContext = new I18nContext(req.i18nLang, this.i18nService, this.i18nOptions);
req.i18nContext = new I18nContext(
req.i18nLang,
this.i18nService,
this.i18nOptions,
);

if (this.i18nOptions.skipAsyncHook) {
next();
Expand Down
2 changes: 0 additions & 2 deletions src/services/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export class I18nService<K = Record<string, unknown>>
}" in "${lang}" does not exist.`;
if (lang !== this.i18nOptions.fallbackLanguage || !!defaultValue) {
if (this.i18nOptions.logging && this.i18nOptions.throwOnMissingKey) {

this.logger.error(translationKeyMissing);
throw new I18nError(translationKeyMissing);
}
Expand All @@ -127,7 +126,6 @@ export class I18nService<K = Record<string, unknown>>
});
}
}

}

return (translation ?? key) as unknown as IfAnyOrNever<R, string, R>;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getContextObject(
case 'rmq':
return context.getArgs()[1];
default:
if(i18nOptions.logging){
if (i18nOptions.logging) {
logger.warn(`context type: ${contextType} not supported`);
}
}
Expand Down
12 changes: 7 additions & 5 deletions tests/app/examples/example.functions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ArgumentsHost, ValidationError } from '@nestjs/common';
import { mapChildrenToValidationErrors } from '../../../src/utils/format';
import {I18nValidationException} from "../../../src";

import { I18nValidationException } from '../../../src';

export const exampleErrorFormatter = (errors: ValidationError[]): object => {
const errorMessages = {};
Expand All @@ -18,12 +17,15 @@ export const exampleErrorFormatter = (errors: ValidationError[]): object => {
return errorMessages;
};


export const exampleResponseBodyFormatter = (host: ArgumentsHost, exc: I18nValidationException, formattedErrors: object) => {
export const exampleResponseBodyFormatter = (
host: ArgumentsHost,
exc: I18nValidationException,
formattedErrors: object,
) => {
return {
type: 'static',
status: exc.getStatus(),
message: exc.getResponse(),
data: formattedErrors,
};
}
};
2 changes: 0 additions & 2 deletions tests/i18n-dto.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ describe('i18n module e2e dto', () => {
});
});


it(`should translate validation messages if a custom response body formatter specified`, async () => {
await request(app.getHttpServer())
.post('/hello/validation-custom-response-body-formatter')
Expand Down Expand Up @@ -345,7 +344,6 @@ describe('i18n module e2e dto', () => {
});
});


it(`should translate validation messages with detailed error`, async () => {
await request(app.getHttpServer())
.post('/hello/validation')
Expand Down
2 changes: 1 addition & 1 deletion tests/i18n-ejs.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Global, Module } from '@nestjs/common';
],
exports: ['OPTIONS'],
})
export class OptionsModule { }
export class OptionsModule {}

describe('i18n module e2e ejs', () => {
let app: NestExpressApplication;
Expand Down
4 changes: 2 additions & 2 deletions tests/i18n-express.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ describe('i18n module e2e express', () => {

it(`/GET hello/no-lang-for-service should return translation when providing x-custom-lang`, () => {
return request(app.getHttpServer())
.get('/hello/no-lang-for-service')
.get('/hello/no-lang-for-service')
.set('x-custom-lang', 'nl')
.expect(200)
.expect('Hallo');
Expand Down Expand Up @@ -671,7 +671,7 @@ describe('i18n module e2e express', () => {

it(`/GET hello/short/no-lang-for-service should return translation when providing x-custom-lang`, () => {
return request(app.getHttpServer())
.get('/hello/short/no-lang-for-service')
.get('/hello/short/no-lang-for-service')
.set('x-custom-lang', 'nl')
.expect(200)
.expect('Hallo');
Expand Down
6 changes: 3 additions & 3 deletions tests/i18n-fastify.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ describe('i18n module e2e fastify', () => {

it(`/GET hello/no-lang-for-service should return translation when providing x-custom-lang`, () => {
return request(app.getHttpServer())
.get('/hello/no-lang-for-service')
.get('/hello/no-lang-for-service')
.set('x-custom-lang', 'nl')
.expect(200)
.expect('Hallo');
Expand Down Expand Up @@ -675,7 +675,7 @@ describe('i18n module e2e fastify', () => {

it(`/GET hello/short/no-lang-for-service should return translation when providing x-custom-lang`, () => {
return request(app.getHttpServer())
.get('/hello/short/no-lang-for-service')
.get('/hello/short/no-lang-for-service')
.set('x-custom-lang', 'nl')
.expect(200)
.expect('Hallo');
Expand All @@ -696,7 +696,7 @@ describe('i18n module e2e fastify', () => {
.expect(200)
.expect('Hallo');
});

it('/POST cats with age 2 should error', async () => {
await request(app.getHttpServer())
.post('/cats')
Expand Down

0 comments on commit ecce5cf

Please sign in to comment.