@typed/http
Version:
HTTP requests for node and browsers
41 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFetchHttpEnv = void 0;
const disposable_1 = require("@typed/disposable");
const withHttpManagement_1 = require("./withHttpManagement");
function createFetchHttpEnv(options) {
const env = { http };
return options ? withHttpManagement_1.withHttpManagement(options, env) : env;
}
exports.createFetchHttpEnv = createFetchHttpEnv;
function http(url, options, callbacks) {
const { success, failure } = callbacks;
const { method = 'GET', headers = {}, body } = options;
const disposable = disposable_1.Disposable.lazy();
const abortController = new AbortController();
disposable.addDisposable({
dispose: () => abortController.abort(),
});
const init = {
method,
headers: Object.entries(headers).map(([key, value = '']) => [key, value]),
body,
signal: abortController.signal,
};
const request = fetch(url, init);
request.then((response) => response.text().then((responseText) => {
const headers = {};
response.headers.forEach((value, key) => {
headers[key] = value;
});
const httpResponse = {
status: response.status,
statusText: response.statusText,
responseText,
headers,
};
return disposable.addDisposable(success(httpResponse));
}), (error) => disposable.addDisposable(failure(error)));
return disposable;
}
//# sourceMappingURL=createFetchHttpEnv.js.map