-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcatr.ts
More file actions
106 lines (94 loc) · 2.38 KB
/
catr.ts
File metadata and controls
106 lines (94 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
type CatrPart =
| 'type'
| 'expadd'
| 'deadline'
| 'cookie-name'
| 'header-name'
| 'cookie-params'
| 'header-params'
| 'code';
type CatrRenewalType = 'automatic' | 'cookie' | 'header' | 'redirect';
const catrPartToLabel: { [key: string]: number } = {
type: 0,
expadd: 1,
deadline: 2,
'cookie-name': 3,
'header-name': 4,
'cookie-params': 5,
'header-params': 6,
code: 7
};
const labelsToCatrPart: { [key: number]: CatrPart } = {
0: 'type',
1: 'expadd',
2: 'deadline',
3: 'cookie-name',
4: 'header-name',
5: 'cookie-params',
6: 'header-params',
7: 'code'
};
const catrRenewalTypeToLabel: { [key: string]: number } = {
automatic: 0,
cookie: 1,
header: 2,
redirect: 3
};
const labelsToCatrRenewalType: { [key: number]: CatrRenewalType } = {
0: 'automatic',
1: 'cookie',
2: 'header',
3: 'redirect'
};
type CatrPartValue = number | string | string[];
export type CommonAccessTokenRenewalMap = Map<number, CatrPartValue>;
export class CommonAccessTokenRenewal {
private catrMap: CommonAccessTokenRenewalMap = new Map();
public static fromDict(dict: { [key: string]: any }) {
const catr = new CommonAccessTokenRenewal();
for (const catrPart in dict) {
if (catrPart === 'type') {
catr.catrMap.set(
catrPartToLabel[catrPart],
catrRenewalTypeToLabel[dict[catrPart]]
);
} else {
catr.catrMap.set(catrPartToLabel[catrPart], dict[catrPart]);
}
}
return catr;
}
public static fromMap(map: CommonAccessTokenRenewalMap) {
const catr = new CommonAccessTokenRenewal();
catr.catrMap = map;
return catr;
}
toDict() {
const result: { [key: string]: any } = {};
for (const [key, value] of this.catrMap.entries()) {
if (labelsToCatrPart[key] === 'type') {
result[labelsToCatrPart[key]] =
labelsToCatrRenewalType[value as number];
} else {
result[labelsToCatrPart[key]] = value;
}
}
return result;
}
isValid() {
if (this.catrMap.get(catrPartToLabel['type']) === undefined) {
return false;
}
if (this.catrMap.get(catrPartToLabel['expadd']) === undefined) {
return false;
}
return true;
}
get renewalType(): CatrRenewalType {
const type = this.catrMap.get(catrPartToLabel['type']);
return labelsToCatrRenewalType[type as number];
}
get payload() {
return this.catrMap;
}
}