Cleaned up some types
This commit is contained in:
@ -4,6 +4,19 @@ import type { HttpContext } from '@adonisjs/core/http'
|
|||||||
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
||||||
const SENTRY_ORG = env.get('SENTRY_ORG')
|
const SENTRY_ORG = env.get('SENTRY_ORG')
|
||||||
let recordsUpdated = 0
|
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 {
|
export default class ReplaysController {
|
||||||
|
|
||||||
|
|
||||||
@ -31,15 +44,23 @@ async function fetchBatch(url: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
const req = await fetch(url, options)
|
const req = await fetch(url, options)
|
||||||
const resp = await req.json() as unknown
|
if (!req.ok) {
|
||||||
const replays = await resp.data as unknown
|
throw new Error(`Request failed with status ${req.status}`);
|
||||||
const headers = await req.headers
|
}
|
||||||
|
|
||||||
|
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))
|
const cleanedData = replays.map(record => sanitizeInput(record, Replay.allowedFields))
|
||||||
|
|
||||||
let updated = await Replay.updateOrCreateMany('id', cleanedData )
|
let updated = await Replay.updateOrCreateMany('id', cleanedData )
|
||||||
recordsUpdated = recordsUpdated + updated.length
|
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) {
|
if (pagination.hasNextResults == true) {
|
||||||
console.log('fetching', pagination.next)
|
console.log('fetching', pagination.next)
|
||||||
@ -49,11 +70,10 @@ async function fetchBatch(url: string) {
|
|||||||
return {recordsUpdated}
|
return {recordsUpdated}
|
||||||
|
|
||||||
}
|
}
|
||||||
function parseSentryLinkHeader(header:string) {
|
function parseSentryLinkHeader(header:string): SentryPagination {
|
||||||
const links = header.split(',').map(part => part.trim())
|
const links = header.split(',').map(part => part.trim())
|
||||||
|
|
||||||
const result = {}
|
let result = {} as SentryPagination
|
||||||
|
|
||||||
for (const link of links) {
|
for (const link of links) {
|
||||||
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
|
const match = link.match(/<([^>]+)>;\s*rel="([^"]+)";\s*results="([^"]+)";\s*cursor="([^"]+)"/)
|
||||||
if (!match) continue
|
if (!match) continue
|
||||||
|
@ -32,10 +32,10 @@ export default class Replay extends BaseModel {
|
|||||||
return JSON.stringify(value)
|
return JSON.stringify(value)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
declare tags: any
|
declare tags: string[]
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare user: any
|
declare user: string[]
|
||||||
|
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
@ -52,6 +52,7 @@ export default class Replay extends BaseModel {
|
|||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare device: any
|
declare device: any
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
declare ota_updates: any
|
declare ota_updates: any
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user