···
} from "@atcute/identity-resolver";
parseCanonicalResourceUri,
} from "@atcute/lexicons/syntax";
import { config } from "./config";
import type {} from "@atcute/atproto";
17
-
import { safeParse } from "@atcute/lexicons";
16
+
import { is, safeParse } from "@atcute/lexicons";
SystemsGazeBarometerState,
SystemsGazeBarometerHost,
SystemsGazeBarometerService,
SystemsGazeBarometerCheck,
} from "barometer-lexicon";
24
-
import { expect, getRecord, putRecord } from "./utils";
23
+
import { err, expect, getRecord, ok, putRecord, type Result } from "./utils";
record: SystemsGazeBarometerCheck.Main;
···
78
+
interface PushRequest {
79
+
serviceName?: string; // service manager service name
80
+
state: SystemsGazeBarometerState.Main;
83
+
const parsePushRequest = (json: unknown): Result<PushRequest, string> => {
84
+
if (typeof json !== "object" || json === null) {
85
+
return err("invalid request");
87
+
if ("serviceName" in json && typeof json.serviceName !== "string") {
88
+
return err("serviceName is not a string");
90
+
if ("state" in json) {
91
+
const parsed = safeParse(SystemsGazeBarometerState.mainSchema, json.state);
93
+
return err(`state is invalid: ${parsed.message}`);
96
+
return err("state not found");
98
+
return ok(json as PushRequest);
const badRequest = <Error extends { msg: string }>(error: Error) => {
return new Response(JSON.stringify(error), { status: 400 });
···
86
-
const maybeState = safeParse(
87
-
SystemsGazeBarometerState.mainSchema,
90
-
if (!maybeState.ok) {
108
+
const maybeData = parsePushRequest(await req.json());
109
+
if (!maybeData.ok) {
92
-
msg: `invalid state: ${maybeState.message}`,
93
-
issues: maybeState.issues,
111
+
msg: `invalid request: ${maybeData.error}`,
96
-
const state = maybeState.value;
114
+
const data = maybeData.value;
const serviceAtUri = expect(
99
-
parseCanonicalResourceUri(state.forService),
117
+
parseCanonicalResourceUri(data.state.forService),
let service = services.get(serviceAtUri.rkey);
···
108
-
return badRequest({ msg: "service was not found" });
126
+
return badRequest({
127
+
msg: `service was not found or is invalid: ${serviceRecord.error}`,
record: serviceRecord.value,
···
services.set(serviceAtUri.rkey, service);
117
-
if (state.generatedBy) {
137
+
if (data.state.generatedBy) {
const checkAtUri = expect(
119
-
parseCanonicalResourceUri(state.generatedBy),
139
+
parseCanonicalResourceUri(data.state.generatedBy),
let check = service.checks.get(checkAtUri.rkey);
···
128
-
return badRequest({ msg: "check record not found" });
148
+
return badRequest({
149
+
msg: `check record not found or is invalid: ${checkRecord.error}`,
record: checkRecord.value,
···
137
-
const result = await putRecord(state);
159
+
const result = await putRecord(data.state);
JSON.stringify({ cid: result.cid, uri: result.uri }),