@@ -76,7 +76,7 @@ export class RESTClient {
76
76
* @param {Response } res - response object
77
77
* @returns {Response } res - transformed response object
78
78
*/
79
- protected responseInterceptor ( res : Response ) : Response {
79
+ protected responseInterceptor ( res : Observable < any > ) : Observable < any > {
80
80
return res ;
81
81
}
82
82
@@ -158,6 +158,27 @@ export function Headers(headersDef: any) {
158
158
} ;
159
159
}
160
160
161
+
162
+ /**
163
+ * Defines the media type(s) that the methods can produce
164
+ * @param MediaType producesDef - mediaType to be parsed
165
+ */
166
+ export function Produces ( producesDef : MediaType ) {
167
+ return function ( target : RESTClient , propertyKey : string , descriptor : any ) {
168
+ descriptor . isJSON = producesDef === MediaType . JSON ;
169
+ return descriptor ;
170
+ } ;
171
+ }
172
+
173
+
174
+ /**
175
+ * Supported @Produces media types
176
+ */
177
+ export enum MediaType {
178
+ JSON
179
+ }
180
+
181
+
161
182
function methodBuilder ( method : number ) {
162
183
return function ( url : string ) {
163
184
return function ( target : RESTClient , propertyKey : string , descriptor : any ) {
@@ -234,8 +255,14 @@ function methodBuilder(method: number) {
234
255
this . requestInterceptor ( req ) ;
235
256
// make the request and store the observable for later transformation
236
257
var observable : Observable < Response > = this . http . request ( req ) ;
258
+
259
+ // transform the obserable in accordance to the @Produces decorator
260
+ if ( descriptor . isJSON ) {
261
+ observable = observable . map ( res => res . json ( ) ) ;
262
+ }
263
+
237
264
// intercept the response
238
- observable = observable . map ( this . responseInterceptor ) ;
265
+ observable = this . responseInterceptor ( observable ) ;
239
266
240
267
return observable ;
241
268
} ;
@@ -270,4 +297,3 @@ export var DELETE = methodBuilder(RequestMethods.Delete);
270
297
* @param {string } url - resource url of the method
271
298
*/
272
299
export var HEAD = methodBuilder ( RequestMethods . Head ) ;
273
-
0 commit comments