Cleaned up some types

This commit is contained in:
Mike Conrad
2025-05-18 21:00:53 -04:00
parent 8ea49772df
commit f12cdbf76a
2 changed files with 30 additions and 9 deletions

View File

@ -4,6 +4,19 @@ import type { HttpContext } from '@adonisjs/core/http'
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
const SENTRY_ORG = env.get('SENTRY_ORG')
let recordsUpdated = 0
interface ApiResponse<T> {
data: T;
// optionally, you can define `meta`, `errors`, etc. if your API returns them
}
interface SentryPagination {
previous: string;
hasPreviousResults: boolean;
hasNextResults: boolean;
next: string
}
export default class ReplaysController {
@ -31,15 +44,23 @@ async function fetchBatch(url: string) {
}
}
const req = await fetch(url, options)
const resp = await req.json() as unknown
const replays = await resp.data as unknown
const headers = await req.headers
if (!req.ok) {
throw new Error(`Request failed with status ${req.status}`);
}
const resp = await req.json() as ApiResponse<Replay[]>;
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 pagination = parseSentryLinkHeader(headers.get('link'))
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)
@ -49,11 +70,10 @@ async function fetchBatch(url: string) {
return {recordsUpdated}
}
function parseSentryLinkHeader(header:string) {
function parseSentryLinkHeader(header:string): SentryPagination {
const links = header.split(',').map(part => part.trim())
const result = {}
let result = {} as SentryPagination
for (const link of links) {
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
if (!match) continue

View File

@ -32,10 +32,10 @@ export default class Replay extends BaseModel {
return JSON.stringify(value)
}
})
declare tags: any
declare tags: string[]
@column()
declare user: any
declare user: string[]
@column()
@ -52,6 +52,7 @@ export default class Replay extends BaseModel {
@column()
declare device: any
@column()
declare ota_updates: any