-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-demock.js
134 lines (116 loc) · 4.91 KB
/
angular-demock.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
125
126
127
128
129
130
131
132
133
134
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([ 'angular', 'demock' ], factory);
} else if (typeof exports === 'object') {
factory(require('angular'), require('demock'));
} else {
factory(root.angular, root.demock);
}
}(this, function (angular, Demock) {
function normalizeParams(config) {
if (config.params) {
return config.params;
} else if (config.data) {
if (typeof config.data === 'object') {
return config.data;
} else if (typeof config.data === 'string') {
try {
return JSON.parse(config.data);
} catch (e) {}
}
}
}
angular.module('demock', [])
.provider('$demock', function () {
var demock = new Demock(),
requestFilters = [],
responseFilters = [],
loggerFactory;
this.requestFilters = Demock.requestFilters;
this.responseFilters = Demock.responseFilters;
this.appendRequestFilter = function (factory, config) {
requestFilters.push({
factory: factory,
config: config
});
return this;
};
this.appendResponseFilter = function (factory, config) {
responseFilters.push({
factory: factory,
config: config
});
return this;
};
this.setLogger = function (factory) {
loggerFactory = factory;
return this;
};
this.$get = [ '$injector', function ($injector) {
requestFilters.forEach(function (filter) {
demock.appendRequestFilter($injector.invoke(filter.factory, null, { config: filter.config }));
});
responseFilters.forEach(function (filter) {
demock.appendResponseFilter($injector.invoke(filter.factory, null, { config: filter.config }));
});
if (loggerFactory) {
demock.setLogger($injector.invoke(loggerFactory));
}
return demock;
}];
})
.config([ '$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push([ '$q', '$window', '$demock', function ($q, $window, $demock) {
return {
request: function (config) {
var request = {
method: config.method,
url: config.url,
params: normalizeParams(config),
headers: {}
};
$demock.filterRequest(request);
config.method = request.method;
config.url = request.url;
config.headers = request.headers;
config.__demockRequest = request;
return config;
},
response: function (_response) {
var request = _response.config.__demockRequest,
response = {
statusCode: _response.status,
data: _response.data
},
dfd = $q.defer();
$demock.filterResponse(request, response);
function resolve() {
_response.status = response.statusCode;
_response.data = response.data;
if (response.timeout) {
_response.status = 0;
dfd.reject(_response);
} else if (response.statusCode >= 400 && response.statusCode < 600) {
dfd.reject(_response);
} else {
dfd.resolve(_response);
}
}
if (response.delay) {
var timeout = $window.setTimeout(resolve, response.delay);
// @todo needs testing
if (_response.config.timeout && _response.config.timeout.then) {
_response.config.timeout.then(function () {
$window.clearTimeout(timeout);
});
}
return dfd.promise;
} else {
resolve();
return dfd.promise;
}
}
};
}]);
}]);
}));