All files / lib/decorators i18n-lang.decorator.ts

88.89% Statements 8/9
42.86% Branches 3/7
100% Functions 3/3
88.89% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 245x   5x 16x   12x       4x             12x       4x 4x    
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
 
export const I18nLang = createParamDecorator((data, ctx: ExecutionContext) => {
  switch (ctx.getType() as string) {
    case 'http':
      return resolveI18nLanguageFromRestRequest(
        ctx.switchToHttp().getRequest(),
      );
    case 'graphql':
      return resolveI18nLanguageFromGraphQLContext(ctx.getArgs());
    default:
      throw Error(`context type: ${ctx.getType()} not supported`);
  }
});
 
function resolveI18nLanguageFromRestRequest(req) {
  return req.i18nLang || (req.raw ? req.raw.i18nLang : undefined);
}
 
function resolveI18nLanguageFromGraphQLContext(req) {
  const [root, args, ctx, info] = req;
  return ctx.req.i18nLang;
}