UNPKG

@btfuse/core

Version:

A native-first framework for building hybdrid web-native applications

62 lines (59 loc) 1.98 kB
"use strict"; /* 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.FuseResponseReader = void 0; /** * A static class with convenience methods for reading common * response content body formats. */ class FuseResponseReader { constructor() { } /** * @remarks * Reads the data buffer as a string * * @param data - input data * @returns The buffer contents as a string */ static async readAsText(data) { return await new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { resolve(reader.result); }; reader.onerror = () => { reject(reader.error); }; reader.readAsText(new Blob([data])); }); } /** * @remarks * Reads the given data buffer as a JSON object. The JSON object * can be typed as T generic. No validations occurs on whether the given * data is actually a type of T. * * @throws {@link SyntaxError} * If data is not parseable as JSON. * * @param data - input data * @returns The buffer contents as a JSON object. */ static async readAsJSON(data) { const str = await this.readAsText(data); return JSON.parse(str); } } exports.FuseResponseReader = FuseResponseReader; //# sourceMappingURL=FuseResponseReader.js.map