mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import { HttpClient, HttpErrorResponse } 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<T>(url: string, body: any): Promise<Result<T, HttpErrorResponse>> {
|
|
|
|
try {
|
|
const result = await this.http.post(url, body).toPromise()
|
|
return ok (result as T)
|
|
} catch (e) {
|
|
return err(e as HttpErrorResponse)
|
|
}
|
|
}
|
|
|
|
async get<T>(url: string): Promise<Result<T, HttpErrorResponse>> {
|
|
try {
|
|
const result = await this.http.get<T>(url).toPromise()
|
|
return ok (result as T)
|
|
} catch (e) {
|
|
return err(e as HttpErrorResponse)
|
|
}
|
|
}
|
|
|
|
async put<T>(url: string, body: any): Promise<Result<T, HttpErrorResponse>> {
|
|
|
|
try {
|
|
const result = await this.http.put<T>(url, body).toPromise()
|
|
return ok (result as T)
|
|
} catch (e) {
|
|
return err(e as HttpErrorResponse)
|
|
}
|
|
|
|
}
|
|
|
|
async delete<T>(url: string): Promise<Result<T, HttpErrorResponse>> {
|
|
|
|
try {
|
|
const result = await this.http.delete<T>(url).toPromise()
|
|
return ok (result as T)
|
|
} catch (e) {
|
|
return err(e as HttpErrorResponse)
|
|
}
|
|
}
|
|
}
|