Fix formatting
This commit is contained in:
@ -1,29 +1,29 @@
|
||||
import Replay from '#models/replay'
|
||||
import {parseSentryLinkHeader, SentryPagination} from './Sentry.js'
|
||||
import { parseSentryLinkHeader, SentryPagination } from './Sentry.js'
|
||||
|
||||
import env from '#start/env'
|
||||
let recordsUpdated = 0
|
||||
const SENTRY_TOKEN = env.get('SENTRY_TOKEN')
|
||||
interface ApiResponse<T> {
|
||||
data: T;
|
||||
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}`
|
||||
}
|
||||
Authorization: `Bearer ${SENTRY_TOKEN}`,
|
||||
},
|
||||
}
|
||||
const req = await fetch(url, options)
|
||||
if (!req.ok) {
|
||||
throw new Error(`Request failed with status ${req.status}`);
|
||||
throw new Error(`Request failed with status ${req.status}`)
|
||||
}
|
||||
|
||||
const resp = await req.json() as ApiResponse<Replay[]>;
|
||||
const replays = resp.data;
|
||||
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)
|
||||
recordsUpdated = recordsUpdated + updated.length
|
||||
@ -39,12 +39,14 @@ export async function fetchBatch(url: string) {
|
||||
}
|
||||
console.log('no more results')
|
||||
return { recordsUpdated }
|
||||
|
||||
}
|
||||
|
||||
function sanitizeInput(data: Record<string, any>, allowedFields: string[]) {
|
||||
return allowedFields.reduce((acc, key) => {
|
||||
if (key in data) acc[key] = data[key]
|
||||
return acc
|
||||
}, {} as Record<string, any>)
|
||||
return allowedFields.reduce(
|
||||
(acc, key) => {
|
||||
if (key in data) acc[key] = data[key]
|
||||
return acc
|
||||
},
|
||||
{} as Record<string, any>
|
||||
)
|
||||
}
|
||||
|
@ -1,12 +1,11 @@
|
||||
|
||||
export interface SentryPagination {
|
||||
previous: string;
|
||||
hasPreviousResults: boolean;
|
||||
hasNextResults: boolean;
|
||||
previous: string
|
||||
hasPreviousResults: boolean
|
||||
hasNextResults: boolean
|
||||
next: string
|
||||
}
|
||||
export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
const links = header.split(',').map(part => part.trim())
|
||||
const links = header.split(',').map((part) => part.trim())
|
||||
|
||||
let result = {} as SentryPagination
|
||||
for (const link of links) {
|
||||
@ -25,4 +24,4 @@ export function parseSentryLinkHeader(header: string): SentryPagination {
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,21 @@
|
||||
import env from '#start/env'
|
||||
|
||||
export async function sendDataToWebhook(responseData:{ version: number, updatedAt: Date, numberOfRecords: number, data: unknown}) {
|
||||
try {
|
||||
console.log('syncing to webhook')
|
||||
await fetch(env.get('WEBHOOK_URL'),
|
||||
{
|
||||
headers:
|
||||
{
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
method: 'POST',
|
||||
body: JSON.stringify(responseData)
|
||||
}
|
||||
)
|
||||
} catch (e) {
|
||||
console.error('error sending webhook data', e)
|
||||
}
|
||||
}
|
||||
export async function sendDataToWebhook(responseData: {
|
||||
version: number
|
||||
updatedAt: Date
|
||||
numberOfRecords: number
|
||||
data: unknown
|
||||
}) {
|
||||
try {
|
||||
console.log('syncing to webhook')
|
||||
await fetch(env.get('WEBHOOK_URL'), {
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
method: 'POST',
|
||||
body: JSON.stringify(responseData),
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('error sending webhook data', e)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user