@platform/http
Version:
Tools for working with HTTP.
125 lines (124 loc) • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetcher = void 0;
var tslib_1 = require("tslib");
var cross_fetch_1 = require("cross-fetch");
var common_1 = require("../common");
var fetcher = function (args) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var timer, tx, url, method, data, fire, mode, modifications, modify, before, respond, payload, _a, response, elapsed, ok, status_1, done, headers, fetched, error_1, statusText, fetched;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
timer = common_1.time.timer();
tx = "request-".concat((0, common_1.slug)());
url = args.url, method = args.method, data = args.data, fire = args.fire, mode = args.mode;
modifications = {
headers: args.headers || {},
data: undefined,
respond: undefined,
};
modify = {
header: function (key, value) {
before.isModified = true;
var headers = modifications.headers || {};
if (value) {
headers[key] = value;
}
else {
delete headers[key];
}
modifications.headers = headers;
},
headers: {
merge: function (input) {
before.isModified = true;
modifications.headers = common_1.value.deleteEmpty(tslib_1.__assign(tslib_1.__assign({}, modifications.headers), input));
},
replace: function (input) {
before.isModified = true;
modifications.headers = common_1.value.deleteEmpty(input);
},
},
};
before = {
tx: tx,
method: method,
url: url,
data: data,
headers: args.headers,
isModified: false,
modify: modify,
respond: function (input) {
modifications.respond = input;
},
};
fire({ type: 'HTTP/method:req', payload: before });
if (!modifications.respond) return [3, 5];
respond = modifications.respond;
if (!(typeof respond === 'function')) return [3, 2];
return [4, respond()];
case 1:
_a = _b.sent();
return [3, 3];
case 2:
_a = respond;
_b.label = 3;
case 3:
payload = _a;
return [4, common_1.util.response.fromPayload(payload, modifications)];
case 4:
response = _b.sent();
elapsed = timer.elapsed.msec;
ok = response.ok, status_1 = response.status;
fire({
type: 'HTTP/method:res',
payload: { tx: tx, method: method, url: url, ok: ok, status: status_1, response: response, elapsed: elapsed },
});
return [2, response];
case 5:
done = function (fetched) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
var response, elapsed, ok, status;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, common_1.util.response.fromFetch(fetched)];
case 1:
response = _a.sent();
elapsed = timer.elapsed.msec;
ok = response.ok, status = response.status;
fire({
type: 'HTTP/method:res',
payload: { tx: tx, method: method, url: url, ok: ok, status: status, response: response, elapsed: elapsed },
});
return [2, response];
}
});
}); };
headers = modifications.headers || args.headers;
_b.label = 6;
case 6:
_b.trys.push([6, 8, , 9]);
return [4, args.fetch({ url: url, mode: mode, method: method, headers: headers, data: data })];
case 7:
fetched = _b.sent();
return [2, done(fetched)];
case 8:
error_1 = _b.sent();
statusText = error_1.message.replace(/^TypeError:/, '').trim();
fetched = {
status: 0,
statusText: statusText,
headers: new cross_fetch_1.Headers(headers),
body: null,
text: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, ''];
}); }); },
json: function () { return tslib_1.__awaiter(void 0, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) {
return [2, undefined];
}); }); },
};
return [2, done(fetched)];
case 9: return [2];
}
});
}); };
exports.fetcher = fetcher;