@platform/http
Version:
Tools for working with HTTP.
204 lines (203 loc) • 8.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.response = exports.isStream = exports.isFormData = exports.headerValue = exports.fromRawHeaders = exports.toRawHeaders = exports.parseJson = exports.stringify = void 0;
var tslib_1 = require("tslib");
var constants_1 = require("./constants");
var common_1 = require("../common");
function stringify(data, errorMessage) {
try {
return data ? JSON.stringify(data) : '';
}
catch (err) {
var message = errorMessage();
message = !constants_1.IS_PROD ? "".concat(message, " ").concat(err.message) : message;
throw new Error(message);
}
}
exports.stringify = stringify;
function parseJson(args) {
var text = args.text;
try {
return (typeof text === 'string' && common_1.value.isJson(args.text) ? JSON.parse(text) : text);
}
catch (error) {
var body = text ? text : '<empty>';
var msg = "Failed while parsing JSON for '".concat(args.url, "'.\nParse Error: ").concat(error.message, "\nBody: ").concat(body);
throw new Error(msg);
}
}
exports.parseJson = parseJson;
function toRawHeaders(input) {
var obj = tslib_1.__assign({}, (input || {}));
Object.keys(obj).forEach(function (key) {
obj[key] = obj[key].toString();
});
return new common_1.Headers(obj);
}
exports.toRawHeaders = toRawHeaders;
function fromRawHeaders(input) {
var hasEntries = typeof input.entries === 'function';
var obj = hasEntries
? walkHeaderEntries(input)
: (input || {})._headers || {};
return Object.keys(obj).reduce(function (acc, key) {
var value = Array.isArray(obj[key]) ? obj[key][0] : obj[key];
acc[key] = common_1.value.toType(value);
return acc;
}, {});
}
exports.fromRawHeaders = fromRawHeaders;
var walkHeaderEntries = function (input) {
var res = {};
var entries = input.entries();
var next;
do {
next = entries.next();
if (next.value) {
var _a = next.value, key = _a[0], value = _a[1];
res[key] = value;
}
} while (!(next === null || next === void 0 ? void 0 : next.done));
return res;
};
function headerValue(key, headers) {
if (headers === void 0) { headers = {}; }
key = key.trim().toLowerCase();
var match = Object.keys(headers)
.filter(function (k) { return k.trim().toLowerCase() === key; })
.find(function (k) { return headers[k]; }) || '';
return match ? headers[match] : '';
}
exports.headerValue = headerValue;
function isFormData(headers) {
if (headers === void 0) { headers = {}; }
var contentType = headerValue('content-type', headers);
return contentType.includes('multipart/form-data');
}
exports.isFormData = isFormData;
function isStream(value) {
var stream = value;
return typeof (stream === null || stream === void 0 ? void 0 : stream.pipe) === 'function';
}
exports.isStream = isStream;
exports.response = {
fromPayload: function (payload, modifications) {
if (modifications === void 0) { modifications = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var status, _a, statusText, data, head, contentType, isBinary, toText, toJson, text, json, res;
return tslib_1.__generator(this, function (_b) {
status = payload.status, _a = payload.statusText, statusText = _a === void 0 ? '' : _a;
data = payload.data || modifications.data;
head = payload.headers || modifications.headers || {};
if (data && !headerValue('content-type', head)) {
head = tslib_1.__assign(tslib_1.__assign({}, head), { 'content-type': isStream(data) ? 'application/octet-stream' : 'application/json' });
}
contentType = headerValue('content-type', head);
isBinary = common_1.Mime.isBinary(contentType);
toText = function (data) {
if (!data) {
return '';
}
if (typeof data === 'string') {
return data;
}
return stringify(data, function () { return "Failed while serializing data to JSON within [text] method."; });
};
toJson = function (data) {
return data && !isBinary ? data : '';
};
text = '';
json = '';
res = {
status: status,
statusText: statusText,
headers: toRawHeaders(head),
body: isBinary ? data : null,
text: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2, text || (text = toText(data))];
});
});
},
json: function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
return [2, json || (json = toJson(data))];
});
});
},
};
return [2, exports.response.fromFetch(res)];
});
});
},
fromFetch: function (res) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var status, ok, body, statusText, headers, contentType, text, _a, json, _b, result;
return tslib_1.__generator(this, function (_c) {
switch (_c.label) {
case 0:
status = res.status;
ok = status.toString()[0] === '2';
body = res.body || undefined;
statusText = res.statusText ? res.statusText : ok ? 'OK' : '';
headers = fromRawHeaders(res.headers);
contentType = exports.response.toContentType(headers);
if (!contentType.is.text) return [3, 2];
return [4, res.text()];
case 1:
_a = _c.sent();
return [3, 3];
case 2:
_a = '';
_c.label = 3;
case 3:
text = _a;
if (!contentType.is.json) return [3, 5];
return [4, res.json()];
case 4:
_b = _c.sent();
return [3, 6];
case 5:
_b = '';
_c.label = 6;
case 6:
json = _b;
result = {
ok: ok,
status: status,
statusText: statusText,
headers: headers,
contentType: contentType,
body: body,
text: text,
json: json,
};
return [2, result];
}
});
});
},
toContentType: function (headers) {
var mime = headerValue('content-type', headers);
var res = {
mime: mime,
is: {
get json() {
return common_1.Mime.isJson(mime);
},
get text() {
return common_1.Mime.isText(mime);
},
get binary() {
return common_1.Mime.isBinary(mime);
},
},
toString: function () {
return mime;
},
};
return res;
},
};