src/interceptors/format-user.interceptor.ts   A
last analyzed

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 36
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 18
mnd 1
bc 1
fnc 1
dl 0
loc 36
rs 10
bpm 1
cpm 2
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A FormatUserInterceptor.responseHandler 0 25 2
1
import ResponseInterceptor from './response-interceptor';
2
import { AxiosResponse } from 'axios';
3
import { ApiResponseBodyOk, BEditaClientResponse } from '../bedita-api-client';
4
5
/**
6
 * Formatter interceptor for user data.
7
 */
8
export default class FormatUserInterceptor extends ResponseInterceptor {
9
10
    /**
11
     * Format user data as
12
     * ```
13
     * {
14
     *     "data": {}, // user attributes
15
     *     "roles": [], // array of user's roles
16
     * }
17
     * ```
18
     *
19
     * @param response The response.
20
     */
21
    public responseHandler(response: AxiosResponse): Promise<BEditaClientResponse<any>> {
22
        const responseData: ApiResponseBodyOk = response.data;
23
        const { data, included = false } = responseData;
24
        let roles = [];
25
        if (Array.isArray(included)) {
26
            roles = included.filter(item => item.type === 'roles')
27
                .map(item => item.attributes.name);
28
        }
29
30
        const beditaResponse = response as BEditaClientResponse;
31
        beditaResponse.formattedData = {data, roles};
32
33
        return Promise.resolve(beditaResponse);
34
    }
35
}
36