at main 1.6 kB view raw
1import { defs } from "@atcute/did-plc"; 2 3const API_BASE = "https://api.aletheia.directory"; 4 5export interface DidDocument { 6 "@context": string[]; 7 id: string; 8 alsoKnownAs: string[]; 9 verificationMethod: any[]; 10 service: any[]; 11} 12 13export class PLCDirectoryAPI { 14 private baseURL: string; 15 16 constructor(baseURL: string = API_BASE) { 17 this.baseURL = baseURL; 18 } 19 20 async fetchDidDocument(did: string): Promise<DidDocument> { 21 const response = await fetch(`${this.baseURL}/${did}`, { 22 mode: 'cors', 23 headers: { 24 'Content-Type': 'application/json', 25 } 26 }); 27 if (!response.ok) { 28 const error = await response 29 .json() 30 .catch(() => ({ message: "Unknown error" })); 31 throw new Error(error.message || `HTTP ${response.status}`); 32 } 33 return response.json(); 34 } 35 36 async fetchDidAuditLog(did: string): Promise<defs.IndexedEntryLog> { 37 const response = await fetch(`${this.baseURL}/${did}/log/audit`, { 38 mode: 'cors', 39 headers: { 40 'Content-Type': 'application/json', 41 } 42 }); 43 if (!response.ok) { 44 const error = await response 45 .json() 46 .catch(() => ({ message: "Unknown error" })); 47 throw new Error(error.message || `HTTP ${response.status}`); 48 } 49 const data = await response.json(); 50 const result = defs.indexedEntryLog.try(data); 51 if (!result.ok) { 52 console.log(result); 53 throw new Error(`Invalid audit log format: ${result.error.message}`); 54 } 55 56 return result.value; 57 } 58} 59 60export const api = new PLCDirectoryAPI();