-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathRetryHandlerOptions.ts
128 lines (114 loc) · 3.87 KB
/
RetryHandlerOptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
/**
* @module RetryHandlerOptions
*/
import { FetchOptions } from "../../IFetchOptions";
import { MiddlewareOptions } from "./IMiddlewareOptions";
/**
* @type
* A type declaration for shouldRetry callback
*/
export type ShouldRetry = (delay: number, attempt: number, request: RequestInfo, options: FetchOptions | undefined, response: Response) => boolean;
/**
* @class
* @implements MiddlewareOptions
* Class for RetryHandlerOptions
*/
export class RetryHandlerOptions implements MiddlewareOptions {
/**
* @private
* @static
* A member holding default delay value in seconds
*/
private static DEFAULT_DELAY = 3;
/**
* @private
* @static
* A member holding default maxRetries value
*/
private static DEFAULT_MAX_RETRIES = 12;
/**
* @private
* @static
* A member holding maximum delay value (1 hour) in seconds
*/
private static MAX_DELAY = 3_600;
/**
* @private
* @static
* A member holding maximum maxRetries value
*/
private static MAX_MAX_RETRIES = 64;
/**
* @public
* A member holding delay value in seconds
*/
public delay: number;
/**
* @public
* A member holding maxRetries value
*/
public maxRetries: number;
/**
* @public
* A member holding shouldRetry callback
*/
public shouldRetry: ShouldRetry;
/**
* @private
* A member holding default shouldRetry callback
*/
private static defaultShouldRetry: ShouldRetry = () => true;
/**
* @public
* @constructor
* To create an instance of RetryHandlerOptions
* @param {number} [delay = RetryHandlerOptions.DEFAULT_DELAY] - The delay value in seconds
* @param {number} [maxRetries = RetryHandlerOptions.DEFAULT_MAX_RETRIES] - The maxRetries value
* @param {ShouldRetry} [shouldRetry = RetryHandlerOptions.DEFAULT_SHOULD_RETRY] - The shouldRetry callback function
* @returns An instance of RetryHandlerOptions
*/
public constructor(delay: number = RetryHandlerOptions.DEFAULT_DELAY, maxRetries: number = RetryHandlerOptions.DEFAULT_MAX_RETRIES, shouldRetry: ShouldRetry = RetryHandlerOptions.defaultShouldRetry) {
if (delay > RetryHandlerOptions.MAX_DELAY && maxRetries > RetryHandlerOptions.MAX_MAX_RETRIES) {
const error = new Error(`Delay and MaxRetries should not be more than ${RetryHandlerOptions.MAX_DELAY} and ${RetryHandlerOptions.MAX_MAX_RETRIES}`);
error.name = "MaxLimitExceeded";
throw error;
} else if (delay > RetryHandlerOptions.MAX_DELAY) {
const error = new Error(`Delay should not be more than ${RetryHandlerOptions.MAX_DELAY}`);
error.name = "MaxLimitExceeded";
throw error;
} else if (maxRetries > RetryHandlerOptions.MAX_MAX_RETRIES) {
const error = new Error(`MaxRetries should not be more than ${RetryHandlerOptions.MAX_MAX_RETRIES}`);
error.name = "MaxLimitExceeded";
throw error;
} else if (delay < 0 && maxRetries < 0) {
const error = new Error(`Delay and MaxRetries should not be negative`);
error.name = "MinExpectationNotMet";
throw error;
} else if (delay < 0) {
const error = new Error(`Delay should not be negative`);
error.name = "MinExpectationNotMet";
throw error;
} else if (maxRetries < 0) {
const error = new Error(`MaxRetries should not be negative`);
error.name = "MinExpectationNotMet";
throw error;
}
this.delay = Math.min(delay, RetryHandlerOptions.MAX_DELAY);
this.maxRetries = Math.min(maxRetries, RetryHandlerOptions.MAX_MAX_RETRIES);
this.shouldRetry = shouldRetry;
}
/**
* @public
* To get the maximum delay
* @returns A maximum delay
*/
public getMaxDelay(): number {
return RetryHandlerOptions.MAX_DELAY;
}
}