fp/packages/fetchers/src/createS3File.ts

31 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-09-06 05:39:08 +00:00
import { configs } from "../../../services/factory/src/config"
2024-09-03 16:28:39 +00:00
import type { S3FileRecord, S3FileResponse } from '@futureporn/types'
export default async function createS3File(s3File?: S3FileRecord): Promise<S3FileResponse|null> {
if (!s3File) throw new Error(`first argument passed to createS3File must be a {S3Record}`);
console.log(s3File)
const url = `${configs.postgrestUrl}/s3_files`
const payload: any = {
s3_id: s3File.s3_id,
s3_key: s3File.s3_key,
}
const fetchOptions = {
method: 'POST',
headers: {
'Authorization': `Bearer ${configs.automationUserJwt}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
'Prefer': 'return=representation'
},
body: JSON.stringify(payload)
}
const res = await fetch (url, fetchOptions)
if (!res.ok) {
const body = await res.json()
throw new Error(`Problem during createS3File. res.status=${res.status}, res.statusText=${res.statusText}, body=${JSON.stringify(body)}`)
}
const json = await res.json() as S3FileResponse[]
const s3FileRes = json[0]
if (!s3FileRes) return null
else return s3FileRes
}