Initial commit.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import type { Session } from './types'
|
||||
|
||||
const SESSION_KEY = 'sapbg-session'
|
||||
|
||||
async function post(path: string, body: unknown): Promise<Session> {
|
||||
const res = await fetch(path, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data.error ?? 'request failed')
|
||||
return data as Session
|
||||
}
|
||||
|
||||
export function createGame(name: string): Promise<Session> {
|
||||
return post('/api/games', { name })
|
||||
}
|
||||
|
||||
export function joinGame(code: string, name: string): Promise<Session> {
|
||||
return post('/api/join', { code, name })
|
||||
}
|
||||
|
||||
export function loadSession(): Session | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(SESSION_KEY)
|
||||
return raw ? (JSON.parse(raw) as Session) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function saveSession(s: Session) {
|
||||
localStorage.setItem(SESSION_KEY, JSON.stringify(s))
|
||||
}
|
||||
|
||||
export function clearSession() {
|
||||
localStorage.removeItem(SESSION_KEY)
|
||||
}
|
||||
Reference in New Issue
Block a user