diff --git a/src/at/did_document.gleam b/src/at/did_document.gleam index 43cf81b..57cb39b 100644 --- a/src/at/did_document.gleam +++ b/src/at/did_document.gleam @@ -4,6 +4,7 @@ import at/did.{type Did} import at/handle.{type Handle} import at/internal/encoding import gleam/dynamic/decode.{type Decoder} +import gleam/function import gleam/json.{type Json} import gleam/list import gleam/option.{type Option, None, Some} @@ -115,31 +116,45 @@ pub fn decoder() -> Decoder(DidDocument) { use controller <- decode.optional_field("controller", [], { decode.list(did.decoder()) }) + use also_known_as <- decode.optional_field("alsoKnownAs", [], { - decode.list(decode.one_of(handle.at_decoder(), [handle.decoder()])) + decode.list( + decode.one_of(decode.map(handle.at_decoder(), Ok), [ + decode.map(handle.decoder(), Ok), + decode.success(Error(Nil)), + ]), + ) + |> decode.map(list.filter_map(_, function.identity)) }) + use verification_method <- decode.optional_field("verificationMethod", [], { decode.list(embedded_verification_method_decoder()) }) + use authentication <- decode.optional_field("authentication", [], { decode.list(verification_method_decoder()) }) + use assertion_method <- decode.optional_field("assertionMethod", [], { decode.list(verification_method_decoder()) }) + use key_agreement <- decode.optional_field("keyAgreement", [], { decode.list(verification_method_decoder()) }) + use capability_invocation <- decode.optional_field( "capabilityInvocation", [], - { decode.list(verification_method_decoder()) }, + decode.list(verification_method_decoder()), ) + use capability_delegation <- decode.optional_field( "capabilityDelegation", [], - { decode.list(verification_method_decoder()) }, + decode.list(verification_method_decoder()), ) + use service <- decode.optional_field("service", [], { decode.list(service_endpoint_decoder()) }) @@ -203,6 +218,12 @@ fn service_endpoint_decoder() -> Decoder(ServiceEndpoint) { // QUERIES --------------------------------------------------------------------- +/// +/// +pub fn also_known_as(document: DidDocument) -> Result(Handle, Nil) { + list.first(document.also_known_as) +} + /// Find the [personal data server](https://atproto.com/guides/glossary#pds-personal-data-server) /// that hosts an account's repository. Once you know the location of the PDS /// you can start to interact with the account by, for example, fetching specific