2024-06-04 09:31:37 +01:00
|
|
|
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
2020-10-30 15:22:35 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2024-06-04 09:31:37 +01:00
|
|
|
import { ok, err, Result } from 'neverthrow';
|
2020-10-30 15:22:35 +01:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class HttpService {
|
|
|
|
|
|
|
|
|
|
constructor(private http:HttpClient) { }
|
|
|
|
|
|
2024-06-04 09:31:37 +01:00
|
|
|
async post<T>(url: string, body: any): Promise<Result<T, HttpErrorResponse>> {
|
2020-10-30 15:22:35 +01:00
|
|
|
|
2024-06-04 09:31:37 +01:00
|
|
|
try {
|
|
|
|
|
const result = await this.http.post(url, body).toPromise()
|
|
|
|
|
return ok (result as T)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return err(e as HttpErrorResponse)
|
|
|
|
|
}
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:31:37 +01:00
|
|
|
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)
|
|
|
|
|
}
|
2020-10-30 15:22:35 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 09:31:37 +01:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|