import Replay from '#models/replay' import { parseSentryLinkHeader, SentryPagination } from './Sentry.js' import env from '#start/env' let recordsUpdated = 0 const SENTRY_TOKEN = env.get('SENTRY_TOKEN') interface ApiResponse { data: T // optionally, you can define `meta`, `errors`, etc. if your API returns them } export async function fetchBatch(url: string) { const options: RequestInit = { headers: { Authorization: `Bearer ${SENTRY_TOKEN}`, }, } const req = await fetch(url, options) if (!req.ok) { throw new Error(`Request failed with status ${req.status}`) } const resp = (await req.json()) as ApiResponse const replays = resp.data const headers = req.headers const cleanedData = replays.map((record) => sanitizeInput(record, Replay.allowedFields)) let updated = await Replay.updateOrCreateMany('id', cleanedData) recordsUpdated = recordsUpdated + updated.length const linkHeader = headers.get('link') if (!linkHeader) { return { error: 'link header missing from Sentry API response' } } const pagination: SentryPagination = parseSentryLinkHeader(linkHeader) if (pagination.hasNextResults == true) { console.log('fetching', pagination.next) await fetchBatch(pagination.next) } console.log('no more results') return { recordsUpdated } } function sanitizeInput(data: Record, allowedFields: string[]) { return allowedFields.reduce( (acc, key) => { if (key in data) acc[key] = data[key] return acc }, {} as Record ) }