-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-adapter.js
57 lines (47 loc) · 1.68 KB
/
angular-adapter.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
/*! theon-angular-adapter - v0.1.0 - MIT License - https://github.com/theonjs/theon-angular-adapter */
angular.module('theon.adapter', [])
.factory('$theonAdapter', ['$http', '$theonResponseAdapter', function ($http, adapter) {
var httpAgent = $http
function $httpAdapter (req, res, done) {
var opts = {
url: req.url,
method: req.method,
auth: req.opts.auth,
params: req.query,
headers: req.headers,
data: req.body,
cache: req.agentOpts.cache,
timeout: +req.opts.timeout || +req.agentOpts.timeout,
withCredentials: req.agentOpts.withCredentials,
xsrfHeaderName: req.agentOpts.xsrfHeaderName,
xsrfCookieName: req.agentOpts.xsrfCookieName,
transformRequest: req.agentOpts.transformRequest,
transformResponse: req.agentOpts.transformResponse,
paramSerializer: req.agentOpts.paramSerializer,
responseType: req.agentOpts.responseType
}
httpAgent(opts).then(function (_res) {
done(null, adapter(res, _res))
}, function (err) {
done(adapter(res, err))
})
}
$httpAdapter.setAgent = function (agent) {
httpAgent = agent
}
return $httpAdapter
}])
.factory('$theonResponseAdapter', ['$q', function ($q) {
return function responseAdapter (res, _res) {
if (!_res) return res
// Expose the agent-specific response
res.setOriginalResponse(_res)
// Define recurrent HTTP fields
res.setStatus(_res.status)
res.setStatusText(_res.statusText)
res.setHeaders(_res.headers)
// Define body, if present
if (_res.data) res.setBody(_res.data)
return res
}
}])