-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
91 lines (82 loc) · 2.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// <reference types="node" />
import { URL } from 'url';
import { SecureContextOptions } from 'tls';
import { IncomingMessage, Agent, IncomingHttpHeaders } from 'http';
import { Readable, Duplex } from 'stream';
declare type TlsOptions = SecureContextOptions & {
rejectUnauthorized?: boolean; // Defaults to true
servername?: string; // SNI TLS Extension
};
declare type Uri = string | URL;
declare type Dictionary<T> = Partial<Record<string, T>>;
export declare type RequestOptions = {
method?: string;
uri: Uri;
followAllRedirects?: boolean;
maxRedirects?: number;
headers?: Dictionary<string | string[]>;
auth?: {
user: string;
pass: string;
};
// Request Body body
body?: Buffer | string | Object;
form?: Dictionary<any>;
qs?: Dictionary<any>;
query?: Dictionary<any>;
formData?: Dictionary<string | Buffer | Readable>;
decompress?: boolean;
agent?: Agent | false;
proxy?: Uri | { uri: Uri; tls?: TlsOptions };
rejectError?: boolean;
raw?: boolean;
tls?: TlsOptions;
simple?: boolean;
path?: string;
};
type PartialRequestOptions = Partial<RequestOptions>;
type Simple = { simple: true };
type WithUri = { uri: Uri };
export declare class CancelError extends Error {}
export declare class HttpError extends Error {
statusCode: number;
headers: IncomingHttpHeaders;
body: any;
}
export declare type ResolvedResponse = IncomingMessage & {
body: any;
redirects?: string[];
};
export declare type Connection<T> = Promise<T extends Simple ? any : ResolvedResponse> &
Duplex & { cancel: () => void; isCancelled: boolean };
export declare type RequestFunction = {
<T extends RequestOptions>(options: T): Connection<T>;
<T extends Uri>(uri: T): Connection<{ uri: typeof uri }>;
<T extends PartialRequestOptions>(uri: string | URL, options: T): Connection<T>;
};
type Eval<T> = { [Key in keyof T]: T[Key] } & {};
type Override<A, B> = Eval<Omit<A, Extract<keyof A, keyof B>> & B>;
type DefaultedRequestFunction<D extends PartialRequestOptions> = {
<T extends Uri>(uri: T): Connection<Override<D, { uri: typeof uri }>>;
<T extends D extends WithUri ? PartialRequestOptions : RequestOptions>(options: T): Connection<Override<D, T>>;
<T extends PartialRequestOptions>(uri: string | URL, options: T): Connection<Override<D, T>>;
};
export declare const beggar: RequestFunction & {
get: RequestFunction;
post: RequestFunction;
put: RequestFunction;
patch: RequestFunction;
head: RequestFunction;
delete: RequestFunction;
defaults: <T extends PartialRequestOptions>(
options: T
) => DefaultedRequestFunction<T> & {
get: DefaultedRequestFunction<T>;
post: DefaultedRequestFunction<T>;
put: DefaultedRequestFunction<T>;
patch: DefaultedRequestFunction<T>;
head: DefaultedRequestFunction<T>;
delete: DefaultedRequestFunction<T>;
};
};
export {};