mirror of
https://github.com/r-freeman/portfolio.git
synced 2024-11-25 04:45:41 +00:00
Converted fetcher function to ts
This commit is contained in:
parent
4be3a3256a
commit
480111d758
@ -1,3 +0,0 @@
|
|||||||
const fetcher = (...args) => fetch(...args).then(res => res.json())
|
|
||||||
|
|
||||||
export default fetcher
|
|
7
lib/fetcher.ts
Normal file
7
lib/fetcher.ts
Normal 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()
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
import fetcher from '@/lib/fetcher'
|
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
|
const GRAFANA_TOKEN = process.env.GRAFANA_TOKEN
|
||||||
|
|
||||||
export const getTemp = async () => {
|
export const getTemp = async () => {
|
||||||
|
@ -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`
|
const STATSFM_LIFETIME_STATS = `https://beta-api.stats.fm/api/v1/users/${STATSFM_USERNAME}/streams/stats?range=lifetime`
|
||||||
|
|
||||||
type StatsFmResponse = {
|
type StatsFmResponse = {
|
||||||
@ -11,12 +11,12 @@ type StatsFmResponse = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const getStats = async () => {
|
export const getStats = async () => {
|
||||||
const response = await fetch(STATSFM_LIFETIME_STATS, {
|
const response = await fetcher(STATSFM_LIFETIME_STATS, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Accept': 'application/json'
|
'Accept': 'application/json'
|
||||||
}
|
}
|
||||||
}).then(r => r.json()) as StatsFmResponse
|
}) as StatsFmResponse
|
||||||
|
|
||||||
const {durationMs} = response.items
|
const {durationMs} = response.items
|
||||||
const hoursListened = (durationMs / 3_600_000).toFixed(0)
|
const hoursListened = (durationMs / 3_600_000).toFixed(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user