From 56d0cddc7311e7ac16084f6b5f62e0d8aa52351b Mon Sep 17 00:00:00 2001 From: ICEatm Date: Fri, 14 Jun 2024 12:50:55 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Updated=20function=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/services/http.service.ts | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/common/services/http.service.ts b/src/common/services/http.service.ts index 7bb9305..800f900 100644 --- a/src/common/services/http.service.ts +++ b/src/common/services/http.service.ts @@ -7,10 +7,25 @@ import { catchError, lastValueFrom } from 'rxjs'; export class HttpService { constructor(private readonly httpService: NestHttpService) {} + /** + * Performs a GET request to the specified URL. + * @param url - The URL to send the GET request to. + * @param config - Optional Axios request configuration. + * @returns A promise that resolves to an AxiosResponse. + * @throws {HttpException} Throws an HttpException if the request fails. + */ async get(url: string, config?: AxiosRequestConfig): Promise { return this.handleRequest(this.httpService.get(url, config)); } + /** + * Performs a POST request to the specified URL with the given data. + * @param url - The URL to send the POST request to. + * @param data - The data to send in the POST request. + * @param config - Optional Axios request configuration. + * @returns A promise that resolves to an AxiosResponse. + * @throws {HttpException} Throws an HttpException if the request fails. + */ async post( url: string, data: any, @@ -19,6 +34,14 @@ export class HttpService { return this.handleRequest(this.httpService.post(url, data, config)); } + /** + * Performs a PUT request to the specified URL with the given data. + * @param url - The URL to send the PUT request to. + * @param data - The data to send in the PUT request. + * @param config - Optional Axios request configuration. + * @returns A promise that resolves to an AxiosResponse. + * @throws {HttpException} Throws an HttpException if the request fails. + */ async put( url: string, data: any, @@ -27,6 +50,13 @@ export class HttpService { return this.handleRequest(this.httpService.put(url, data, config)); } + /** + * Performs a DELETE request to the specified URL. + * @param url - The URL to send the DELETE request to. + * @param config - Optional Axios request configuration. + * @returns A promise that resolves to an AxiosResponse. + * @throws {HttpException} Throws an HttpException if the request fails. + */ async delete( url: string, config?: AxiosRequestConfig, @@ -34,6 +64,12 @@ export class HttpService { return this.handleRequest(this.httpService.delete(url, config)); } + /** + * Handles the HTTP request and catches errors to throw appropriate HttpExceptions. + * @param request - The HTTP request observable. + * @returns A promise that resolves to an AxiosResponse. + * @throws {HttpException} Throws an HttpException with the appropriate status and message. + */ private async handleRequest(request: any): Promise { return await lastValueFrom( request.pipe(