-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.js
124 lines (107 loc) · 3.88 KB
/
rest.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
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
angular.module('rest', [])
.factory('rest', ['$http', '$q', function($http, $q) {
var _DS_ = '/',
_SP_ = '@',
extend = angular.extend,
copy = angular.copy,
forEach = angular.forEach,
isObject = angular.isObject,
isString = angular.isString,
isFunction = angular.isFunction,
noop = angular.noop,
fromJson = angular.fromJson,
toJson = angular.toJson;
var _methods = {
post: {
method: 'POST',
},
get: {
method: 'GET',
},
put: {
method: 'PUT',
},
delete: {
method: 'DELETE',
}
};
function _mergeConfigs(target, source) {
if (source == undefined)
return target;
var configs = extend({}, target);
forEach(source, function(config, name) {
if (isObject(config))
configs[name] = extend({}, target[name], config);
else
configs[name] = config;
});
return configs;
}
function _formatData(object) {
var data = {};
forEach(object, function(value, property) {
if(!isFunction(value))
data[property] = value;
});
return data;
}
return function (url, defaultConfigs, methods) {
var urlParams = url.split(_SP_);
defaultConfigs = extend({url: urlParams.shift()}, defaultConfigs);
forEach(_methods, function(_methodConfigs, name) {
if (methods[name])
methods[name] = _mergeConfigs(methods[name], _methodConfigs);
else if(methods[name] != false)
methods[name] = _methodConfigs;
else
delete methods[name];
});
function RESTObject(properties) {
var _this = this;
forEach(properties, function(value, property) {
_this[property] = value;
});
}
var prototype = RESTObject.prototype,
refMethods = {};
forEach(methods, function(methodConfigs, name) {
if (isString(methodConfigs)) {
refMethods[name] = methodConfigs;
} else if (isFunction(methodConfigs)) {
prototype[name] = methodConfigs;
} else {
var _before = methodConfigs.before || noop,
_after = methodConfigs.after || noop,
_data = (/^(POST|PUT)$/i.test((methodConfigs.method))) ? 'data' : 'params';
prototype[name] = function(data) {
var _this = this,
deferred = $q.defer(),
httpConfigs = copy(_mergeConfigs(defaultConfigs, methodConfigs));
extend(_this, data);
httpConfigs[_data] = _formatData(_this);//fromJson(toJson(_this));
forEach(urlParams, function(property) {
if (_this[property])
httpConfigs.url += _this[property];
_this[property] += _DS_;
});
if(_before.bind(_this)(httpConfigs) == false) {
deferred.reject(_this);
} else {
$http(httpConfigs)
.success(function(res) {
extend(_this, res);
deferred.resolve(_after.bind(_this)(res) || _this);
}).error(function(res) {
deferred.reject(res);
});
}
return deferred.promise;
}
}
});
forEach(refMethods, function(methodConfigs, name) {
prototype[name] = prototype[methodConfigs];
});
return RESTObject;
}
}]);