Repository navigation
Expand file tree
/
Copy pathHttp.ts
More file actions
77 lines (65 loc) · 2.07 KB
/
Copy pathHttp.ts
File metadata and controls
77 lines (65 loc) · 2.07 KB
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
import {Injectable} from 'angular2/core';
import {
Http as NgHttp,
RequestOptionsArgs,
RequestOptions,
Headers,
Response,
} from 'angular2/http';
import {HttpAuthError, HttpClientError, HttpServerError} from './HttpError'
const mergeAuthToken = (options:RequestOptionsArgs) => {
let newOptions = new RequestOptions({}).merge(options);
let newHeaders = new Headers(newOptions.headers);
newHeaders.set('X-AUTH-TOKEN', localStorage.getItem('jwt'));
newHeaders.set('Accept', 'application/json');
newHeaders.set('Content-Type', 'application/json');
newOptions.headers = newHeaders;
return newOptions;
};
@Injectable()
export class Http extends NgHttp {
constructor(protected _backend:any, protected _defaultOptions:any) {
super(_backend, _defaultOptions);
}
static checkStatus(response:Response):void {
if (response.status == 401) {
throw new HttpAuthError(response.statusText, response);
}
if (response.status >= 400 && response.status < 500) {
throw new HttpClientError(response.statusText, response);
}
if (response.status >= 500) {
throw new HttpServerError(response.statusText, response);
}
}
get(url:string, options?:RequestOptionsArgs):any {
return super.get(url, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
post(url:string, body:string, options?:RequestOptionsArgs):any {
return super.post(url, body, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
put(url:string, body:string, options?:RequestOptionsArgs):any {
return super.put(url, body, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
delete(url:string, options?:RequestOptionsArgs):any {
return super.delete(url, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
patch(url:string, body:string, options?:RequestOptionsArgs):any {
return super.patch(url, body, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
head(url:string, options?:RequestOptionsArgs):any {
return super.head(url, mergeAuthToken(options))
.do(Http.checkStatus)
;
}
}