Converted fetcher function to ts

This commit is contained in:
r-freeman 2023-02-22 22:17:24 +00:00
parent 4be3a3256a
commit 480111d758
4 changed files with 12 additions and 8 deletions

View File

@ -1,3 +0,0 @@
const fetcher = (...args) => fetch(...args).then(res => res.json())
export default fetcher

7
lib/fetcher.ts Normal file
View File

@ -0,0 +1,7 @@
export default async function fetcher<JSON = any>(
input: RequestInfo,
init?: RequestInit
): Promise<JSON> {
const res = await fetch(input, init)
return res.json()
}

View File

@ -1,6 +1,6 @@
import fetcher from '@/lib/fetcher'
const GRAFANA_URL = process.env.GRAFANA_URL
const GRAFANA_URL: string = process.env.GRAFANA_URL ?? ""
const GRAFANA_TOKEN = process.env.GRAFANA_TOKEN
export const getTemp = async () => {

View File

@ -1,6 +1,6 @@
import fetch from 'node-fetch'
import fetcher from './fetcher'
const STATSFM_USERNAME = process.env.STATSFM_USERNAME
const STATSFM_USERNAME: string = process.env.STATSFM_USERNAME ?? ""
const STATSFM_LIFETIME_STATS = `https://beta-api.stats.fm/api/v1/users/${STATSFM_USERNAME}/streams/stats?range=lifetime`
type StatsFmResponse = {
@ -11,12 +11,12 @@ type StatsFmResponse = {
}
export const getStats = async () => {
const response = await fetch(STATSFM_LIFETIME_STATS, {
const response = await fetcher(STATSFM_LIFETIME_STATS, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
}).then(r => r.json()) as StatsFmResponse
}) as StatsFmResponse
const {durationMs} = response.items
const hoursListened = (durationMs / 3_600_000).toFixed(0)