Repository navigation
Expand file tree
/
Copy pathangular-api.js
More file actions
156 lines (119 loc) · 5.45 KB
/
Copy pathangular-api.js
File metadata and controls
156 lines (119 loc) · 5.45 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
** angular-api version: 1.0.0 (https://github.com/Shinomix/angular-api)
** Simple REST API service easy to use and customize supporting custom headers and default request params
** 17-05-2015 - by : Maxence Haltel <maxence.haltel@gmail.com>
*/
(function() {
angular.module('angular-api', [])
.service('AngularApi', ApiService)
.config(ConfigHttpHeader);
ConfigHttpHeader.$inject = ['$httpProvider'];
function ConfigHttpHeader($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.headers.post['Accept'] = 'application/json';
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json, charset=utf-8';
$httpProvider.defaults.headers.post['Access-Control-Max-Age'] = '1728000';
$httpProvider.defaults.header.common['Accept'] = 'application/json, application/text';
$httpProvider.defaults.header.common['Content-Type'] = 'application/json, charset=utf-8';
$httpProvider.defaults.header.common['Access-Control-Max-Age'] = '1728000';
$httpProvider.defaults.useXDomain = true;
}
Api.$inject = ['$http'];
function ApiService($http) {
var ApiServiceObject;
var _settings = {
url: 'localhost',
defaultRequestParams : {}
};
var _authorizedMethod = ['get', 'put', 'post', 'delete'];
var _requestPrototype = function(relativePath, method, data, isDefaultRequestParamsIncluded) {
var _absolutePath = settings.url;
var request = {
url: '',
method: ''
};
//execute request on baseUrl if relativePath is not of type string
if (typeof relativePath !== 'string')
relativePath = '';
//if relativePath start by a '|' (by default invalid URL character)
//consider relativePath is now absolutePath. otherwise append to baseUrl
if (relativePath.substring(0,1) === '|')
_absolutePath = relativePath.slice(1, path.length);
else
_absolutePath += relativePath;
request.url = _absolutePath;
if (authorizedMethod.indexOf(method) === -1)
return null;
request.method = method;
//if data is json object or alike, encode it as json string
if (typeof data === 'object' && data !== null) {
//if there is any default params, append data with it
if (settings.defaultRequestParams !== {} && isDefaultRequestParamsIncluded === true)
data = angular.extend({}, data, settings.defaultRequestParams);
data = JSON.stringify(data);
}
//otherwise if data is another type but not string, convert it to string
else if (typeof data !== 'string')
data = data.toString();
else
data = data;
//if method is get, send data as query string instead of json string
if (method === 'get')
request.params = data;
else
request.data = data;
//return completed requst object
return request;
};
function setApiUrl(newApiUrl) {
if (!newApiUrl || typeof newApiUrl !== 'string')
return null;
settings.url = newApiUrl;
return true;
}
function setDefaultRequestParams(newObject) {
if (newObject === null || typeof newObject !== 'object')
return null;
settings.defaultRequestParams = newObject;
}
function POST(route, data, enableDefaultRequestParams) {
if (typeof enableDefaultRequestParams === 'undefined')
enableDefaultRequestParams = true;
angular.extend(POST, {
request: new _requestPrototype(route, 'post', data, enableDefaultRequestParams)
});
return $http(POST.request);
}
function GET(route, data, enableDefaultRequestParams) {
if (typeof enableDefaultRequestParams === 'undefined')
enableDefaultRequestParams = true;
angular.extend(GET, {
request: new _requestPrototype(route, 'get', data, enableDefaultRequestParams)
});
return $http(GET.request);
}
function PUT(route, data, enableDefaultRequestParams) {
if (typeof enableDefaultRequestParams === 'undefined')
enableDefaultRequestParams = true;
angular.extend(PUT, {
request: new _requestPrototype(route, 'put', data, enableDefaultRequestParams)
});
return $http(PUT.request);
}
function DELETE(route, data, enableDefaultRequestParams) {
if (typeof enableDefaultRequestParams === 'undefined')
enableDefaultRequestParams = true;
angular.extend(DELETE, {
request: new _requestPrototype(route, 'delete', data, enableDefaultRequestParams)
});
return $http(DELETE.request);
}
ApiServiceObject.setApiUrl = setApiUrl;
ApiServiceObject.setDefaultRequestParams = setDefaultRequestParams;
ApiServiceObject.post = POST;
ApiServiceObject.get = GET;
ApiServiceObject.put = PUT;
ApiServiceObject.delete = DELETE;
return ApiServiceObject;
}
})();