import { HttpClient, HttpErrorResponse, HttpResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { ok, err, Result } from 'neverthrow'; @Injectable({ providedIn: 'root' }) export class HttpService { constructor(private http:HttpClient) { } async post(url: string, body: any): Promise> { try { const result = await this.http.post(url, body).toPromise() return ok (result as T) } catch (e) { return err(e as HttpErrorResponse) } } async get(url: string, options ={}): Promise> { try { const result = await this.http.get(url, options).toPromise() return ok (result as T) } catch (e) { return err(e as HttpErrorResponse) } } async put(url: string, body: any): Promise> { try { const result = await this.http.put(url, body).toPromise() return ok (result as T) } catch (e) { return err(e as HttpErrorResponse) } } async patch(url: string, body: any = {}): Promise> { try { const result = await this.http.patch(url, body).toPromise() return ok (result as T) } catch (e) { return err(e as HttpErrorResponse) } } async delete(url: string, body = {}): Promise> { const options = { body: body // Pass payload as the body of the request }; try { const result = await this.http.delete(url, options).toPromise() return ok (result as T) } catch (e) { return err(e as HttpErrorResponse) } } } export function isHttpResponse(data: any): data is HttpResponse { return typeof data.status == 'number'; }