UNPKG

@clickup/rest-client

Version:

A syntax sugar tool around Node fetch() API, tailored to work with TypeScript and response validators

81 lines 2.96 kB
/// <reference types="node" /> /// <reference types="node" /> import type { Agent as HttpAgent } from "http"; import type { RequestInit } from "node-fetch"; import fetch from "node-fetch"; export interface RestFetchReaderOptions { timeoutMs?: number; heartbeat?: () => Promise<void>; onTimeout?: (reader: RestFetchReader, e: any) => void; onAfterRead?: (reader: RestFetchReader) => void; responseEncoding?: NodeJS.BufferEncoding; } /** * A low-level stateful reader engine on top of node-fetch which implements * "preload first N chars and then leave the rest ready for iteration" pattern, * with global timeout for the entire fetching time. * * Once created, the object MUST be iterated in full to consume the rest of the * stream and close the connection. In case you're not interested in its entire * content, you must prematurely "return" (close) the iterator. * * The abstraction is intentionally kept independent on all others, to make it * simple and testable separately. */ export default class RestFetchReader { private _url; private _reqInit; private _options; private _status; private _headers; private _textFetched; private _textIsPartial; private _charsRead; constructor(_url: string, _reqInit: RequestInit, _options: RestFetchReaderOptions); /** * Returns the number of characters read from the stream so far. */ get charsRead(): number; /** * Returns the Agent instance used for this request. It's implied that * RestRequest#agent always points to a http.Agent object. */ get agent(): HttpAgent | null; /** * Returns HTTP status after preload() was called. */ get status(): number; /** * Returns HTTP headers after preload() was called. */ get headers(): fetch.Headers; /** * Returns the data preloaded so far. */ get textFetched(): string; /** * If true, then there is a chance that reading more from the stream will * return more data. */ get textIsPartial(): boolean; /** * Reads preloadChars chars or a little bit more from the response and puts * them to this.textFetched. Leaves the rest of the data in res.body for * future reads if there are more data to fetch (you must consume them or * close the stream, otherwise the connection will remain open). */ preload(preloadChars: number): Promise<void>; /** * Closes the connection. */ close(): Promise<void>; /** * Returns an async generator for the rest of the data. Must be consumed * entirely, otherwise the connection may remain dangling. * * Memoization is important here, to return the same generator when we call * this method multiple times and to not start a new iteration over and over. */ [Symbol.asyncIterator](): AsyncGenerator<string, void, unknown>; } //# sourceMappingURL=RestFetchReader.d.ts.map