export class AuthorizedServiceCaller { private serverURL: string = "http://192.168.1.218:8503"; //todo request deve fazer replace de ${serverURL} pelo conteúdo da variável private unauthorizedPath: string = "/"; constructor(serverURL: string, unauthorizedPath: string) { this.serverURL = serverURL; this.unauthorizedPath = unauthorizedPath; } async requestService(request: AuthorizedRequest, callback?: Function) { const token = localStorage.getItem('token'); const body = request?.body; const method = request?.method; let headers; if (!token) { window.location.pathname = this.unauthorizedPath; } else { headers = { 'Authorization': `${token}`, 'Content-Type': 'application/json', 'Customer-Host': 'http://localhost:8080/sankhyaw' } } const response: void | Response = await await fetch(this.serverURL, { method: method, headers: headers, body: body }) .catch(e => { console.log(e); }); if (response instanceof Response && response.status === 401) { localStorage.removeItem('token'); window.location.pathname = this.unauthorizedPath; } if (callback && response instanceof Response) { callback(response.json().then()); } else { return response; } } } export interface AuthorizedRequest { url: string, body: string, method: string }