Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
nestjs-axios-retry
Advanced tools
A NestJS module that provides Axios with configurable retry functionality, leveraging axios-retry for enhanced HTTP request resilience.
NestJS Axios Retry Module
nestjs-axios-retry is a module for NestJS that adds retry functionality to Axios HTTP requests. It leverages axios-retry to provide configurable options for retrying failed HTTP requests, making your NestJS applications more resilient to transient errors.
Features
Installation
npm install nestjs-axios-retry axios-retry
Usage First, import and configure AxiosRetryModule in your NestJS module:
import { Module } from '@nestjs/common';
import { AxiosRetryModule } from 'nestjs-axios-retry';
import axiosRetry from 'axios-retry';
@Module({
imports: [
AxiosRetryModule.forRoot({
axiosRetryConfig: {
retries: 5,
retryDelay: axiosRetry.exponentialDelay,
shouldResetTimeout: true,
retryCondition: (error) => error.response.status === 429,
onRetry: (retryCount, error, requestConfig) => {
console.log(`Retrying request attempt ${retryCount}`);
},
},
}),
],
// ... Other configurations
})
export class AppModule {}
Then, inject the HttpService provided by NestJS into your service or controller:
Basic Usage
Use the HttpService provided by NestJS to make HTTP requests. The retry functionality is automatically applied based on your module configuration.
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class AppService {
constructor(private httpService: HttpService) {}
async fetchData() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data'),
);
return response.data;
}
}
Advanced Usage
Custom Retry Configuration You can customize the retry behavior for individual requests:
async fetchDataWithCustomRetry() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 5,
retryDelay: (retryCount) => retryCount * 1000,
},
}),
);
return response.data;
}
Exponential Backoff Retry For exponential backoff strategy:
async fetchDataWithExponentialBackoff() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
},
}),
);
return response.data;
}
Specific Retry Condition Customize the condition under which a
retry should occur:
async fetchDataWithRetryCondition() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
retryCondition: (error) => error.response.status === 503,
},
}),
);
return response.data;
}
OnRetry Callback Implement custom actions on each retry:
async fetchDataWithOnRetryCallback() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
onRetry: (retryCount, error, requestConfig) => {
console.log(`Retrying request attempt ${retryCount}`);
},
},
}),
);
return response.data;
}
// Basic fetch data example
async fetchData() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data'),
);
return response.data;
}
// Fetch with custom retry configuration
async fetchDataWithCustomRetry() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 5,
retryDelay: (retryCount) => retryCount * 1000,
},
}),
);
return response.data;
}
// Fetch with exponential backoff retry
async fetchDataWithExponentialBackoff() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
retryDelay: axiosRetry.exponentialDelay,
},
}),
);
return response.data;
}
// Fetch with specific retry condition
async fetchDataWithRetryCondition() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
retryCondition: (error) => error?.response?.status === 503,
},
}),
);
return response.data;
}
// Fetch with onRetry callback
async fetchDataWithOnRetryCallback() {
const response = await firstValueFrom(
this.httpService.get('https://example.com/data', {
'axios-retry': {
retries: 3,
onRetry: (retryCount, error, requestConfig) => {
console.log(`Retrying request attempt ${retryCount}`);
},
},
}),
);
return response.data;
}
Contributing Contributions to nestjs-axios-retry are welcome! Please refer to the project's issues and pull request sections for more details.
License This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A NestJS module that provides Axios with configurable retry functionality, leveraging axios-retry for enhanced HTTP request resilience.
The npm package nestjs-axios-retry receives a total of 4,173 weekly downloads. As such, nestjs-axios-retry popularity was classified as popular.
We found that nestjs-axios-retry demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.