improve api interface

This commit is contained in:
Peter Maquiran
2023-12-27 10:37:32 +01:00
parent 9e4d5dbf82
commit eea0ecc971
4 changed files with 236 additions and 22 deletions
+29 -15
View File
@@ -1,6 +1,6 @@
import { HttpContext, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { tap, shareReplay, catchError } from "rxjs/operators";
import { Observable, of } from "rxjs";
@@ -11,8 +11,7 @@ export class HttpServiceService {
constructor(private http: HttpClient) {}
put(url: string, body: any | null, options: Options): Observable<any> {
put<T>(url: string, body: any | null, options: Options): Observable<T> {
return this.http.put(url, body, options as any).pipe(
tap((response) => {
// Handle success response if needed
@@ -24,20 +23,35 @@ export class HttpServiceService {
);
}
post<T>(url: string, body: any | null, options: Options): Observable<T> {
return this.http.post(url, body, options as any).pipe(
tap((response) => {
// Handle success response if needed
}),
catchError((error) => {
// Handle error response if needed
return of(error);
})
);
}
get<T>(url: string, options: Options): Observable<T> {
return this.http.get(url, options ).pipe(
tap((response) => {
// Handle success response if needed
}),
catchError((error) => {
// Handle error response if needed
return of(error);
})
);
}
}
interface Options {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
reportProgress?: boolean;
responseType?: 'arraybuffer';
withCredentials?: boolean;
headers?: HttpHeaders
params?: HttpParams
}