All files / lib/interceptors i18n-language.interceptor.ts

74.42% Statements 32/43
61.11% Branches 11/18
75% Functions 3/4
73.17% Lines 30/41

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 961x               1x   1x 1x 1x       1x     1x     1x   1x 1x 1x             18x   18x   18x 18x   18x 45x   45x   45x 6x     45x 15x       18x     18x                                                   45x 24x 18x 18x   6x     21x        
import {
  Inject,
  Injectable,
  Type,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { I18N_OPTIONS, I18N_RESOLVERS } from '../i18n.constants';
import { I18nOptions, I18nResolver, ResolverWithOptions } from '../index';
import { I18nService } from '../services/i18n.service';
import { ModuleRef, Reflector } from '@nestjs/core';
import { shouldResolve } from '../utils/util';
import { I18nOptionResolver } from '../interfaces/i18n-options.interface';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { getContextObject } from '../utils/context';
 
@Injectable()
export class I18nLanguageInterceptor implements NestInterceptor {
  constructor(
    @Inject(I18N_OPTIONS)
    private readonly i18nOptions: I18nOptions,
    @Inject(I18N_RESOLVERS)
    private readonly i18nResolvers: I18nOptionResolver[],
    private readonly i18nService: I18nService,
    private readonly moduleRef: ModuleRef,
  ) {}
 
  async intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Promise<Observable<any>> {
    let language = null;
 
    const ctx = getContextObject(context);
 
    Eif (ctx) {
      ctx.i18nService = this.i18nService;
 
      for (const r of this.i18nResolvers) {
        const resolver = await this.getResolver(r);
 
        language = resolver.resolve(context);
 
        if (language instanceof Promise) {
          language = await (language as Promise<string>);
        }
 
        if (language !== undefined) {
          break;
        }
      }
 
      ctx.i18nLang = language;
    }
 
    return next.handle();
  }
 
  async use(req: any, res: any, next: () => void) {
    let language = null;
 
    req.i18nService = this.i18nService;
    for (const r of this.i18nResolvers) {
      const resolver = await this.getResolver(r);
 
      language = resolver.resolve(req);
 
      if (language instanceof Promise) {
        language = await (language as Promise<string>);
      }
 
      if (language !== undefined) {
        break;
      }
    }
    req.i18nLang = language || this.i18nOptions.fallbackLanguage;
 
    next();
  }
 
  private async getResolver(r: I18nOptionResolver): Promise<I18nResolver> {
    if (shouldResolve(r)) {
      if (r.hasOwnProperty('use') && r.hasOwnProperty('options')) {
        const resolver = r as ResolverWithOptions;
        return await this.moduleRef.resolve(resolver.use);
      } else {
        return await this.moduleRef.resolve(r as Type<I18nResolver>);
      }
    } else {
      return r as I18nResolver;
    }
  }
}