···1+import { expect, test } from "vitest";
2+import { uriToRkey } from "../utils";
3+4+test("should throw if invalid at uri is passed in", () => {
5+ expect(() => uriToRkey("test")).toThrowError("get rkey");
6+});
7+8+test("should pass if valid at uri is passed in", () => {
9+ expect(
10+ uriToRkey(
11+ "at://did:plc:qttsv4e7pu2jl3ilanfgc3zn/pub.leaflet.document/3lvl7m6jd4s2e",
12+ ),
13+ ).toBe("3lvl7m6jd4s2e");
14+});
+4-4
src/utils.ts
···1-import { UnicodeString } from "@atproto/api";
2import sanitizeHTML from "sanitize-html";
3import {
4 PubLeafletBlocksHeader,
···19} from "./types.js";
2021export function uriToRkey(uri: string): string {
22- const rkey = uri.split("/").pop();
23- if (!rkey) {
24 throw new Error("Failed to get rkey from uri.");
25 }
26- return rkey;
27}
2829export async function resolveMiniDoc(handleOrDid: string) {
···1+import { AtUri, UnicodeString } from "@atproto/api";
2import sanitizeHTML from "sanitize-html";
3import {
4 PubLeafletBlocksHeader,
···19} from "./types.js";
2021export function uriToRkey(uri: string): string {
22+ const u = AtUri.make(uri);
23+ if (!u.rkey) {
24 throw new Error("Failed to get rkey from uri.");
25 }
26+ return u.rkey;
27}
2829export async function resolveMiniDoc(handleOrDid: string) {