Newer
Older
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
/**
* This class will handle the http request that send to server.
* It will help debuging and handle cookie if needed.
*/
import * as Constants from "./Constans";
export class ServerHttpService{
Get(path: string){
let url = Constants.Server_URL + path;
console.log("GET from: " + url);
return fetch(url,{
method: "GET"
})
.then((response) =>{
if(response.ok){
return response.json();
}
throw new Error("Unable to receive GET request from server with url:" + url);
})
.catch((reason) =>{
console.log("Error on GET request", reason);
});
}
Post(path: string, bodyData: any){
let url = Constants.Server_URL + path;
console.log("POST from: " + url);
return fetch(url,{
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(bodyData)
})
.then((response)=>{
if(response.ok){
return response.json();
}
throw new Error("Unable to receive POST request from server with url:" + url);
})
.catch((reason) =>{
console.log("Error on POST request", reason);
});
}
Put(path: string, bodyData: any){
let url = Constants.Server_URL + path;
console.log("PUT from: " + url);
return fetch(url,{
method: "PUT",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(bodyData)
})
.then((response)=>{
if(response.ok){
return response.json();
}
throw new Error("Unable to receive PUT request from server with url:" + url);
})
.catch((reason) =>{
console.log("Error on PUT request", reason);
});
}
Delete(path:string){
let url = Constants.Server_URL + path;
console.log("Delete from: " + url);
return fetch(url,{
method: "Delete",
})
.then((response)=>{
if(response.ok){
return response.json();
}
throw new Error("Unable to receive DELETE request from server with url:" + url);
})
.catch((reason) =>{
console.log("Error on DELETE request", reason);
});
}
}
/*
Change logs:
Date | Author | Description
2022-10-12 | Fangzheng Zhang | create class and init
2022-10-17 | Fangzheng Zhang | change to TS
*/