All files / lib/resolvers cookie.resolver.ts

100% Statements 14/14
88.89% Branches 8/9
100% Functions 2/2
100% Lines 12/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 305x 5x   5x           5x     3x       20x 8x   20x 8x 8x 8x       12x      
import * as cookie from 'cookie';
import { Injectable } from '@nestjs/common';
import { I18nResolver } from '..';
import { I18nResolverOptions } from '../decorators/i18n-resolver-options.decorator';
 
/**
 * Simple resolver to fetch language/locale from cookie
 */
@Injectable()
export class CookieResolver implements I18nResolver {
  constructor(
    @I18nResolverOptions()
    private readonly cookieNames: string[] = ['lang'],
  ) {}
 
  resolve(req) {
    if (!req.cookies && req.headers.cookie) {
      req.cookies = cookie.parse(req.headers.cookie);
    }
    if (req.cookies) {
      for (const i in this.cookieNames) {
        Eif (req.cookies[this.cookieNames[i]]) {
          return req.cookies[this.cookieNames[i]];
        }
      }
    }
    return undefined;
  }
}