UNPKG

@clickup/rest-client

Version:

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

81 lines 3.29 kB
/// <reference types="node" /> /// <reference types="node" /> import { Headers } from "node-fetch"; import type RestOptions from "./RestOptions"; import RestResponse from "./RestResponse"; import RestStream from "./RestStream"; /** * Type TAssertShape allows to limit json()'s assert callbacks to only those * which return an object compatible with TAssertShape. */ export default class RestRequest<TAssertShape = any> { readonly method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; url: string; readonly headers: Headers; readonly body: string | Buffer | NodeJS.ReadableStream; readonly shape?: string | undefined; readonly options: RestOptions; constructor(options: RestOptions, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", url: string, headers: Headers, body: string | Buffer | NodeJS.ReadableStream, shape?: string | undefined); /** * Modifies the request by adding a custom HTTP header. */ setHeader(name: string, value: string): this; /** * Modifies the request by adding a custom request option. */ setOptions(options: Partial<RestOptions>): this; /** * Forces RestClient to debug-output the request and response to console. * Never use in production. */ setDebug(flag?: boolean): this; /** * Sends the request and reads the response a JSON. In absolute most of the * cases, this method is used to reach API responses. The assert callback * (typically generated by typescript-is) is intentionally made mandatory to * not let people to do anti-patterns. */ json<TJson extends TAssertShape>(assert: ((obj: any) => TJson) | { mask(obj: any): TJson; } | { $assert(obj: any): TJson; }, ...checkers: Array<(json: TJson, res: RestResponse) => false | Error>): Promise<TJson>; /** * Sends the request and returns plaintext response. */ text(): Promise<string>; /** * Returns the entire RestResponse object with response status and headers * information in it. Try to minimize usage of this method, because it doesn't * make any assumptions on the response structure. */ response(): Promise<RestResponse>; /** * Sends the requests and returns RestStream object. You MUST iterate over * this object entirely (or call its return() method), otherwise the * connection will remain dangling. */ stream(preloadChars?: number): Promise<RestStream>; /** * We can actually only create RequestInit. We can't create an instance of * node-fetch.Request object since it doesn't allow injection of * AbortController later, and we don't want to deal with AbortController here. */ private _createFetchRequest; /** * Creates an instance of RestFetchReader. */ private _createFetchReader; /** * Creates a RestResponse from a RestFetchReader. Assumes that * RestFetchReader.preload() has already been called. */ private _createRestResponse; /** * Logs a response event, an error event or a backoff event. If RestResponse * is not yet known (e.g. an exception happened in a DNS resolution), res must * be passed as null. */ private _logResponse; } //# sourceMappingURL=RestRequest.d.ts.map