1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
8 |
|
9 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
10 |
|
11 | var _angular = require('angular');
|
12 |
|
13 | var _angular2 = _interopRequireDefault(_angular);
|
14 |
|
15 | var _moment = require('moment');
|
16 |
|
17 | var _moment2 = _interopRequireDefault(_moment);
|
18 |
|
19 | var _module = require('../module');
|
20 |
|
21 | var _module2 = _interopRequireDefault(_module);
|
22 |
|
23 | require('./constants');
|
24 |
|
25 | require('../polling');
|
26 |
|
27 | require('../localStorage');
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | var ApiResourceProvider = function () {
|
34 | function ApiResourceProvider(AV_API) {
|
35 | _classCallCheck(this, ApiResourceProvider);
|
36 |
|
37 | this.defaultOptions = _extends({}, AV_API.OPTIONS);
|
38 | }
|
39 |
|
40 | _createClass(ApiResourceProvider, [{
|
41 | key: 'setOptions',
|
42 | value: function setOptions(options) {
|
43 | _extends(this.defaultOptions, options);
|
44 | }
|
45 | }, {
|
46 | key: 'getOptions',
|
47 | value: function getOptions() {
|
48 | return _angular2.default.copy(this.defaultOptions);
|
49 | }
|
50 | }, {
|
51 | key: '$get',
|
52 | value: function $get($http, $q, avPollingService, avLocalStorageService, AV_STORAGE) {
|
53 |
|
54 | var that = this;
|
55 |
|
56 | var AvApiResource = function () {
|
57 | function AvApiResource(_options) {
|
58 | _classCallCheck(this, AvApiResource);
|
59 |
|
60 | this.options = _options;
|
61 |
|
62 | if (!this.options) {
|
63 | throw new Error('[options] cannot be null or undefined');
|
64 | }
|
65 |
|
66 | if (this.options.name) {
|
67 | this.options.name = this.options.name.charAt(0) === '/' ? this.options.name : '/' + this.options.name;
|
68 | }
|
69 |
|
70 | if (!this.options.url && !this.options.name) {
|
71 | throw new Error('AvApiResource options [url] or [name] cannot be null');
|
72 | }
|
73 |
|
74 |
|
75 | this.options = _angular2.default.extend({}, that.defaultOptions, this.options);
|
76 |
|
77 | this.pageBustValue;
|
78 | }
|
79 |
|
80 | _createClass(AvApiResource, [{
|
81 | key: 'config',
|
82 | value: function config(_config2) {
|
83 | return _angular2.default.extend({}, this.options, _config2 || {});
|
84 | }
|
85 | }, {
|
86 | key: 'cacheBust',
|
87 | value: function cacheBust(config) {
|
88 |
|
89 | if (config.cacheBust === true) {
|
90 | config.params.cacheBust = (0, _moment2.default)().unix();
|
91 | } else if (_angular2.default.isFunction(config.cacheBust)) {
|
92 | config.params.cacheBust = config.cacheBust();
|
93 | } else {
|
94 | config.params.cacheBust = config.cacheBust;
|
95 | }
|
96 | }
|
97 | }, {
|
98 | key: 'setPageBust',
|
99 | value: function setPageBust(value) {
|
100 | this.pageBustValue = _angular2.default.isUndefined(value) ? (0, _moment2.default)().unix() : value;
|
101 | }
|
102 | }, {
|
103 | key: 'getPageBust',
|
104 | value: function getPageBust() {
|
105 | if (_angular2.default.isUndefined(this.pageBustValue)) {
|
106 | this.setPageBust();
|
107 | }
|
108 | return this.pageBustValue;
|
109 | }
|
110 | }, {
|
111 | key: 'pageBust',
|
112 | value: function pageBust(config) {
|
113 | if (config.pageBust === true) {
|
114 | config.params.pageBust = this.getPageBust();
|
115 | } else if (_angular2.default.isFunction(config.pageBust)) {
|
116 | config.params.pageBust = config.pageBust();
|
117 | } else {
|
118 | config.params.pageBust = config.pageBust;
|
119 | }
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | }, {
|
131 | key: 'cacheParams',
|
132 | value: function cacheParams(_config) {
|
133 |
|
134 | var config = _angular2.default.copy(_config);
|
135 | config.params = config.params || {};
|
136 |
|
137 | if (config.cacheBust) {
|
138 | this.cacheBust(config);
|
139 | }
|
140 |
|
141 | if (config.pageBust) {
|
142 | this.pageBust(config);
|
143 | }
|
144 |
|
145 | if (config.sessionBust) {
|
146 | config.params.sessionBust = avLocalStorageService.getVal(AV_STORAGE.SESSION_CACHE) || this.getPageBust();
|
147 | }
|
148 |
|
149 | return config;
|
150 | }
|
151 | }, {
|
152 | key: 'getUrl',
|
153 | value: function getUrl(id) {
|
154 |
|
155 | if (this.options.api) {
|
156 | return this.getApiUrl(id);
|
157 | }
|
158 |
|
159 | return this.options.url;
|
160 | }
|
161 | }, {
|
162 | key: 'request',
|
163 | value: function request(config, afterCallback) {
|
164 |
|
165 | var self = this;
|
166 | var defer = $q.defer();
|
167 |
|
168 | $http(config).then(function (response) {
|
169 |
|
170 |
|
171 | var _promise = $q.when(avPollingService.response(response));
|
172 |
|
173 |
|
174 | defer.notify(response);
|
175 |
|
176 |
|
177 | _promise.then(function (_successResponse) {
|
178 |
|
179 | var successResponse = _successResponse;
|
180 |
|
181 | if (afterCallback) {
|
182 | successResponse = afterCallback.call(self, successResponse, config.data);
|
183 | }
|
184 | defer.resolve(successResponse);
|
185 | }, function (errorResponse) {
|
186 | return defer.reject(errorResponse);
|
187 | }, function (notifyResponse) {
|
188 | return defer.notify(notifyResponse);
|
189 | });
|
190 | }).catch(function (response) {
|
191 | defer.reject(response);
|
192 | });
|
193 |
|
194 | var promise = defer.promise;
|
195 |
|
196 |
|
197 | promise.success = function (fn) {
|
198 | promise.then(function (response) {
|
199 | fn(response.data, response.status, response.headers, response.config);
|
200 | });
|
201 | return promise;
|
202 | };
|
203 |
|
204 |
|
205 | promise.error = function (fn) {
|
206 |
|
207 | promise.then(null, function (response) {
|
208 | fn(response.data, response.status, response.headers, config);
|
209 | });
|
210 |
|
211 | return promise;
|
212 | };
|
213 |
|
214 | promise.always = promise.finally;
|
215 |
|
216 | return promise;
|
217 | }
|
218 | }, {
|
219 | key: 'normalize',
|
220 | value: function normalize(url) {
|
221 | return url.replace(/[\/]+/g, '/').replace(/\/$/, '');
|
222 | }
|
223 | }, {
|
224 | key: 'join',
|
225 | value: function join() {
|
226 | var joined = [].slice.call(arguments, 0).join('/');
|
227 | return this.normalize(joined);
|
228 | }
|
229 | }, {
|
230 | key: 'getApiUrl',
|
231 | value: function getApiUrl(_id) {
|
232 |
|
233 | var id = _id ? '/' + _id : '';
|
234 |
|
235 | var uri = void 0;
|
236 |
|
237 | var _options2 = this.options,
|
238 | path = _options2.path,
|
239 | version = _options2.version,
|
240 | name = _options2.name,
|
241 | url = _options2.url;
|
242 |
|
243 |
|
244 | if (name) {
|
245 | uri = this.join('/', path, version, name, id);
|
246 | } else {
|
247 | uri = this.join(url, id);
|
248 | }
|
249 |
|
250 | return uri;
|
251 | }
|
252 | }, {
|
253 | key: 'create',
|
254 | value: function create(_data, _config) {
|
255 |
|
256 | var data = _data;
|
257 | var config = _config;
|
258 |
|
259 | if (!data) {
|
260 | throw new Error('called method without [data]');
|
261 | }
|
262 |
|
263 | if (this.beforeCreate) {
|
264 | data = this.beforeCreate(data);
|
265 | }
|
266 |
|
267 | config = this.config(config);
|
268 | config.method = 'POST';
|
269 | config.url = this.getUrl();
|
270 | config.data = data;
|
271 |
|
272 | return this.request(config, this.afterCreate);
|
273 | }
|
274 | }, {
|
275 | key: 'postGet',
|
276 | value: function postGet(_data, _config) {
|
277 | var data = _data;
|
278 | var config = _config;
|
279 |
|
280 | if (!data) {
|
281 | throw new Error('called method without [data]');
|
282 | }
|
283 | if (this.beforePostGet) {
|
284 | data = this.beforePostGet(data);
|
285 | }
|
286 | config = this.config(config);
|
287 | config.method = 'POST';
|
288 | config.headers['X-HTTP-Method-Override'] = 'GET';
|
289 | config.url = this.getUrl();
|
290 | config.data = data;
|
291 |
|
292 | return this.request(config, this.afterPostGet);
|
293 | }
|
294 | }, {
|
295 | key: 'get',
|
296 | value: function get(id, _config) {
|
297 |
|
298 | var config = _config;
|
299 |
|
300 | if (!id) {
|
301 | throw new Error('called method without [id]');
|
302 | }
|
303 |
|
304 | config = this.config(config);
|
305 | config = this.cacheParams(config);
|
306 |
|
307 | config.method = 'GET';
|
308 | config.url = this.getUrl(id);
|
309 |
|
310 | return this.request(config, this.afterGet);
|
311 | }
|
312 | }, {
|
313 | key: 'query',
|
314 | value: function query(_config) {
|
315 |
|
316 | var config = _config;
|
317 |
|
318 | config = this.config(config);
|
319 | config = this.cacheParams(config);
|
320 |
|
321 | config.method = 'GET';
|
322 | config.url = this.getUrl();
|
323 |
|
324 | return this.request(config, this.afterQuery);
|
325 | }
|
326 | }, {
|
327 | key: 'update',
|
328 | value: function update(id, _data, _config) {
|
329 |
|
330 | var config = _config;
|
331 | var data = _data;
|
332 |
|
333 | var url = void 0;
|
334 |
|
335 | if (_angular2.default.isString(id) || _angular2.default.isNumber(id)) {
|
336 | url = this.getUrl(id);
|
337 | } else {
|
338 | url = this.getUrl();
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 | config = data;
|
345 | data = id;
|
346 | }
|
347 |
|
348 | if (this.beforeUpdate) {
|
349 | data = this.beforeUpdate(data);
|
350 | }
|
351 |
|
352 | config = this.config(config);
|
353 | config.method = 'PUT';
|
354 | config.url = url;
|
355 | config.data = data;
|
356 |
|
357 | return this.request(config, this.afterUpdate);
|
358 | }
|
359 | }, {
|
360 | key: 'remove',
|
361 | value: function remove(id, _config) {
|
362 |
|
363 | var config = _config;
|
364 |
|
365 | var url = void 0;
|
366 | var data = void 0;
|
367 |
|
368 | if (_angular2.default.isString(id) || _angular2.default.isNumber(id)) {
|
369 | url = this.getUrl(id);
|
370 | } else {
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | url = this.getUrl();
|
377 | data = id;
|
378 | }
|
379 |
|
380 | config = this.config(config);
|
381 | config.method = 'DELETE';
|
382 | config.url = url;
|
383 | config.data = data;
|
384 |
|
385 | return this.request(config, this.afterRemove);
|
386 | }
|
387 | }, {
|
388 | key: 'beforeCreate',
|
389 | value: function beforeCreate(data) {
|
390 | return data;
|
391 | }
|
392 | }, {
|
393 | key: 'afterCreate',
|
394 | value: function afterCreate(response) {
|
395 | return response;
|
396 | }
|
397 | }, {
|
398 | key: 'beforePostGet',
|
399 | value: function beforePostGet(data) {
|
400 | return data;
|
401 | }
|
402 | }, {
|
403 | key: 'afterPostGet',
|
404 | value: function afterPostGet(response) {
|
405 | return response;
|
406 | }
|
407 | }, {
|
408 | key: 'afterQuery',
|
409 | value: function afterQuery(response) {
|
410 | return response;
|
411 | }
|
412 | }, {
|
413 | key: 'afterGet',
|
414 | value: function afterGet(response) {
|
415 | return response;
|
416 | }
|
417 | }, {
|
418 | key: 'beforeUpdate',
|
419 | value: function beforeUpdate(data) {
|
420 | return data;
|
421 | }
|
422 | }, {
|
423 | key: 'afterUpdate',
|
424 | value: function afterUpdate(response) {
|
425 | return response;
|
426 | }
|
427 | }, {
|
428 | key: 'afterRemove',
|
429 | value: function afterRemove(response) {
|
430 | return response;
|
431 | }
|
432 | }]);
|
433 |
|
434 | return AvApiResource;
|
435 | }();
|
436 |
|
437 | return AvApiResource;
|
438 | }
|
439 | }]);
|
440 |
|
441 | return ApiResourceProvider;
|
442 | }();
|
443 |
|
444 | _module2.default.provider('AvApiResource', ApiResourceProvider);
|
445 |
|
446 | exports.default = _module2.default; |
\ | No newline at end of file |