forked from ethanent/phin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.d.ts
103 lines (84 loc) · 2.73 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
// Default Options feature is not supported because it's basically impossible to write strongly-typed definitions for it.
import * as http from 'http'
interface IOptionsBase {
url: string
method?: string
headers?: object
core?: http.ClientRequestArgs
followRedirects?: boolean
stream?: boolean
compression?: boolean
timeout?: number
hostname?: string
port?: number
path?: string
}
// Form and data property has been written this way so they're mutually exclusive.
type IWithData<T extends {}> = T & {
data: {
toString(): string
}
}
type IWithForm<T extends {}> = T & {
form: {
[index: string]: string
}
}
declare function phin<T>(options:
phin.IJSONResponseOptions |
IWithData<phin.IJSONResponseOptions> |
IWithForm<phin.IJSONResponseOptions>): Promise<phin.IJSONResponse<T>>
declare function phin(options:
phin.IStreamResponseOptions |
IWithData<phin.IStreamResponseOptions> |
IWithForm<phin.IStreamResponseOptions>): Promise<phin.IStreamResponse>
declare function phin(options:
phin.IOptions |
IWithData<phin.IOptions> |
IWithForm<phin.IOptions> |
string): Promise<phin.IResponse>
declare namespace phin {
export interface IJSONResponseOptions extends IOptionsBase {
parse: 'json'
}
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 IStreamResponse extends http.IncomingMessage {
stream: http.IncomingMessage
}
export interface IResponse extends http.IncomingMessage {
body: string
}
// 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:
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