Skip to content

Commit 719bb8a

Browse files
committed
feat: add bson-objectid package and custom serialization for ObjectId in API client
1 parent ec28d16 commit 719bb8a

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"@types/uuid": "^9.0.8",
4444
"@typescript-eslint/eslint-plugin": "^6.x",
4545
"@typescript-eslint/parser": "^6.x",
46+
"bson-objectid": "^2.0.4",
4647
"dompurify": "^3.2.4",
4748
"elysia": "^1.3.2",
4849
"eslint": "^8.28.0",

src/lib/APIClient.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ import { base } from "$app/paths";
33
import { treaty, type Treaty } from "@elysiajs/eden";
44
import { browser } from "$app/environment";
55
import superjson from "superjson";
6+
import ObjectId from "bson-objectid";
7+
8+
superjson.registerCustom<ObjectId, string>(
9+
{
10+
isApplicable: (value): value is ObjectId => {
11+
if (ObjectId.isValid(value)) {
12+
const str = value.toString();
13+
return /^[0-9a-fA-F]{24}$/.test(str);
14+
}
15+
return false;
16+
},
17+
serialize: (value) => value.toString(),
18+
deserialize: (value) => new ObjectId(value),
19+
},
20+
"ObjectId"
21+
);
622

723
export function useAPIClient({ fetch }: { fetch?: Treaty.Config["fetcher"] } = {}) {
824
let url;

0 commit comments

Comments
 (0)