-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequester.ts
66 lines (61 loc) · 1.89 KB
/
requester.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
export abstract class cRequester {
public token: string;
public url: string;
public messenger: string;
public debugging: boolean = false;
constructor(obj: {token: string}) {
this.token = obj.token;
}
responseText(obj: {
responseType: string,
responseText: any
}){
try{
let result = {
ok: true,
messenger: this.messenger,
mask: obj.responseType,
date: Math.round(new Date().getTime()/1000),
statusText: this.responseTransformer({
expr: obj.responseType,
responce: obj.responseText
})
};
if(this.debugging) console.log(result);
return result;
}catch(error){
return this.errorHandler(error);
}
}
errorHandler(error: any){
let result = {
ok: false,
messenger: null,
mask: null,
date: Math.round(new Date().getTime()/1000),
statusText: error
}
console.log(result);
return result;
}
responseTransformer(obj: {expr: string, responce: any}){
return obj.responce;
}
paramToString(obj: {
param: any
}){
let first = true;
let string = '';
for (let key in obj.param) {
string += (first) ? '?' : '&';
string += (
typeof obj.param[key] === 'object' &&
!Array.isArray(obj.param[key]) &&
obj.param[key] !== null
) ? key + '=' + encodeURIComponent(JSON.stringify(obj.param[key]))
: key + '=' + encodeURIComponent(obj.param[key]);
first = false;
}
return string;
}
}