fp/packages/fetchers/src/getVod.ts
CJ_Clippy 4d65294f7d
Some checks failed
ci / build (push) Failing after 1s
move fetchers to their own package
2024-09-05 21:39:08 -08:00

23 lines
796 B
TypeScript

import { configs } from "./config"
import type { VodResponse } from "@futureporn/types"
export default async function getVod(vodId: string) {
const url = `${configs.postgrestUrl}/vods?select=*,segments(*)&id=eq.${vodId}`
try {
const res = await fetch(url)
if (!res.ok) {
throw new Error(`failed fetching getVod ${vodId}. status=${res.status}, statusText=${res.statusText}`)
}
const body = await res.json() as VodResponse[]
if (!body[0]) throw new Error('body[0] was expected to be Vod data, but it was either null or undefined.');
return body[0];
} catch (e) {
console.error(`encountered an error during getVod()`)
if (e instanceof Error) {
console.error(e.message)
} else {
console.error(JSON.stringify(e))
}
return null
}
}