-
Notifications
You must be signed in to change notification settings - Fork 1
/
Networking.ts
65 lines (55 loc) · 1.61 KB
/
Networking.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
/**
* Created by rohittalwar on 13/04/16.
*/
import Axios, * as axios from "axios";
import { NetworkInterface } from "./Protocols";
export class MyNetworkClient implements NetworkInterface {
isReactive: boolean = false;
private headers: any = {};
constructor() { }
setHttpHeaders(headersMap: any) {
for (let key in headersMap) {
this.headers[key] = headersMap[key];
}
}
private _net(): axios.AxiosInstance {
return Axios.create({
timeout: 60000,
validateStatus: function (status) {
return true; // default
},
headers: {
...this.headers,
},
});
}
private async getDataFromCache(url: string, params?: Object): Promise<any> {
return new Promise((resolve, reject) => {
this.networkGET(url, params).then(resolve, reject);
});
}
private async networkGET(url: string, params?: Object): Promise<any> {
return this._net().get(url, { params });
}
async get(url: string, params?: Object): Promise<any> {
return this.getDataFromCache(url, params);
}
async post(url: string, params?: Object): Promise<any> {
return this._net().post(url, params);
}
async put(url: string, params?: Object): Promise<any> {
return this._net().put(url, params);
}
async delete(url: string, params?: Object): Promise<any> {
return this._net().put(url, params);
}
}
export class NetworkFactory {
static createSimpleClient(): NetworkInterface {
return NetworkFactory._createSimpleClient();
}
private static _createSimpleClient(token?: string): NetworkInterface {
let net = new MyNetworkClient();
return net;
}
}