Passed
Push — master ( 198edd...da006d )
by Sergey
04:08 queued 01:46
created

request.async.filter.ts ➔ getRawHeader   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
1
import { IFilterPermission } from '../../../src';
2
import { Injectable } from '@nestjs/common';
3
4
function getRawHeader(name: string, rawHeaders: string[]): string | boolean {
5
  const key = rawHeaders.indexOf(name);
6
  if (key < 0) {
7
    return false;
8
  }
9
  const value = key + 1;
10
  return rawHeaders[value];
11
}
12
13
@Injectable()
14
export class RequestAsyncFilter implements IFilterPermission {
15
16
  async canAsync(params?: any[]): Promise<boolean> {
17
    const name = 'test-header';
18
    // Fix: missing `headers`, using `rawHeaders` for fixing tests on Node.js 15
19
    const header = params[0]?.headers?.[name] ?? getRawHeader(name, params[0].rawHeaders);
20
    return Promise.resolve(header === 'test');
21
  }
22
}
23