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

RequestAsyncFilter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 9
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 9
dl 0
loc 9
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A canAsync 0 6 5
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