@btfuse/core
Version:
A native-first framework for building hybdrid web-native applications
84 lines (81 loc) • 2.73 kB
JavaScript
;
/*
Copyright 2023 Breautek
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FuseAPIResponse = void 0;
const FuseResponseReader_1 = require("./FuseResponseReader");
const FuseError_1 = require("./FuseError");
class FuseAPIResponse {
constructor(content, headers, status) {
this.$status = status;
this.$content = content;
this.$headers = this.$parseHeaders(headers);
}
isError() {
return this.$status >= 400;
}
getContentLength() {
var _a;
const lenStr = (_a = this.$headers.get('content-type')) === null || _a === void 0 ? void 0 : _a[0];
let length = parseInt(lenStr);
if (isNaN(length)) {
length = 0;
}
return length;
}
getContentType() {
var _a;
return (_a = this.$headers.get('content-type')) === null || _a === void 0 ? void 0 : _a[0];
}
async readAsArrayBuffer() {
return this.$content;
}
async readAsBlob() {
return new Blob([this.$content]);
}
async readAsText() {
return await FuseResponseReader_1.FuseResponseReader.readAsText(this.$content);
}
async readAsJSON() {
return await FuseResponseReader_1.FuseResponseReader.readAsJSON(this.$content);
}
async readAsError() {
const serializedError = await FuseResponseReader_1.FuseResponseReader.readAsJSON(this.$content);
return FuseError_1.FuseError.fromSerialized(serializedError);
}
getHeaders() {
return this.$headers;
}
getHeader(key) {
return this.$headers.get(key);
}
$parseHeaders(headers) {
const map = new Map();
if (!headers) {
return map;
}
const lines = headers.split('\r\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].split(':');
const key = line[0];
if (!map.has(key)) {
map.set(key, []);
}
const headerValue = map.get(key);
headerValue.push(line[1]);
}
return map;
}
}
exports.FuseAPIResponse = FuseAPIResponse;
//# sourceMappingURL=FuseAPIResponse.js.map