fuse-http-client
What is this repository for?
A utility that allows retrying a function with an exponential delay between attempts - inspired from package
Installation
@appfire/fuse-http-client
Usage
import { FuseHttpClient } from '@appfire/fuse-http-client';
type Body = {
test: string
}
try {
type Body = {
test: string
}
type Response = {
data: string
}
const appKey = 'org.swift.confluence.refapp';
const serviceUrl = 'https://services.sand.bobswift.appfire.app/snapshot/utils/2.0/analytics'
const appApiUrl = 'https://apiconfluence-ref-app-dev.sand-bobswift.appfire.app/api/helloworld'
const fuseConfluenceAnalytics = FuseHttpClient.createInstance('Confluence', {
appKey: appKey,
serviceUrl: serviceUrl,
backOffOptions: {
delayFirstAttempt: true,
jitter: 'full',
logRetry: true,
numOfAttempts: 5,
startingDelay: 100,
timeMultiple: 5
}
})
// Response
// Body
// Params // optional
const responsePost = await fuseConfluenceAnalytics.post<Response,Body>({
test: 'sendAnalytics'
})
console.log("fuseAnalytics", responsePost.data);
const fuseConfluenceApp = FuseHttpClient.createInstance('Confluence', {
appKey: appKey,
serviceUrl: appApiUrl,
backOffOptions: {
delayFirstAttempt: true,
jitter: 'full',
logRetry: true,
numOfAttempts: 5,
startingDelay: 100,
timeMultiple: 5
}
})
// Response
// Params //optional
const responseGet = await fuseConfluenceApp.get<Response>();
console.log("fuseAnalytics", responseGet.data);
} catch (error) {
console.error('Final error', error);
}
BackOffOptions
delayFirstAttempt?: boolean
Decides whether the
startingDelay
should be applied before the first call. Iffalse
, the first call will occur without a delay.Default value is
false
.jitter?: JitterType | string
Decides whether a jitter should be applied to the delay. Possible values are
full
andnone
.Default value is
none
.maxDelay?: number
The maximum delay, in milliseconds, between two consecutive attempts.
Default value is
Infinity
.numOfAttempts?: number
The maximum number of times to attempt the function.
Default value is
10
.Minimum value is
1
.retry?: (e: any, attemptNumber: number) => boolean
The
retry
function can be used to run logic after every failed attempt (e.g. logging a message, assessing the last error, etc.). It is called with the last error and the upcoming attempt number. Returningtrue
will retry the function as long as thenumOfAttempts
has not been exceeded. Returningfalse
will end the execution.Default value is a function that always returns
true
.startingDelay?: number
The delay, in milliseconds, before executing the function for the first time.
Default value is
100
ms.timeMultiple?: number
The
startingDelay
is multiplied by thetimeMultiple
to increase the delay between reattempts.Default value is
2
.