Skip to content

Commit cf995cd

Browse files
committed
Bug fix with @Map
1 parent 3ada464 commit cf995cd

File tree

2 files changed

+302
-1
lines changed

2 files changed

+302
-1
lines changed

angular2-rest.js

+300
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/*
2+
3+
angular2-rest
4+
(c) Domonkos Pal
5+
License: MIT
6+
7+
Table of Contents:
8+
9+
- class RESTClient
10+
11+
- Class Decorators:
12+
@BaseUrl(String)
13+
@DefaultHeaders(Object)
14+
15+
- Method Decorators:
16+
@GET(url: String)
17+
@POST(url: String)
18+
@PUT(url: String)
19+
@DELETE(url: String)
20+
@Headers(object)
21+
@Produces(MediaType)
22+
23+
- Parameter Decorators:
24+
@Path(string)
25+
@Query(string)
26+
@Header(string)
27+
@Body
28+
*/
29+
"use strict";
30+
var http_1 = require("@angular/http");
31+
/**
32+
* Angular 2 RestClient class.
33+
*
34+
* @class RESTClient
35+
* @constructor
36+
*/
37+
var RestClient = (function () {
38+
function RestClient(httpClient) {
39+
this.httpClient = httpClient;
40+
}
41+
RestClient.prototype.getServiceId = function () {
42+
return null;
43+
};
44+
RestClient.prototype.getBaseUrl = function () {
45+
return null;
46+
};
47+
;
48+
RestClient.prototype.getDefaultHeaders = function () {
49+
return null;
50+
};
51+
;
52+
/**
53+
* Request Interceptor
54+
*
55+
* @method requestInterceptor
56+
* @param {Request} req - request object
57+
*/
58+
RestClient.prototype.requestInterceptor = function (req) {
59+
//
60+
};
61+
/**
62+
* Response Interceptor
63+
*
64+
* @method responseInterceptor
65+
* @param {Response} res - response object
66+
* @returns {Response} res - transformed response object
67+
*/
68+
RestClient.prototype.responseInterceptor = function (res) {
69+
return res;
70+
};
71+
return RestClient;
72+
}());
73+
exports.RestClient = RestClient;
74+
/**
75+
* Configure the REST Client
76+
* @param {String} url - base URL
77+
* @param {String} serviceId - Service ID
78+
* @param {Object} headers - deafult headers in a key-value pair
79+
*/
80+
function Client(args) {
81+
return function (Target) {
82+
if (args.serviceId) {
83+
Target.prototype.getServiceId = function () {
84+
return args.serviceId;
85+
};
86+
}
87+
if (args.baseUrl) {
88+
Target.prototype.getBaseUrl = function () {
89+
return args.baseUrl;
90+
};
91+
}
92+
if (args.headers) {
93+
Target.prototype.getDefaultHeaders = function () {
94+
return args.headers;
95+
};
96+
}
97+
return Target;
98+
};
99+
}
100+
exports.Client = Client;
101+
function paramBuilder(paramName) {
102+
return function (key) {
103+
return function (target, propertyKey, parameterIndex) {
104+
var metadataKey = propertyKey + "_" + paramName + "_parameters";
105+
var paramObj = {
106+
key: key,
107+
parameterIndex: parameterIndex
108+
};
109+
if (Array.isArray(target[metadataKey])) {
110+
target[metadataKey].push(paramObj);
111+
}
112+
else {
113+
target[metadataKey] = [paramObj];
114+
}
115+
};
116+
};
117+
}
118+
/**
119+
* Path variable of a method's url, type: string
120+
* @param {string} key - path key to bind value
121+
*/
122+
exports.Path = paramBuilder("Path");
123+
/**
124+
* Query value of a method's url, type: string
125+
* @param {string} key - query key to bind value
126+
*/
127+
exports.Query = paramBuilder("Query");
128+
/**
129+
* Body of a REST method, type: key-value pair object
130+
* Only one body per method!
131+
*/
132+
exports.Body = paramBuilder("Body")("Body");
133+
/**
134+
* Custom header of a REST method, type: string
135+
* @param {string} key - header key to bind value
136+
*/
137+
exports.Header = paramBuilder("Header");
138+
/**
139+
* Set custom headers for a REST method
140+
* @param {Object} headersDef - custom headers in a key-value pair
141+
*/
142+
function Headers(headersDef) {
143+
return function (target, propertyKey, descriptor) {
144+
descriptor.headers = headersDef;
145+
return descriptor;
146+
};
147+
}
148+
exports.Headers = Headers;
149+
/**
150+
* Defines a custom mapper function
151+
* Overrides @Produces
152+
* @param MediaType media type or custom mapper function
153+
*/
154+
function Map(mapper) {
155+
return function (target, propertyKey, descriptor) {
156+
descriptor.mapper = mapper;
157+
return descriptor;
158+
};
159+
}
160+
exports.Map = Map;
161+
/**
162+
* Defines the media type(s) that the methods can produce
163+
* @param MediaType media type or custom mapper function
164+
*/
165+
function Produces(mime) {
166+
return function (target, propertyKey, descriptor) {
167+
if (mime != undefined) {
168+
if (mime === MediaType.JSON) {
169+
descriptor.mime = function (res) { return res.json(); };
170+
}
171+
}
172+
return descriptor;
173+
};
174+
}
175+
exports.Produces = Produces;
176+
/**
177+
* Supported @Produces media types
178+
*/
179+
(function (MediaType) {
180+
MediaType[MediaType["JSON"] = 0] = "JSON";
181+
})(exports.MediaType || (exports.MediaType = {}));
182+
var MediaType = exports.MediaType;
183+
function methodBuilder(method) {
184+
return function (url) {
185+
return function (target, propertyKey, descriptor) {
186+
var pPath = target[(propertyKey + "_Path_parameters")];
187+
var pQuery = target[(propertyKey + "_Query_parameters")];
188+
var pBody = target[(propertyKey + "_Body_parameters")];
189+
var pHeader = target[(propertyKey + "_Header_parameters")];
190+
descriptor.value = function () {
191+
var args = [];
192+
for (var _i = 0; _i < arguments.length; _i++) {
193+
args[_i - 0] = arguments[_i];
194+
}
195+
// Body
196+
var body = null;
197+
if (pBody) {
198+
body = JSON.stringify(args[pBody[0].parameterIndex]);
199+
}
200+
// Path
201+
var resUrl = url;
202+
if (pPath) {
203+
for (var k in pPath) {
204+
if (pPath.hasOwnProperty(k)) {
205+
resUrl = resUrl.replace("{" + pPath[k].key + "}", args[pPath[k].parameterIndex]);
206+
}
207+
}
208+
}
209+
if (this.getBaseUrl() != null) {
210+
var baseUrl = this.getBaseUrl();
211+
if (baseUrl.indexOf("/") == baseUrl.length - 1 && resUrl.indexOf("/") == 0) {
212+
baseUrl = baseUrl.substring(0, 1);
213+
}
214+
resUrl = baseUrl + resUrl;
215+
}
216+
// Query
217+
var search = new http_1.URLSearchParams();
218+
if (pQuery) {
219+
pQuery
220+
.filter(function (p) { return args[p.parameterIndex]; }) // filter out optional parameters
221+
.forEach(function (p) {
222+
var key = p.key;
223+
var value = args[p.parameterIndex];
224+
// if the value is a instance of Object, we stringify it
225+
if (value instanceof Object) {
226+
value = JSON.stringify(value);
227+
}
228+
search.set(encodeURIComponent(key), encodeURIComponent(value));
229+
});
230+
}
231+
// Headers
232+
// set class default headers
233+
var headers = new http_1.Headers(this.getDefaultHeaders());
234+
// set method specific headers
235+
for (var k in descriptor.headers) {
236+
if (descriptor.headers.hasOwnProperty(k)) {
237+
headers.append(k, descriptor.headers[k]);
238+
}
239+
}
240+
// set parameter specific headers
241+
if (pHeader) {
242+
for (var k in pHeader) {
243+
if (pHeader.hasOwnProperty(k)) {
244+
headers.append(pHeader[k].key, args[pHeader[k].parameterIndex]);
245+
}
246+
}
247+
}
248+
// Request options
249+
var options = new http_1.RequestOptions({
250+
method: method,
251+
url: resUrl,
252+
headers: headers,
253+
body: body,
254+
search: search
255+
});
256+
var req = new http_1.Request(options);
257+
// intercept the request
258+
this.requestInterceptor(req);
259+
// make the request and store the observable for later transformation
260+
var observable = this.httpClient.request(req);
261+
// transform the observable in accordance to the @Produces decorator
262+
if (descriptor.mapper != undefined) {
263+
observable = observable.map(descriptor.mapper);
264+
}
265+
else if (descriptor.mime != undefined) {
266+
observable = observable.map(descriptor.mime);
267+
}
268+
// intercept the response
269+
observable = this.responseInterceptor(observable);
270+
return observable;
271+
};
272+
return descriptor;
273+
};
274+
};
275+
}
276+
/**
277+
* Get method
278+
* @param {string} url - resource url of the method
279+
*/
280+
exports.Get = methodBuilder(http_1.RequestMethod.Get);
281+
/**
282+
* Post method
283+
* @param {string} url - resource url of the method
284+
*/
285+
exports.Post = methodBuilder(http_1.RequestMethod.Post);
286+
/**
287+
* Put method
288+
* @param {string} url - resource url of the method
289+
*/
290+
exports.Put = methodBuilder(http_1.RequestMethod.Put);
291+
/**
292+
* Delete method
293+
* @param {string} url - resource url of the method
294+
*/
295+
exports.Delete = methodBuilder(http_1.RequestMethod.Delete);
296+
/**
297+
* Head method
298+
* @param {string} url - resource url of the method
299+
*/
300+
exports.Head = methodBuilder(http_1.RequestMethod.Head);

angular2-rest.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ export function Headers(headersDef: any) {
169169
*/
170170
export function Map(mapper:(resp : any)=>any){
171171
return function(target: RestClient, propertyKey: string, descriptor: any) {
172-
descriptor.mapper = res => res.json();
172+
descriptor.mapper = mapper;
173+
return descriptor;
173174
}
174175
}
175176

0 commit comments

Comments
 (0)