///
///
import { PassThrough, Stream } from "stream";
type HttpMethod = "GET" | "POST" | "DELETE" | "PUT" | "PATCH" | "HEAD" | "OPTIONS" | "CONNECT" | "TRACE";
type StatusCode = number;
type BaseUrl = string;
interface Headers {
[key: string]: any;
}
// Type first
declare function bent(type: "string", ...args: bent.Options[]): bent.RequestFunction;
declare function bent(type: "buffer", ...args: bent.Options[]): bent.RequestFunction;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
declare function bent(type: "json", ...args: bent.Options[]): bent.RequestFunction;
// Method or url first
declare function bent(baseUrl: string, type: "string", ...args: bent.Options[]): bent.RequestFunction;
declare function bent(
baseUrl: string,
type: "buffer",
...args: bent.Options[]
): bent.RequestFunction;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
declare function bent(
baseUrl: string,
type: "json",
...args: bent.Options[]
): bent.RequestFunction;
declare function bent(baseUrl: string, ...args: bent.Options[]): bent.RequestFunction;
// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
declare function bent(...args: bent.Options[]): bent.RequestFunction;
// If we get some sort of custom nominal types, or regex literals, we might be able to simplify to something similar to
// declare function bent(...args: bent.Options[]): bent.RequestFunction;
// declare function bent(...args: (bent.Options | 'string')[]): bent.RequestFunction;
// declare function bent(...args: (bent.Options | 'buffer')[]): bent.RequestFunction;
// declare function bent(...args: (bent.Options | 'json')[]): bent.RequestFunction;
declare namespace bent {
type RequestFunction = (url: string, body?: RequestBody, headers?: Headers) => Promise;
type Options = HttpMethod | StatusCode | Headers | BaseUrl;
type RequestBody = string | Stream | Buffer | ArrayBuffer | Json;
type NodeResponse = PassThrough & {
statusCode: number;
statusMessage: string;
headers: Headers;
arrayBuffer(): Promise;
text(): Promise;
json(): Promise;
};
type FetchResponse = Response & { statusCode: number };
type BentResponse = NodeResponse | FetchResponse;
type Json = { [key: string]: any; [key: number]: any } | any[];
type ValidResponse = BentResponse | string | Buffer | ArrayBuffer | Json;
class StatusError extends Error {
statusCode: number;
arrayBuffer(): Promise;
text(): Promise;
json(): Promise;
responseBody: Promise;
headers: { [key: string]: any };
}
}
export = bent;