fastify-multipart
fastify-multipart copied to clipboard
Typescript: there is no way to make req.saveRequestFiles() works when we want to validate schema
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:
- I am getting the error: typescript error:
Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator. for this - 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
Would you like to send a Pull Request to address this issue? Remember to add unit tests.
cc @fastify/typescript
@Eomm I am not really too good about Typescript. I don't understand what is this error. 😀
@meotimdihia Could you provide a minimal reproducible code? Further information, see: https://stackoverflow.com/help/minimal-reproducible-example
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.
@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
attachFieldsToBodyinapp.ts: https://github.com/meotimdihia/fastify-multipart-bug-report/blob/main/src/app.ts#L17- and remove
schema: { body: body}inroot.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.
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)
cc @fastify/typescript @simoneb could you take a look?
I will be taking a look at it to see if I can figure that out
- 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.
- 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 is this fixed?
@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.
I'd say let's close this issue, the OP didn't follow up in months