-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata.js
48 lines (41 loc) · 1.01 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import useSWR from "swr"
const fetcher = async (url) => {
const res = await fetch(url)
if (!res.ok) {
console.log("error", res.status)
const err = new Error("The server responded with an error")
err.info = await res.json()
err.status = res.status
console.log(err)
throw err
}
return await res.json()
}
const wrapper = (url, options) => {
const { data, error } = useSWR(url, fetcher, options)
return {
data,
isLoading: !error && !data,
isError: error,
}
}
export function server(options = {}) {
return wrapper("/api/server/", options)
}
export function players(options = {}) {
return wrapper("/api/players/", options)
}
export async function maps(options = {}) {
const res = await fetch(
`http://${process.env.NEXT_PUBLIC_API}/servers/main/static/maps.json`,
options
)
return await res.json()
}
export async function discord(options = {}) {
const res = await fetch(
`http://${process.env.NEXT_PUBLIC_API}/discord`,
options
)
return await res.json()
}