Improve validation code. Add throws tsdocs to validateOptions
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import {
|
import {
|
||||||
ExponentialBackoffStoppedRetriesError,
|
ExponentialBackoffStoppedRetriesError,
|
||||||
ExponentialBackoffMaxRetriesHitError,
|
ExponentialBackoffMaxRetriesHitError,
|
||||||
ExponentialBackoffInvalidInfiniteIntegerError,
|
|
||||||
ExponentialBackoffNonIntegerError,
|
ExponentialBackoffNonIntegerError,
|
||||||
ExponentialBackoffNumberTooSmallError,
|
ExponentialBackoffNumberTooSmallError,
|
||||||
ExponentialBackoffNumberOutOfBoundsError,
|
ExponentialBackoffNumberOutOfBoundsError,
|
||||||
|
ExponentialBackoffNumberNotFiniteError,
|
||||||
} from './errors.ts';
|
} from './errors.ts';
|
||||||
|
import { isWithinBounds } from './misc.ts';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exponential backoff is a technique used to retry a function after a delay.
|
* Exponential backoff is a technique used to retry a function after a delay.
|
||||||
@@ -110,60 +111,60 @@ export class ExponentialBackoff {
|
|||||||
*
|
*
|
||||||
* @param options - The options to validate
|
* @param options - The options to validate
|
||||||
*
|
*
|
||||||
* @throws An error if the options are invalid
|
* @throws {@link ExponentialBackoffNumberNotFiniteError} if a provided option is not a finite number
|
||||||
|
* @throws {@link ExponentialBackoffNonIntegerError} if a provided option is not an integer
|
||||||
|
* @throws {@link ExponentialBackoffNumberOutOfBoundsError} if a provided option is out of bounds
|
||||||
|
* @throws {@link ExponentialBackoffNumberTooSmallError} if a provided option is too small
|
||||||
*/
|
*/
|
||||||
public static validateOptions(options: ExponentialBackoffOptions): void {
|
public static validateOptions(options: ExponentialBackoffOptions): void {
|
||||||
/** Validate the value is finite, throwing an {@link ExponentialBackoffInvalidInfiniteIntegerError} if the value is infinite */
|
/** Validate the value is finite, throwing an {@link ExponentialBackoffInvalidInfiniteIntegerError} if the value is infinite */
|
||||||
const isFinite = (key: string, value: number): void => {
|
const assertIsFinite = (key: string, value: number): void => {
|
||||||
if (!Number.isFinite(value)) {
|
if (!Number.isFinite(value)) {
|
||||||
throw new ExponentialBackoffInvalidInfiniteIntegerError(key);
|
throw new ExponentialBackoffNumberNotFiniteError(key, value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Validate the value is an integer, throwing a {@link ExponentialBackoffNonIntegerError} if it is not an integer */
|
/** Validate the value is an integer, throwing a {@link ExponentialBackoffNonIntegerError} if it is not an integer */
|
||||||
const isInteger = (key: string, value: number): void => {
|
const assertIsInteger = (key: string, value: number): void => {
|
||||||
if (!Number.isInteger(value)) {
|
if (!Number.isInteger(value)) {
|
||||||
throw new ExponentialBackoffNonIntegerError(key);
|
throw new ExponentialBackoffNonIntegerError(key, value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Validate the value is within the bounds, throwing a {@link ExponentialBackoffNumberOutOfBoundsError} if it is not within the bounds */
|
/** Validate the value is greater than the minimum, throwing a {@link ExponentialBackoffNumberTooSmallError} if it is not */
|
||||||
const isWithinBounds = (key: string, value: number, min: number, max?: number): void => {
|
const assertIsHigherThan = (key: string, value: number, min: number): void => {
|
||||||
// If both the min and max are defined, validate the value, throwing a number out of bounds error if it is not within the bounds
|
|
||||||
if (min !== undefined && max !== undefined) {
|
|
||||||
if (value < min || value > max) {
|
|
||||||
throw new ExponentialBackoffNumberOutOfBoundsError(key, value, min, max);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If only the min is defined, validate the value, throwing a number too small error if it is less than the min
|
|
||||||
if (value < min) {
|
if (value < min) {
|
||||||
throw new ExponentialBackoffNumberTooSmallError(key, value, min);
|
throw new ExponentialBackoffNumberTooSmallError(key, value, min);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Validate the value is within the bounds, throwing a {@link ExponentialBackoffNumberOutOfBoundsError} if it is not within the bounds */
|
||||||
|
const assertIsWithinBounds = (key: string, value: number, min: number, max: number): void => {
|
||||||
|
if (!isWithinBounds(value, min, max)) {
|
||||||
|
throw new ExponentialBackoffNumberOutOfBoundsError(key, value, min, max);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Validate the max delay
|
// Validate the max delay
|
||||||
isFinite('maxDelay', options.maxDelay);
|
assertIsFinite('maxDelay', options.maxDelay);
|
||||||
isWithinBounds('maxDelay', options.maxDelay, 0);
|
assertIsHigherThan('maxDelay', options.maxDelay, 0);
|
||||||
|
|
||||||
// Validate the max attempts
|
// Validate the max attempts
|
||||||
isFinite('maxAttempts', options.maxAttempts);
|
assertIsFinite('maxAttempts', options.maxAttempts);
|
||||||
isInteger('maxAttempts', options.maxAttempts);
|
assertIsInteger('maxAttempts', options.maxAttempts);
|
||||||
isWithinBounds('maxAttempts', options.maxAttempts, 0);
|
assertIsHigherThan('maxAttempts', options.maxAttempts, 0);
|
||||||
|
|
||||||
// Validate the base delay
|
// Validate the base delay
|
||||||
isFinite('baseDelay', options.baseDelay);
|
assertIsFinite('baseDelay', options.baseDelay);
|
||||||
isWithinBounds('baseDelay', options.baseDelay, 0);
|
assertIsHigherThan('baseDelay', options.baseDelay, 0);
|
||||||
|
|
||||||
// Validate the growth rate
|
// Validate the growth rate
|
||||||
isFinite('growthRate', options.growthRate);
|
assertIsFinite('growthRate', options.growthRate);
|
||||||
isWithinBounds('growthRate', options.growthRate, 0);
|
assertIsHigherThan('growthRate', options.growthRate, 0);
|
||||||
|
|
||||||
// Validate the jitter
|
// Validate the jitter
|
||||||
isFinite('jitter', options.jitter);
|
assertIsFinite('jitter', options.jitter);
|
||||||
isWithinBounds('jitter', options.jitter, 0, 1);
|
assertIsWithinBounds('jitter', options.jitter, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
/**
|
||||||
|
* Validate the value is within the bounds, returning true if it is within the bounds, false otherwise
|
||||||
|
*
|
||||||
|
* @param value - The value to validate
|
||||||
|
* @param min - The minimum value
|
||||||
|
* @param max - The maximum value
|
||||||
|
*
|
||||||
|
* @returns True if the value is within the bounds, false otherwise
|
||||||
|
*/
|
||||||
|
export const isWithinBounds = (value: number, min: number, max: number): boolean => {
|
||||||
|
if (value < min || value > max) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user