impit
    Preparing search index...

    Class ImpitResponse

    Represents an HTTP response.

    The ImpitResponse class provides access to the response status, headers, and body. It also includes methods to read the response body in various formats such as text, JSON, ArrayBuffer, and as a stream.

    This class is designed to be API-compatible with the Fetch API Response class.

    Index

    Properties

    headers: Headers

    HTTP headers of the response.

    An instance of the Headers class.

    ok: boolean

    true if the response status code is in the range 200-299.

    status: number

    HTTP status code of the response.

    Example: 200 for a successful response.

    statusText: string

    Status text of the response.

    A short description of the status code.

    Example: "OK" for status code 200.

    url: string

    URL of the response.

    In case of redirects, this will be the final URL after all redirects have been followed.

    Accessors

    • get body(): ReadableStream<Uint8Array<ArrayBufferLike>>

      Returns the response body as a ReadableStream.

      This property provides access to the response body as a stream of data, allowing you to read it in chunks.

      Returns ReadableStream<Uint8Array<ArrayBufferLike>>

      const response = await impit.fetch('https://example.com');
      const reader = response.body.getReader();

      let result;
      while (!(result = await reader.read()).done) {
      console.log(result.value); // Uint8Array chunk
      }

    Methods

    • Returns the response body as an ArrayBuffer.

      This method is asynchronous and returns a promise that resolves to an ArrayBuffer containing the response body data.

      Returns Promise<ArrayBuffer>

      const response = await impit.fetch('https://example.com');
      const arrayBuffer = await response.arrayBuffer();

      console.log(arrayBuffer); // ArrayBuffer([ 0x3c, 0x68, 0x74, 0x6d, 0x6c, ... ])

      Note that you cannot call this method multiple times on the same response instance, as the response body can only be consumed once. Subsequent calls will result in an error.

    • Returns the response body as a Uint8Array.

      This method is asynchronous and returns a promise that resolves to a Uint8Array containing the response body data.

      Returns Promise<Uint8Array<ArrayBufferLike>>

      const response = await impit.fetch('https://example.com');
      const uint8Array = await response.bytes();

      console.log(uint8Array); // Uint8Array([ 0x3c, 0x68, 0x74, 0x6d, 0x6c, ... ])

      Note that you cannot call this method multiple times on the same response instance, as the response body can only be consumed once. Subsequent calls will result in an error.

    • Parses the response body as JSON.

      This method is asynchronous and returns a promise that resolves to the parsed JSON object.

      Returns Promise<any>

      const response = await impit.fetch('https://api.example.com/data');
      const data = await response.json();

      console.log(data); // Parsed JSON object
    • Returns the response body as a string.

      This method is asynchronous and returns a promise that resolves to a string containing the response body data.

      Returns Promise<string>

      const response = await impit.fetch('https://example.com');
      const text = await response.text();

      console.log(text); // "<!doctype html><html>...</html>"