tsed: FormData property not available from BodyParams in a Middleware

Describe the bug

This is working in v7.33.0 and Iโ€™m not sure when it broke.

Trying to access a FormData property from @BodyParams('key') does not work from within a Middleware. However, I am able to access the property from the Controller method.

Example

I have a frontend form that sets a file for upload and some additional form information in a FormData object, and then sends that to the server.

Ex pseudo-code (frontend):

const formData = new FormData();
formData.append('list', JSON.stringify({ foo: 'bar' });
formData.append('file', file); // This is a File object from when a user uploads a file
await fetch('https://my-url.com', {
  method: 'POST',
  body: formData
});

Ex code (server):

@Middleware()
export class TestBodyParamsMiddleware implements MiddlewareMethods {
    public use(@BodyParams('list') list: string, @Next() next: Next) {
        console.log('List', list);
        next();
    }
}

// Controller method
@Post()
@MulterOptions({ storage: multer.diskStorage({}) })
@Use(TestBodyParamsMiddleware)
@Returns(200)
public async testBodyParams(
    @MultipartFile('file') file: PlatformMulterFile,
    @BodyParams('list') list: string,
): Promise<object> {
    const listObj = JSON.parse(list);
    console.log('List', listObj);
    console.log('file', file);
    return {};
}

With this code, the following happens in the Middleware - list is undefined: Screenshot 2024-07-02 at 1 56 09 PM

However in the actual controller method, list is available as well as the file from the FormData. Screenshot 2024-07-02 at 1 57 27 PM

To Reproduce

See description for steps

Expected behavior

@BodyParams('key') gets the request data from a FormData object from within a Middleware

Code snippets

No response

Repository URL example

No response

OS

macOS, Docker

Node version

Node 20.11.1

Library version

v7.75.0

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

@csnate yes multipart page should be the good place ๐Ÿ˜‰

@csnate I see where the problem. Ok Iโ€™m sorry, to fix an issue with the middleware order execution on endpoint I add also a prioritary option on middleware decorator. Order isnโ€™t easy to determine when decorators can be applied on class or on method (or twice). For example, the authentication middleware must be applied before all other middlewares like PlatformMulterMiddleware as is decribed in this issue https://github.com/tsedio/tsed/issues/865. The prioritary option solve that.

Unfortunately your middleware is called before PlatformMulterMiddleware. Adding {prioritary: 11} on your middleware decorator option will solve your issue ^^. If somebody have time to add this point on the multipart page documentation would be great and help me a lot ๐Ÿ˜‰

Note: @ochrstn could be also a good idea to add the JsonPipe solution for multipart page documentation ?

Here the final solution:

@Middleware({ prioritary: 11 })
export class TestBodyParamsMiddleware implements MiddlewareMethods {
    public use(@BodyParams('list') list: string, @Next() next: Next) {
        console.log('List', list);
        next();
    }
}

See you