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] Change computedFields to compute function that returns an Object #396

Open
cangSDARM opened this issue Mar 15, 2023 · 2 comments
Open

Comments

@cangSDARM
Copy link

because sometimes more content is needed to generate

like:

 computedFields: {
    extra_info: {
      type: "json",
      resolve: (post) => {
        const url = post._raw.flattenedPath;
        const [collection, ...slug] = url.split("/");

        return { collection, slug, url };
      },
    },
 },

into:

 compute: (post) =>{
    return {
      type: "json",
      resolve: (post) => {
        const url = post._raw.flattenedPath;
        const [collection, ...slug] = url.split("/");

        return { collection, slug, url };
      },
    };
 },

with function, it can be simpler.

@schickling
Copy link
Collaborator

Thanks a lot for opening this issue. I like this API design idea in general however we'd need to figure out a way to either automatically derive or let the user specify the return type (schema) of the compute function. Any ideas?

@cangSDARM
Copy link
Author

How about passing an option parameter? Can the user call something like option.defineSchema('schema')?

 compute: (post, option) =>{
       const url = post._raw.flattenedPath;
       const [collection, ...slug] = url.split("/");
   
       option.defineSchema({
          collection: 'string',
          slug: 'string[]',
          url: 'string'
       });

       return { collection, slug, url };
 },

Or does the underlying internal use Object.getOwnProperties(returned) to iterate over all the properties to generate those types?

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

No branches or pull requests

2 participants