From 695a09c4beab22b801a2551f48d82833e86bf04a Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Thu, 4 Apr 2019 04:58:41 +0000 Subject: [PATCH] Refactor TypeScript definition to CommonJS compatible export (#11) --- index.d.ts | 113 +++++++++++++++++++++++++++++------------------- index.js | 1 + index.test-d.ts | 8 ++-- package.json | 8 ++-- 4 files changed, 79 insertions(+), 51 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4731ff1..cd3dbbd 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,47 +1,72 @@ -/** - * Timeout a promise after a specified amount of time. - * - * If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. - * - * @param input - Promise to decorate. - * @param milliseconds - Milliseconds before timing out. - * @param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`. - * @returns A decorated `input` that times out after `milliseconds` time. - */ -export default function pTimeout( - input: PromiseLike, - milliseconds: number, - message?: string | Error -): Promise; - -/** - * Timeout a promise after a specified amount of time. - * - * If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. - * - * @param input - Promise to decorate. - * @param milliseconds - Milliseconds before timing out. - * @param fallback - Do something other than rejecting with an error on timeout. You could for example retry. - * @returns A decorated `input` that times out after `milliseconds` time. - * - * @example - * - * import delay from 'delay'; - * import pTimeout from 'p-timeout'; - * - * const delayedPromise = () => delay(200); - * - * pTimeout(delayedPromise(), 50, () => { - * return pTimeout(delayedPromise(), 300); - * }); - */ -export default function pTimeout( - input: PromiseLike, - milliseconds: number, - fallback: () => ReturnType | Promise -): Promise; - -export class TimeoutError extends Error { +declare class TimeoutErrorClass extends Error { readonly name: 'TimeoutError'; constructor(message?: string); } + +declare namespace pTimeout { + type TimeoutError = TimeoutErrorClass; +} + +declare const pTimeout: { + /** + Timeout a promise after a specified amount of time. + + If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. + + @param input - Promise to decorate. + @param milliseconds - Milliseconds before timing out. + @param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`. + @returns A decorated `input` that times out after `milliseconds` time. + + @example + ``` + import delay = require('delay'); + import pTimeout = require('p-timeout'); + + const delayedPromise = delay(200); + + pTimeout(delayedPromise, 50).then(() => 'foo'); + //=> [TimeoutError: Promise timed out after 50 milliseconds] + ``` + */ + ( + input: PromiseLike, + milliseconds: number, + message?: string | Error + ): Promise; + + /** + Timeout a promise after a specified amount of time. + + If you pass in a cancelable promise, specifically a promise with a `.cancel()` method, that method will be called when the `pTimeout` promise times out. + + @param input - Promise to decorate. + @param milliseconds - Milliseconds before timing out. + @param fallback - Do something other than rejecting with an error on timeout. You could for example retry. + @returns A decorated `input` that times out after `milliseconds` time. + + @example + ``` + import delay = require('delay'); + import pTimeout = require('p-timeout'); + + const delayedPromise = () => delay(200); + + pTimeout(delayedPromise(), 50, () => { + return pTimeout(delayedPromise(), 300); + }); + ``` + */ + ( + input: PromiseLike, + milliseconds: number, + fallback: () => ReturnType | Promise + ): Promise; + + TimeoutError: typeof TimeoutErrorClass; + + // TODO: Remove this for the next major release + default: typeof pTimeout; +}; + +export = pTimeout; diff --git a/index.js b/index.js index 51f5732..e8ca13d 100644 --- a/index.js +++ b/index.js @@ -46,6 +46,7 @@ const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reje }); module.exports = pTimeout; +// TODO: Remove this for the next major release module.exports.default = pTimeout; module.exports.TimeoutError = TimeoutError; diff --git a/index.test-d.ts b/index.test-d.ts index e030706..f81bea3 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,6 @@ -import {expectType} from 'tsd-check'; -import pTimeout, {TimeoutError} from '.'; +import {expectType} from 'tsd'; +import pTimeout = require('.'); +import {TimeoutError} from '.'; const delayedPromise: () => Promise = () => new Promise(resolve => setTimeout(() => resolve('foo'), 200)); @@ -22,4 +23,5 @@ pTimeout(delayedPromise(), 50, () => 10).then(value => { expectType(value); }); -expectType(TimeoutError); +const timeoutError = new TimeoutError(); +expectType(timeoutError); diff --git a/package.json b/package.json index 3bd9c8f..e8f0189 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -36,10 +36,10 @@ "p-finally": "^1.0.0" }, "devDependencies": { - "ava": "^1.3.1", + "ava": "^1.4.1", "delay": "^4.1.0", - "p-cancelable": "^1.1.0", - "tsd-check": "^0.3.0", + "p-cancelable": "^2.0.0", + "tsd": "^0.7.2", "xo": "^0.24.0" } }