Scrapboard.org client
1import { Agent, AtUri } from "@atproto/api";
2import { PostView } from "@atproto/api/dist/client/types/app/bsky/feed/defs";
3import { Record } from "@atproto/api/dist/client/types/com/atproto/repo/listRecords";
4
5/**
6 * Fetches all records for a given repo & collection, handling pagination via cursors.
7 */
8export async function getAllRecords({
9 repo,
10 collection,
11 limit = 100,
12 agent,
13}: {
14 repo: string;
15 collection: string;
16 limit?: number;
17 agent: Agent;
18}) {
19 let records: Record[] = [];
20 let cursor: string | undefined = undefined;
21
22 do {
23 const res = await agent.com.atproto.repo.listRecords({
24 repo,
25 collection,
26 limit,
27 cursor,
28 });
29
30 records = records.concat(res.data.records);
31 cursor = res.data.cursor;
32 } while (cursor);
33
34 return records;
35}
36
37export async function getAllPosts({
38 posts,
39 agent,
40}: {
41 posts: AtUri[];
42 agent: Agent;
43}) {
44 let records: PostView[] = [];
45 let urisLeft = posts;
46
47 while (urisLeft.length > 0) {
48 const batch = urisLeft.slice(0, 25);
49 urisLeft = urisLeft.slice(25);
50
51 const res = await agent.getPosts({
52 uris: batch.map((it) => it.toString()),
53 });
54
55 // Combine returned posts into our results
56 if (res.success && res.data.posts) {
57 records = records.concat(res.data.posts);
58 }
59 }
60
61 return records;
62}