Completed
Push — master ( 5d49e4...5f62fb )
by Mark
35s queued 12s
created

ConnectionSettings.getSettings   F

Complexity

Conditions 19

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 11
dl 0
loc 13
rs 0.5999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like ConnectionSettings.getSettings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import {AdapterSettingsOptions, ModelStaticInterface} from "../../JeloquentInterfaces";
2
3
/**
4
 *
5
 */
6
export default class ConnectionSettings {
7
8
    private _baseUrl: string;
9
10
    private cache: string;
11
12
    private contentType: string;
13
14
    private headers: object;
15
16
    private mode: string;
17
18
    private modelPathMappings: Map<string, string>;
19
20
    /**
21
     *
22
     */
23
    constructor(options: AdapterSettingsOptions) {
24
        this.contentType = options?.contentType ?? 'application/json';
25
        this.mode = options?.mode ?? 'cors';
26
        this.cache = options?.cache ?? 'no-cache';
27
        this.headers = options?.headers ?? {};
28
        this._baseUrl = options?.baseUrl ?? 'http://localhost';
29
        this.modelPathMappings = options?.modelPathMappings ?? new Map();
30
    }
31
32
    get baseUrl(): string {
33
        return this._baseUrl;
34
    }
35
36
    getSettings(): object {
37
        return {
38
            "mode": this.mode, // no-cors, *cors, same-origin
39
            "cache": this.cache, // *default, no-cache, reload, force-cache, only-if-cached
40
            //credentials: 'same-origin', // include, *same-origin, omit
41
            "headers": {
42
                'Accept': this.contentType,
43
                'Content-Type': this.contentType,
44
                ...this.headers
45
            },
46
            "redirect": 'follow', // manual, *follow, error
47
            "referrerPolicy": 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
48
        };
49
    }
50
51
    modelEndPoint(model:ModelStaticInterface): string {
52
        return `${this.baseUrl}/${this.modelPathMappings.get(model.className) ?? model.kebabCaseClassName}`
53
    }
54
}
55