acme.js/lib/browser/http.js

34 lines
682 B
JavaScript
Raw Permalink Normal View History

2019-10-04 23:35:59 +00:00
'use strict';
var http = module.exports;
2020-07-28 21:53:50 +00:00
http.request = function (opts) {
2019-10-25 00:49:42 +00:00
opts.cors = true;
2020-07-28 21:53:50 +00:00
return window.fetch(opts.url, opts).then(function (resp) {
2019-10-04 23:35:59 +00:00
var headers = {};
var result = {
statusCode: resp.status,
headers: headers,
2020-07-28 21:53:50 +00:00
toJSON: function () {
2019-10-04 23:35:59 +00:00
return this;
}
};
2020-07-28 21:53:50 +00:00
Array.from(resp.headers.entries()).forEach(function (h) {
2019-10-04 23:35:59 +00:00
headers[h[0]] = h[1];
});
if (!headers['content-type']) {
return result;
}
if (/json/.test(headers['content-type'])) {
2020-07-28 21:53:50 +00:00
return resp.json().then(function (json) {
2019-10-04 23:35:59 +00:00
result.body = json;
return result;
});
}
2020-07-28 21:53:50 +00:00
return resp.text().then(function (txt) {
2019-10-04 23:35:59 +00:00
result.body = txt;
return result;
});
});
};