-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp-client.ts
40 lines (37 loc) · 1.19 KB
/
http-client.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
export class HttpClient {
public get<T>(url: string, args?: { [key: string]: string }): Promise<T> {
return this.getRequest(url, args).then(p => {
return Object.assign({} as T, JSON.parse(HttpClient.toLowerKey(p)));
});
}
public getRequest(url: string, args?: { [key: string]: string }): Promise<any> {
return new Promise<any>(function(resolve, reject) {
const request = new XMLHttpRequest();
request.onload = function() {
if (this.status === 200) {
resolve(this.response);
} else {
reject(new Error(this.statusText));
}
};
request.onerror = function() {
reject(new Error('XMLHttpRequest Error: ' + this.statusText));
};
request.open('GET', url);
if (args != null) {
for (const arg in args) {
request.setRequestHeader(arg, args[arg]);
}
}
request.send();
});
}
public static toLowerKey(json: string): string {
return json.replace(/"([\w]+)":/g, function($0, $1) {
return '"' + HttpClient.toFirstLowerCase($1) + '":';
});
}
public static toFirstLowerCase(src: string): string {
return src.charAt(0).toLowerCase() + src.slice(1);
}
}