fp/packages/fetchers/src/createStreamInDatabase.ts

30 lines
1023 B
TypeScript
Raw Normal View History

2024-09-16 16:31:51 +00:00
import { configs } from './config.ts'
2024-08-27 07:11:24 +00:00
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
}