A simple AtProto app to read pet.mewsse.link records on my PDS.
at main 1.8 kB view raw
1import { CompositeDidDocumentResolver, PlcDidDocumentResolver, WebDidDocumentResolver } from '@atcute/identity-resolver' 2import { isDid } from '@atcute/lexicons/syntax' 3import process from 'node:process' 4 5import type { DidDocument } from '@atcute/identity' 6import type { Did } from '@atcute/lexicons/syntax' 7 8const docResolver = new CompositeDidDocumentResolver({ 9 methods: { 10 plc: new PlcDidDocumentResolver(), 11 web: new WebDidDocumentResolver() 12 } 13}) 14 15export class DIDError extends Error { 16 constructor(msg: string) { 17 super(msg) 18 19 Object.setPrototypeOf(this, DIDError.prototype) 20 } 21} 22 23export class ServiceError extends Error { 24 constructor(msg: string) { 25 super(msg) 26 27 Object.setPrototypeOf(this, ServiceError.prototype) 28 } 29} 30 31export function getUserDID(): Did<"web"> | Did<"plc"> { 32 const did = process.env.DID 33 34 if (!did || did == "") { 35 throw new DIDError("Missing DID to ingest") 36 } 37 38 if (!isDid(did)) { 39 throw new DIDError("DID is not in the correct format") 40 } 41 42 return did as Did<"web"> | Did<"plc"> 43} 44 45export async function findUserDIDDoc(): Promise<DidDocument> { 46 47 try { 48 const did = getUserDID() 49 const doc = await docResolver.resolve(did) 50 return doc 51 } catch (err) { 52 throw err 53 } 54} 55 56export async function findUserPDS(): Promise<string> { 57 const didDoc = await findUserDIDDoc() 58 59 if (!didDoc.service) { 60 throw new ServiceError("No service found on user did doc") 61 } 62 63 const pds = didDoc.service.filter(service => service.id == "#atproto_pds") 64 65 if (pds.length < 1) { 66 throw new ServiceError(`No valid service found for ${process.env.DID}`) 67 } 68 69 let serviceEndpoint = pds.shift()?.serviceEndpoint 70 71 if (!serviceEndpoint || typeof serviceEndpoint != 'string') { 72 throw new ServiceError(`No valid service found for ${process.env.DID}`) 73 } 74 75 return serviceEndpoint 76}