frontend client for gemstone. decentralised workplace app
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: constellation helpers

serenity d084d574 d1fda020

+74
+4
src/lib/consts.ts
··· 19 19 }; 20 20 21 21 export const DEFAULT_STALE_TIME = 5 * 60 * 1000; 22 + 23 + export const CONSTELLATION_URL = new URL( 24 + "https://constellation.microcosm.blue/", 25 + );
+18
src/lib/types/constellation.ts
··· 1 + import { didSchema, nsidSchema } from "@/lib/types/atproto"; 2 + import { z } from "zod"; 3 + 4 + export const constellationBacklinkSchema = z.object({ 5 + did: didSchema, 6 + collection: nsidSchema, 7 + rkey: z.string(), 8 + }); 9 + export type ConstellationBacklink = z.infer<typeof constellationBacklinkSchema>; 10 + 11 + export const constellationBacklinkResponseSchema = z.object({ 12 + total: z.number(), 13 + records: z.array(constellationBacklinkSchema), 14 + cursor: z.optional(z.string().nullish()), 15 + }); 16 + export type ConstellationBacklinkResponse = z.infer< 17 + typeof constellationBacklinkResponseSchema 18 + >;
+52
src/lib/utils/constellation.ts
··· 1 + import { CONSTELLATION_URL } from "@/lib/consts"; 2 + import type { 3 + ConstellationBacklinkResponse} from "@/lib/types/constellation"; 4 + import { 5 + constellationBacklinkResponseSchema, 6 + } from "@/lib/types/constellation"; 7 + import type { Result } from "@/lib/utils/result"; 8 + import type { ZodError } from "zod"; 9 + 10 + export const getConstellationBacklink = async ({ 11 + subject, 12 + source, 13 + }: { 14 + subject: string; 15 + source: { 16 + nsid: string; 17 + fieldName?: string; 18 + }; 19 + }): Promise< 20 + Result< 21 + ConstellationBacklinkResponse, 22 + ZodError | { message: string; fetchStatus: number } 23 + > 24 + > => { 25 + const { nsid, fieldName } = source; 26 + const sourceParam = fieldName ? `${nsid}:${fieldName}` : nsid; 27 + const req = new Request( 28 + `${CONSTELLATION_URL.origin}/xrpc/blue.microcosm.links.getBacklinks?subject=${encodeURIComponent(subject)}&source=${encodeURIComponent(sourceParam)}`, 29 + ); 30 + const res = await fetch(req); 31 + 32 + if (!res.ok) 33 + return { 34 + ok: false, 35 + error: { 36 + message: "Fetch request to Constellation did not return 200.", 37 + fetchStatus: res.status, 38 + }, 39 + }; 40 + 41 + const data: unknown = await res.json(); 42 + 43 + const { 44 + success, 45 + error, 46 + data: constellationResponse, 47 + } = constellationBacklinkResponseSchema.safeParse(data); 48 + 49 + if (!success) { 50 + return { ok: false, error }; 51 + } else return { ok: true, data: constellationResponse }; 52 + };