leaflet.pub astro loader
1import type { Agent } from "@atproto/api";
2import type { MiniDoc } from "./types.js";
3import { LiveLoaderError } from "./loader.js";
4
5export function uriToRkey(uri: string) {
6 const rkey = uri.split("/").pop();
7 if (!rkey) {
8 throw new Error("Failed to get rkey from uri.");
9 }
10 return rkey;
11}
12
13export async function resolveMiniDoc(handleOrDid: string) {
14 try {
15 const response = await fetch(
16 `https://slingshot.microcosm.blue/xrpc/com.bad-example.identity.resolveMiniDoc?identifier=${handleOrDid}`,
17 );
18
19 if (!response.ok || response.status >= 300) {
20 throw new Error(
21 `could not resolve did doc due to invalid handle or did ${handleOrDid}`,
22 );
23 }
24 const data = (await response.json()) as MiniDoc;
25
26 return data.pds;
27 } catch (error) {
28 throw new Error(`failed to resolve handle: ${handleOrDid}`);
29 }
30}
31
32export async function getLeafletDocuments(repo: string, agent: Agent) {
33 const response = await agent.com.atproto.repo.listRecords({
34 repo,
35 collection: "pub.leaflet.document",
36 });
37
38 if (response.success === false) {
39 throw new LiveLoaderError(
40 "Could not fetch leaflet documents",
41 "FETCH_FAILED",
42 );
43 }
44
45 return response;
46}