Skip to content

Open-source status page, incident response and uptime monitoring

fix: text body (#960)

Maximilian Kaskeb707085143b2
diff --git a/apps/web/src/components/forms/monitor/form.tsx b/apps/web/src/components/forms/monitor/form.tsx
index f3a81cabf..0649cf343 100644
--- a/apps/web/src/components/forms/monitor/form.tsx
+++ b/apps/web/src/components/forms/monitor/form.tsx
@@ -91,7 +91,10 @@ export function MonitorForm({
statusAssertions: _assertions.filter((a) => a.type === "status") as any, // TS considers a.type === "header"
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
headerAssertions: _assertions.filter((a) => a.type === "header") as any, // TS considers a.type === "status"
-
+ textBodyAssertions: _assertions.filter(
+ (a) => a.type === "textBody",
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
+ ) as any, // TS considers a.type === "textBody"
degradedAfter: defaultValues?.degradedAfter,
timeout: defaultValues?.timeout || 45000,
},
@@ -165,8 +168,15 @@ export function MonitorForm({
const pingEndpoint = async (region?: MonitorFlyRegion) => {
try {
- const { url, body, method, headers, statusAssertions, headerAssertions } =
- form.getValues();
+ const {
+ url,
+ body,
+ method,
+ headers,
+ statusAssertions,
+ headerAssertions,
+ textBodyAssertions,
+ } = form.getValues();
if (body && body !== "") {
const validJSON = validateJSON(body);
@@ -194,6 +204,7 @@ export function MonitorForm({
JSON.stringify([
...(statusAssertions || []),
...(headerAssertions || []),
+ ...(textBodyAssertions || []),
]),
);
@@ -206,7 +217,7 @@ export function MonitorForm({
if (as.length > 0) {
for (const a of as) {
const { success, message } = a.assert({
- body: "", // data.body ?? "",
+ body: data.body ?? "",
header: data.headers ?? {},
status: data.status,
});
diff --git a/apps/web/src/components/forms/monitor/section-assertions.tsx b/apps/web/src/components/forms/monitor/section-assertions.tsx
index 30dca5c01..b1655e2d7 100644
--- a/apps/web/src/components/forms/monitor/section-assertions.tsx
+++ b/apps/web/src/components/forms/monitor/section-assertions.tsx
@@ -249,7 +249,7 @@ export function SectionAssertions({ form }: Props) {
<div className="col-span-3" />
<FormField
control={form.control}
- name={`statusAssertions.${i}.compare`}
+ name={`textBodyAssertions.${i}.compare`}
render={({ field }) => (
<FormItem className="col-span-3 w-full">
<Select
diff --git a/apps/web/src/components/ping-response-analysis/utils.ts b/apps/web/src/components/ping-response-analysis/utils.ts
index 8ea5afc64..67fdd40e1 100644
--- a/apps/web/src/components/ping-response-analysis/utils.ts
+++ b/apps/web/src/components/ping-response-analysis/utils.ts
@@ -100,6 +100,7 @@ export const checkerSchema = z.object({
headers: z.record(z.string()),
time: z.number(),
timing: timingSchema,
+ body: z.string().optional().nullable(),
});
export const cachedCheckerSchema = z.object({
diff --git a/apps/web/src/hooks/use-cookie-state.ts b/apps/web/src/hooks/use-cookie-state.ts
index c41666bec..0bc1d8c1c 100644
--- a/apps/web/src/hooks/use-cookie-state.ts
+++ b/apps/web/src/hooks/use-cookie-state.ts
@@ -2,17 +2,25 @@
import { useCallback, useEffect, useState } from "react";
-export function useCookieState(name: string, defaultValue?: string) {
- const [state, setState] = useState<string>();
+export function useCookieState<T extends string>(
+ name: string,
+ defaultValue?: T,
+ config?: { expires?: number },
+) {
+ const [state, setState] = useState<T>();
const handleChange = useCallback(
- (value: string) => {
+ (value: T) => {
if (document) {
- document.cookie = `${name}=${value}; path=/`;
+ const date = new Date();
+ date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000); // in one year
+ document.cookie = `${name}=${value}; path=/; expires=${
+ config?.expires ?? date.toUTCString()
+ }`;
setState(value);
}
},
- [name],
+ [name, config],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
@@ -25,7 +33,8 @@ export function useCookieState(name: string, defaultValue?: string) {
setState(defaultValue);
return;
}
- setState(cookie.split("=")[1]);
+ const value = cookie.split("=")[1] as T;
+ setState(value);
}
}, []);
diff --git a/packages/api/src/router/monitor.ts b/packages/api/src/router/monitor.ts
index b7420620a..a7c9f9310 100644
--- a/packages/api/src/router/monitor.ts
+++ b/packages/api/src/router/monitor.ts
@@ -5,6 +5,7 @@ import {
type Assertion,
HeaderAssertion,
StatusAssertion,
+ TextBodyAssertion,
serialize,
} from "@openstatus/assertions";
import { and, eq, inArray, isNull, sql } from "@openstatus/db";
@@ -24,7 +25,6 @@ import {
selectNotificationSchema,
selectPublicMonitorSchema,
} from "@openstatus/db/src/schema";
-import { allPlans } from "@openstatus/db/src/schema/plan/config";
import { trackNewMonitor } from "../analytics";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
@@ -90,6 +90,7 @@ export const monitorRouter = createTRPCRouter({
tags,
statusAssertions,
headerAssertions,
+ textBodyAssertions,
...data
} = opts.input;
@@ -100,6 +101,9 @@ export const monitorRouter = createTRPCRouter({
for (const a of headerAssertions ?? []) {
assertions.push(new HeaderAssertion(a));
}
+ for (const a of textBodyAssertions ?? []) {
+ assertions.push(new TextBodyAssertion(a));
+ }
const newMonitor = await opts.ctx.db
.insert(monitor)
@@ -288,6 +292,7 @@ export const monitorRouter = createTRPCRouter({
tags,
statusAssertions,
headerAssertions,
+ textBodyAssertions,
...data
} = opts.input;
@@ -298,6 +303,9 @@ export const monitorRouter = createTRPCRouter({
for (const a of headerAssertions ?? []) {
assertions.push(new HeaderAssertion(a));
}
+ for (const a of textBodyAssertions ?? []) {
+ assertions.push(new TextBodyAssertion(a));
+ }
const currentMonitor = await opts.ctx.db
.update(monitor)