From 08067d993747cf14da03fe3b4ca537397bfd4e6b Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Tue, 7 May 2024 16:16:22 -0400 Subject: [PATCH] Revert "Revert API URL change" This reverts commit 4ef670733320ba23e1fa44e7abe28515a17f5f35. --- src/utils.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 822f02f05..ef277630d 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -222,7 +222,8 @@ export function executeIf(condition: boolean, promiseFunc: () => Promise): export const sessionIdKey = 'pokerogue_sessionId'; export const isLocal = window.location.hostname === 'localhost'; export const serverUrl = isLocal ? 'http://localhost:8001' : ''; -export const apiUrl = isLocal ? serverUrl : 'api'; +export const apiUrl = isLocal ? serverUrl : 'https://api.pokerogue.net'; +export const fallbackApiUrl = isLocal ? serverUrl : 'api'; export function setCookie(cName: string, cValue: string): void { const expiration = new Date(); @@ -243,7 +244,7 @@ export function getCookie(cName: string): string { return ''; } -export function apiFetch(path: string, authed: boolean = false): Promise { +export function apiFetch(path: string, authed: boolean = false, fallback: boolean = false): Promise { return new Promise((resolve, reject) => { const request = {}; if (authed) { @@ -251,13 +252,22 @@ export function apiFetch(path: string, authed: boolean = false): Promise resolve(response)) - .catch(err => reject(err)); + fetch(`${!fallback ? apiUrl : fallbackApiUrl}/${path}`, request) + .then(response => { + if (!response.ok && response.status === 404 && !fallback) + return apiFetch(path, authed, true).then(res => resolve(res)); + resolve(response); + }) + .catch(err => { + if (fallback) + reject(err); + else + apiFetch(path, authed, true).then(res => resolve(res)); + }); }); } -export function apiPost(path: string, data?: any, contentType: string = 'application/json', authed: boolean = false): Promise { +export function apiPost(path: string, data?: any, contentType: string = 'application/json', authed: boolean = false, fallback: boolean = false): Promise { return new Promise((resolve, reject) => { const headers = { 'Accept': contentType, @@ -268,9 +278,14 @@ export function apiPost(path: string, data?: any, contentType: string = 'applica if (sId) headers['Authorization'] = sId; } - fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data }) + fetch(`${!fallback ? apiUrl : fallbackApiUrl}/${path}`, { method: 'POST', headers: headers, body: data }) .then(response => resolve(response)) - .catch(err => reject(err)); + .catch(err => { + if (fallback) + reject(err); + else + apiPost(path, data, contentType, authed, true).then(res => resolve(res)); + }); }); }