import { ISessionStorageHelper } from "../helpers"; import { TokenPrefix } from "../types"; import { ApiResponse } from "../apiResponse"; import { ApiError } from ".."; export abstract class AbstractApiService { sessionStorageHelper: ISessionStorageHelper; abstract tokenPrefix: TokenPrefix; abstract baseUrl: string; constructor(sessionStorageHelper: ISessionStorageHelper) { this.sessionStorageHelper = sessionStorageHelper; } protected async GetResponseFromJsonAsync(url: string) : Promise> { const response = new ApiResponse(); try { await this.ReponseAsync(url).then(async (resp) => { if(resp.ok) response.data = await resp.json() as T; else response.error = await resp.json() as ApiError; }).catch((error) => { response.error = error as ApiError; }); } catch (error) { response.error = error; } return response; } protected async GetResponseFromTextAsync(url: string) : Promise> { const response = new ApiResponse(); try { await this.ReponseAsync(url).then(async (resp) => { if(resp.ok) response.data = await resp.text(); else response.error = await resp.json() as ApiError; }).catch((error) => { response.error = error as ApiError; }); } catch (error) { response.error = error; } return response; } protected async PostResponseAsync(url: string, body: unknown) : Promise> { const response = new ApiResponse(); try { await this.ReponseWithBodyAsync(url, 'POST', body).then(async (resp) => { if(resp.ok) response.data = resp; else response.error = await resp.json() as ApiError; }).catch((error) => { response.error = error as ApiError; }); } catch(error) { response.error = error; } return response; } protected async PutResponseAsync(url: string, body: unknown) : Promise> { const response = new ApiResponse(); try { await this.ReponseWithBodyAsync(url, 'PUT', body).then(async resp => { if(resp.ok) response.data = resp; else response.error = await resp.json() as ApiError; }) .catch(error => { response.error = error as ApiError; }); } catch (error) { response.error = error; } return response; } protected async DeleteResponseWithBodyAsync(url: string, body: unknown) : Promise> { const response = new ApiResponse(); try { await this.ReponseWithBodyAsync(url, 'DELETE', body).then(async resp => { if (resp.ok) response.data = resp; else response.error = await resp.json() as ApiError; }) .catch(error => { response.error = error as ApiError; }); } catch (error) { response.error = error; } return response; } protected async DeleteResponseAsync(url: string) : Promise> { const response = new ApiResponse(); try { await this.ReponseAsync(url, 'DELETE').then(async resp => { if (resp.ok) response.data = resp; else response.error = await resp.json() as ApiError; }) .catch(error => { response.error = error; }); } catch (error) { response.error = error; } return response; } private async ReponseWithBodyAsync(url:string, method: string, body: unknown) { const token = await this.sessionStorageHelper.GetToken(this.tokenPrefix); return await fetch(`${this.baseUrl}${url}`, { method: `${method}`, body: JSON.stringify(body), headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } }); } private async ReponseAsync(url:string, method = 'GET') { const token = await this.sessionStorageHelper.GetToken(this.tokenPrefix) return await fetch(`${this.baseUrl}${url}`, { method: `${method}`, headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token } }); } }