Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
Fix api update (#1287)
Thibault Le Ouayb0058e301f25
diff --git a/apps/server/src/routes/v1/monitors/put.test.ts b/apps/server/src/routes/v1/monitors/put.test.tsindex c0816dc5e..71b951002 100644--- a/apps/server/src/routes/v1/monitors/put.test.ts+++ b/apps/server/src/routes/v1/monitors/put.test.ts@@ -22,21 +22,6 @@ test("update the monitor", async () => {expect(result.data?.name).toBe("New Name");});-test("update the monitor with a different jobType should return 400", async () => {- const res = await app.request("/v1/monitor/1", {- method: "PUT",- headers: {- "x-openstatus-key": "1",- "Content-Type": "application/json",- },- body: JSON.stringify({- jobType: "tcp",- }),- });-- expect(res.status).toBe(400);-});-test("invalid monitor id should return 404", async () => {const res = await app.request("/v1/page/404", {method: "PUT",diff --git a/apps/server/src/routes/v1/monitors/put.ts b/apps/server/src/routes/v1/monitors/put.tsindex 958367e31..ce575f9c0 100644--- a/apps/server/src/routes/v1/monitors/put.ts+++ b/apps/server/src/routes/v1/monitors/put.ts@@ -8,8 +8,13 @@ import { trackMiddleware } from "@/libs/middlewares";import { Events } from "@openstatus/analytics";import { serialize } from "@openstatus/assertions";import type { monitorsApi } from "./index";-import { MonitorSchema, ParamsSchema } from "./schema";-import { getAssertions } from "./utils";+import {+ HTTPMonitorSchema,+ MonitorSchema,+ ParamsSchema,+ TCPMonitorSchema,+} from "./schema";+import { getAssertionNew } from "./utils";const putRoute = createRoute({method: "put",@@ -23,7 +28,7 @@ const putRoute = createRoute({description: "The monitor to update",content: {"application/json": {- schema: MonitorSchema.omit({ id: true }).partial(),+ schema: HTTPMonitorSchema.partial().or(TCPMonitorSchema.partial()),},},},@@ -48,7 +53,7 @@ export function registerPutMonitor(api: typeof monitorsApi) {const { id } = c.req.valid("param");const input = c.req.valid("json");- if (input.periodicity && !limits.periodicity.includes(input.periodicity)) {+ if (input.frequency && !limits.periodicity.includes(input.frequency)) {throw new OpenStatusApiError({code: "PAYMENT_REQUIRED",message: "Upgrade for more periodicity",@@ -85,33 +90,80 @@ export function registerPutMonitor(api: typeof monitorsApi) {});}- if (input.jobType && input.jobType !== _monitor.jobType) {- throw new OpenStatusApiError({- code: "BAD_REQUEST",- message:- "Cannot change jobType. Please delete and create a new monitor instead.",- });+ if (_monitor.jobType === "http") {+ const data = HTTPMonitorSchema.partial().parse(input);+ const { request, regions, assertions, otelHeaders, ...rest } = data;++ const headers = data?.request?.headers+ ? Object.entries(data?.request.headers)+ : undefined;++ const otelHeadersEntries = otelHeaders+ ? Object.entries(otelHeaders).map(([key, value]) => ({+ key: key,+ value: value,+ }))+ : undefined;+ const headersEntries = headers+ ? headers.map(([key, value]) => ({ key: key, value: value }))+ : undefined;++ const assert = assertions ? getAssertionNew(assertions) : [];++ const _newMonitor = await db+ .update(monitor)+ .set({+ ...rest,+ regions: regions ? regions.join(",") : _monitor.regions,+ headers: headersEntries+ ? JSON.stringify(headersEntries)+ : _monitor.headers,+ otelHeaders: otelHeadersEntries+ ? JSON.stringify(otelHeadersEntries)+ : _monitor.otelHeaders,+ assertions:+ assert.length > 0 ? serialize(assert) : _monitor.assertions,+ timeout: input.timeout || 45000,+ updatedAt: new Date(),+ })+ .where(eq(monitor.id, Number(_monitor.id)))+ .returning()+ .get();+ const r = MonitorSchema.parse(_newMonitor);+ return c.json(r, 200);}+ if (_monitor.jobType === "tcp") {+ const data = TCPMonitorSchema.partial().parse(input);+ const { request, regions, otelHeaders, ...rest } = data;- const { headers, regions, assertions, ...rest } = input;-- const assert = assertions ? getAssertions(assertions) : [];-- const _newMonitor = await db- .update(monitor)- .set({- ...rest,- regions: regions ? regions.join(",") : undefined,- headers: input.headers ? JSON.stringify(input.headers) : undefined,- assertions: assert.length > 0 ? serialize(assert) : undefined,- timeout: input.timeout || 45000,- updatedAt: new Date(),- })- .where(eq(monitor.id, Number(_monitor.id)))- .returning()- .get();+ const otelHeadersEntries = otelHeaders+ ? Object.entries(otelHeaders).map(([key, value]) => ({+ key: key,+ value: value,+ }))+ : undefined;++ const _newMonitor = await db+ .update(monitor)+ .set({+ ...rest,+ regions: regions ? regions.join(",") : _monitor.regions,+ otelHeaders: otelHeadersEntries+ ? JSON.stringify(otelHeadersEntries)+ : _monitor.otelHeaders,+ timeout: input.timeout || 45000,+ updatedAt: new Date(),+ })+ .where(eq(monitor.id, Number(_monitor.id)))+ .returning()+ .get();+ const r = MonitorSchema.parse(_newMonitor);+ return c.json(r, 200);+ }- const data = MonitorSchema.parse(_newMonitor);- return c.json(data, 200);+ throw new OpenStatusApiError({+ code: "NOT_FOUND",+ message: "Something went wrong",+ });});}diff --git a/apps/server/src/routes/v1/monitors/schema.ts b/apps/server/src/routes/v1/monitors/schema.tsindex e992c5ee2..d221992e9 100644--- a/apps/server/src/routes/v1/monitors/schema.ts+++ b/apps/server/src/routes/v1/monitors/schema.ts@@ -444,17 +444,25 @@ export const assertionsSchema = z.discriminatedUnion("kind", [textBodyAssertions,]);-export const HTTPMonitorSchema = baseRequest.extend({- assertions: z.array(assertionsSchema).optional().openapi({- description: "Assertions to run on the response",- }),- request: httpRequestSchema.openapi({- description: "The HTTP Request we are sending",- }),-});+export const HTTPMonitorSchema = baseRequest+ .extend({+ assertions: z.array(assertionsSchema).optional().openapi({+ description: "Assertions to run on the response",+ }),+ request: httpRequestSchema.openapi({+ description: "The HTTP Request we are sending",+ }),+ })+ .openapi({+ title: "HTTP Monitor Schema",+ });-export const TCPMonitorSchema = baseRequest.extend({- request: tcpRequestSchema.openapi({- description: "The TCP Request we are sending",- }),-});+export const TCPMonitorSchema = baseRequest+ .extend({+ request: tcpRequestSchema.openapi({+ description: "The TCP Request we are sending",+ }),+ })+ .openapi({+ title: "TCP Monitor Schema",+ });