/
api.service.ts
81 lines (76 loc) · 2.9 KB
/
api.service.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
import { isDevMode, Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
@Injectable()
export class APIService {
constructor(private http: HttpClient) {}
public rootURL : string = isDevMode() ? "http://localhost:5000/api" : "/api"
public option : any = {
observe: "body" as "body",
withCredentials: true,
responseType: 'json' as 'json'
}
callAPI(path: string, method: "GET" | "POST" | "PUT" | "DELETE", reqdata?: any, header?: { [header: string]: string | string[] }) {
let url = `${this.rootURL}/${path}`
let _header = header? header : {'Content-Type': 'application/json'}
let option = {
...this.option,
headers: {
..._header,
},
params: method === "GET" ? this.GetParams(reqdata) : {},
}
switch (method) {
case "GET":
return(this.http.get(url, option))
.pipe(catchError(this.handleError))
case "POST":
return(this.http.post(url, reqdata, option))
.pipe(catchError(this.handleError))
case "PUT":
return(this.http.put(url, reqdata, option))
.pipe(catchError(this.handleError))
case "DELETE":
return(this.http.delete(url, {
body: reqdata,
...option
})
).pipe(catchError(this.handleError))
}
}
download(path : string, filename : string, payload : object, mimetype : object){
let option = { responseType: "blob" as "json" };
this.http.post(this.rootURL + "/" + path, payload, option)
.pipe(catchError(this.handleError))
.subscribe((res:any) => {
let blob: Blob = new Blob([res], mimetype);
let downloadUrl = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.href = downloadUrl;
link.download = filename;
link.click();
})
}
handleError(error: HttpErrorResponse){
if(error.status === 0){
window.alert(error.error)
console.error("Error occured : ", error.error)
}
else{
window.alert(error.error)
console.error(`Backend return code ${error.status}, body was`, error.error)
}
return throwError( () => new Error("Something bad happened; please try again later."))
}
GetParams(originData : any){
let params = new HttpParams();
if(originData > 0){
originData.forEach( (item : any) => {
let keyname = Object.keys(item)[0]
params.set(keyname, item[keyname])
})
}
return params
}
}