nsarrazin commited on
Commit
874ab60
·
1 Parent(s): dc1bd76

feat: add bson-objectid package and custom serialization for ObjectId in API client

Browse files
Files changed (3) hide show
  1. package-lock.json +8 -0
  2. package.json +1 -0
  3. src/lib/APIClient.ts +16 -0
package-lock.json CHANGED
@@ -87,6 +87,7 @@
87
  "@types/uuid": "^9.0.8",
88
  "@typescript-eslint/eslint-plugin": "^6.x",
89
  "@typescript-eslint/parser": "^6.x",
 
90
  "dompurify": "^3.2.4",
91
  "elysia": "^1.3.2",
92
  "eslint": "^8.28.0",
@@ -7995,6 +7996,13 @@
7995
  "node": ">=14.20.1"
7996
  }
7997
  },
 
 
 
 
 
 
 
7998
  "node_modules/buffer": {
7999
  "version": "6.0.3",
8000
  "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
 
87
  "@types/uuid": "^9.0.8",
88
  "@typescript-eslint/eslint-plugin": "^6.x",
89
  "@typescript-eslint/parser": "^6.x",
90
+ "bson-objectid": "^2.0.4",
91
  "dompurify": "^3.2.4",
92
  "elysia": "^1.3.2",
93
  "eslint": "^8.28.0",
 
7996
  "node": ">=14.20.1"
7997
  }
7998
  },
7999
+ "node_modules/bson-objectid": {
8000
+ "version": "2.0.4",
8001
+ "resolved": "https://registry.npmjs.org/bson-objectid/-/bson-objectid-2.0.4.tgz",
8002
+ "integrity": "sha512-vgnKAUzcDoa+AeyYwXCoHyF2q6u/8H46dxu5JN+4/TZeq/Dlinn0K6GvxsCLb3LHUJl0m/TLiEK31kUwtgocMQ==",
8003
+ "dev": true,
8004
+ "license": "Apache-2.0"
8005
+ },
8006
  "node_modules/buffer": {
8007
  "version": "6.0.3",
8008
  "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
package.json CHANGED
@@ -43,6 +43,7 @@
43
  "@types/uuid": "^9.0.8",
44
  "@typescript-eslint/eslint-plugin": "^6.x",
45
  "@typescript-eslint/parser": "^6.x",
 
46
  "dompurify": "^3.2.4",
47
  "elysia": "^1.3.2",
48
  "eslint": "^8.28.0",
 
43
  "@types/uuid": "^9.0.8",
44
  "@typescript-eslint/eslint-plugin": "^6.x",
45
  "@typescript-eslint/parser": "^6.x",
46
+ "bson-objectid": "^2.0.4",
47
  "dompurify": "^3.2.4",
48
  "elysia": "^1.3.2",
49
  "eslint": "^8.28.0",
src/lib/APIClient.ts CHANGED
@@ -3,6 +3,22 @@ import { base } from "$app/paths";
3
  import { treaty, type Treaty } from "@elysiajs/eden";
4
  import { browser } from "$app/environment";
5
  import superjson from "superjson";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  export function useAPIClient({ fetch }: { fetch?: Treaty.Config["fetcher"] } = {}) {
8
  let url;
 
3
  import { treaty, type Treaty } from "@elysiajs/eden";
4
  import { browser } from "$app/environment";
5
  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
+ );
22
 
23
  export function useAPIClient({ fetch }: { fetch?: Treaty.Config["fetcher"] } = {}) {
24
  let url;