Revert "Revert API URL change"

This reverts commit 4ef6707333.
pull/560/head
Flashfyre 2024-05-07 16:16:22 -04:00
parent bd839a1ffb
commit 08067d9937
1 changed files with 23 additions and 8 deletions

View File

@ -222,7 +222,8 @@ export function executeIf<T>(condition: boolean, promiseFunc: () => Promise<T>):
export const sessionIdKey = 'pokerogue_sessionId'; export const sessionIdKey = 'pokerogue_sessionId';
export const isLocal = window.location.hostname === 'localhost'; export const isLocal = window.location.hostname === 'localhost';
export const serverUrl = isLocal ? 'http://localhost:8001' : ''; 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 { export function setCookie(cName: string, cValue: string): void {
const expiration = new Date(); const expiration = new Date();
@ -243,7 +244,7 @@ export function getCookie(cName: string): string {
return ''; return '';
} }
export function apiFetch(path: string, authed: boolean = false): Promise<Response> { export function apiFetch(path: string, authed: boolean = false, fallback: boolean = false): Promise<Response> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const request = {}; const request = {};
if (authed) { if (authed) {
@ -251,13 +252,22 @@ export function apiFetch(path: string, authed: boolean = false): Promise<Respons
if (sId) if (sId)
request['headers'] = { 'Authorization': sId }; request['headers'] = { 'Authorization': sId };
} }
fetch(`${apiUrl}/${path}`, request) fetch(`${!fallback ? apiUrl : fallbackApiUrl}/${path}`, request)
.then(response => resolve(response)) .then(response => {
.catch(err => reject(err)); 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<Response> { export function apiPost(path: string, data?: any, contentType: string = 'application/json', authed: boolean = false, fallback: boolean = false): Promise<Response> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const headers = { const headers = {
'Accept': contentType, 'Accept': contentType,
@ -268,9 +278,14 @@ export function apiPost(path: string, data?: any, contentType: string = 'applica
if (sId) if (sId)
headers['Authorization'] = 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)) .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));
});
}); });
} }