···11+import { expect, test } from "vitest";
22+import { uriToRkey } from "../utils";
33+44+test("should throw if invalid at uri is passed in", () => {
55+ expect(() => uriToRkey("test")).toThrowError("get rkey");
66+});
77+88+test("should pass if valid at uri is passed in", () => {
99+ expect(
1010+ uriToRkey(
1111+ "at://did:plc:qttsv4e7pu2jl3ilanfgc3zn/pub.leaflet.document/3lvl7m6jd4s2e",
1212+ ),
1313+ ).toBe("3lvl7m6jd4s2e");
1414+});
+4-4
src/utils.ts
···11-import { UnicodeString } from "@atproto/api";
11+import { AtUri, UnicodeString } from "@atproto/api";
22import sanitizeHTML from "sanitize-html";
33import {
44 PubLeafletBlocksHeader,
···1919} from "./types.js";
20202121export function uriToRkey(uri: string): string {
2222- const rkey = uri.split("/").pop();
2323- if (!rkey) {
2222+ const u = AtUri.make(uri);
2323+ if (!u.rkey) {
2424 throw new Error("Failed to get rkey from uri.");
2525 }
2626- return rkey;
2626+ return u.rkey;
2727}
28282929export async function resolveMiniDoc(handleOrDid: string) {