diff --git a/README.md b/README.md index a721001..5214d96 100644 --- a/README.md +++ b/README.md @@ -20,19 +20,28 @@ Then you can import it with: ```typescript import { retry } from 'ts-retry-promise'; -const result = await retry(() => Promise.resolve(1), {retries: 3}); +const result: number = await retry(() => Promise.resolve(1), {retries: 3}); ``` -## Interface +This will instantly start calling your function until it returns a resolved promise, no retries are left or a timeout occurred. + +If you want to add retries to an existing function, use the decorator: ```typescript -function retry(f: () => Promise, config?: RetryConfig): Promise {} +import { retryDecorator } from 'ts-retry-promise'; + +const asyncFunction = async (s: String) => s; + +const decoratedFunction = retryDecorator(asyncFunction, {timeout: 1}); + +const result: string = await decoratedFunction("1"); ``` -_retry_ will repeatedly call _f_ until a resolved _Promise_ is returned. -Optionally a predicate can be specified, against which the result will be checked. +Here `decoratedFunction` is a function with the same signature as `asyncFunction`, but will do retries in case of failures. + +## Configuration -Several aspects of the execution can be configured: +Both `retry` and `retryDecorator` take an optional second argument where you can configure the number of retries and timeouts: ```typescript export interface RetryConfig { @@ -64,6 +73,8 @@ export interface RetryConfig { _customizeRetry_ returns a new instance of _retry_ that has the defined default configuration. ```typescript +import { customizeRetry } from 'ts-retry-promise'; + const impatientRetry = customizeRetry({timeout: 5}); await expect(impatientRetry(async () => wait(10))).to.be.rejectedWith("Timeout"); @@ -77,9 +88,36 @@ const result = await retryUntilNotEmpty(async () => [1, 2]); expect(result).to.deep.eq([1, 2]); ``` + +You can do the same for decorators: +```typescript +import { customizeDecorator } from 'ts-retry-promise'; + +const asyncFunction = async (s: string) => { + await wait(3); + return s; +}; + +const impatientDecorator = customizeDecorator({timeout: 1}); + +expect(impatientDecorator(asyncFunction)("1")).to.be.rejectedWith("Timeout"); +``` + + ## Samples ## -_retry_ is well suited for acceptance tests (but not restricted to). +_retryDecorator_ can be used on any function that returns a promise + +```typescript +const loadUserProfile: (id: number) => Promise<{ name: string }> = async id => ({name: "Mr " + id}); + +const robustProfileLoader = retryDecorator(loadUserProfile, {retries: 2}); + +const profile = await robustProfileLoader(123); +``` + + +_retry_ is well suited for acceptance tests (but not restricted to) ```typescript // ts-retry-promise/test/retry-promise.demo.test.ts diff --git a/src/retry-promise.ts b/src/retry-promise.ts index f532d25..4138e53 100644 --- a/src/retry-promise.ts +++ b/src/retry-promise.ts @@ -46,6 +46,14 @@ export async function retry(f: () => Promise, config?: Partial _retry(f, effectiveConfig, done)); } +export function retryDecorator Promise>(func: F, config?: Partial>): (...funcArgs: Parameters) => ReturnType { + return (...args: Parameters) => retry(() => func(...args), config) as ReturnType; +} + +export function customizeDecorator(customConfig: Partial>): typeof retryDecorator { + return (args, config) => retryDecorator(args, Object.assign({}, customConfig, config)); +} + // tslint:disable-next-line export function customizeRetry(customConfig: Partial>): (f: () => Promise, config?: RetryConfig) => Promise { return (f, c) => { diff --git a/test/retry-promise.decorator.test.ts b/test/retry-promise.decorator.test.ts new file mode 100644 index 0000000..7492157 --- /dev/null +++ b/test/retry-promise.decorator.test.ts @@ -0,0 +1,57 @@ +import {expect} from "./index"; +import {customizeDecorator, retryDecorator, wait} from "../src/retry-promise"; + +describe("Retry decorator test", () => { + + it("can use decorator", async () => { + const asyncFunction: (s: string) => Promise = async s => s; + + const asyncFunctionDecorated = retryDecorator(asyncFunction); + + const result = asyncFunctionDecorated("1"); + + expect(result).to.eventually.eq("1"); + }); + + it("can use decorator with custom config", async () => { + const asyncFunction: (s: string) => Promise = async s => { + await wait(5); + return s; + }; + + const asyncFunctionDecorated = retryDecorator(asyncFunction, {timeout: 1}); + + expect(asyncFunctionDecorated("1")).to.be.rejectedWith("Timeout") + }); + + it("can use decorator with multiple args", async () => { + const asyncFunction: (s1: string, s2: string) => Promise = async (s1, s2) => s1 + s2; + + const asyncFunctionDecorated: (s1: string, s2: string) => Promise = retryDecorator(asyncFunction); + + expect(asyncFunctionDecorated("1", "2")).to.eventually.eq("12") + }); + + it("can customize decorator", async () => { + const asyncFunction = async (s: string) => { + await wait(3); + return s; + }; + + const myDecorator = customizeDecorator({timeout: 1}); + + expect(myDecorator(asyncFunction)("1")).to.be.rejectedWith("Timeout"); + }); + + it("can overwrite customized decorator", async () => { + const asyncFunction = async (s: string) => { + await wait(3); + return s; + }; + + const myDecorator = customizeDecorator({timeout: 1}); + + expect(myDecorator(asyncFunction, {timeout: 5})("1")).to.eventually.eq("1"); + }); + +}); diff --git a/test/retry-promise.demo.test.ts b/test/retry-promise.demo.test.ts index 8d6ba00..b993a16 100644 --- a/test/retry-promise.demo.test.ts +++ b/test/retry-promise.demo.test.ts @@ -1,5 +1,5 @@ import {expect} from "./index"; -import {customizeRetry, defaultRetryConfig, retry, wait} from "../src/retry-promise"; +import {customizeRetry, defaultRetryConfig, retry, retryDecorator, wait} from "../src/retry-promise"; describe("Retry Promise Demo", () => { @@ -75,6 +75,28 @@ describe("Retry Promise Demo", () => { } }); + it("can use decorator", async () => { + + type AsyncFunc = (s: string) => Promise + + const asyncFunction: AsyncFunc = async s => s; + + const asyncFunctionDecorated: AsyncFunc = retryDecorator(asyncFunction, {timeout: 1}); + + expect(asyncFunctionDecorated("1")).to.eventually.eq("1") + }); + + it("decorator demo", async () => { + + const loadUserProfile: (id: number) => Promise<{ name: string }> = async id => ({name: "Mr " + id}); + + const robustProfileLoader = retryDecorator(loadUserProfile, {retries: 2}); + + const profile = await robustProfileLoader(123); + + expect(profile.name).to.eq("Mr 123"); + }) + }); const browser = {