added services and interfaces for expense types
This commit is contained in:
parent
bd18fecdff
commit
7bb21f8796
8 changed files with 142 additions and 5 deletions
40
src/app/services/http.ts
Normal file
40
src/app/services/http.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { firstValueFrom } from 'rxjs';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class Http {
|
||||
public constructor(private httpClient: HttpClient) { }
|
||||
|
||||
public async get<T>(url: string): Promise<T> {
|
||||
return this.request<T>(url, 'get');
|
||||
}
|
||||
|
||||
public async post<T>(url: string, body?: any): Promise<T> {
|
||||
return this.request<T>(url, 'post', body);
|
||||
}
|
||||
|
||||
public async put<T>(url: string, body?: any): Promise<T> {
|
||||
return this.request<T>(url, 'put', body);
|
||||
}
|
||||
|
||||
public async delete<T>(url: string): Promise<T> {
|
||||
return this.request<T>(url, 'delete');
|
||||
}
|
||||
|
||||
private async request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete', body?: any): Promise<T> {
|
||||
const headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' };
|
||||
switch (method) {
|
||||
case 'post':
|
||||
return firstValueFrom(this.httpClient.post<T>(url, body, { headers }));
|
||||
case 'put':
|
||||
return firstValueFrom(this.httpClient.put<T>(url, body, { headers }));
|
||||
case 'delete':
|
||||
return firstValueFrom(this.httpClient.delete<T>(url, { headers }));
|
||||
default:
|
||||
return firstValueFrom(this.httpClient.get<T>(url, { headers }));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue