fp/packages/fetchers/src/createStreamInDatabase.ts

30 lines
1023 B
TypeScript

import { configs } from './config.ts'
export default async function createStreamInDatabase(url: string, discordMessageId: string): Promise<string> {
const streamPayload = {
url,
status: 'pending_recording',
discord_message_id: discordMessageId,
date: new Date().toISOString()
}
const res = await fetch(`${configs.postgrestUrl}/streams`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Prefer': 'return=headers-only',
'Authorization': `Bearer ${configs.automationUserJwt}`,
},
body: JSON.stringify(streamPayload)
})
if (!res.ok) {
const status = res.status
const statusText = res.statusText
const body = await res.text()
const msg = `Failed to create stream in database. status=${status}, statusText=${statusText}, body=${body}`
console.error(msg)
throw new Error(msg)
}
const id = res.headers.get('location')?.split('.').at(-1)
if (!id) throw new Error('id could not be parsed from location header');
return id
}