From 7ccfb9a52cc78a95a4533ab4b971d959bdeecc1c Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Fri, 4 Jul 2025 22:37:36 -0700 Subject: add score json upload functionality --- frontend/src/utils/api.ts | 161 ------------------------------------- frontend/src/utils/authApi.ts | 163 ++++++++++++++++++++++++++++++++++++++ frontend/src/utils/scoreUpload.ts | 39 +++++++++ 3 files changed, 202 insertions(+), 161 deletions(-) delete mode 100644 frontend/src/utils/api.ts create mode 100644 frontend/src/utils/authApi.ts create mode 100644 frontend/src/utils/scoreUpload.ts (limited to 'frontend/src/utils') diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts deleted file mode 100644 index 528c170..0000000 --- a/frontend/src/utils/api.ts +++ /dev/null @@ -1,161 +0,0 @@ -const API_BASE_URL = import.meta.env.VITE_API_URL; - -// Auth API functions -export const authApi = { - async login(credentials: { username: string; password: string }) { - try { - const response = await fetch(`${API_BASE_URL}/authenticate`, { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(credentials), - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Login failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, - - async register(userData: { - username: string; - email: string; - password: string; - }) { - try { - const response = await fetch(`${API_BASE_URL}/register`, { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(userData), - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Registration failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, - - async logout() { - try { - const response = await fetch(`${API_BASE_URL}/logout`, { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Logout failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, - - async getSession() { - try { - const response = await fetch(`${API_BASE_URL}/session`, { - method: 'GET', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Session check failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, - - async getCurrentUser() { - try { - const response = await fetch(`${API_BASE_URL}/me`, { - method: 'GET', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Get current user failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, -}; - -export const infoApi = { - async getUsers() { - try { - const response = await fetch(`${API_BASE_URL}/users`, { - method: 'GET', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - }); - - const data = await response.json(); - - if (!response.ok) { - return { error: data.error || `HTTP ${response.status}` }; - } - - return { data }; - } catch (error) { - console.error('Get users failed:', error); - return { error: 'Network error. Please check your connection.' }; - } - }, -}; - -export interface User { - id: number; - username: string; - email: string; -} - -export interface SessionResponse { - authenticated: boolean; - user?: User; -} diff --git a/frontend/src/utils/authApi.ts b/frontend/src/utils/authApi.ts new file mode 100644 index 0000000..469553d --- /dev/null +++ b/frontend/src/utils/authApi.ts @@ -0,0 +1,163 @@ +const API_BASE_URL = import.meta.env.VITE_API_URL; + +// Auth API functions +export const authApi = { + async login(credentials: { username: string; password: string }) { + credentials.username = credentials.username.trim(); + try { + const response = await fetch(`${API_BASE_URL}/authenticate`, { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(credentials), + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Login failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, + + async register(userData: { + username: string; + email: string; + password: string; + }) { + userData.username = userData.username.trim(); + try { + const response = await fetch(`${API_BASE_URL}/register`, { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(userData), + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Registration failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, + + async logout() { + try { + const response = await fetch(`${API_BASE_URL}/logout`, { + method: 'POST', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Logout failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, + + async getSession() { + try { + const response = await fetch(`${API_BASE_URL}/session`, { + method: 'GET', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Session check failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, + + async getCurrentUser() { + try { + const response = await fetch(`${API_BASE_URL}/me`, { + method: 'GET', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Get current user failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, +}; + +export const infoApi = { + async getUsers() { + try { + const response = await fetch(`${API_BASE_URL}/users`, { + method: 'GET', + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + }, + }); + + const data = await response.json(); + + if (!response.ok) { + return { error: data.error || `HTTP ${response.status}` }; + } + + return { data }; + } catch (error) { + console.error('Get users failed:', error); + return { error: 'Network error. Please check your connection.' }; + } + }, +}; + +export interface User { + id: number; + username: string; + email: string; +} + +export interface SessionResponse { + authenticated: boolean; + user?: User; +} diff --git a/frontend/src/utils/scoreUpload.ts b/frontend/src/utils/scoreUpload.ts new file mode 100644 index 0000000..7fa7c86 --- /dev/null +++ b/frontend/src/utils/scoreUpload.ts @@ -0,0 +1,39 @@ +interface UploadScoreData { + meta: { + game: string; + service: string; + playtype: string; + }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + scores: any[]; +} + +interface UploadScoreResponse { + scoreCount: number; + message?: string; +} + +export async function uploadScore(data: UploadScoreData): Promise { + const response = await fetch(`${import.meta.env.VITE_API_URL}/uploadScore`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + meta: { + game: data.meta.game, + service: data.meta.service, + playtype: data.meta.playtype + }, + scores: data.scores + }) + }); + + if (!response.ok) { + const errorData = await response.json(); + throw new Error(errorData.error || 'Failed to upload scores'); + } + + return response.json(); +} -- cgit v1.2.3