UNPKG

3.52 kBTypeScriptView Raw
1// Default Options feature is not supported because it's basically impossible to write strongly-typed definitions for it.
2
3import * as http from 'http'
4import { URL } from 'url';
5
6interface IOptionsBase {
7 url: string | URL
8 method?: string
9 headers?: object
10 core?: http.ClientRequestArgs
11 followRedirects?: boolean
12 stream?: boolean
13 compression?: boolean
14 timeout?: number
15 hostname?: string
16 port?: number
17 path?: string
18}
19
20declare function phin<T>(options:
21 phin.IJSONResponseOptions |
22 phin.IWithData<phin.IJSONResponseOptions> |
23 phin.IWithForm<phin.IJSONResponseOptions>): Promise<phin.IJSONResponse<T>>
24
25declare function phin(options:
26 phin.IStringResponseOptions |
27 phin.IWithData<phin.IStringResponseOptions> |
28 phin.IWithForm<phin.IStringResponseOptions>): Promise<phin.IStringResponse>
29
30declare function phin(options:
31 phin.IStreamResponseOptions |
32 phin.IWithData<phin.IStreamResponseOptions> |
33 phin.IWithForm<phin.IStreamResponseOptions>): Promise<phin.IStreamResponse>
34
35declare function phin(options:
36 phin.IOptions |
37 phin.IWithData<phin.IOptions> |
38 phin.IWithForm<phin.IOptions> |
39 string): Promise<phin.IResponse>
40
41declare namespace phin {
42 // Form and data property has been written this way so they're mutually exclusive.
43 export type IWithData<T extends IOptionsBase> = T & {
44 data: string | Buffer | object;
45 }
46
47 export type IWithForm<T extends IOptionsBase> = T & {
48 form: {
49 [index: string]: string
50 }
51 }
52
53 export interface IJSONResponseOptions extends IOptionsBase {
54 parse: 'json'
55 }
56
57 export interface IStringResponseOptions extends IOptionsBase {
58 parse: 'string';
59 }
60
61 export interface IStreamResponseOptions extends IOptionsBase {
62 stream: true
63 }
64
65 export interface IOptions extends IOptionsBase {
66 parse?: 'none'
67 }
68
69 export interface IJSONResponse<T> extends http.IncomingMessage {
70 body: T
71 }
72
73 export interface IStringResponse extends http.IncomingMessage {
74 body: string;
75 }
76
77 export interface IStreamResponse extends http.IncomingMessage {
78 stream: http.IncomingMessage
79 }
80
81 export interface IResponse extends http.IncomingMessage {
82 body: Buffer;
83 }
84
85 // NOTE: Typescript cannot infer type of union callback on the consumer side
86 // https://github.com/Microsoft/TypeScript/pull/17819#issuecomment-363636904
87 type IErrorCallback = (error: Error | string, response: null) => void
88 type ICallback<T> = (error: null, response: NonNullable<T>) => void
89
90 export let promisified: typeof phin
91
92 export function unpromisified<T>(
93 options:
94 IJSONResponseOptions |
95 IWithData<IJSONResponseOptions> |
96 IWithForm<IJSONResponseOptions>,
97 callback: IErrorCallback | ICallback<IJSONResponse<T>>): void
98
99 export function unpromisified(
100 options:
101 IStringResponseOptions |
102 IWithData<IStringResponseOptions> |
103 IWithForm<IStringResponseOptions>,
104 callback: IErrorCallback | ICallback<IStringResponse>): void
105
106 export function unpromisified(
107 options:
108 IStreamResponseOptions |
109 IWithData<IStreamResponseOptions> |
110 IWithForm<IStreamResponseOptions>,
111 callback: IErrorCallback | ICallback<IStreamResponse>): void
112
113 export function unpromisified(
114 options:
115 IOptions |
116 IWithData<IOptions> |
117 IWithForm<IOptions> |
118 string,
119 callback: IErrorCallback | ICallback<IResponse>): void
120}
121
122export = phin
123
\No newline at end of file