Skip to content

SriApiRetry.js is a JavaScript library that simplifies the implementation of retry logic for API calls. It handles scenarios where API requests may fail due to network issues, server errors, or rate limits, and automatically retries the requests with customizable backoff strategies.

Notifications You must be signed in to change notification settings

SankarSrinivasan1/SriAPIretry.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

SriApiRetry.js

SriApiRetry.js is a JavaScript library that simplifies the implementation of retry logic for API calls. It handles scenarios where API requests may fail due to network issues, server errors, or rate limits, and automatically retries the requests with customizable backoff strategies. By integrating SriApiRetry.js into your application, you can enhance the reliability and resilience of your API interactions.

Features

  • Automatic retry of API requests that fail due to various error conditions.
  • Customizable retry options including maximum attempts, backoff strategies, and retryable errors.
  • Exponential and linear backoff strategies for delaying retries.
  • Simple and straightforward API for making API requests with retry logic.

Installation

You can install SriApiRetry.js using npm:

npm install sri-api-retry

Alternatively, you can include the library in your HTML file:

<script src="path/to/sri-api-retry.js"></script>

Usage

  1. Import or include SriApiRetry.js into your JavaScript file.
import SriApiRetry from 'sri-api-retry'; // If using ES modules

or

<script src="path/to/sri-api-retry.js"></script> <!-- If including via <script> tag -->
  1. Create an instance of SriApiRetry with desired options.
const apiRetry = new SriApiRetry({
  maxAttempts: 5,
  backoffStrategy: 'exponential',
  retryableErrors: ['500', '502', '503'],
});
  1. Use the request method to make API requests with retry logic.
const apiUrl = 'https://example.com/api/endpoint';
const requestOptions = {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
  },
};

apiRetry
  .request(apiUrl, requestOptions)
  .then((response) => {
    console.log('API response:', response);
    // Handle the API response
  })
  .catch((error) => {
    console.error('API request failed:', error);
    // Handle the API request failure
  });

Configuration Options

The SriApiRetry constructor accepts an optional options object with the following properties:

  • maxAttempts (number, default: 3): The maximum number of retry attempts for a failed API request.
  • backoffStrategy (string, default: 'exponential'): The backoff strategy to use for delaying retries. Possible values are 'exponential' or 'linear'.
  • retryableErrors (array of strings, default: []): An array of error codes or messages that should trigger a retry of the API request.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.

License

This project is licensed under the MIT License.

That's it! You can now use SriApiRetry.js to simplify the implementation of retry logic for your API calls and enhance the reliability of your application's API interactions.

Blog post

https://sankarsrinivasan.stck.me/post/100401/SriApiRetry-js-Simplify-API-Retry-Logic

About

SriApiRetry.js is a JavaScript library that simplifies the implementation of retry logic for API calls. It handles scenarios where API requests may fail due to network issues, server errors, or rate limits, and automatically retries the requests with customizable backoff strategies.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published