social bookmarking for atproto
1/*
2 * clippr: a social bookmarking service for the AT Protocol
3 * Copyright (c) 2025 clippr contributors.
4 * SPDX-License-Identifier: AGPL-3.0-only
5 */
6
7import {
8 CompositeDidDocumentResolver,
9 DocumentNotFoundError,
10 FailedDocumentResolutionError,
11 HandleResolutionError,
12 ImproperDidError,
13 PlcDidDocumentResolver,
14 UnsupportedDidMethodError,
15 WebDidDocumentResolver,
16} from "@atcute/identity-resolver";
17
18// Gets a DID document from a given DID. DID method agnostic.
19export async function getDidDocument(
20 did: `did:plc:${string}` | `did:web:${string}`,
21) {
22 const docResolver = new CompositeDidDocumentResolver({
23 methods: {
24 plc: new PlcDidDocumentResolver(),
25 web: new WebDidDocumentResolver(),
26 },
27 });
28
29 let doc;
30 try {
31 doc = await docResolver.resolve(did);
32 } catch (err) {
33 if (err instanceof DocumentNotFoundError) {
34 throw new Error("Document not found");
35 }
36 if (err instanceof UnsupportedDidMethodError) {
37 throw new Error("Unsupported DID method");
38 }
39 if (err instanceof ImproperDidError) {
40 throw new Error("Invalid DID");
41 }
42 if (err instanceof FailedDocumentResolutionError) {
43 throw new Error("Failed document resolution");
44 }
45 if (err instanceof HandleResolutionError) {
46 throw new Error("Unknown resolution error");
47 }
48 }
49
50 return doc;
51}