Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 36 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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 |