Add new API url with fallback
parent
d5e462ba7d
commit
ad818aa314
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
39
src/utils.ts
39
src/utils.ts
|
@ -212,7 +212,8 @@ export function executeIf<T>(condition: boolean, promiseFunc: () => Promise<T>):
|
|||
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<Response> {
|
||||
export function apiFetch(path: string, authed: boolean = false, fallback: boolean = false): Promise<Response> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = {};
|
||||
if (authed) {
|
||||
|
@ -241,24 +242,40 @@ export function apiFetch(path: string, authed: boolean = false): Promise<Respons
|
|||
if (sId)
|
||||
request['headers'] = { 'Authorization': sId };
|
||||
}
|
||||
fetch(`${apiUrl}/${path}`, request)
|
||||
.then(response => 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<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) => {
|
||||
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));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue