All files / lib/decorators i18n.decorator.ts

75% Statements 9/12
66.67% Branches 8/12
75% Functions 3/4
75% Lines 9/12

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 425x   5x   5x 43x   42x       1x                       42x                 1x 1x                
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { I18nService } from '..';
import { I18nContext } from '../i18n.context';
 
export const I18n = createParamDecorator((data, ctx: ExecutionContext) => {
  switch (ctx.getType() as string) {
    case 'http':
      return new I18nContext(
        ...resolveI18nServiceFromRequest(ctx.switchToHttp().getRequest()),
      );
    case 'graphql':
      return new I18nContext(
        ...resolveI18nServiceFromGraphQLContext(ctx.getArgs()),
      );
    case 'rpc':
      return new I18nContext(
        ...resolveI18nServiceFromRpcContext(ctx.switchToRpc().getContext()),
      );
    default:
      throw Error(`context type: ${ctx.getType()} not supported`);
  }
});
function resolveI18nServiceFromRequest(req: any): [string, I18nService] {
  return [
    req.raw && req.raw.i18nLang ? req.raw.i18nLang : req.i18nLang,
    req.raw && req.raw.i18nService ? req.raw.i18nService : req.i18nService,
  ];
}
 
function resolveI18nServiceFromGraphQLContext(
  graphqlContext,
): [string, I18nService] {
  const [root, args, ctx, info] = graphqlContext;
  return [ctx.i18nLang, ctx.i18nService];
}
 
function resolveI18nServiceFromRpcContext(
  rpcContext: any,
): [string, I18nService] {
  return [rpcContext.i18nLang, rpcContext.i18nService];
}