summaryrefslogtreecommitdiff
path: root/packages/integrations/web-vitals/src/endpoint.ts
blob: d3bf214bb5af259bf92272a3d95d94c4568dc947 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// @ts-expect-error — AstrojsWebVitals requires type-gen which we can’t use.
import { AstrojsWebVitals_Metric, db, sql } from 'astro:db';
import type { APIRoute } from 'astro';
import { ServerMetricSchema } from './schemas.js';

export const prerender = false;

export const ALL: APIRoute = async ({ request }) => {
	try {
		const rawBody = await request.json();
		const body = ServerMetricSchema.array().parse(rawBody);
		await db
			.insert(AstrojsWebVitals_Metric)
			.values(body)
			.onConflictDoUpdate({
				target: AstrojsWebVitals_Metric.id,
				set: { value: sql`excluded.value` },
			});
	} catch (error) {
		console.error(error);
	}
	return new Response();
};