ATProto forum built with ESAV
at main 65 lines 1.8 kB view raw
1export type ResolvedIdentity = 2 | { 3 handle: string 4 did: string 5 pdsUrl: string 6 } 7 | undefined 8const HANDLE_DID_CACHE_TIMEOUT = 60 * 60 * 1000; // 1 hour 9export async function cachedResolveIdentity({ 10 didOrHandle, 11 cacheTimeout = HANDLE_DID_CACHE_TIMEOUT, 12 get, 13 set, 14}: { 15 didOrHandle: string; 16 cacheTimeout?: number; 17 get: (key: string) => any; 18 set: (key: string, value: string) => void; 19}): Promise<ResolvedIdentity|undefined> { 20 const isDidInput = didOrHandle.startsWith("did:"); 21 const cacheKey = `handleDid:${didOrHandle}`; 22 const now = Date.now(); 23 const cached = get(cacheKey); 24 if ( 25 cached && 26 cached.value && 27 cached.time && 28 now - cached.time < cacheTimeout 29 ) { 30 try { 31 return JSON.parse(cached.value); 32 } catch {} 33 } 34 const url = `https://free-fly-24.deno.dev/?${ 35 isDidInput 36 ? `did=${encodeURIComponent(didOrHandle)}` 37 : `handle=${encodeURIComponent(didOrHandle)}` 38 }`; 39 const res = await fetch(url); 40 if (!res.ok) throw new Error("Failed to resolve handle/did"); 41 const data = await res.json(); 42 set(cacheKey, JSON.stringify(data)); 43 // also cache by did if input was handle 44 if (!isDidInput && data.did) { 45 set(`handleDid:${data.did}`, JSON.stringify(data)); 46 } 47 return data; 48} 49 50export async function resolveIdentity({ 51 didOrHandle, 52}: { 53 didOrHandle: string; 54}): Promise<ResolvedIdentity|undefined> { 55 const isDidInput = didOrHandle.startsWith("did:"); 56 const url = `https://free-fly-24.deno.dev/?${ 57 isDidInput 58 ? `did=${encodeURIComponent(didOrHandle)}` 59 : `handle=${encodeURIComponent(didOrHandle)}` 60 }`; 61 const res = await fetch(url); 62 if (!res.ok) throw new Error("Failed to resolve handle/did"); 63 const data = await res.json(); 64 return data; 65}