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) { if (r) {
return r; return r;
} }
const url = e.request.url; const url = new URL(e.request.url);
if (url.includes("_api_frm")) {
cache.add(e.request); 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) => { const replacer = (key: string, value: string) => {
if (typeof value === "bigint") {
return `BigInt::${value}`;
}
if (typeof value === "string" && value.startsWith("BigInt::")) { if (typeof value === "string" && value.startsWith("BigInt::")) {
return BigInt(value.substring(8)); return BigInt(value.substring(8));
} }

View File

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