space-smasher-9001/game/src/api.ts
2023-03-01 03:36:09 +01:00

77 lines
1.4 KiB
TypeScript

const baseApiPath = process.env.API_PATH ?? "http://localhost:3000"
export default {
async post(path: string, data?: any) {
const headers = {
'Content-Type': 'application/json'
}
if (window.myStuff.token) {
headers['Authorization'] = `Bearer ${window.myStuff.token}`
}
let response;
try {
response = await fetch(`${baseApiPath}/${path}`, {
method: "POST",
headers,
body: JSON.stringify(data)
})
} catch (err) {
console.log("oops");
console.error(err)
}
return response.json()
},
async get(path: string, data?: any) {
const headers = {
'Content-Type': 'application/json'
}
if (window.myStuff.token) {
headers['Authorization'] = `Bearer ${window.myStuff.token}`
}
let response;
try {
response = await fetch(`${baseApiPath}/${path}`, {
method: "GET",
headers,
body: JSON.stringify(data)
})
} catch (err) {
console.log("oops");
console.error(err)
}
return response.json()
},
async signup(name: string) {
return this.post("signup", { name })
},
async login(key: string) {
return this.post("login", { key })
},
async record(points: number, shoots: number, time: number) {
return this.post("record", {
points,
shoots,
time
})
},
async top() {
return this.get("top")
},
async start() {
return this.get("start")
}
}