From ad818aa3148ae16478e232bbf79af52876317f8a Mon Sep 17 00:00:00 2001 From: Flashfyre Date: Wed, 24 Apr 2024 19:08:02 -0400 Subject: [PATCH] Add new API url with fallback --- src/ui/login-form-ui-handler.ts | 6 +--- src/ui/registration-form-ui-handler.ts | 8 ++---- src/utils.ts | 39 ++++++++++++++++++-------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/ui/login-form-ui-handler.ts b/src/ui/login-form-ui-handler.ts index e5af320ab..27de95463 100644 --- a/src/ui/login-form-ui-handler.ts +++ b/src/ui/login-form-ui-handler.ts @@ -59,11 +59,7 @@ export default class LoginFormUiHandler extends FormModalUiHandler { }; if (!this.inputs[0].text) return onFail(i18next.t('menu:emptyUsername')); - const contentType = 'application/x-www-form-urlencoded'; - const headers = { - 'Content-Type': contentType, - }; - fetch(`${Utils.apiUrl}/account/login`, { method: 'POST', headers: headers, body: `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}` }) + Utils.apiPost(`account/login`, `username=${this.inputs[0].text}&password=${this.inputs[1].text}`, 'application/x-www-form-urlencoded') .then(response => { if (!response.ok) return response.text(); diff --git a/src/ui/registration-form-ui-handler.ts b/src/ui/registration-form-ui-handler.ts index 239f8aba1..f5ff8c218 100644 --- a/src/ui/registration-form-ui-handler.ts +++ b/src/ui/registration-form-ui-handler.ts @@ -74,15 +74,11 @@ export default class RegistrationFormUiHandler extends FormModalUiHandler { return onFail(this.getReadableErrorMessage('invalid password')); if (this.inputs[1].text !== this.inputs[2].text) return onFail(i18next.t('menu:passwordNotMatchingConfirmPassword')); - const contentType = 'application/x-www-form-urlencoded'; - const headers = { - 'Content-Type': contentType, - }; - fetch(`${Utils.apiUrl}/account/register`, { method: 'POST', headers: headers, body: `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}` }) + Utils.apiPost(`account/register`, `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, 'application/x-www-form-urlencoded') .then(response => response.text()) .then(response => { if (!response) { - fetch(`${Utils.apiUrl}/account/login`, { method: 'POST', headers: headers, body: `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}` }) + Utils.apiPost(`account/login`, `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, 'application/x-www-form-urlencoded') .then(response => { if (!response.ok) return response.text(); diff --git a/src/utils.ts b/src/utils.ts index 3cbbfd842..c90633ca3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -212,7 +212,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(); @@ -233,7 +234,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) { @@ -241,24 +242,40 @@ 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'): 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, 'Content-Type': contentType, }; - const sId = getCookie(sessionIdKey); - if (sId) - headers['Authorization'] = sId; - fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data }) + if (authed) { + const sId = getCookie(sessionIdKey); + if (sId) + headers['Authorization'] = sId; + } + 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)); + }); }); }