fix bigint

This commit is contained in:
Rizky 2023-10-15 14:24:46 +07:00
parent 8eeff6a727
commit f894e70c69
3 changed files with 22 additions and 5 deletions

View File

@ -32,12 +32,17 @@ addEventListener("fetch", async (evt) => {
if (r) {
return r;
}
const url = e.request.url;
if (url.includes("_api_frm")) {
cache.add(e.request);
const url = new URL(e.request.url);
const res = await fetch(e.request);
if (
url.pathname.includes("_api_frm") ||
url.pathname.startsWith("/_prasi")
) {
await cache.put(e.request, res);
}
return await fetch(e.request);
return res;
})()
);
});

View File

@ -38,6 +38,9 @@ export const apiContext = (ctx: any) => {
};
const replacer = (key: string, value: string) => {
if (typeof value === "bigint") {
return `BigInt::${value}`;
}
if (typeof value === "string" && value.startsWith("BigInt::")) {
return BigInt(value.substring(8));
}

View File

@ -1,6 +1,13 @@
import { createResponse } from "./api-ctx";
import { g } from "../utils/global";
const replacer = (key: string, value: string) => {
if (typeof value === "string" && value.startsWith("BigInt::")) {
return BigInt(value.substring(8));
}
return value;
};
export const serveAPI = async (url: URL, req: Request) => {
let found = g.router.lookup(url.pathname);
if (!found?.url) {
@ -23,7 +30,9 @@ export const serveAPI = async (url: URL, req: Request) => {
if (req.method !== "GET") {
if (!req.headers.get("content-type")?.startsWith("multipart/form-data")) {
try {
const json: any = await req.json();
const text = await req.text();
const json = JSON.parse(text, replacer);
if (typeof json === "object") {
if (Array.isArray(json)) {
args = json;