From 2155b3e40d582be3fd077965d7344c3abef66172 Mon Sep 17 00:00:00 2001 From: LuminescentMoon Date: Mon, 4 Feb 2019 19:22:05 -0800 Subject: [PATCH 1/2] Add TypeScript declaration --- package.json | 1 + types.d.ts | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 types.d.ts diff --git a/package.json b/package.json index 21b78c9..87b945f 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "3.3.0", "description": "The ultra-lightweight Node.js HTTP client", "main": "lib/phin.min.js", + "types": "types.d.ts", "scripts": { "test": "node ./tests/test.js", "prepublishOnly": "npm test", diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..4cfe2a9 --- /dev/null +++ b/types.d.ts @@ -0,0 +1,102 @@ +// 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 +} + +type IWithData = T & { + data: { + toString(): string + } +} + +type IWithForm = T & { + form: { + [index: string]: string + } +} + +declare function phin(options: + phin.IJSONResponseOptions | + IWithData | + IWithForm): Promise + +declare function phin(options: + phin.IStreamResponseOptions | + IWithData | + IWithForm): Promise + +declare function phin(options: + phin.IOptions | + IWithData | + IWithForm | + string): Promise + +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 extends http.IncomingMessage { + body: object + } + + 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 = (error: null, response: NonNullable) => void + + export let promisified: typeof phin + + export function unpromisified( + options: + IJSONResponseOptions | + IWithData | + IWithForm, + callback: IErrorCallback | ICallback): void + + export function unpromisified( + options: + IStreamResponseOptions | + IWithData | + IWithForm, + callback: IErrorCallback | ICallback): void + + export function unpromisified( + options: + IOptions | + IWithData | + IWithForm | + string, + callback: IErrorCallback | ICallback): void +} + +export = phin From 2ad0b104ae042a53021247a23eecb7a7ef70c218 Mon Sep 17 00:00:00 2001 From: LuminescentMoon Date: Mon, 4 Feb 2019 19:43:25 -0800 Subject: [PATCH 2/2] Make JSON phin generic --- types.d.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/types.d.ts b/types.d.ts index 4cfe2a9..1b15622 100644 --- a/types.d.ts +++ b/types.d.ts @@ -16,6 +16,7 @@ interface IOptionsBase { path?: string } +// Form and data property has been written this way so they're mutually exclusive. type IWithData = T & { data: { toString(): string @@ -28,10 +29,10 @@ type IWithForm = T & { } } -declare function phin(options: +declare function phin(options: phin.IJSONResponseOptions | IWithData | - IWithForm): Promise + IWithForm): Promise> declare function phin(options: phin.IStreamResponseOptions | @@ -57,8 +58,8 @@ declare namespace phin { parse?: 'none' } - export interface IJSONResponse extends http.IncomingMessage { - body: object + export interface IJSONResponse extends http.IncomingMessage { + body: T } export interface IStreamResponse extends http.IncomingMessage { @@ -76,12 +77,12 @@ declare namespace phin { export let promisified: typeof phin - export function unpromisified( + export function unpromisified( options: IJSONResponseOptions | IWithData | IWithForm, - callback: IErrorCallback | ICallback): void + callback: IErrorCallback | ICallback>): void export function unpromisified( options: