import { useQuery } from "@tanstack/react-query"; import type { HttpAssertion, HttpAssertions, HttpRequest } from "@yaakapp-internal/models"; import { patchModelDebounced } from "@yaakapp-internal/models"; import { usePinnedHttpResponse } from "../hooks/usePinnedHttpResponse"; import { JsonPathInput } from "./JsonPathInput"; import { useRequestUpdateKey } from "../hooks/useRequestUpdateKey"; import { useStateWithDeps } from "../hooks/useStateWithDeps"; import { generateId } from "../lib/generateId"; import { rpc } from "../lib/rpc"; import { Button } from "./core/Button"; import { Checkbox } from "./core/Checkbox"; import { IconButton } from "./core/IconButton"; import { PlainInput } from "./core/PlainInput"; import { Select } from "./core/Select"; import { HttpAssertionPreview } from "./HttpAssertionPreview"; const EMPTY: HttpAssertions = { version: 1, checks: [] }; const operators = [ { value: "equals", label: "Equals" }, { value: "not_equals", label: "Does not equal" }, { value: "greater_than", label: "Greater than" }, { value: "less_than", label: "Less than" }, { value: "contains", label: "Contains" }, { value: "exists", label: "Exists" }, ]; export function HttpAssertionsEditor({ request }: { request: HttpRequest }) { const updateKey = useRequestUpdateKey(request.id); const { activeResponse } = usePinnedHttpResponse(request.id); const [definition, setDefinition] = useStateWithDeps(request.assertions ?? EMPTY, [updateKey]); const validation = useQuery({ queryKey: ["validate_assertions", definition], queryFn: () => rpc>("cmd_validate_http_assertions", { assertions: definition }), staleTime: Infinity, gcTime: 0, retry: false, }); const save = (checks: HttpAssertion[]) => { const next = { ...definition, checks }; setDefinition(next); patchModelDebounced(request, { assertions: next }); }; const update = (id: string, patch: Partial) => save(definition.checks.map((c) => (c.id === id ? { ...c, ...patch } : c))); return (

Check the response each time you send. Results appear in the response’s Assertions tab.

{definition.checks.map((check, index) => (
update(check.id, { enabled })} /> save(definition.checks.filter((c) => c.id !== check.id))} />
check.target === "json" || (check.target === "status" ? o.value !== "contains" && o.value !== "exists" : !["greater_than", "less_than"].includes(o.value)), )} onChange={(operator) => update(check.id, { operator, ...(["greater_than", "less_than"].includes(operator) ? { expectedType: "number" } : operator === "contains" ? { expectedType: "text" } : {}), }) } /> {check.operator !== "exists" && (
{check.expectedType !== "null" && (
update(check.id, { expected })} />
)} {check.target === "json" && !["greater_than", "less_than", "contains"].includes(check.operator) && (