UNPKG

@push.rocks/smartrequest

Version:

A module for modern HTTP/HTTPS requests with support for form data, file uploads, JSON, binary data, streams, and more.

45 lines (38 loc) 1.04 kB
import * as types from './types.js'; /** * Abstract Core Response class that provides a fetch-like API */ export abstract class CoreResponse<T = any> implements types.ICoreResponse<T> { protected consumed = false; // Public properties public abstract readonly ok: boolean; public abstract readonly status: number; public abstract readonly statusText: string; public abstract readonly headers: types.Headers; public abstract readonly url: string; /** * Ensures the body can only be consumed once */ protected ensureNotConsumed(): void { if (this.consumed) { throw new Error('Body has already been consumed'); } this.consumed = true; } /** * Parse response as JSON */ abstract json(): Promise<T>; /** * Get response as text */ abstract text(): Promise<string>; /** * Get response as ArrayBuffer */ abstract arrayBuffer(): Promise<ArrayBuffer>; /** * Get response as a web-style ReadableStream */ abstract stream(): ReadableStream<Uint8Array> | null; }