[APP-2160] Add parseDidFromAtUri util for PublicationEmbed
This commit is contained in:
committed by
Eric Bailey
parent
6159cd7777
commit
feb623520c
@@ -0,0 +1,32 @@
|
||||
import {parseDidFromAtUri} from '#/components/Post/Embed/ExternalEmbed/PublicationEmbed/util'
|
||||
|
||||
describe('parseDidFromAtUri', () => {
|
||||
it('extracts a plc DID from a publication at-uri', () => {
|
||||
expect(
|
||||
parseDidFromAtUri('at://did:plc:abc123/site.standard.publication/3jx'),
|
||||
).toBe('did:plc:abc123')
|
||||
})
|
||||
|
||||
it('extracts a web DID', () => {
|
||||
expect(
|
||||
parseDidFromAtUri(
|
||||
'at://did:web:example.com/site.standard.publication/3jx',
|
||||
),
|
||||
).toBe('did:web:example.com')
|
||||
})
|
||||
|
||||
it('returns undefined for non-at-uri strings', () => {
|
||||
expect(parseDidFromAtUri('https://example.com')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined for empty / nullish input', () => {
|
||||
expect(parseDidFromAtUri('')).toBeUndefined()
|
||||
expect(parseDidFromAtUri(undefined)).toBeUndefined()
|
||||
})
|
||||
|
||||
it('returns undefined when the authority is not a DID', () => {
|
||||
expect(
|
||||
parseDidFromAtUri('at://alice.bsky.social/site.standard.publication/3jx'),
|
||||
).toBeUndefined()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Extract the DID from an at-uri of the form `at://did:<method>:<id>/<nsid>/<rkey>`.
|
||||
* Returns undefined if the input is falsy, not an at-uri, or the authority is not a DID.
|
||||
*/
|
||||
export function parseDidFromAtUri(uri: string | undefined): string | undefined {
|
||||
if (!uri || !uri.startsWith('at://')) return undefined
|
||||
const authority = uri.slice('at://'.length).split('/')[0]
|
||||
if (!authority || !authority.startsWith('did:')) return undefined
|
||||
return authority
|
||||
}
|
||||
Reference in New Issue
Block a user