-
Notifications
You must be signed in to change notification settings - Fork 72
/
angular2-rest.ts
295 lines (255 loc) · 8.02 KB
/
angular2-rest.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
angular2-rest
(c) Domonkos Pal
License: MIT
Table of Contents:
- class RESTClient
- Class Decorators:
@BaseUrl(String)
@DefaultHeaders(Object)
- Method Decorators:
@GET(url: String)
@POST(url: String)
@PUT(url: String)
@DELETE(url: String)
@Headers(object)
@Produces(MediaType)
- Parameter Decorators:
@Path(string)
@Query(string)
@Header(string)
@Body
*/
import {Inject} from "angular2/core";
import {
Http, Headers as AngularHeaders,
Request, RequestOptions, RequestMethod as RequestMethods,
Response,
URLSearchParams
} from "angular2/http";
import {Observable} from "rxjs/Observable";
/**
* Angular 2 RESTClient class.
*
* @class RESTClient
* @constructor
*/
export class RESTClient {
public constructor( @Inject(Http) protected http: Http) {
}
protected getBaseUrl(): string {
return null;
};
protected getDefaultHeaders(): Object {
return null;
};
/**
* Request Interceptor
*
* @method requestInterceptor
* @param {Request} req - request object
*/
protected requestInterceptor(req: Request) {
//
}
/**
* Response Interceptor
*
* @method responseInterceptor
* @param {Response} res - response object
* @returns {Response} res - transformed response object
*/
protected responseInterceptor(res: Observable<any>): Observable<any> {
return res;
}
}
/**
* Set the base URL of REST resource
* @param {String} url - base URL
*/
export function BaseUrl(url: string) {
return function <TFunction extends Function>(Target: TFunction): TFunction {
Target.prototype.getBaseUrl = function() {
return url;
};
return Target;
};
}
/**
* Set default headers for every method of the RESTClient
* @param {Object} headers - deafult headers in a key-value pair
*/
export function DefaultHeaders(headers: any) {
return function <TFunction extends Function>(Target: TFunction): TFunction {
Target.prototype.getDefaultHeaders = function() {
return headers;
};
return Target;
};
}
function paramBuilder(paramName: string) {
return function(key: string) {
return function(target: RESTClient, propertyKey: string | symbol, parameterIndex: number) {
var metadataKey = `${propertyKey}_${paramName}_parameters`;
var paramObj: any = {
key: key,
parameterIndex: parameterIndex
};
if (Array.isArray(target[metadataKey])) {
target[metadataKey].push(paramObj);
} else {
target[metadataKey] = [paramObj];
}
};
};
}
/**
* Path variable of a method's url, type: string
* @param {string} key - path key to bind value
*/
export var Path = paramBuilder("Path");
/**
* Query value of a method's url, type: string
* @param {string} key - query key to bind value
*/
export var Query = paramBuilder("Query");
/**
* Body of a REST method, type: key-value pair object
* Only one body per method!
*/
export var Body = paramBuilder("Body")("Body");
/**
* Custom header of a REST method, type: string
* @param {string} key - header key to bind value
*/
export var Header = paramBuilder("Header");
/**
* Set custom headers for a REST method
* @param {Object} headersDef - custom headers in a key-value pair
*/
export function Headers(headersDef: any) {
return function(target: RESTClient, propertyKey: string, descriptor: any) {
descriptor.headers = headersDef;
return descriptor;
};
}
/**
* Defines the media type(s) that the methods can produce
* @param MediaType producesDef - mediaType to be parsed
*/
export function Produces(producesDef: MediaType) {
return function(target: RESTClient, propertyKey: string, descriptor: any) {
descriptor.isJSON = producesDef === MediaType.JSON;
return descriptor;
};
}
/**
* Supported @Produces media types
*/
export enum MediaType {
JSON
}
function methodBuilder(method: number) {
return function(url: string) {
return function(target: RESTClient, propertyKey: string, descriptor: any) {
var pPath = target[`${propertyKey}_Path_parameters`];
var pQuery = target[`${propertyKey}_Query_parameters`];
var pBody = target[`${propertyKey}_Body_parameters`];
var pHeader = target[`${propertyKey}_Header_parameters`];
descriptor.value = function(...args: any[]) {
// Body
var body = null;
if (pBody) {
body = JSON.stringify(args[pBody[0].parameterIndex]);
}
// Path
var resUrl: string = url;
if (pPath) {
for (var k in pPath) {
if (pPath.hasOwnProperty(k)) {
resUrl = resUrl.replace("{" + pPath[k].key + "}", args[pPath[k].parameterIndex]);
}
}
}
// Query
var search = new URLSearchParams();
if (pQuery) {
pQuery
.filter(p => args[p.parameterIndex]) // filter out optional parameters
.forEach(p => {
var key = p.key;
var value = args[p.parameterIndex];
// if the value is a instance of Object, we stringify it
if (value instanceof Object) {
value = JSON.stringify(value);
}
search.set(encodeURIComponent(key), encodeURIComponent(value));
});
}
// Headers
// set class default headers
var headers = new AngularHeaders(this.getDefaultHeaders());
// set method specific headers
for (var k in descriptor.headers) {
if (descriptor.headers.hasOwnProperty(k)) {
headers.append(k, descriptor.headers[k]);
}
}
// set parameter specific headers
if (pHeader) {
for (var k in pHeader) {
if (pHeader.hasOwnProperty(k)) {
headers.append(pHeader[k].key, args[pHeader[k].parameterIndex]);
}
}
}
// Request options
var options = new RequestOptions({
method,
url: this.getBaseUrl() + resUrl,
headers,
body,
search
});
var req = new Request(options);
// intercept the request
this.requestInterceptor(req);
// make the request and store the observable for later transformation
var observable: Observable<Response> = this.http.request(req);
// transform the obserable in accordance to the @Produces decorator
if (descriptor.isJSON) {
observable = observable.map(res => res.json());
}
// intercept the response
observable = this.responseInterceptor(observable);
return observable;
};
return descriptor;
};
};
}
/**
* GET method
* @param {string} url - resource url of the method
*/
export var GET = methodBuilder(RequestMethods.Get);
/**
* POST method
* @param {string} url - resource url of the method
*/
export var POST = methodBuilder(RequestMethods.Post);
/**
* PUT method
* @param {string} url - resource url of the method
*/
export var PUT = methodBuilder(RequestMethods.Put);
/**
* DELETE method
* @param {string} url - resource url of the method
*/
export var DELETE = methodBuilder(RequestMethods.Delete);
/**
* HEAD method
* @param {string} url - resource url of the method
*/
export var HEAD = methodBuilder(RequestMethods.Head);