fastify-multipart icon indicating copy to clipboard operation
fastify-multipart copied to clipboard

Typescript: there is no way to make req.saveRequestFiles() works when we want to validate schema

Open meotimdihia opened this issue 3 years ago • 5 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have searched existing issues to ensure the issue has not already been raised

Issue

I have multi problems with the plugin:

  1. I am getting the error: typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator. for this
  2. await req.saveRequestFiles() doesn't work unless I remove all validations and configs: attachFieldsToBody, sharedSchemaId, onFile
// config
fastify.register(fastifyMultipart, {
	attachFieldsToBody: true,
	sharedSchemaId: "#mySharedSchema",
	onFile
})

//routes
  const body = {
    type: "object",
    properties: {
      images: {
            type: "array",
            items: fastify.getSchema("mySharedSchema")
      }
    },
    additionalProperties: true
  } as const

  fastify.post<{
    Params: FromSchema<typeof params>
    Body: FromSchema<typeof body>
  }>("/upload",
    {
      schema: {
        body: body,
        response: {
          200: {
            type: "object",
            additionalProperties: true
          }
        }
      }
    },
    async (req, reply) => {
     // typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
      for (const file of req.body.images) {

      }  
      
      // doesn't work
      const files = await req.saveRequestFiles()
      
      // doesn't work
      req.files
      

meotimdihia avatar Dec 21 '21 14:12 meotimdihia

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

cc @fastify/typescript

Eomm avatar Jan 08 '22 09:01 Eomm

@Eomm I am not really too good about Typescript. I don't understand what is this error. 😀

meotimdihia avatar Jan 08 '22 09:01 meotimdihia

@meotimdihia Could you provide a minimal reproducible code? Further information, see: https://stackoverflow.com/help/minimal-reproducible-example

RafaelGSS avatar Jan 22 '22 23:01 RafaelGSS

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 17 '22 06:04 stale[bot]

@RafaelGSS @Eomm Please re-open this issue.

This is the minimal reproducible code https://github.com/meotimdihia/fastify-multipart-bug-report

STEP 1 FAILED

When you upload files you will get this response []

STEP 2 OK

If you remove

  • attachFieldsToBody in app.ts: https://github.com/meotimdihia/fastify-multipart-bug-report/blob/main/src/app.ts#L17
  • and remove schema: { body: body} in root.ts. https://github.com/meotimdihia/fastify-multipart-bug-report/blob/main/src/routes/root.ts#L38 Then when you upload files, you will get this response.
Untitled 19

meotimdihia avatar Sep 11 '22 10:09 meotimdihia

Guys, any update on this? I confirm @meotimdihia issue with saveRequestFiles method: if the attachFieldsToBody option is enabled, this method doesn't work. It would be nice to have both saveRequestFiles method and attachFieldsToBody options working properly, without doing by hand what saveRequestFiles does (save files in the temp dir and delete them when the request ends)

fmenis avatar Dec 26 '22 18:12 fmenis

cc @fastify/typescript @simoneb could you take a look?

mcollina avatar Dec 28 '22 18:12 mcollina

I will be taking a look at it to see if I can figure that out

Ceres6 avatar Jan 05 '23 10:01 Ceres6

  1. I am getting the error: typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator. for this

With the code provided I have not been able to reproduce. Maybe you could give further code for this.

  1. await req.saveRequestFiles() doesn't work unless I remove all validations and configs: attachFieldsToBody, sharedSchemaId, onFile

I will soon upload a PR to address this issue which will work with all of the above but the onFile which will depend on the behaviour of such function

async (req, reply) => {
 // typescript error: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
  for (const file of req.body.images) {

  }  

Here I was getting 'req.body.images' is possibly 'undefined'. which is solved by either adding required: [images] to schema or a non-null assertion

  // doesn't work
  const files = await req.saveRequestFiles()
  
  // doesn't work
  req.files

Addressing this in my PR

Let me know if I can help with something else

Ceres6 avatar Jan 09 '23 12:01 Ceres6

@Ceres6 is this fixed?

mcollina avatar Jan 13 '23 14:01 mcollina

@mcollina The problem with saveRequestFiles when attachFieldsToBody is. And I couldn't reproduce the Typescript problem so I guess it is. Anyway I'd be happy to help if there is code with the reproducible typing problem.

Ceres6 avatar Jan 13 '23 16:01 Ceres6

I'd say let's close this issue, the OP didn't follow up in months

simoneb avatar Jan 13 '23 16:01 simoneb