Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
add tsonURL
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT committed Sep 30, 2023
1 parent ed29989 commit cf8d785
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/handlers/tsonURL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, test } from "vitest";

import { createTupleson } from "../tson.js";
import { tsonURL } from "./tsonURL.js";

test("URL", () => {
const ctx = createTupleson({
types: [tsonURL],
});

const expected = new URL("https://trpc.io/");
expected.hash = "foo";
expected.pathname = "sponsor";

const stringified = ctx.stringify(expected, 2);

expect(stringified).toMatchInlineSnapshot(
`
"{
\\"json\\": [
\\"URL\\",
\\"https://trpc.io/sponsor#foo\\",
\\"__tson\\"
],
\\"nonce\\": \\"__tson\\"
}"
`,
);
const deserialized = ctx.parse(stringified);
expect(deserialized).toEqual(expected);
});
8 changes: 8 additions & 0 deletions src/handlers/tsonURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TsonType } from "../types.js";

export const tsonURL: TsonType<URL, string> = {
deserialize: (value) => new URL(value),
key: "URL",
serialize: (value) => value.toString(),
test: (value) => value instanceof URL,
};

0 comments on commit cf8d785

Please sign in to comment.