
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
angular-http-handler
Advanced tools
An Angular library for handling HTTP requests with loading state management, error handling, retry logic, and fallback values. Supported Angular versions: 15, 16, 17, 18.
To install the library, run the following command:
npm install angular-http-handler
In your Angular component, you can apply the handle function to any HTTP request. Here's an example using HttpClient:
import { HttpClient } from '@angular/common/http';
import { Component, inject, OnInit } from '@angular/core';
import { handle } from 'angular-http-handler';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
private apiUrl = 'YOUR API URL';
http = inject(HttpClient);
loading = false;
response: any[] = [];
ngOnInit(): void {
this.http.get<any[]>(this.apiUrl).pipe(
handle(
(response) => {
this.response = response;
console.log(response);
},
(loading) => { // OPTIONAL - loading indicator
this.loading = loading;
console.log(loading);
},
[], // OPTIONAL - custom fallback value
(error) => { // OPTIONAL - custom error handler
console.log(error);
},
2, // OPTIONAL - retry count
1000, // OPTIONAL - retry delay
)
).subscribe();
}
}
The handle function manages HTTP requests with loading state, error handling and retry.
Parameters:
dataSetter: (response: T) => void Function to set the data when the request succeeds (e.g., this.data = response).
loadingSetter?: (loading: boolean) => void Function to set the loading state (e.g., this.loading = loading).
fallbackValue?: any Value that will be returned in case of error.
errorHandler?: (error: HttpErrorResponse) => void Optional function to handle errors (e.g., console.error(error)).
retryCount?: number Number of retries for failed requests. Defaults to 0.
retryDelay?: number Delay between retries in milliseconds.
Returns An Observable that handles the request, error, retry.
If you want to define custom default parameters you should do it in your root component before any http call.
defaultErrorHandler set default error handler for every request wrapped by handler. In case custom error handler is passed as a parameter to handle function it will overwrite the default one.defaultRetryCount and defaultRetryDelay set default number of retry in case of error and time between the calls.import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { configureHandler } from 'angular-http-handler';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
ngOnInit(): void {
configureHandler({
defaultErrorHandler: (error: HttpErrorResponse) => {
console.log('deafult handler', error);
},
defaultRetryCount: 0,
defaultRetryDelay: 0
});
}
}
The handle function returns a fallback response in case you define it as a 3rd parameter. In case you do not define it it will remain undefined and it will not trigger dataSetter function.
pendingRequestsCount()This function returns an Observable that emits the total number of pending requests. It helps track how many requests wrapped by handler are still in progress.
import { Component, OnDestroy, OnInit } from '@angular/core';
import { pendingRequestsCount } from 'angular-http-handler';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
subs = new Subscription;
ngOnInit(): void {
this.subs = pendingRequestsCount().subscribe(count => {
console.log('Pending request count: ', count);
});
}
ngOnDestroy(): void {
this.subs.unsubscribe();
}
}
FAQs
This is the http handler utility for Angular http requests
The npm package angular-http-handler receives a total of 7 weekly downloads. As such, angular-http-handler popularity was classified as not popular.
We found that angular-http-handler demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.