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

Add support for BigInt #474

Closed
1 task done
bitbytebit1 opened this issue Jul 31, 2023 · 5 comments
Closed
1 task done

Add support for BigInt #474

bitbytebit1 opened this issue Jul 31, 2023 · 5 comments

Comments

@bitbytebit1
Copy link

Describe the feature

Currently if a BigInt needs to be sent to the client an error will be thrown.

[nuxt] [request error] [unhandled] [500] Do not know how to serialize a BigInt                                                                     1:31:58 PM
  at JSON.stringify (<anonymous>)  
  at Object.handler (./node_modules/h3/dist/index.mjs:1304:18)  
  at processTicksAndRejections (node:internal/process/task_queues:96:5)  
  at async toNodeHandle (./node_modules/h3/dist/index.mjs:1359:7)  
  at async ufetch (./node_modules/unenv/runtime/fetch/index.mjs:9:17)  
  at async $fetchRaw2 (./node_modules/ofetch/dist/shared/ofetch.d438bb6f.mjs:206:26)  
  at async $fetch2 (./node_modules/ofetch/dist/shared/ofetch.d438bb6f.mjs:239:15)  

Would love to see support for this!

One possible solution would be to use the reviver function. However I understand this would add complexity to both the sever and client. Wondering what the teams thoughts are regarding this functionality?

https://stackoverflow.com/questions/62539236/how-to-parse-a-json-data-type-bigint-in-typescript

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 closed this as completed in c5bbee9 Aug 1, 2023
@pi0
Copy link
Member

pi0 commented Aug 1, 2023

Thanks for issue. It will be supported in next version.

@pi0 pi0 mentioned this issue Aug 4, 2023
@bitbytebit1
Copy link
Author

Amazing! One step closer to the Nuxter role!

@chenneil69
Copy link

@pi0 Hello, thought this might help providing some info to your work. I am using [email protected] and it seems to still have the same issue..

 ERROR  [nuxt] [request error] [unhandled] [500] Do not know how to serialize a BigInt
  at JSON.stringify (<anonymous>)  
  at handleHandlerResponse (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2087:29)  
  at ./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1983:15  
  at async Object.callAsync (./node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:72:16)  
  at async Server.toNodeHandle (./node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2266:7)

Thank you for any tips

@rmarquet21
Copy link

The issue with serializing BigInt values within objects or arrays remains unresolved.

Here's an example of an error encountered with [email protected]:

ERROR  [nuxt] [request error] [unhandled] [500] Do not know how to serialize a BigInt
  at JSON.stringify (<anonymous>)  
  at handleHandlerResponse (./node_modules/h3/dist/index.mjs:2071:29)  
  at ./node_modules/h3/dist/index.mjs:1969:15  
  at async Object.callAsync (./node_modules/unctx/dist/index.mjs:72:16)  
  at async Server.toNodeHandle (./node_modules/h3/dist/index.mjs:2250:7)

It appears that the serialization of BigInt values, particularly when nested within objects or arrays, remains problematic.

Alternative solution for the moment that I propose:

type StringifyBigInt<T> = T extends bigint
  ? string
  : T extends (infer U)[]
    ? StringifyBigInt<U>[]
    : T extends object
      ? { [K in keyof T]: StringifyBigInt<T[K]> }
      : T;

export function stringifyBigInt<T>(body: T): StringifyBigInt<T> {
  if (typeof body !== 'object' || body === null) {
    return (typeof body === 'bigint' ? body.toString() : body) as StringifyBigInt<T>;
  }

  if (Array.isArray(body)) {
    return body.map(stringifyBigInt) as StringifyBigInt<T>;
  }

  return Object.fromEntries(
    Object.entries(body).map(([key, value]) => [key, stringifyBigInt(value)]),
  ) as StringifyBigInt<T>;
}

@Urriel
Copy link

Urriel commented Oct 22, 2024

type StringifyBigInt<T> = T extends bigint
  ? string
  : T extends (infer U)[]
    ? StringifyBigInt<U>[]
    : T extends object
      ? { [K in keyof T]: StringifyBigInt<T[K]> }
      : T;

export function stringifyBigInt<T>(body: T): StringifyBigInt<T> {
  if (typeof body !== 'object' || body === null) {
    return (typeof body === 'bigint' ? body.toString() : body) as StringifyBigInt<T>;
  }

  if (Array.isArray(body)) {
    return body.map(stringifyBigInt) as StringifyBigInt<T>;
  }

  return Object.fromEntries(
    Object.entries(body).map(([key, value]) => [key, stringifyBigInt(value)]),
  ) as StringifyBigInt<T>;
}

Your solution is not supporting Dates, here my updated version.
Thx for the previous code, I used it to create a nitro plugin for my nuxt app.

type StringifyBigInt<T> = T extends bigint
  ? string
  : T extends Date
    ? Date
    : T extends (infer U)[]
      ? StringifyBigInt<U>[]
      : T extends object
        ? { [K in keyof T]: StringifyBigInt<T[K]> }
        : T;

export function stringifyBigInt<T>(body: T): StringifyBigInt<T> {
  if (typeof body === 'bigint') {
    return body.toString() as StringifyBigInt<T>;
  }

  if (body instanceof Date || typeof body !== 'object' || body === null) {
    return body as StringifyBigInt<T>;
  }

  if (Array.isArray(body)) {
    return body.map(stringifyBigInt) as StringifyBigInt<T>;
  }

  return Object.fromEntries(
    Object.entries(body).map(([key, value]) => [key, stringifyBigInt(value)])
  ) as StringifyBigInt<T>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants