···1-import type { Did, Handle } from '@atcute/lexicons';
2import { user } from './auth.svelte';
3import type { AllowedCollection } from './settings';
4import {
···10 WellKnownHandleResolver
11} from '@atcute/identity-resolver';
12import { Client, simpleFetchHandler } from '@atcute/client';
13-import type { AppBskyActorDefs } from '@atcute/bluesky';
1415export type Collection = `${string}.${string}.${string}`;
0160000017export function parseUri(uri: string) {
18- const [did, collection, rkey] = uri.replace('at://', '').split('/');
19- return { did, collection, rkey } as {
20- collection: Collection;
21- rkey: string;
22- did: string;
23- };
24}
250000026export async function resolveHandle({ handle }: { handle: Handle }) {
27 const handleResolver = new CompositeHandleResolver({
28 methods: {
···42 }
43});
4400000045export async function getPDS(did: Did) {
46- const doc = await didResolver.resolve(did as `did:plc:${string}` | `did:web:${string}`);
47 if (!doc.service) throw new Error('No PDS found');
48 for (const service of doc.service) {
49 if (service.id === '#atproto_pds') {
···52 }
53}
54000000055export async function getDetailedProfile(data?: { did?: Did; client?: Client }) {
56 data ??= {};
57 data.did ??= user.did;
···71 return response.data;
72}
7300000074export async function getClient({ did }: { did: Did }) {
75 const pds = await getPDS(did);
76 if (!pds) throw new Error('PDS not found');
···82 return client;
83}
8400000000085export async function listRecords({
86 did,
87 collection,
···129 return allRecords;
130}
13100000000132export async function getRecord({
133 did,
134 collection,
···162 return JSON.parse(JSON.stringify(record.data));
163}
164000000000165export async function putRecord({
166 collection,
167 rkey = 'self',
···187 return response;
188}
18900000000190export async function deleteRecord({
191 collection,
192 rkey = 'self'
···207 return response.ok;
208}
209000000210export async function uploadBlob({ blob }: { blob: Blob }) {
211 if (!user.did || !user.client) throw new Error("Can't upload blob: Not logged in");
212···231 return blobInfo;
232}
233000000234export async function describeRepo({ client, did }: { client: Client; did?: Did }) {
235 did ??= user.did;
236 if (!did) {
···248 return repo.data;
249}
250000000251export async function getBlobURL({
252 did,
253 blob
···264 return `${pds}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${blob.ref.$link}`;
265}
266000000267export function getCDNImageBlobUrl({
268 did,
269 blob
270}: {
271- did: string;
272 blob: {
273 $type: 'blob';
274 ref: {
···276 };
277 };
278}) {
00279 return `https://cdn.bsky.app/img/feed_thumbnail/plain/${did}/${blob.ref.$link}@webp`;
280}
2810000000282export async function searchActorsTypeahead(
283 q: string,
284 limit: number = 10,
···301302 return { actors: response.data.actors, q };
303}
000000000
···1+import { parseResourceUri, type Did, type Handle } from '@atcute/lexicons';
2import { user } from './auth.svelte';
3import type { AllowedCollection } from './settings';
4import {
···10 WellKnownHandleResolver
11} from '@atcute/identity-resolver';
12import { Client, simpleFetchHandler } from '@atcute/client';
13+import { type AppBskyActorDefs } from '@atcute/bluesky';
1415export type Collection = `${string}.${string}.${string}`;
16+import * as TID from '@atcute/tid';
1718+/**
19+ * Parses an AT Protocol URI into its components.
20+ * @param uri - The AT URI to parse (e.g., "at://did:plc:xyz/app.bsky.feed.post/abc123")
21+ * @returns An object containing the repo, collection, and rkey or undefined if not an AT uri
22+ */
23export function parseUri(uri: string) {
24+ const parts = parseResourceUri(uri);
25+ if (!parts.ok) return;
26+ return parts.value;
00027}
2829+/**
30+ * Resolves a handle to a DID using DNS and HTTP methods.
31+ * @param handle - The handle to resolve (e.g., "alice.bsky.social")
32+ * @returns The DID associated with the handle
33+ */
34export async function resolveHandle({ handle }: { handle: Handle }) {
35 const handleResolver = new CompositeHandleResolver({
36 methods: {
···50 }
51});
5253+/**
54+ * Gets the PDS (Personal Data Server) URL for a given DID.
55+ * @param did - The DID to look up
56+ * @returns The PDS service endpoint URL
57+ * @throws If no PDS is found in the DID document
58+ */
59export async function getPDS(did: Did) {
60+ const doc = await didResolver.resolve(did as Did<'plc'> | Did<'web'>);
61 if (!doc.service) throw new Error('No PDS found');
62 for (const service of doc.service) {
63 if (service.id === '#atproto_pds') {
···66 }
67}
6869+/**
70+ * Fetches a detailed Bluesky profile for a user.
71+ * @param data - Optional object with did and client
72+ * @param data.did - The DID to fetch the profile for (defaults to current user)
73+ * @param data.client - The client to use (defaults to public Bluesky API)
74+ * @returns The profile data or undefined if not found
75+ */
76export async function getDetailedProfile(data?: { did?: Did; client?: Client }) {
77 data ??= {};
78 data.did ??= user.did;
···92 return response.data;
93}
9495+/**
96+ * Creates an AT Protocol client for a user's PDS.
97+ * @param did - The DID of the user
98+ * @returns A client configured for the user's PDS
99+ * @throws If the PDS cannot be found
100+ */
101export async function getClient({ did }: { did: Did }) {
102 const pds = await getPDS(did);
103 if (!pds) throw new Error('PDS not found');
···109 return client;
110}
111112+/**
113+ * Lists records from a repository collection with pagination support.
114+ * @param did - The DID of the repository (defaults to current user)
115+ * @param collection - The collection to list records from
116+ * @param cursor - Pagination cursor for continuing from a previous request
117+ * @param limit - Maximum number of records to return (default 100, set to 0 for all records)
118+ * @param client - The client to use (defaults to user's PDS client)
119+ * @returns An array of records from the collection
120+ */
121export async function listRecords({
122 did,
123 collection,
···165 return allRecords;
166}
167168+/**
169+ * Fetches a single record from a repository.
170+ * @param did - The DID of the repository (defaults to current user)
171+ * @param collection - The collection the record belongs to
172+ * @param rkey - The record key (defaults to "self")
173+ * @param client - The client to use (defaults to user's PDS client)
174+ * @returns The record data
175+ */
176export async function getRecord({
177 did,
178 collection,
···206 return JSON.parse(JSON.stringify(record.data));
207}
208209+/**
210+ * Creates or updates a record in the current user's repository.
211+ * Only accepts collections that are configured in permissions.
212+ * @param collection - The collection to write to (must be in permissions.collections)
213+ * @param rkey - The record key (defaults to "self")
214+ * @param record - The record data to write
215+ * @returns The response from the PDS
216+ * @throws If the user is not logged in
217+ */
218export async function putRecord({
219 collection,
220 rkey = 'self',
···240 return response;
241}
242243+/**
244+ * Deletes a record from the current user's repository.
245+ * Only accepts collections that are configured in permissions.
246+ * @param collection - The collection the record belongs to (must be in permissions.collections)
247+ * @param rkey - The record key (defaults to "self")
248+ * @returns True if the deletion was successful
249+ * @throws If the user is not logged in
250+ */
251export async function deleteRecord({
252 collection,
253 rkey = 'self'
···268 return response.ok;
269}
270271+/**
272+ * Uploads a blob to the current user's PDS.
273+ * @param blob - The blob data to upload
274+ * @returns The blob metadata including ref, mimeType, and size, or undefined on failure
275+ * @throws If the user is not logged in
276+ */
277export async function uploadBlob({ blob }: { blob: Blob }) {
278 if (!user.did || !user.client) throw new Error("Can't upload blob: Not logged in");
279···298 return blobInfo;
299}
300301+/**
302+ * Gets metadata about a repository.
303+ * @param client - The client to use
304+ * @param did - The DID of the repository (defaults to current user)
305+ * @returns Repository metadata or undefined on failure
306+ */
307export async function describeRepo({ client, did }: { client: Client; did?: Did }) {
308 did ??= user.did;
309 if (!did) {
···321 return repo.data;
322}
323324+/**
325+ * Constructs a URL to fetch a blob directly from a user's PDS.
326+ * @param did - The DID of the user who owns the blob
327+ * @param blob - The blob reference object
328+ * @returns The URL to fetch the blob
329+ */
330export async function getBlobURL({
331 did,
332 blob
···343 return `${pds}/xrpc/com.atproto.sync.getBlob?did=${did}&cid=${blob.ref.$link}`;
344}
345346+/**
347+ * Constructs a Bluesky CDN URL for an image blob.
348+ * @param did - The DID of the user who owns the blob (defaults to current user)
349+ * @param blob - The blob reference object
350+ * @returns The CDN URL for the image in webp format
351+ */
352export function getCDNImageBlobUrl({
353 did,
354 blob
355}: {
356+ did?: string;
357 blob: {
358 $type: 'blob';
359 ref: {
···361 };
362 };
363}) {
364+ did ??= user.did;
365+366 return `https://cdn.bsky.app/img/feed_thumbnail/plain/${did}/${blob.ref.$link}@webp`;
367}
368369+/**
370+ * Searches for actors with typeahead/autocomplete functionality.
371+ * @param q - The search query
372+ * @param limit - Maximum number of results (default 10)
373+ * @param host - The API host to use (defaults to public Bluesky API)
374+ * @returns An object containing matching actors and the original query
375+ */
376export async function searchActorsTypeahead(
377 q: string,
378 limit: number = 10,
···395396 return { actors: response.data.actors, q };
397}
398+399+/**
400+ * Return a TID based on current time
401+ *
402+ * @returns TID for current time
403+ */
404+export function createTID() {
405+ return TID.now();
406+}