init
This commit is contained in:
16
client/src/services/api.ts
Normal file
16
client/src/services/api.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
const BASE = import.meta.env.DEV ? 'http://localhost:3000' : ''
|
||||
|
||||
export type DataRow = Record<string, number | string | null>
|
||||
|
||||
async function get<T>(path: string, params: Record<string, string> = {}): Promise<T> {
|
||||
const url = new URL(`${BASE}${path}`, window.location.origin)
|
||||
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v))
|
||||
const res = await fetch(url.toString())
|
||||
return res.json()
|
||||
}
|
||||
|
||||
export const api = {
|
||||
current: (fields?: string) => get<DataRow>('/api/data', fields ? { fields } : {}),
|
||||
hourly: (start?: string, end?: string, fields?: string) => get<DataRow[]>('/api/hourly', { ...(start ? { start } : {}), ...(end ? { end } : {}), ...(fields ? { fields } : {}) }),
|
||||
daily: (start?: string, end?: string, fields?: string) => get<DataRow[]>('/api/daily', { ...(start ? { start } : {}), ...(end ? { end } : {}), ...(fields ? { fields } : {}) }),
|
||||
}
|
||||
Reference in New Issue
Block a user