-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
240005e
commit 5fd944d
Showing
7 changed files
with
194 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,64 @@ | ||
import { HttpErrorResponse } from "@angular/common/http"; | ||
import { catchError, finalize, Observable, of, retry, tap } from "rxjs"; | ||
|
||
let defaultErrorHandler: ((error: HttpErrorResponse) => void) | undefined; | ||
export function setDefaultErrorHandler(handler: (error: HttpErrorResponse) => void) { | ||
defaultErrorHandler = handler; | ||
} | ||
|
||
let defaultRetryCount: number = 0; | ||
export function setDefaultRetryCount(count: number) { | ||
defaultRetryCount = count; | ||
} | ||
|
||
let defaultRetryDelay: number = 0; | ||
export function setDefaultRetryDelay(delay: number) { | ||
defaultRetryDelay = delay; | ||
} | ||
|
||
export function handle<T>( | ||
dataSetter: (response: T) => void, | ||
loadingSetter?: (loading: boolean) => void, | ||
fallbackValue?: any, | ||
errorHandler?: (error: HttpErrorResponse) => void, | ||
retryCount: number = 0, | ||
retryDelay?: number, | ||
retryCount: number = defaultRetryCount, | ||
retryDelay: number = defaultRetryDelay, | ||
): (source$: Observable<T>) => Observable<T> { | ||
return (source$: Observable<T>) => | ||
source$.pipe( | ||
tap(() => { | ||
if(loadingSetter){ | ||
loadingSetter(true); | ||
} | ||
}), | ||
return (source$: Observable<T>) => { | ||
if(loadingSetter){ | ||
loadingSetter(true); | ||
} | ||
return source$.pipe( | ||
|
||
retry({ | ||
count: retryCount, | ||
delay: retryDelay, | ||
resetOnSuccess: true | ||
}), | ||
|
||
tap((data: T) => dataSetter(data)), | ||
catchError((error) => { | ||
/* const errorMsg = error.error instanceof ErrorEvent | ||
? `Error: ${error.error.message}` // Client-side error | ||
: `Error Code: ${error.status}, Message: ${error.message}`; // Server-side error | ||
console.error(errorMsg); */ | ||
|
||
// Call the provided error handler if it exists | ||
if (errorHandler) { | ||
errorHandler(error); | ||
} | ||
|
||
const fallback = (Array.isArray([] as unknown as T) ? [] : null) as T; | ||
|
||
dataSetter(fallback); | ||
return of(fallback); | ||
|
||
catchError((error: HttpErrorResponse, caught: Observable<T>) => { | ||
if (errorHandler) { | ||
errorHandler(error); | ||
} else if (defaultErrorHandler) { | ||
defaultErrorHandler(error); | ||
} | ||
|
||
if(fallbackValue !== undefined){ | ||
dataSetter(fallbackValue as T); | ||
return of(fallbackValue as T); | ||
} | ||
return of(null as T); | ||
}), | ||
|
||
finalize(() => { | ||
if(loadingSetter){ | ||
loadingSetter(false); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div *ngIf="loading"> | ||
Loading... | ||
</div> | ||
<div *ngIf="!loading"> | ||
{{response | json}} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,46 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; | ||
import { Component, inject, OnInit } from '@angular/core'; | ||
import { handle } from 'angular-http-handler'; | ||
import { handle, setDefaultErrorHandler, setDefaultRetryCount, setDefaultRetryDelay } from 'angular-http-handler'; | ||
|
||
interface Post { | ||
userId: number; | ||
id: number; | ||
title: string; | ||
body: string; | ||
} | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'] | ||
}) | ||
export class AppComponent implements OnInit { | ||
private apiUrl = 'YOUR API URL'; | ||
private apiUrl = 'https://jsonplaceholder.typicode.com/posts'; | ||
http = inject(HttpClient); | ||
|
||
loading = false; | ||
response: any[] = []; | ||
response: any = null; | ||
|
||
ngOnInit(): void { | ||
this.http.get<any[]>(this.apiUrl).pipe( | ||
handle( | ||
(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 | ||
) | ||
).subscribe(); | ||
setDefaultErrorHandler((error: HttpErrorResponse) => { | ||
console.log('deafult handler', error) | ||
}); | ||
setDefaultRetryCount(0); | ||
setDefaultRetryDelay(500); | ||
|
||
this.http.get<Post[]>(this.apiUrl).pipe(handle( | ||
(response) => { | ||
this.response = response; | ||
console.log(response) | ||
}, | ||
(loading) => { // OPTIONAL - loader indicator | ||
this.loading = loading; | ||
console.log(loading) | ||
}, | ||
[], // OPTIONAL - custom fallback value | ||
(e) => console.log(e, 'custom'), // OPTIONAL - custom error handler | ||
2, // OPTIONAL - retry count | ||
1000, // OTIONAL - retry delay | ||
)).subscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters