frontend client for gemstone. decentralised workplace app
1import { CONSTELLATION_URL } from "@/lib/consts";
2import type { ConstellationBacklinkResponse } from "@/lib/types/constellation";
3import { constellationBacklinkResponseSchema } from "@/lib/types/constellation";
4import type { Result } from "@/lib/utils/result";
5import type { ZodError } from "zod";
6
7export const getConstellationBacklinks = async ({
8 subject,
9 source,
10}: {
11 subject: string;
12 source: {
13 nsid: string;
14 fieldName?: string;
15 };
16}): Promise<
17 Result<
18 ConstellationBacklinkResponse,
19 ZodError | { message: string; fetchStatus: number }
20 >
21> => {
22 const { nsid, fieldName } = source;
23 const sourceParam = fieldName ? `${nsid}:${fieldName}` : nsid;
24 const req = new Request(
25 `${CONSTELLATION_URL.origin}/xrpc/blue.microcosm.links.getBacklinks?subject=${encodeURIComponent(subject)}&source=${encodeURIComponent(sourceParam)}`,
26 );
27 const res = await fetch(req);
28
29 if (!res.ok)
30 return {
31 ok: false,
32 error: {
33 message: "Fetch request to Constellation did not return 200.",
34 fetchStatus: res.status,
35 },
36 };
37
38 const data: unknown = await res.json();
39
40 const {
41 success,
42 error,
43 data: constellationResponse,
44 } = constellationBacklinkResponseSchema.safeParse(data);
45
46 if (!success) {
47 return { ok: false, error };
48 } else return { ok: true, data: constellationResponse };
49};