Skip to content

Commit

Permalink
improve(metrics): schema refactor to enforce common fields
Browse files Browse the repository at this point in the history
  • Loading branch information
sdnts committed Sep 15, 2023
1 parent 3c773c4 commit 0f40f2a
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/endpoints/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { z } from "zod";
import { Endpoint, EnvironmentSchema } from "./types";

const schema = z.discriminatedUnion("name", [
z.object({
name: z.literal("page_view"),
const schema = z
.object({
environment: EnvironmentSchema,
origin: z.string(),
path: z.string(),
fields: z.object({
visitor: z.string(),
location: z.string(),
}),
}),
service: z.string(),
})
.and(
z.discriminatedUnion("name", [
z.object({
name: z.literal("page_view"),
path: z.string(),
fields: z.object({
visitor: z.string(),
location: z.string(),
}),
}),

z.object({
name: z.literal("request"),
environment: EnvironmentSchema,
origin: z.string(),
method: z.string(),
path: z.string(),
status: z.number(),
fields: z.record(z.string(), z.string()),
}),
]);
z.object({
name: z.literal("request"),
method: z.string(),
path: z.string(),
status: z.number(),
fields: z.record(z.string(), z.string()),
}),
]),
);

/*
* Endpoint for metrics, secured by Access. Generally used by server-side code.
Expand Down

0 comments on commit 0f40f2a

Please sign in to comment.