| 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 |
4×
4×
9×
9×
5×
4×
4×
3×
4×
4×
9×
9×
9×
9×
4×
4×
8×
2×
3×
4×
6×
6×
2×
6×
6×
6×
6×
6×
6×
4×
3×
4×
1×
4×
2×
4×
1×
4×
2×
4×
1×
4×
1×
4×
1×
4×
| import { Observable } from 'rxjs/Observable';
import { Headers, Http, Request, RequestMethod, RequestOptions, RequestOptionsArgs, Response, URLSearchParams } from '@angular/http';
import { RestangularConfig } from './config';
import { RestangularPath } from './path';
import 'rxjs/add/operator/map';
export function mergeHeaders(headers: Headers, defaultHeaders: Headers): Headers {
defaultHeaders = new Headers(defaultHeaders);
if (!headers) {
return defaultHeaders;
}
let newHeaders: Headers = defaultHeaders;
(headers as Headers).forEach((values: string[], name: string) => {
newHeaders.set(name, values)
});
return newHeaders;
}
export function mergeSearch(params: URLSearchParams, defaultParams: URLSearchParams): URLSearchParams {
let newParams = new URLSearchParams();
newParams.setAll(defaultParams || new URLSearchParams());
newParams.setAll(params || new URLSearchParams());
return newParams;
}
export namespace RestangularHttp {
export function interceptResponse(path: RestangularPath, operation: string, url: string): (value: any, index: number) => any {
return (res: Response, index: number) => {
return path.config.responseInterceptors.reduce((data: any, intercepor: any) => {
return intercepor(data, operation, path, url, res);
}, res.json());
};
}
export function makeRequest(operation: string, path: RestangularPath, requestArgs: RequestOptionsArgs, additionalOptions?: RequestOptionsArgs): Observable<Response> {
let options = new RequestOptions(requestArgs);
if (additionalOptions) {
options = options.merge(additionalOptions);
}
options.headers = mergeHeaders(options.headers, path.config.defaultHeaders);
options.search = mergeSearch(options.search, path.config.defaultParams);
let request: Request = new Request(options);
request.url = path.toString();
request = path.config.requestInterceptors.reduce((req: Request, interceptor: any) => interceptor(req, operation, path), request);
return path.config.http.request(request).map(RestangularHttp.interceptResponse(path, operation, request.url));
}
export function get(path: RestangularPath, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('get', path, {
body: '',
method: RequestMethod.Get
}, options);
}
export function getList(path: RestangularPath, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('getList', path, {
body: '',
method: RequestMethod.Get
}, options);
}
export function post(path: RestangularPath, body: any, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('post', path, {
body: body,
method: RequestMethod.Post
}, options);
}
export function put(path: RestangularPath, body: any, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('put', path, {
body: body,
method: RequestMethod.Put
}, options);
}
export function remove(path: RestangularPath, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('remove', path, {
body: '',
method: RequestMethod.Delete
}, options);
}
export function patch(path: RestangularPath, body: any, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('patch', path, {
body: body,
method: RequestMethod.Patch
}, options);
}
export function head(path: RestangularPath, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('head', path, {
body: '',
method: RequestMethod.Head
}, options);
}
export function options(path: RestangularPath, options?: RequestOptionsArgs): Observable<Response> {
return RestangularHttp.makeRequest('options', path, {
body: '',
method: RequestMethod.Options
}, options);
}
}
|