-
Notifications
You must be signed in to change notification settings - Fork 4
/
responsetemplatemanager.ts
132 lines (122 loc) · 3.94 KB
/
responsetemplatemanager.ts
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { ResponseParser } from "./responseparser.js";
import { ResponseTemplate } from "./responsetemplate.js";
/**
* ResponseTemplateManager Singleton Class
*/
export class ResponseTemplateManager {
/**
* Get ResponseTemplateManager Instance
* @returns ResponseTemplateManager Instance
*/
public static getInstance(): ResponseTemplateManager {
if (!ResponseTemplateManager.instance) {
ResponseTemplateManager.instance = new ResponseTemplateManager();
}
return ResponseTemplateManager.instance;
}
/**
* ResponseTemplateManager Instance
*/
private static instance: ResponseTemplateManager;
/**
* template container
*/
private templates: any;
private constructor() {
this.templates = {
404: this.generateTemplate("421", "Page not found"),
500: this.generateTemplate("500", "Internal server error"),
empty: this.generateTemplate(
"423",
"Empty API response. Probably unreachable API end point {CONNECTION_URL}",
),
error: this.generateTemplate(
"421",
"Command failed due to server error. Client should try again",
),
expired: this.generateTemplate("530", "SESSION NOT FOUND"),
httperror: this.generateTemplate(
"421",
"Command failed due to HTTP communication error",
),
invalid: this.generateTemplate(
"423",
"Invalid API response. Contact Support",
),
unauthorized: this.generateTemplate("530", "Unauthorized"),
};
}
/**
* Generate API response template string for given code and description
* @param code API response code
* @param description API response description
* @returns generate response template string
*/
public generateTemplate(code: string, description: string): string {
return `[RESPONSE]\r\nCODE=${code}\r\nDESCRIPTION=${description}\r\nEOF\r\n`;
}
/**
* Add response template to template container
* @param id template id
* @param plain API plain response
* @returns ResponseTemplateManager instance for method chaining
*/
public addTemplate(id: string, plain: string): ResponseTemplateManager {
this.templates[id] = plain;
return ResponseTemplateManager.instance;
}
/**
* Get response template instance from template container
* @param id template id
* @returns template instance
*/
public getTemplate(id: string): ResponseTemplate {
if (this.hasTemplate(id)) {
return new ResponseTemplate(this.templates[id]);
}
return new ResponseTemplate(
this.generateTemplate("500", "Response Template not found"),
);
}
/**
* Return all available response templates
* @returns all available response template instances
*/
public getTemplates(): any {
const tpls: any = {};
Object.keys(this.templates).forEach((key) => {
tpls[key] = new ResponseTemplate(this.templates[key]);
});
return tpls;
}
/**
* Check if given template exists in template container
* @param id template id
* @returns boolean result
*/
public hasTemplate(id: string): boolean {
return Object.prototype.hasOwnProperty.call(this.templates, id);
}
/**
* Check if given API response hash matches a given template by code and description
* @param tpl2 api response hash
* @param id template id
* @returns boolean result
*/
public isTemplateMatchHash(tpl2: any, id: string): boolean {
const h = this.getTemplate(id).getHash();
return h.CODE === tpl2.CODE && h.DESCRIPTION === tpl2.DESCRIPTION;
}
/**
* Check if given API plain response matches a given template by code and description
* @param plain API plain response
* @param id template id
* @returns boolean result
*/
public isTemplateMatchPlain(plain: string, id: string): boolean {
const h = this.getTemplate(id).getHash();
const tpl2 = ResponseParser.parse(plain);
return h.CODE === tpl2.CODE && h.DESCRIPTION === tpl2.DESCRIPTION;
}
}
ResponseTemplateManager.getInstance();