src/Store/Connection/ConnectionSettings.ts   A
last analyzed

Complexity

Total Complexity 22
Complexity/F 11

Size

Lines of Code 87
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 22
mnd 20
bc 20
fnc 2
bpm 10
cpm 11
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A ConnectionSettings.modelEndPoint 0 3 3
F ConnectionSettings.getSettings 0 12 19
1
import {AdapterSettingsOptions, ModelStaticInterface} from "../../JeloquentInterfaces";
2
3
enum MODE {
4
    NO_CORS = 'no-cors',
5
    CORS = 'cors',
6
    SAME_ORIGIN = 'same-origin',
7
}
8
9
enum CACHE {
10
    DEFAULT = 'default',
11
    NO_CACHE = 'no-cache',
12
    RELOAD = 'reload',
13
    FORCE_CACHE = 'force-cache',
14
    ONLY_IF_CACHED = 'only-if-cached',
15
}
16
17
enum REDIRECT {
18
    MANUAL = 'manual',
19
    FOLLOW = 'follow',
20
    ERROR = 'error',
21
}
22
23
enum REFERRER_POLICY {
24
    NO_REFERRER = 'no-referrer', 
25
    NO_REFERRER_WHEN_DOWNGRADE = '*no-referrer-when-downgrade', 
26
    ORIGIN = 'origin', 
27
    ORIGIN_WHEN_CROSS_ORIGIN = 'origin-when-cross-origin',
28
    SAME_ORIGIN = 'same-origin',
29
    STRICT_ORIGIN = 'strict-origin',
30
    STRICT_ORIGIN_WHEN_CROSS_ORIGIN = 'strict-origin-when-cross-origin',
31
    UNSAFE_URL = 'unsafe-url',
32
}
33
34
export default class ConnectionSettings {
35
36
    static OPTION_CACHES = CACHE;
37
38
    static OPTION_MODE = MODE;
39
40
    static OPTION_REDIRECT = REDIRECT;
41
42
    static OPTION_REFERRER_POLICY = REFERRER_POLICY;
43
44
    private _baseUrl: string;
45
46
    private cache: string;
47
48
    private contentType: string;
49
50
    private headers: object;
51
52
    private mode: string;
53
54
    private modelPathMappings: Map<string, string>;
55
56
    constructor(options: AdapterSettingsOptions) {
57
        this.contentType = options?.contentType ?? 'application/json';
58
        this.mode = options?.mode ?? ConnectionSettings.OPTION_MODE.CORS;
59
        this.cache = options?.cache ?? ConnectionSettings.OPTION_CACHES.NO_CACHE;
60
        this.headers = options?.headers ?? {};
61
        this._baseUrl = options?.baseUrl ?? 'http://localhost';
62
        this.modelPathMappings = options?.modelPathMappings ?? new Map();
63
    }
64
65
    get baseUrl(): string {
66
        return this._baseUrl;
67
    }
68
69
    getSettings(): object {
70
        return {
71
            "mode": this.mode,
72
            "cache": this.cache,
73
            "headers": {
74
                'Accept': this.contentType,
75
                'Content-Type': this.contentType,
76
                ...this.headers
77
            },
78
            "redirect": ConnectionSettings.OPTION_REDIRECT.FOLLOW,
79
            "referrerPolicy": ConnectionSettings.OPTION_REFERRER_POLICY.NO_REFERRER,
80
        };
81
    }
82
83
    modelEndPoint(model:ModelStaticInterface): string {
84
        return `${this.baseUrl}/${this.modelPathMappings.get(model.className) ?? model.kebabCaseClassName}`
85
    }
86
}
87