|
| 1 | +/* |
| 2 | +
|
| 3 | +angular2-rest |
| 4 | +(c) Domonkos Pal |
| 5 | +License: MIT |
| 6 | +
|
| 7 | +Table of Contents: |
| 8 | +
|
| 9 | +- interface IRequest |
| 10 | +- interface IResponse |
| 11 | +- class RESTClient |
| 12 | +
|
| 13 | +- Class Decorators: |
| 14 | + @BaseUrl(String) |
| 15 | + @DefaultHeaders(Object) |
| 16 | +
|
| 17 | +- Method Decorators: |
| 18 | + @GET(url: String) |
| 19 | + @POST(url: String) |
| 20 | + @PUT(url: String) |
| 21 | + @DELETE(url: String) |
| 22 | + @Headers(object) |
| 23 | +
|
| 24 | +- Parameter Decorators: |
| 25 | + @Path(string) |
| 26 | + @Query(string) |
| 27 | + @Header(string) |
| 28 | + @Body |
| 29 | +
|
| 30 | +*/ |
| 31 | +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { |
| 32 | + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); |
| 33 | + switch (arguments.length) { |
| 34 | + case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); |
| 35 | + case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); |
| 36 | + case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); |
| 37 | + } |
| 38 | +}; |
| 39 | +var __metadata = (this && this.__metadata) || function (k, v) { |
| 40 | + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); |
| 41 | +}; |
| 42 | +var __param = (this && this.__param) || function (paramIndex, decorator) { |
| 43 | + return function (target, key) { decorator(target, key, paramIndex); } |
| 44 | +}; |
| 45 | +var angular2_1 = require('angular2/angular2'); |
| 46 | +var http_1 = require('angular2/http'); |
| 47 | +/** |
| 48 | +* Angular 2 RESTClient class. |
| 49 | +* |
| 50 | +* @class RESTClient |
| 51 | +* @constructor |
| 52 | +*/ |
| 53 | +var RESTClient = (function () { |
| 54 | + function RESTClient(http) { |
| 55 | + this.http = http; |
| 56 | + } |
| 57 | + RESTClient.prototype.getBaseUrl = function () { |
| 58 | + return null; |
| 59 | + }; |
| 60 | + ; |
| 61 | + RESTClient.prototype.getDefaultHeaders = function () { |
| 62 | + return null; |
| 63 | + }; |
| 64 | + ; |
| 65 | + /** |
| 66 | + * Request Interceptor |
| 67 | + * |
| 68 | + * @method requestInterceptor |
| 69 | + * @param {IRequest} req - request object |
| 70 | + */ |
| 71 | + RESTClient.prototype.requestInterceptor = function (req) { |
| 72 | + }; |
| 73 | + /** |
| 74 | + * Response Interceptor |
| 75 | + * NOT IMPLEMENTED YET! |
| 76 | + * |
| 77 | + * @method responseInterceptor |
| 78 | + * @param {IResponse} resp - response object |
| 79 | + */ |
| 80 | + RESTClient.prototype.responseInterceptor = function (resp) { |
| 81 | + // TODO |
| 82 | + throw new Error("Not implemented yet!"); |
| 83 | + }; |
| 84 | + RESTClient = __decorate([ |
| 85 | + __param(0, angular2_1.Inject(http_1.Http)), |
| 86 | + __metadata('design:paramtypes', [http_1.Http]) |
| 87 | + ], RESTClient); |
| 88 | + return RESTClient; |
| 89 | +})(); |
| 90 | +exports.RESTClient = RESTClient; |
| 91 | +/** |
| 92 | + * Set the base URL of REST resource |
| 93 | + * @param {String} url - base URL |
| 94 | + */ |
| 95 | +function BaseUrl(url) { |
| 96 | + return function (Target) { |
| 97 | + Target.prototype.getBaseUrl = function () { |
| 98 | + return url; |
| 99 | + }; |
| 100 | + return Target; |
| 101 | + }; |
| 102 | +} |
| 103 | +exports.BaseUrl = BaseUrl; |
| 104 | +/** |
| 105 | + * Set default headers for every method of the RESTClient |
| 106 | + * @param {Object} headers - deafult headers in a key-value pair |
| 107 | + */ |
| 108 | +function DefaultHeaders(headers) { |
| 109 | + return function (Target) { |
| 110 | + Target.prototype.getDefaultHeaders = function () { |
| 111 | + return headers; |
| 112 | + }; |
| 113 | + return Target; |
| 114 | + }; |
| 115 | +} |
| 116 | +exports.DefaultHeaders = DefaultHeaders; |
| 117 | +function paramBuilder(paramName) { |
| 118 | + return function (key) { |
| 119 | + return function (target, propertyKey, parameterIndex) { |
| 120 | + var metadataKey = propertyKey + "_" + paramName + "_parameters"; |
| 121 | + var paramObj = { |
| 122 | + parameterIndex: parameterIndex, |
| 123 | + key: key |
| 124 | + }; |
| 125 | + if (Array.isArray(target[metadataKey])) { |
| 126 | + target[metadataKey].push(paramObj); |
| 127 | + } |
| 128 | + else { |
| 129 | + target[metadataKey] = [paramObj]; |
| 130 | + } |
| 131 | + }; |
| 132 | + }; |
| 133 | +} |
| 134 | +/** |
| 135 | + * Path variable of a method's url, type: string |
| 136 | + * @param {string} key - path key to bind value |
| 137 | + */ |
| 138 | +exports.Path = paramBuilder("Path"); |
| 139 | +/** |
| 140 | + * Query value of a method's url, type: string |
| 141 | + * @param {string} key - query key to bind value |
| 142 | + */ |
| 143 | +exports.Query = paramBuilder("Query"); |
| 144 | +/** |
| 145 | + * Body of a REST method, type: key-value pair object |
| 146 | + * Only one body per method! |
| 147 | + */ |
| 148 | +exports.Body = paramBuilder("Body")("Body"); |
| 149 | +/** |
| 150 | + * Custom header of a REST method, type: string |
| 151 | + * @param {string} key - header key to bind value |
| 152 | + */ |
| 153 | +exports.Header = paramBuilder("Header"); |
| 154 | +/** |
| 155 | + * Set custom headers for a REST method |
| 156 | + * @param {Object} headersDef - custom headers in a key-value pair |
| 157 | + */ |
| 158 | +function Headers(headersDef) { |
| 159 | + return function (target, propertyKey, descriptor) { |
| 160 | + descriptor.headers = headersDef; |
| 161 | + return descriptor; |
| 162 | + }; |
| 163 | +} |
| 164 | +exports.Headers = Headers; |
| 165 | +function methodBuilder(name) { |
| 166 | + return function (url) { |
| 167 | + return function (target, propertyKey, descriptor) { |
| 168 | + var pPath = target[(propertyKey + "_Path_parameters")]; |
| 169 | + var pQuery = target[(propertyKey + "_Query_parameters")]; |
| 170 | + var pBody = target[(propertyKey + "_Body_parameters")]; |
| 171 | + var pHeader = target[(propertyKey + "_Header_parameters")]; |
| 172 | + descriptor.value = function () { |
| 173 | + var args = []; |
| 174 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 175 | + args[_i - 0] = arguments[_i]; |
| 176 | + } |
| 177 | + //Body |
| 178 | + var body = null; |
| 179 | + if (pBody) { |
| 180 | + body = JSON.stringify(args[pBody[0].parameterIndex]); |
| 181 | + } |
| 182 | + // Path |
| 183 | + var resUrl = url; |
| 184 | + if (pPath) { |
| 185 | + for (k in pPath) { |
| 186 | + resUrl = resUrl.replace("{" + pPath[k].key + "}", args[pPath[k].parameterIndex]); |
| 187 | + } |
| 188 | + } |
| 189 | + // Query |
| 190 | + var queryString = ""; |
| 191 | + if (pQuery) { |
| 192 | + queryString = pQuery.map(function (p) { |
| 193 | + return encodeURIComponent(p.key) + '=' + encodeURIComponent(args[p.parameterIndex]); |
| 194 | + }).join('&'); |
| 195 | + } |
| 196 | + if (queryString) { |
| 197 | + queryString = "?" + queryString; |
| 198 | + } |
| 199 | + resUrl = resUrl + queryString; |
| 200 | + // Headers |
| 201 | + var headers = new http_1.Headers(); |
| 202 | + var defaultHeadersDef = this.getDefaultHeaders(); |
| 203 | + for (var k in defaultHeadersDef) { |
| 204 | + headers.append(k, defaultHeadersDef[k]); |
| 205 | + } |
| 206 | + for (var k in descriptor.headers) { |
| 207 | + headers.append(k, descriptor.headers[k]); |
| 208 | + } |
| 209 | + if (pHeader) { |
| 210 | + for (var k in pHeader) { |
| 211 | + headers.append(pHeader[k].key, args[pHeader[k].parameterIndex]); |
| 212 | + } |
| 213 | + } |
| 214 | + var self = this; |
| 215 | + return new Promise(function (resolve, reject) { |
| 216 | + var req = { |
| 217 | + url: self.getBaseUrl() + resUrl, |
| 218 | + headers: headers, |
| 219 | + body: body |
| 220 | + }; |
| 221 | + self.requestInterceptor(req); |
| 222 | + if (name === "post" || name === "put") { |
| 223 | + self.http[name](req.url, req.body, { |
| 224 | + 'headers': req.headers |
| 225 | + }).subscribe(function (res) { |
| 226 | + resolve(res.json()); |
| 227 | + }); |
| 228 | + } |
| 229 | + else { |
| 230 | + self.http[name](req.url, { |
| 231 | + 'headers': req.headers |
| 232 | + }).subscribe(function (res) { |
| 233 | + resolve(res.json()); |
| 234 | + }); |
| 235 | + } |
| 236 | + }); |
| 237 | + }; |
| 238 | + return descriptor; |
| 239 | + }; |
| 240 | + }; |
| 241 | +} |
| 242 | +/** |
| 243 | + * GET method |
| 244 | + * @param {string} url - resource url of the method |
| 245 | + */ |
| 246 | +exports.GET = methodBuilder("get"); |
| 247 | +/** |
| 248 | + * POST method |
| 249 | + * @param {string} url - resource url of the method |
| 250 | + */ |
| 251 | +exports.POST = methodBuilder("post"); |
| 252 | +/** |
| 253 | + * PUT method |
| 254 | + * @param {string} url - resource url of the method |
| 255 | + */ |
| 256 | +exports.PUT = methodBuilder("put"); |
| 257 | +/** |
| 258 | + * DELETE method |
| 259 | + * @param {string} url - resource url of the method |
| 260 | + */ |
| 261 | +exports.DELETE = methodBuilder("delete"); |
0 commit comments