UNPKG

@vector-im/matrix-bot-sdk

Version:

TypeScript/JavaScript SDK for Matrix bots and appservices

23 lines (20 loc) 703 B
export class MXCUrl { static parse(mxcUrl: string): MXCUrl { if (!mxcUrl?.toLowerCase()?.startsWith("mxc://")) { throw Error("Not a MXC URI"); } const [domain, ...mediaIdParts] = mxcUrl.slice("mxc://".length).split("/"); if (!domain) { throw Error("missing domain component"); } const mediaId = mediaIdParts?.join('/') ?? undefined; if (!mediaId) { throw Error("missing mediaId component"); } return new MXCUrl(domain, mediaId); } constructor(public domain: string, public mediaId: string) { } public toString() { return `mxc://${this.domain}/${this.mediaId}`; } }