Skip to content

Commit

Permalink
use url searchparam for profile request
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Sep 16, 2024
1 parent edb858d commit 74a49bc
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/modules/player-stats.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ const requestMethod = 'GET';

const playerStats = {
useTurnstile: false,
request: async (path, gameMode, body) => {
request: async (path, body) => {
try {
const method = body ? 'POST' : 'GET';
const response = await fetch(apiUrl + path, {
method,
body,
headers: {
'game-mode': gameMode,
}
});

if (response.status !== 200) {
Expand Down Expand Up @@ -46,16 +43,16 @@ const playerStats = {
// Create a form request to send the Turnstile token
// This avoids sending an extra pre-flight request
let body;
let searchParams = '';
let searchParams = `?gameMode=${gameMode}`;
if (turnstileToken) {
if (requestMethod === 'POST') {
body = new FormData();
body.append('Turnstile-Token', turnstileToken);
} else {
searchParams = `?token=${turnstileToken}`;
searchParams += `&token=${turnstileToken}`;
}
}
return playerStats.request(`/name/${searchString}${searchParams}`, gameMode, body).catch(error => {
return playerStats.request(`/name/${searchString}${searchParams}`, body).catch(error => {
if (error.message.includes('Malformed')) {
return Promise.reject(new Error('Error searching player profile; try removing one character from the end until the search works.'));
}
Expand All @@ -64,16 +61,16 @@ const playerStats = {
},
getProfile: async (accountId, gameMode = 'regular', turnstileToken) => {
let body;
let searchParams = '';
let searchParams = `?gameMode=${gameMode}`;
if (turnstileToken) {
if (requestMethod === 'POST') {
body = new FormData();
body.append('Turnstile-Token', turnstileToken);
} else {
searchParams = `?token=${turnstileToken}`;
searchParams += `&token=${turnstileToken}`;
}
}
return playerStats.request(`/account/${accountId}${searchParams}`, gameMode, body);
return playerStats.request(`/account/${accountId}${searchParams}`, body);
},
};

Expand Down

0 comments on commit 74a49bc

Please sign in to comment.