mirror of
https://github.com/pockethost/pockethost.git
synced 2025-06-06 14:16:41 +00:00
21 lines
555 B
TypeScript
21 lines
555 B
TypeScript
import { existsSync, readFileSync, writeFileSync } from 'fs'
|
|
import fetch from 'node-fetch'
|
|
import { dbg } from './logger'
|
|
|
|
export const smartFetch = async <TRet>(
|
|
url: string,
|
|
path: string
|
|
): Promise<TRet> => {
|
|
const res = await fetch(url)
|
|
if (res.status !== 200) {
|
|
if (!existsSync(path)) {
|
|
throw new Error(`API down and no cache`)
|
|
}
|
|
dbg(`Using data from cache`)
|
|
return JSON.parse(readFileSync(path).toString()) as TRet
|
|
}
|
|
const data = await res.json()
|
|
writeFileSync(path, JSON.stringify(data))
|
|
return data as TRet
|
|
}
|