-
Notifications
You must be signed in to change notification settings - Fork 33
/
types.d.ts
122 lines (99 loc) · 3.44 KB
/
types.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Default Options feature is not supported because it's basically impossible to write strongly-typed definitions for it.
import * as http from 'http'
import { URL } from 'url';
interface IOptionsBase {
url: string | URL
method?: string
headers?: object
core?: http.ClientRequestArgs
followRedirects?: boolean
stream?: boolean
compression?: boolean
timeout?: number
hostname?: string
port?: number
path?: string
}
declare function phin<T>(options:
phin.IJSONResponseOptions |
phin.IWithData<phin.IJSONResponseOptions> |
phin.IWithForm<phin.IJSONResponseOptions>): Promise<phin.IJSONResponse<T>>
declare function phin(options:
phin.IStringResponseOptions |
phin.IWithData<phin.IStringResponseOptions> |
phin.IWithForm<phin.IStringResponseOptions>): Promise<phin.IStringResponse>
declare function phin(options:
phin.IStreamResponseOptions |
phin.IWithData<phin.IStreamResponseOptions> |
phin.IWithForm<phin.IStreamResponseOptions>): Promise<phin.IStreamResponse>
declare function phin(options:
phin.IOptions |
phin.IWithData<phin.IOptions> |
phin.IWithForm<phin.IOptions> |
string): Promise<phin.IResponse>
declare namespace phin {
// Form and data property has been written this way so they're mutually exclusive.
export type IWithData<T extends IOptionsBase> = T & {
data: string | Buffer | object;
}
export type IWithForm<T extends IOptionsBase> = T & {
form: {
[index: string]: string
}
}
export interface IJSONResponseOptions extends IOptionsBase {
parse: 'json'
}
export interface IStringResponseOptions extends IOptionsBase {
parse: 'string';
}
export interface IStreamResponseOptions extends IOptionsBase {
stream: true
}
export interface IOptions extends IOptionsBase {
parse?: 'none'
}
export interface IJSONResponse<T> extends http.IncomingMessage {
body: T
}
export interface IStringResponse extends http.IncomingMessage {
body: string;
}
export interface IStreamResponse extends http.IncomingMessage {
stream: http.IncomingMessage
}
export interface IResponse extends http.IncomingMessage {
body: Buffer;
}
// NOTE: Typescript cannot infer type of union callback on the consumer side
// https://github.com/Microsoft/TypeScript/pull/17819#issuecomment-363636904
type IErrorCallback = (error: Error | string, response: null) => void
type ICallback<T> = (error: null, response: NonNullable<T>) => void
export let promisified: typeof phin
export function unpromisified<T>(
options:
IJSONResponseOptions |
IWithData<IJSONResponseOptions> |
IWithForm<IJSONResponseOptions>,
callback: IErrorCallback | ICallback<IJSONResponse<T>>): void
export function unpromisified(
options:
IStringResponseOptions |
IWithData<IStringResponseOptions> |
IWithForm<IStringResponseOptions>,
callback: IErrorCallback | ICallback<IStringResponse>): void
export function unpromisified(
options:
IStreamResponseOptions |
IWithData<IStreamResponseOptions> |
IWithForm<IStreamResponseOptions>,
callback: IErrorCallback | ICallback<IStreamResponse>): void
export function unpromisified(
options:
IOptions |
IWithData<IOptions> |
IWithForm<IOptions> |
string,
callback: IErrorCallback | ICallback<IResponse>): void
}
export = phin