redwood icon indicating copy to clipboard operation
redwood copied to clipboard

feat: add customize content type parsers for api plugin

Open scott1028 opened this issue 10 months ago • 12 comments

This PR allows users to extend supported contentType for native API plugin. I follow https://redwoodjs.com/docs/docker#using-the-server-file, but it doesn't work.

    ...
  await server.register(redwoodFastifyAPI, {          <-- work after remove "await"
    redwood: {
      apiRootPath,
      fastGlobOptions: {
        ignore: ['**/dist/functions/graphql.js'],
      },
    },
  })
   ...

  // This doesn't work after a "await server.register(...)"
  server.addContentTypeParser('text/csv', { parseAs: 'string' }, (request, body, done) => {
    const csvData = body.split('\n').map(row => row.split(','))
    done(null, csvData)
  })
  • ref: https://fastify.dev/docs/v2.15.x/Documentation/ContentTypeParser/#content-type-parser
  • ref: https://redwoodjs.com/docs/docker#using-the-server-file
  • ref: https://github.com/fastify/fastify/issues/5401
  • ref: https://fastify.dev/docs/latest/Reference/Plugins/#asyncawait

scott1028 avatar Apr 13 '24 02:04 scott1028

Thanks for your PR @scott1028 I'm sorry no one got back to you sooner. We'll discuss this internally and see who can help move this forward

Tobbe avatar May 01 '24 05:05 Tobbe

Thanks for your PR @scott1028 I'm sorry no one got back to you sooner. We'll discuss this internally and see who can help move this forward

Thanks for your reply :)

scott1028 avatar May 02 '24 12:05 scott1028

@scott1028 I found someone who can look into this. He's busy with other things right now, but will get to this when he has some more time available

Tobbe avatar May 02 '24 13:05 Tobbe

Hey @scott1028 👋 Again sorry for the radio silence on this. I'll get to this one in the next day or two

Josh-Walker-GM avatar May 08 '24 07:05 Josh-Walker-GM

Hey @scott1028 👋 Again sorry for the radio silence on this. I'll get to this one in the next day or two

updated test.

scott1028 avatar May 10 '24 06:05 scott1028

@scott1028 Thanks for your effort on this PR!

We've been discussing this PR internally and have a couple of things I wanted to bring up.

  1. We decided we wanted to explore some alternative ways of implementing this.

  2. We think the naming of the parameter could benefit from some additional thought. Could you please help us with some alternative names? Something that makes it more clear what it is, what it does and how it's supposed to be used.

Tobbe avatar May 11 '24 08:05 Tobbe

@Tobbe Thanks for reply.

  1. We decided we wanted to explore some alternative ways of implementing this.

Need I work on this PR if you guys want to implement the same somehow?

  1. We think the naming of the parameter could benefit from some additional thought. Could you please help us with some alternative names? Something that makes it more clear what it is, what it does and how it's supposed to be used.

hmm, I will name it something like preCustomizeAPI, customizeAPI etc, because it's executed before await server.register(redwoodFastifyAPI, .... I just want expose a interface for developer to mutate the fasitfy instance.

scott1028 avatar May 11 '24 10:05 scott1028

Need I work on this PR if you guys want to implement the same somehow?

No, nothing for you to do right now 🙂 We'll get back to you with how to move forward next week

Tobbe avatar May 11 '24 11:05 Tobbe

Sorry it's take me longer than expected to get to this. I'm going to look through this now. One thing that might help me understand better is where you had been planing on using this additional content parser within your project?

Josh-Walker-GM avatar May 15 '24 16:05 Josh-Walker-GM

@scott1028 and @Tobbe - I didn't have permission to push to your fork @scott1028 so I had to open a new PR in the redwood repo: https://github.com/redwoodjs/redwood/pull/10573. This uses your original approach but just renames and moves things around a little.

I'm happy with this approach until we decide we need to rework things in a more destructive way. @Tobbe might have naming or other decisions to make but I'd love to know what you think @scott1028 if this is okay and works for your problem?

Josh-Walker-GM avatar May 15 '24 17:05 Josh-Walker-GM

Sorry it's take me longer than expected to get to this. I'm going to look through this now. One thing that might help me understand better is where you had been planing on using this additional content parser within your project?

Yes. That's what I found the problem and I want to resolve after I surveyed fastify. :)

scott1028 avatar May 15 '24 19:05 scott1028

I'm happy with this approach until we decide we need to rework things in a more destructive way. @Tobbe might have naming or other decisions to make but I'd love to know what you think @scott1028 if this is okay and works for your problem?

It's great and works for me. Thanks.

scott1028 avatar May 15 '24 20:05 scott1028

@Josh-Walker-GM can we close this in favor of your PR?

dthyresson avatar May 22 '24 17:05 dthyresson

@Josh-Walker-GM can we close this in favor of your PR?

yes. I think we can just use https://github.com/redwoodjs/redwood/pull/10573

scott1028 avatar May 22 '24 17:05 scott1028

use https://github.com/redwoodjs/redwood/pull/10573

scott1028 avatar May 22 '24 17:05 scott1028