Openstatus
www.openstatus.dev
1import { z } from "zod";
2
3import type { Assertion } from "./types";
4import {
5 DnsRecordAssertion,
6 HeaderAssertion,
7 JsonBodyAssertion,
8 StatusAssertion,
9 TextBodyAssertion,
10 base,
11 headerAssertion,
12 jsonBodyAssertion,
13 recordAssertion,
14 statusAssertion,
15 textBodyAssertion,
16} from "./v1";
17
18export function serialize(assertions: Assertion[]): string {
19 return JSON.stringify(assertions.map((a) => a.schema));
20}
21export function deserialize(s: string): Assertion[] {
22 const bases = z.array(base).parse(JSON.parse(s));
23 return bases.map((b) => {
24 switch (b.type) {
25 case "status":
26 return new StatusAssertion(statusAssertion.parse(b));
27 case "header":
28 return new HeaderAssertion(headerAssertion.parse(b));
29 case "jsonBody":
30 return new JsonBodyAssertion(jsonBodyAssertion.parse(b));
31 case "textBody":
32 return new TextBodyAssertion(textBodyAssertion.parse(b));
33 case "dnsRecord":
34 return new DnsRecordAssertion(recordAssertion.parse(b));
35
36 default:
37 throw new Error(`unknown assertion type: ${b.type}`);
38 }
39 });
40}