your personal website on atproto - mirror
blento.app
1import { resolveHandle } from '$lib/atproto';
2import type { Handle } from '@atcute/lexicons';
3
4// Matches URLs like https://bsky.app/profile/jyc.dev/post/3mdfjepjpls24
5const blueskyPostUrlPattern =
6 /^https?:\/\/(?:www\.)?bsky\.app\/profile\/([^/]+)\/post\/([A-Za-z0-9]+)\/?$/;
7
8/**
9 * Extract handle and rkey from a Bluesky post URL
10 * @param url URL to parse
11 * @returns Object with handle and rkey, or undefined if not a valid Bluesky post URL
12 */
13export function parseBlueskyPostUrl(url: string): { handle: string; rkey: string } | undefined {
14 const match = url.match(blueskyPostUrlPattern);
15 if (!match) return undefined;
16 return { handle: match[1], rkey: match[2] };
17}
18
19// Resolve handle to DID if URI contains a handle (not starting with did:)
20export async function resolveUri(atUri: string): Promise<string> {
21 const match = atUri.match(/^at:\/\/([^/]+)\/(.+)$/);
22 if (!match) return atUri;
23
24 const [, authority, rest] = match;
25
26 // If already a DID, return as-is
27 if (authority.startsWith('did:')) return atUri;
28
29 // Resolve handle to DID
30 try {
31 const did = await resolveHandle({ handle: authority as Handle });
32 if (!did) return atUri;
33 return `at://${did}/${rest}`;
34 } catch {
35 return atUri;
36 }
37}