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