添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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
  • Builder: vite
  • User Config: nitro , ssr , modules , app
  • Runtime Modules: @pinia/[email protected] , @vueuse/[email protected] , @element-plus/[email protected] , @unocss/[email protected]
  • Build Modules: -
  • Reproduction

    https://stackblitz.com/edit/github-xbwzpm?file=server%2Fapi%2Ftest.ts

    Describe the bug

    server return type why add SerializeObject ?
    接口返回类型为什么要添加 SerializeObject ?

    Additional context

    No response

    No response

    This is actually a feature. All that you return from an event handler is going to be serialised to JSON, so this ensures that the return type accurately matches what you actually get when calling $fetch . That is, you won't have actual Date objects, but Dates that have been stringified.

    Ok, can I export SerializeObject?

    This is actually a feature. All that you return from an event handler is going to be serialised to JSON, so this ensures that the return type accurately matches what you actually get when calling $fetch . That is, you won't have actual Date objects, but Dates that have been stringified.

    Is there an easy way to retrieve objects including dates? Or adding some sort of serialization in between?

    @danielroe this feature is really confusing and I could not find any mention of SerializeObject literally anywhere in the docs or even this repo using GitHub’s search, could you link to a documentation page or at least the module that creates these type definitions?

    UPD: for anyone else encountering this issue, the SerializeObject type comes from https://github.com/unjs/nitro/blob/0fe5fcc9476280465517f890596213ed609e774b/src/types/serialize.ts and it strips any type which is not serializable by JSON.stringify. You can provide your own serialization but it will require TypeScript hacks https://nuxt.com/docs/getting-started/data-fetching#using-an-alternative-serializer

    Just in case anyone has this issue as well, I found that this utility function helps for arrays or objects that are returned from the server api routes. Seems to work for me

    import type { SerializeObject } from 'nitropack';
    type Flatten<T> = T extends Array<SerializeObject<infer R>> ? Array<R> : T extends SerializeObject<infer R> ? R : T;
    export default function jsonParse <T> (data: T): Flatten<T> {
        const deserialized = JSON.parse(JSON.stringify(data));
        return deserialized;
    

    This is actually a feature. All that you return from an event handler is going to be serialised to JSON, so this ensures that the return type accurately matches what you actually get when calling $fetch. That is, you won't have actual Date objects, but Dates that have been stringified.

    Is this documented anywhere with examples?