Add new API url with fallback

This commit is contained in:
Flashfyre 2024-04-24 19:08:02 -04:00
parent d5e462ba7d
commit ad818aa314
3 changed files with 31 additions and 22 deletions

View File

@ -59,11 +59,7 @@ export default class LoginFormUiHandler extends FormModalUiHandler {
}; };
if (!this.inputs[0].text) if (!this.inputs[0].text)
return onFail(i18next.t('menu:emptyUsername')); return onFail(i18next.t('menu:emptyUsername'));
const contentType = 'application/x-www-form-urlencoded'; Utils.apiPost(`account/login`, `username=${this.inputs[0].text}&password=${this.inputs[1].text}`, '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)}` })
.then(response => { .then(response => {
if (!response.ok) if (!response.ok)
return response.text(); return response.text();

View File

@ -74,15 +74,11 @@ export default class RegistrationFormUiHandler extends FormModalUiHandler {
return onFail(this.getReadableErrorMessage('invalid password')); return onFail(this.getReadableErrorMessage('invalid password'));
if (this.inputs[1].text !== this.inputs[2].text) if (this.inputs[1].text !== this.inputs[2].text)
return onFail(i18next.t('menu:passwordNotMatchingConfirmPassword')); return onFail(i18next.t('menu:passwordNotMatchingConfirmPassword'));
const contentType = 'application/x-www-form-urlencoded'; Utils.apiPost(`account/register`, `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, '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)}` })
.then(response => response.text()) .then(response => response.text())
.then(response => { .then(response => {
if (!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 => { .then(response => {
if (!response.ok) if (!response.ok)
return response.text(); return response.text();

View File

@ -212,7 +212,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();
@ -233,7 +234,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) {
@ -241,24 +242,40 @@ 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'): 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,
'Content-Type': contentType, 'Content-Type': contentType,
}; };
const sId = getCookie(sessionIdKey); if (authed) {
if (sId) const sId = getCookie(sessionIdKey);
headers['Authorization'] = sId; if (sId)
fetch(`${apiUrl}/${path}`, { method: 'POST', headers: headers, body: data }) headers['Authorization'] = sId;
}
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));
});
}); });
} }