/
Resource.ts
executable file
·180 lines (142 loc) · 4.05 KB
/
Resource.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Http, Request } from '@angular/http';
import { Injector } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { ResourceGlobalConfig } from './ResourceGlobalConfig';
import { ResourceParamsBase } from './Interfaces';
import { ResourceActionBase } from './Interfaces';
import { ResourceModel } from './ResourceModel';
export class Resource {
private _url: string = null;
private _path: string = null;
private _headers: any = null;
private _params: any = null;
private _data: any = null;
constructor(protected http: Http, protected injector: Injector) {}
/**
* Get main url of the resource
* @returns {string|Promise<string>}
*/
getUrl(methodOptions?: ResourceActionBase): string | Promise<string> {
return this._url || this._getUrl(methodOptions) || ResourceGlobalConfig.url || '';
}
/**
* Set resource url
* @param url
*/
setUrl(url: string) {
this._url = url;
}
/**
* Get path of the resource
* @returns {string|Promise<string>}
*/
getPath(methodOptions?: ResourceActionBase): string | Promise<string> {
return this._path || this._getPath(methodOptions) || ResourceGlobalConfig.path || '';
}
/**
* Set resource path
* @param path
*/
setPath(path: string) {
this._path = path;
}
/**
* Get headers
* @returns {any|Promise<any>}
*/
getHeaders(methodOptions?: ResourceActionBase): any | Promise<any> {
return this._headers || this._getHeaders(methodOptions) || ResourceGlobalConfig.headers || {};
}
/**
* Set resource headers
* @param headers
*/
setHeaders(headers: any) {
this._headers = headers;
}
/**
* Get default params
* @returns {any|Promise<any>|{}}
*/
getParams(methodOptions?: ResourceActionBase): any | Promise<any> {
return this._params || this._getParams(methodOptions) || ResourceGlobalConfig.params || {};
}
/**
* Set default resource params
* @param params
*/
setParams(params: any) {
this._params = params;
}
/**
* Get default data
* @returns {any|Promise<any>|{}}
*/
getData(methodOptions?: ResourceActionBase): any | Promise<any> {
return this._data || this._getData(methodOptions) || ResourceGlobalConfig.data || {};
}
/**
* Set default resource params
* @param data
*/
setData(data: any) {
this._data = data;
}
/**
* That is called before executing request
* @param req
*/
requestInterceptor(req: Request, methodOptions?: ResourceActionBase): Request {
return req;
}
/**
* Request observable interceptor
* @param observable
* @returns {Observable<any>}
*/
responseInterceptor(observable: Observable<any>, req: Request, methodOptions?: ResourceActionBase): Observable<any> {
return observable.map(res => res._body ? res.json() : null);
}
removeTrailingSlash(): boolean {
return true;
}
initResultObject(): any {
return {};
}
map(item: any): any {
return item;
}
filter(item: any): boolean {
return true;
}
getResourceOptions(): ResourceParamsBase {
return null;
}
createModel(): ResourceModel<any> {
let ret = this.initResultObject();
ret.$resource = this;
return ret;
}
protected _request(req: Request, methodOptions: ResourceActionBase = {}): Observable<any> {
let requestObservable = this.http.request(req);
// noinspection TypeScriptValidateTypes
return methodOptions.responseInterceptor ?
methodOptions.responseInterceptor(requestObservable, req, methodOptions) :
this.responseInterceptor(requestObservable, req, methodOptions);
}
private _getUrl(methodOptions?: ResourceActionBase): string|Promise<string> {
return null;
}
private _getPath(methodOptions?: ResourceActionBase): string|Promise<string> {
return null;
}
private _getHeaders(methodOptions?: ResourceActionBase): any | Promise<any> {
return null;
}
private _getParams(methodOptions?: ResourceActionBase): any | Promise<any> {
return null;
}
private _getData(methodOptions?: ResourceActionBase): any | Promise<any> {
return null;
}
}