@codingaryan/smoothapi 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/state.d.ts +12 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +57 -0
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/backoff.d.ts +4 -0
- package/dist/utils/backoff.d.ts.map +1 -0
- package/dist/utils/backoff.js +10 -0
- package/dist/utils/backoff.js.map +1 -0
- package/package.json +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aryan Sharma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAUpE,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,YAAY,EAAE,oBAAoB,CAAC,CAAC,CAAC,IAMzE,KAAK,MAAM,GAAG,GAAG,EACjB,UAAU,WAAW,KACpB,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC,CAsCzB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { CircuitBreakerState } from "./state.js";
|
|
2
|
+
import { calculateBackoff, sleep } from "./utils/backoff.js";
|
|
3
|
+
import { CircuitOpenError } from "./types.js";
|
|
4
|
+
const BACKOFF_DEFAULTS = {
|
|
5
|
+
baseDelay: 100,
|
|
6
|
+
maxDelay: 30_000,
|
|
7
|
+
maxRetries: 3,
|
|
8
|
+
};
|
|
9
|
+
const DEFAULT_RETRY_ON = [429, 500, 502, 503, 504];
|
|
10
|
+
export function createResilientFetch(globalConfig) {
|
|
11
|
+
const backoffConfig = { ...BACKOFF_DEFAULTS, ...globalConfig.backoff };
|
|
12
|
+
const retryOn = globalConfig.retryOn ?? DEFAULT_RETRY_ON;
|
|
13
|
+
const breaker = new CircuitBreakerState(globalConfig.circuitBreaker);
|
|
14
|
+
return async function resilientFetch(url, options) {
|
|
15
|
+
const domain = new URL(url).hostname;
|
|
16
|
+
// Block before any network IO if the circuit is OPEN.
|
|
17
|
+
if (!breaker.canRequest(domain)) {
|
|
18
|
+
if (globalConfig.fallback !== undefined) {
|
|
19
|
+
return globalConfig.fallback;
|
|
20
|
+
}
|
|
21
|
+
throw new CircuitOpenError(domain);
|
|
22
|
+
}
|
|
23
|
+
let lastError;
|
|
24
|
+
for (let attempt = 0; attempt <= backoffConfig.maxRetries; attempt++) {
|
|
25
|
+
try {
|
|
26
|
+
const response = await fetch(url, options);
|
|
27
|
+
// fetch() resolves for any HTTP status. Retryable codes need to be
|
|
28
|
+
// treated as failures manually so they dont throw on their own.
|
|
29
|
+
if (retryOn.includes(response.status)) {
|
|
30
|
+
throw new Error(`HTTP ${response.status}`);
|
|
31
|
+
}
|
|
32
|
+
breaker.recordSuccess(domain);
|
|
33
|
+
return response;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
lastError = err;
|
|
37
|
+
breaker.recordFailure(domain);
|
|
38
|
+
// Don't sleep after the final attempt
|
|
39
|
+
if (attempt < backoffConfig.maxRetries) {
|
|
40
|
+
await sleep(calculateBackoff(attempt, backoffConfig));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
throw lastError;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAwB,MAAM,YAAY,CAAC;AAEpE,MAAM,gBAAgB,GAAG;IACvB,SAAS,EAAE,GAAG;IACd,QAAQ,EAAE,MAAM;IAChB,UAAU,EAAE,CAAC;CACd,CAAC;AAEF,MAAM,gBAAgB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAEnD,MAAM,UAAU,oBAAoB,CAAI,YAAqC;IAC3E,MAAM,aAAa,GAAG,EAAE,GAAG,gBAAgB,EAAE,GAAG,YAAY,CAAC,OAAO,EAAE,CAAC;IACvE,MAAM,OAAO,GAAG,YAAY,CAAC,OAAO,IAAI,gBAAgB,CAAC;IACzD,MAAM,OAAO,GAAG,IAAI,mBAAmB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAErE,OAAO,KAAK,UAAU,cAAc,CAClC,GAAiB,EACjB,OAAqB;QAErB,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;QAErC,sDAAsD;QACtD,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;YAChC,IAAI,YAAY,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;gBACxC,OAAO,YAAY,CAAC,QAAa,CAAC;YACpC,CAAC;YACD,MAAM,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC;QACrC,CAAC;QAED,IAAI,SAAkB,CAAC;QAEvB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,aAAa,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAAC;YACrE,IAAI,CAAC;gBACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;gBAE3C,mEAAmE;gBACnE,gEAAgE;gBAChE,IAAI,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;oBACtC,MAAM,IAAI,KAAK,CAAC,QAAQ,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;gBAC7C,CAAC;gBAED,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;gBAC9B,OAAO,QAAQ,CAAC;YAClB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,SAAS,GAAG,GAAG,CAAC;gBAChB,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;gBAE9B,sCAAsC;gBACtC,IAAI,OAAO,GAAG,aAAa,CAAC,UAAU,EAAE,CAAC;oBACvC,MAAM,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;gBACxD,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,SAAS,CAAC;IAClB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/state.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CircuitState, CircuitBreakerConfig } from "./types.js";
|
|
2
|
+
export declare class CircuitBreakerState {
|
|
3
|
+
private readonly config;
|
|
4
|
+
private readonly map;
|
|
5
|
+
constructor(config?: Partial<CircuitBreakerConfig>);
|
|
6
|
+
private getOrCreate;
|
|
7
|
+
canRequest(domain: string): boolean;
|
|
8
|
+
recordSuccess(domain: string): void;
|
|
9
|
+
recordFailure(domain: string): void;
|
|
10
|
+
getState(domain: string): CircuitState;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,oBAAoB,EAAiB,MAAM,YAAY,CAAC;AAOpF,qBAAa,mBAAmB;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiC;IACxD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAA4B;gBAEpC,MAAM,GAAE,OAAO,CAAC,oBAAoB,CAAM;IAKtD,OAAO,CAAC,WAAW;IAWnB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAcnC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMnC,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAcnC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;CAKzC"}
|
package/dist/state.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const DEFAULTS = {
|
|
2
|
+
failureThreshold: 3,
|
|
3
|
+
cooldownMs: 10_000,
|
|
4
|
+
};
|
|
5
|
+
export class CircuitBreakerState {
|
|
6
|
+
config;
|
|
7
|
+
map;
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.config = { ...DEFAULTS, ...config };
|
|
10
|
+
this.map = new Map();
|
|
11
|
+
}
|
|
12
|
+
getOrCreate(domain) {
|
|
13
|
+
if (!this.map.has(domain)) {
|
|
14
|
+
this.map.set(domain, {
|
|
15
|
+
state: 'CLOSED',
|
|
16
|
+
failureCount: 0,
|
|
17
|
+
lastFailureTime: 0,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return this.map.get(domain);
|
|
21
|
+
}
|
|
22
|
+
canRequest(domain) {
|
|
23
|
+
const entry = this.getOrCreate(domain);
|
|
24
|
+
if (entry.state === "CLOSED" || entry.state === "HALF_OPEN")
|
|
25
|
+
return true;
|
|
26
|
+
const elapsedTime = Date.now() - entry.lastFailureTime;
|
|
27
|
+
if (elapsedTime > this.config.cooldownMs) {
|
|
28
|
+
entry.state = "HALF_OPEN";
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
recordSuccess(domain) {
|
|
36
|
+
const entry = this.getOrCreate(domain);
|
|
37
|
+
entry.state = "CLOSED";
|
|
38
|
+
entry.failureCount = 0;
|
|
39
|
+
}
|
|
40
|
+
recordFailure(domain) {
|
|
41
|
+
const entry = this.getOrCreate(domain);
|
|
42
|
+
entry.failureCount++;
|
|
43
|
+
if (entry.state === "HALF_OPEN") {
|
|
44
|
+
entry.state = "OPEN";
|
|
45
|
+
entry.lastFailureTime = Date.now();
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
else if (entry.failureCount >= this.config.failureThreshold) {
|
|
49
|
+
entry.state = "OPEN";
|
|
50
|
+
entry.lastFailureTime = Date.now();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
getState(domain) {
|
|
54
|
+
return this.getOrCreate(domain).state;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAEA,MAAM,QAAQ,GAAmC;IAC7C,gBAAgB,EAAE,CAAC;IACnB,UAAU,EAAE,MAAM;CACrB,CAAA;AAED,MAAM,OAAO,mBAAmB;IACX,MAAM,CAAiC;IACvC,GAAG,CAA4B;IAEhD,YAAY,SAAwC,EAAE;QAClD,IAAI,CAAC,MAAM,GAAG,EAAE,GAAG,QAAQ,EAAE,GAAG,MAAM,EAAC,CAAC;QACxC,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAE,CAAC;IACzB,CAAC;IAEO,WAAW,CAAC,MAAc;QAClC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;YACxB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE;gBACrB,KAAK,EAAE,QAAQ;gBACf,YAAY,EAAE,CAAC;gBACf,eAAe,EAAE,CAAC;aACjB,CAAC,CAAC;QACP,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAE,CAAC;IAC7B,CAAC;IAED,UAAU,CAAC,MAAc;QACrB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACvC,IAAG,KAAK,CAAC,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,KAAK,KAAK,WAAW;YACtD,OAAO,IAAI,CAAC;QAChB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,eAAe,CAAC;QACvD,IAAG,WAAW,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,EAAC,CAAC;YACrC,KAAK,CAAC,KAAK,GAAG,WAAW,CAAC;YAC1B,OAAO,IAAI,CAAC;QAChB,CAAC;aACG,CAAC;YACD,OAAO,KAAK,CAAC;QACjB,CAAC;IACL,CAAC;IAED,aAAa,CAAC,MAAc;QACxB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACvC,KAAK,CAAC,KAAK,GAAG,QAAQ,CAAC;QACvB,KAAK,CAAC,YAAY,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,aAAa,CAAC,MAAc;QACxB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QACvC,KAAK,CAAC,YAAY,EAAE,CAAC;QACrB,IAAG,KAAK,CAAC,KAAK,KAAK,WAAW,EAAC,CAAC;YAC5B,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;YACrB,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACnC,OAAM;QACV,CAAC;aACI,IAAI,KAAK,CAAC,YAAY,IAAI,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAC,CAAC;YACzD,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC;YACrB,KAAK,CAAC,eAAe,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvC,CAAC;IACL,CAAC;IAED,QAAQ,CAAC,MAAc;QACnB,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;IAC1C,CAAC;CAGJ"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
|
|
2
|
+
export interface CircuitEntry {
|
|
3
|
+
state: CircuitState;
|
|
4
|
+
failureCount: number;
|
|
5
|
+
lastFailureTime: number;
|
|
6
|
+
}
|
|
7
|
+
export interface BackoffConfig {
|
|
8
|
+
baseDelay: number;
|
|
9
|
+
maxDelay: number;
|
|
10
|
+
maxRetries: number;
|
|
11
|
+
}
|
|
12
|
+
export interface CircuitBreakerConfig {
|
|
13
|
+
failureThreshold: number;
|
|
14
|
+
cooldownMs: number;
|
|
15
|
+
}
|
|
16
|
+
export interface ResilientFetchConfig<T = unknown> {
|
|
17
|
+
backoff?: Partial<BackoffConfig>;
|
|
18
|
+
circuitBreaker?: Partial<CircuitBreakerConfig>;
|
|
19
|
+
fallback?: T;
|
|
20
|
+
retryOn?: number[];
|
|
21
|
+
}
|
|
22
|
+
export declare class CircuitOpenError extends Error {
|
|
23
|
+
readonly domain: string;
|
|
24
|
+
constructor(domain: string);
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;AAG3D,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,YAAY,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,oBAAoB;IACnC,gBAAgB,EAAE,MAAM,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;CACpB;AAGD,MAAM,WAAW,oBAAoB,CAAC,CAAC,GAAG,OAAO;IAC/C,OAAO,CAAC,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;IACjC,cAAc,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAC/C,QAAQ,CAAC,EAAE,CAAC,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;CACpB;AAGD,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,MAAM,EAAE,MAAM;CAO3B"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Thrown when the circuit is OPEN and no fallback is configured.
|
|
2
|
+
export class CircuitOpenError extends Error {
|
|
3
|
+
domain;
|
|
4
|
+
constructor(domain) {
|
|
5
|
+
super(`Circuit breaker is OPEN for domain: ${domain}`);
|
|
6
|
+
this.name = 'CircuitOpenError';
|
|
7
|
+
this.domain = domain;
|
|
8
|
+
// Fixes instanceof checks when output is downleveled past ES2022.
|
|
9
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA4BA,iEAAiE;AACjE,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAChC,MAAM,CAAS;IAExB,YAAY,MAAc;QACxB,KAAK,CAAC,uCAAuC,MAAM,EAAE,CAAC,CAAC;QACvD,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,kEAAkE;QAClE,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IACpD,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../../src/utils/backoff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAEjD,wBAAgB,gBAAgB,CAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAI,MAAM,CAKjF;AAED,wBAAgB,KAAK,CAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhD"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function calculateBackoff(attempt, config) {
|
|
2
|
+
const exponential = config.baseDelay * (2 ** attempt);
|
|
3
|
+
const capped = Math.min(config.maxDelay, exponential);
|
|
4
|
+
const jitter = Math.random() * capped;
|
|
5
|
+
return jitter;
|
|
6
|
+
}
|
|
7
|
+
export function sleep(ms) {
|
|
8
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backoff.js","sourceRoot":"","sources":["../../src/utils/backoff.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB,CAAE,OAAe,EAAE,MAAqB;IACpE,MAAM,WAAW,GAAG,MAAM,CAAC,SAAS,GAAG,CAAE,CAAC,IAAE,OAAO,CAAC,CAAC;IACrD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAE,MAAM,CAAC,QAAQ,EAAE,WAAW,CAAE,CAAC;IACxD,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC;IACtC,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,MAAM,UAAU,KAAK,CAAE,EAAU;IAC7B,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codingaryan/smoothapi",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "API resilience library — exponential backoff and circuit breaker for fetch",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["dist"],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"build:watch": "tsc --watch",
|
|
18
|
+
"test": "tsc -p tsconfig.test.json && node --test dist-test/tests/resilience.test.js",
|
|
19
|
+
"prepublishOnly": "npm run build"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.0.0",
|
|
23
|
+
"typescript": "^5.4.0"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=18.0.0"
|
|
27
|
+
},
|
|
28
|
+
"keywords": ["resilience", "circuit-breaker", "retry", "backoff", "fetch"],
|
|
29
|
+
"license": "MIT"
|
|
30
|
+
}
|