[p-retry] introduce typings, [retry] export options interfaces, linting (#18671)

This commit is contained in:
Dimitri Benin
2017-08-08 14:11:30 -07:00
committed by Mohamed Hegazy
parent 0c30eb38ee
commit 85b9cc459f
7 changed files with 135 additions and 81 deletions
+18
View File
@@ -0,0 +1,18 @@
// Type definitions for p-retry 1.0
// Project: https://github.com/sindresorhus/p-retry#readme
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import { OperationOptions } from 'retry';
export = pRetry;
declare function pRetry<T>(input: (attemptCount: number) => PromiseLike<T> | T, options?: OperationOptions): Promise<T>;
declare namespace pRetry {
class AbortError extends Error {
readonly name: 'AbortError';
readonly originalError: Error;
constructor(message: string | Error);
}
}
+16
View File
@@ -0,0 +1,16 @@
import pRetry = require('p-retry');
import fetch from 'node-fetch';
const run = () => fetch('https://sindresorhus.com/unicorn')
.then(response => {
// abort retrying if the resource doesn't exist
if (response.status === 404) {
throw new pRetry.AbortError(response.statusText);
}
return response.text();
});
pRetry(run, {retries: 5}).then(result => {
const str: string = result;
});
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"p-retry-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+51 -40
View File
@@ -15,10 +15,7 @@ export interface RetryOperation {
*
* @return {void}
*/
attempt(callback: (current: number) => void, options?: {
timeout?: number
callback?: () => void
}): void
attempt(callback: (current: number) => void, options?: AttemptOptions): void;
/**
* Returns false when no error value is given, or the maximum amount of retries has been reached.
@@ -28,14 +25,14 @@ export interface RetryOperation {
*
* @return {boolean}
*/
retry(err?: Error): boolean
retry(err?: Error): boolean;
/**
* The number of attempts it took to call the retrying function before it was successful.
*
* @return {number}
*/
attempts(): number
attempts(): number;
/**
* A reference to the error object that occured most frequently.
@@ -43,20 +40,26 @@ export interface RetryOperation {
*
* @return {(Error|null)} If no errors occured so far the value will be null.
*/
mainError(): Error | null
mainError(): Error | null;
/**
* Returns an array of all errors that have been passed to RetryOperation.retry() so far.
*
* @return {Error[]}
*/
errors(): Error[]
errors(): Error[];
/**
* Stops the operation being retried. Useful for aborting the operation on a fatal error etc.
*/
stop(): void
stop(): void;
}
export interface AttemptOptions {
timeout?: number;
callback?(): void;
}
/**
* Create a new RetryOperation object.
*
@@ -70,15 +73,17 @@ export interface RetryOperation {
*
* @return {RetryOperation}
*/
export function operation(options?: {
retries?: number
factor?: number
minTimeout?: number
maxTimeout?: number
randomize?: boolean
forever?: boolean
unref?: boolean
}): RetryOperation
export function operation(options?: OperationOptions): RetryOperation;
export interface OperationOptions {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
randomize?: boolean;
forever?: boolean;
unref?: boolean;
}
/**
* Get an array with timeouts and their return values in milliseconds.
@@ -91,13 +96,15 @@ export function operation(options?: {
*
* @return {number[]}
*/
export function timeouts(options?: {
retries?: number
factor?: number
minTimeout?: number
maxTimeout?: number
randomize?: boolean
}): number[]
export function timeouts(options?: TimeoutsOptions): number[];
export interface TimeoutsOptions {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
randomize?: boolean;
}
/**
* Create a new timeout (in milliseconds) based on the given parameters.
@@ -110,12 +117,14 @@ export function timeouts(options?: {
*
* @return {number} timeout
*/
export function createTimeout(attempt: number, options?: {
factor?: number
minTimeout?: number
maxTimeout?: number
randomize?: boolean
}): number
export function createTimeout(attempt: number, options?: CreateTimeoutOptions): number;
export interface CreateTimeoutOptions {
factor?: number;
minTimeout?: number;
maxTimeout?: number;
randomize?: boolean;
}
/**
* Wrap all functions of the object with retry.
@@ -132,12 +141,14 @@ export function createTimeout(attempt: number, options?: {
*
* @return {void}
*/
export function wrap(object: object, options?: {
retries?: number
factor?: number
minTimeout?: number
maxTimeout?: number
randomize?: boolean
forever?: boolean
unref?: boolean // tslint:disable-next-line:align
}, methods?: string[]): void
export function wrap(object: object, options?: WrapOptions, methods?: string[]): void;
export interface WrapOptions {
retries?: number;
factor?: number;
minTimeout?: number;
maxTimeout?: number;
randomize?: boolean;
forever?: boolean;
unref?: boolean;
}
+26 -27
View File
@@ -1,14 +1,14 @@
import retry = require('retry')
import retry = require('retry');
// Option values
const att = 4
const ret = 2
const fac = 1.5
const min = 2000
const max = 4000
const rnd = false
const evr = false
const unr = false
const att = 4;
const ret = 2;
const fac = 1.5;
const min = 2000;
const max = 4000;
const rnd = false;
const evr = false;
const unr = false;
// Options themselves
const operationOptions = {
@@ -19,14 +19,14 @@ const operationOptions = {
randomize: rnd,
forever: evr,
unref: unr,
}
};
const timeoutOptions = {
factor: fac,
minTimeout: min,
maxTimeout: max,
randomize: rnd,
}
};
const timeoutsOptions = {
retries: ret,
@@ -34,40 +34,39 @@ const timeoutsOptions = {
minTimeout: min,
maxTimeout: max,
randomize: rnd,
}
};
// Class to be wrapped later on
class Foo {
public bar() {
bar() {
//
}
public baz() {
baz() {
//
}
}
const operation = retry.operation(operationOptions)
const operation = retry.operation(operationOptions);
operation.attempt((current) => {
const err = Math.random() >= 0.5 ? new Error('Happens to the best of us') : undefined
const err = Math.random() >= 0.5 ? new Error('Happens to the best of us') : undefined;
const retry = operation.retry(err)
const retry = operation.retry(err);
if (retry) {
const after = operation.attempts()
}
else {
const errors = operation.errors()
const after = operation.attempts();
} else {
const errors = operation.errors();
const main = operation.mainError()
const main = operation.mainError();
}
operation.stop()
})
operation.stop();
});
const timeout = retry.createTimeout(att, timeoutOptions)
const timeout = retry.createTimeout(att, timeoutOptions);
const timeouts = retry.timeouts(timeoutsOptions)
const timeouts = retry.timeouts(timeoutsOptions);
const wrap = retry.wrap(new Foo(), operationOptions, ['bar'])
const wrap = retry.wrap(new Foo(), operationOptions, ['bar']);
+1 -14
View File
@@ -1,14 +1 @@
{
"extends": "dtslint/dt.json",
"rules": {
"member-access": true,
"prefer-method-signature": false,
"semicolon": false,
"one-line": {
"options": [
"check-open-brace",
"check-whitespace"
]
}
}
}
{ "extends": "dtslint/dt.json" }