Accepting file array

A product may have more than a single image.

Now, validation is working and we also made it more elegant. However, we should allow a product to have many images of it on the store, and not just one. So, we should make some simple adjustments in the route handler and its decorators:

  • Pluralize the FileInterceptor to FilesInterceptor

  • Pluralize the @UploadedFile() decorator to @UploadedFiles()

  • Change the type of file to an array of files

And, for correctness, let's also pluralize:

  • The route path, from image to images

  • The extracted field and the variable, from file to files

  • The method, from uploadImage() to uploadImages()

And lastly, also define a constant for the maximum amount of files, we'll do that in files -> util -> file.constants. The max amount of a product's images will be defined here.

export const MaxFileCount = {
  PRODUCT_IMAGES: 5,
} as const satisfies Record<string, number>;

Then, use this value in the FilesInterceptor as the second argument. Now, no more than 5 files may be sent at once.

Commit - Accepting a file array in route handler

Last updated