From 240005effae5872c8af3a0cc070d8b9014034ba6 Mon Sep 17 00:00:00 2001 From: vojvodicn23 Date: Sun, 15 Sep 2024 11:51:36 +0200 Subject: [PATCH] set loader optional --- README.md | 14 +++++++------- projects/angular-http-handler/README.md | 14 +++++++------- projects/angular-http-handler/package.json | 2 +- .../angular-http-handler/src/lib/http-handler.ts | 14 +++++++++++--- .../http-handler-example/src/app/app.component.ts | 8 ++++---- 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index f1eafe4..cb60651 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,14 @@ export class AppComponent implements OnInit { ngOnInit(): void { this.http.get(this.apiUrl).pipe( handle( - (loading) => { - this.loading = loading; - console.log(loading); - }, (response) => { this.response = response; console.log(response); }, + (loading) => { // OPTIONAL - loading indicator + this.loading = loading; + console.log(loading); + }, (error) => { // OPTIONAL - custom error handler console.log(error); }, @@ -80,12 +80,12 @@ The handle function manages HTTP requests with loading state, error handling and Parameters: -- loadingSetter: (loading: boolean) => void -Function to set the loading state (e.g., this.loading = loading). - - 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). + - errorHandler?: (error: HttpErrorResponse) => void Optional function to handle errors (e.g., console.error(error)). diff --git a/projects/angular-http-handler/README.md b/projects/angular-http-handler/README.md index f1eafe4..cb60651 100644 --- a/projects/angular-http-handler/README.md +++ b/projects/angular-http-handler/README.md @@ -45,14 +45,14 @@ export class AppComponent implements OnInit { ngOnInit(): void { this.http.get(this.apiUrl).pipe( handle( - (loading) => { - this.loading = loading; - console.log(loading); - }, (response) => { this.response = response; console.log(response); }, + (loading) => { // OPTIONAL - loading indicator + this.loading = loading; + console.log(loading); + }, (error) => { // OPTIONAL - custom error handler console.log(error); }, @@ -80,12 +80,12 @@ The handle function manages HTTP requests with loading state, error handling and Parameters: -- loadingSetter: (loading: boolean) => void -Function to set the loading state (e.g., this.loading = loading). - - 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). + - errorHandler?: (error: HttpErrorResponse) => void Optional function to handle errors (e.g., console.error(error)). diff --git a/projects/angular-http-handler/package.json b/projects/angular-http-handler/package.json index 645e8ff..55d57f5 100644 --- a/projects/angular-http-handler/package.json +++ b/projects/angular-http-handler/package.json @@ -1,6 +1,6 @@ { "name": "angular-http-handler", - "version": "0.0.1", + "version": "0.0.2", "description": "This is the http handler utility for Angular http requests", "keywords": ["http", "angular", "loading", "error-handler"], "author": { diff --git a/projects/angular-http-handler/src/lib/http-handler.ts b/projects/angular-http-handler/src/lib/http-handler.ts index 38fd9f8..c77bacf 100644 --- a/projects/angular-http-handler/src/lib/http-handler.ts +++ b/projects/angular-http-handler/src/lib/http-handler.ts @@ -2,15 +2,19 @@ import { HttpErrorResponse } from "@angular/common/http"; import { catchError, finalize, Observable, of, retry, tap } from "rxjs"; export function handle( - loadingSetter: (loading: boolean) => void, dataSetter: (response: T) => void, + loadingSetter?: (loading: boolean) => void, errorHandler?: (error: HttpErrorResponse) => void, retryCount: number = 0, retryDelay?: number, ): (source$: Observable) => Observable { return (source$: Observable) => source$.pipe( - tap(() => loadingSetter(true)), + tap(() => { + if(loadingSetter){ + loadingSetter(true); + } + }), retry({ count: retryCount, delay: retryDelay, @@ -33,6 +37,10 @@ export function handle( dataSetter(fallback); return of(fallback); }), - finalize(() => loadingSetter(false)) + finalize(() => { + if(loadingSetter){ + loadingSetter(false); + } + }) ); } \ No newline at end of file diff --git a/projects/http-handler-example/src/app/app.component.ts b/projects/http-handler-example/src/app/app.component.ts index 8a5a14c..7e1c80c 100644 --- a/projects/http-handler-example/src/app/app.component.ts +++ b/projects/http-handler-example/src/app/app.component.ts @@ -17,14 +17,14 @@ export class AppComponent implements OnInit { ngOnInit(): void { this.http.get(this.apiUrl).pipe( handle( - (loading) => { - this.loading = loading; - console.log(loading) - }, (response) => { this.response = response; console.log(response) }, + (loading) => { // OPTIONAL - loader indicator + this.loading = loading; + console.log(loading) + }, (e) => console.log(e, 'custom'), // OPTIONAL - custom error handler 2, // OPTIONAL - retry count 1000, // OTIONAL - retry delay