mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 05:16:07 +00:00
sync message on recoonect on ui
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
|
||||
import { HttpResult } from './type';
|
||||
import { Result } from 'neverthrow';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
|
||||
export abstract class HttpAdapter {
|
||||
abstract post<T>(url: string, body: any): Promise<Result<HttpResult<T>, HttpErrorResponse>>
|
||||
abstract get<T>(url: string, options?: Object): Promise<Result<HttpResult<T>, HttpErrorResponse>>
|
||||
abstract put<T>(url: string, body: any): Promise<Result<HttpResult<T>, HttpErrorResponse>>
|
||||
abstract patch<T>(url: string, body?: Object): Promise<Result<HttpResult<T>, HttpErrorResponse>>
|
||||
abstract delete<T>(url: string, body?: Object): Promise<Result<HttpResult<T>, HttpErrorResponse>>
|
||||
abstract listen():Observable<Result<HttpResult<any>, HttpErrorResponse>>
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
|
||||
import { HttpAdapter } from './adapter';
|
||||
import { HttpService } from './http.service';
|
||||
|
||||
@NgModule({
|
||||
imports: [],
|
||||
declarations: [],
|
||||
schemas: [],
|
||||
providers: [
|
||||
{
|
||||
provide: HttpAdapter,
|
||||
useClass: HttpService, // or MockDataService
|
||||
}
|
||||
],
|
||||
entryComponents: []
|
||||
})
|
||||
export class HttpModule {}
|
||||
@@ -0,0 +1,123 @@
|
||||
import { HttpErrorResponse, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { ok, err, Result } from 'neverthrow';
|
||||
import { HttpResult } from './type';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class HttpService {
|
||||
|
||||
private responseSubject = new BehaviorSubject<Result<HttpResult<any>, HttpErrorResponse>>(null);
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
|
||||
async post<T>(url: string, body: any): Promise<Result<HttpResult<T>, HttpErrorResponse>> {
|
||||
try {
|
||||
const response = await this.http.post<T>(url, body, { observe: 'response' }).toPromise();
|
||||
const data = {
|
||||
data: response.body,
|
||||
status: response.status,
|
||||
headers: response.headers,
|
||||
url: response.url || url
|
||||
}
|
||||
this.responseSubject.next(ok(data))
|
||||
return ok(data);
|
||||
} catch (e) {
|
||||
this.responseSubject.next(err(e))
|
||||
return err(e as HttpErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
async get<T>(url: string, options = {}): Promise<Result<HttpResult<T>, HttpErrorResponse>> {
|
||||
try {
|
||||
const response = await this.http.get<T>(url, { ...options, observe: 'response' }).toPromise();
|
||||
|
||||
const data = {
|
||||
data: response.body,
|
||||
status: response.status,
|
||||
headers: response.headers,
|
||||
url: response.url || url
|
||||
}
|
||||
|
||||
this.responseSubject.next(ok(data))
|
||||
return ok(data);
|
||||
} catch (e) {
|
||||
this.responseSubject.next(err(e))
|
||||
return err(e as HttpErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
async put<T>(url: string, body: any): Promise<Result<HttpResult<T>, HttpErrorResponse>> {
|
||||
try {
|
||||
const response = await this.http.put<T>(url, body, { observe: 'response' }).toPromise();
|
||||
|
||||
const data = {
|
||||
data: response.body,
|
||||
status: response.status,
|
||||
headers: response.headers,
|
||||
url: response.url || url
|
||||
}
|
||||
|
||||
this.responseSubject.next(ok(data))
|
||||
return ok(data);
|
||||
} catch (e) {
|
||||
this.responseSubject.next(err(e))
|
||||
return err(e as HttpErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
async patch<T>(url: string, body: any = {}): Promise<Result<HttpResult<T>, HttpErrorResponse>> {
|
||||
try {
|
||||
const response = await this.http.patch<T>(url, body, { observe: 'response' }).toPromise();
|
||||
|
||||
const data = {
|
||||
data: response.body,
|
||||
status: response.status,
|
||||
headers: response.headers,
|
||||
url: response.url || url
|
||||
}
|
||||
|
||||
this.responseSubject.next(ok(data))
|
||||
return ok(data);
|
||||
} catch (e) {
|
||||
this.responseSubject.next(err(e))
|
||||
return err(e as HttpErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
async delete<T>(url: string, body = {}): Promise<Result<HttpResult<T>, HttpErrorResponse>> {
|
||||
const options = {
|
||||
body: body, // Pass payload as the body of the request
|
||||
observe: 'response' as 'body'
|
||||
};
|
||||
|
||||
try {
|
||||
const response: any = await this.http.delete<T>(url, options).toPromise();
|
||||
|
||||
const data = {
|
||||
data: response?.body,
|
||||
status: response?.status,
|
||||
headers: response?.headers,
|
||||
url: response?.url || url
|
||||
}
|
||||
|
||||
this.responseSubject.next(ok(data))
|
||||
return ok(data as any);
|
||||
} catch (e) {
|
||||
this.responseSubject.next(err(e))
|
||||
return err(e as HttpErrorResponse);
|
||||
}
|
||||
}
|
||||
|
||||
listen() {
|
||||
return this.responseSubject.asObservable()
|
||||
}
|
||||
}
|
||||
|
||||
export function isHttpResponse(data: any): data is HttpResponse<any> {
|
||||
return typeof data.status == 'number';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
|
||||
export interface Options {
|
||||
headers?: HttpHeaders
|
||||
params?: HttpParams
|
||||
}
|
||||
|
||||
export interface HttpResult<T> {
|
||||
data: T | null;
|
||||
status: number;
|
||||
headers: HttpHeaders;
|
||||
url: string;
|
||||
}
|
||||
Reference in New Issue
Block a user