Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: can handle bigint data #186

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/client/context/RDTContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Dispatch } from "react"
import type React from "react"
import { createContext, useEffect, useMemo, useReducer } from "react"
import { bigIntReplacer } from "../../shared/bigint-util.js"
import { useRemoveBody } from "../hooks/detached/useRemoveBody.js"
import { checkIsDetached, checkIsDetachedOwner, checkIsDetachedWindow } from "../utils/detached.js"
import { tryParseJson } from "../utils/sanitize.js"
Expand Down Expand Up @@ -126,7 +127,7 @@ export const RDTContextProvider = ({ children, config }: ContextProps) => {
// Store user settings for dev tools into local storage
setStorageItem(REACT_ROUTER_DEV_TOOLS_SETTINGS, JSON.stringify(settings))
// Store general state into local storage
setStorageItem(REACT_ROUTER_DEV_TOOLS_STATE, JSON.stringify(rest))
setStorageItem(REACT_ROUTER_DEV_TOOLS_STATE, JSON.stringify(rest, bigIntReplacer))
}, [state])

return <RDTContext.Provider value={value}>{children}</RDTContext.Provider>
Expand Down
4 changes: 4 additions & 0 deletions src/client/hof.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { ClientActionFunctionArgs, ClientLoaderFunctionArgs, LinksFunction } from "react-router"
import { convertBigIntToString } from "../shared/bigint-util"
import type { RequestEvent } from "../shared/request-event"

const sendEventToDevServer = (req: RequestEvent) => {
if (req.data) {
req.data = convertBigIntToString(req.data)
}
import.meta.hot?.send("request-event", req)
}

Expand Down
17 changes: 17 additions & 0 deletions src/shared/bigint-util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const bigIntReplacer = (key: any, value: any) => (typeof value === "bigint" ? value.toString() : value)

export const convertBigIntToString = (data: any): any => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this util wouldn't work if the bigInt is nested?

if (typeof data === "bigint") {
return data.toString()
}

if (Array.isArray(data)) {
return data.map((item) => convertBigIntToString(item))
}

if (data !== null && typeof data === "object") {
return Object.fromEntries(Object.entries(data).map(([key, value]) => [key, convertBigIntToString(value)]))
}

return data
}
3 changes: 2 additions & 1 deletion src/shared/send-event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bigIntReplacer } from "./bigint-util"
import type { RequestEvent } from "./request-event"

export const sendEvent = (event: RequestEvent) => {
Expand All @@ -9,7 +10,7 @@ export const sendEvent = (event: RequestEvent) => {
if (port) {
fetch(`http://localhost:${port}/react-router-devtools-request`, {
method: "POST",
body: JSON.stringify({ routine: "request-event", ...event }),
body: JSON.stringify({ routine: "request-event", ...event }, bigIntReplacer),
})
.then(async (res) => {
// avoid memory leaks
Expand Down
6 changes: 3 additions & 3 deletions test-apps/react-router-vite/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const loader = ({context, devTools }: LoaderFunctionArgs) => {
});
const start =devTools?.tracing.start("test")!;
devTools?.tracing.end("test", start);
return data({ message: "Hello World", mainPromise }, { headers: { "Cache-Control": "max-age=3600, private" } });
return data({ message: "Hello World", mainPromise, bigInt: BigInt(10) }, { headers: { "Cache-Control": "max-age=3600, private" } });
}

export const action =async ({devTools}: ActionFunctionArgs) => {
Expand All @@ -39,7 +39,7 @@ export const action =async ({devTools}: ActionFunctionArgs) => {
}, 2000);
});
devTools?.tracing.end("action submission", start!)
return ({ message: "Hello World" });
return ({ message: "Hello World", bigInt: BigInt(10) });
}

export default function App() {
Expand All @@ -65,4 +65,4 @@ export default function App() {
</body>
</html>
);
}
}
2 changes: 1 addition & 1 deletion test-apps/react-router-vite/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const loader = async ({ request, context,devTools, params }: LoaderFunct
resolve("test1");
}, 3500);
});
return { message: "Hello World!", test, test1, data };
return { message: "Hello World!", test, test1, data, bigInt: BigInt(10) };
};

export const clientLoader = async ({ request, serverLoader, devTools }: ClientLoaderFunctionArgs) => {
Expand Down