fp/packages/fetchers/src/createVod.ts
CJ_Clippy d89bf4fdea
Some checks failed
ci / build (push) Failing after 1s
add velero
2024-09-16 08:31:51 -08:00

27 lines
1.1 KiB
TypeScript

import { configs } from "./config.ts"
import type { VodResponse, VodRecord } from '@futureporn/types'
export default async function createVod(vodPartial?: Partial<VodRecord>): Promise<VodResponse|null> {
if (!vodPartial) console.warn(`createVod() was called with no vodPartial as argument. It's recommended to supply this argument with key/values stream_id, date, and vtuber`);
if (!vodPartial?.date) throw new Error('createVod requires argument vodPartial.date');
const url = `${configs.postgrestUrl}/vods`
const fetchOptions = {
method: 'POST',
headers: {
'Authorization': `Bearer ${configs.automationUserJwt}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Prefer': 'return=representation'
},
body: JSON.stringify(vodPartial)
}
const res = await fetch (url, fetchOptions)
if (!res.ok) {
const body = await res.json()
throw new Error(`Problem during createVod. res.status=${res.status}, res.statusText=${res.statusText}, body=${JSON.stringify(body)}`)
}
const json = await res.json() as VodResponse[]
const vod = json[0]
if (!vod) return null
else return vod
}