-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-base-api-es6.js
78 lines (59 loc) · 2.53 KB
/
ng-base-api-es6.js
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
/**
* BASE API FOR ANGULARJS APPS
*
* Created by rahpal<[email protected]> on 13-07-2015.
*/
"use strict";
((angular, factory) => {
if (typeof define === 'function' && define.amd) {
define(['angular'], function (angular) {
return factory(angular);
});
} else {
return factory(angular);
}
})(angular || null, (angular) => {
angular.module('AngularJS.BaseApi', [])
.factory('BaseApiFactory', ['$http', '$q', function ($http, $q) {
let api = {},
_httpAjax = (httpMethod, webApiMethod, id, query, postData, enableCache) => {
let deferred = $q.defer(),
url,
requestPayload;
if (!!query && !query.startsWith('?')) {
query = '?' + query;
}
url = webApiUrl + "api/" + webApiMethod + (!!id ? "/" + id : "") + (query || "");
requestPayload = {
cache: !!enableCache,
method: httpMethod,
url: url,
data: !!postData ? JSON.stringify(postData) : null,
headers: {
'Content-Type': 'application/json'
}
};
$http(requestPayload)
.success(function (data, status, headers, config) {
deferred.resolve(data);
}).error(function (data, status, headers, config) {
// do not reject the defer since we are redirecting the user to error page.
deferred.reject(data);
});
return deferred.promise;
};
api.get = (method, id, query, enableCache = false) => {
return _httpAjax('GET', method, id, query, null, enableCache);
};
api.post = (method, id, query, postData, enableCache = false) => {
return _httpAjax('POST', method, id, query, postData, enableCache);
};
api.put = (method, id, query, postData, enableCache = false) => {
return _httpAjax('PUT', method, id, query, postData, enableCache);
};
api.delete = (method, id, query, enableCache = false)=> {
return _httpAjax('DELETE', method, id, query, null, enableCache);
};
return api;
}]);
});