-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (32 loc) · 985 Bytes
/
index.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
'use strict';
module.exports = {
"handle": function(request, assigner){
var params = Object.assign({}, request, {
headers: new Headers(request.headers)
});
return new Promise(function(resolve, reject){
fetch(new Request(params.uri, params))
.then(function(response){
response.text().then(function(text){
resolve(assigner(formatResponse(response, text)));
}
)
})
.catch(function(response){
reject(assigner(formatResponse(response)));
});
});
}
};
function formatResponse(response, text){
var result = {
"headers": {},
"status": {
"code": response.status,
"message": response.statusText
},
"body": text
};
response.headers.forEach( function(value, key) { result.headers[key] = value;} );
return result;
}