https://domlink.deployments.hotsocket.fyi/
1import { AtURI, AtURIString, DID, NSID, ValidateNSID } from "./atproto.ts";
2
3const BASEURL = "https://constellation.microcosm.blue";
4
5/** Essentially an AtURI, fed back by Constellation. */
6export type Reference = {
7 did: string;
8 collection: string;
9 rkey: string;
10}
11
12/** Raw response type from /links */
13type LinksResponse = {
14 total: number;
15 linking_records: Reference[];
16 cursor: string;
17};
18
19type Target = AtURIString | DID;
20/**
21 * Retrieves an array of record references to records containing links to the specified target.
22 * @throws When the provided NSID is invalid.
23 */
24export async function getLinks(target: Target, collection: NSID, path: string): Promise<AtURI[]> {
25 if (ValidateNSID(collection) == null) {
26 throw new Error("invalid NSID for collection parameter");
27 }
28 const _target = encodeURIComponent(target.toString()!);
29 const _path = encodeURIComponent(path);
30 let cursor = "";
31 let records: AtURI[] = [];
32 while (cursor != null) {
33 const rsp = await fetch(`${BASEURL}/links?target=${_target.toString()}&collection=${collection}&path=${_path}${cursor ? `&cursor=${cursor}` : ""}`);
34 const data = await rsp.json() as LinksResponse;
35 records = records.concat(data.linking_records.map(x=>new AtURI(x.did, x.collection, x.rkey)));
36 cursor = data.cursor;
37 }
38 return records;
39}