npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-form-upload

v1.0.3

Published

NestJS Form Upload is a module for NestJS that allows you to upload files using a form (multipart/form-data).

Downloads

7

Readme

@nestjs-form-upload

NestJS Form Upload is a module for NestJS that allows you to upload files using a form (multipart/form-data).

  • Process files nested in a form
  • Integration with class-validator and class-transformer
  • Support for multiple files
  • Process a single request fields and files at the same time
  • Support image resizing and image compression (using sharp)

Providers available

  • Memory (default)

Installation

$ npm install --save nestjs-form-upload

Usage

Import the module

import { Module } from "@nestjs/common";
import { FormUploadModule, FileUploadProvider } from "nestjs-form-upload";

@Module({
  imports: [
    FormUploadModule.register({
      provider: FileUploadProvider.MEMORY,
      options: {},
    }),
  ],
})
export class AppModule {}

Usage

Add the @FileUpload() decorator to your controller method. The decorator will parse the request and extract the files and fields.

import { Controller, Post, UseInterceptors } from "@nestjs/common";
import { FileInterceptor } from "nestjs-form-upload";
import { FileUploadService } from "nestjs-form-upload";

@Controller()
export class AppController {
  public constructor(private fileUploadService: AppService) {}

  @Post()
  @FileUpload({
    options: {
      extensions: ["jpg", "png"],
      maxFileSize: 1024 * 1024 * 5, // exprese in bytes = 5MB
      maxFiles: 5, // max files to upload at the same time
    },
  })
  public async create(@Body() file: FileUpload) {
    // Do something with the file

    return this.fileUploadService.create(file);
  }
}

Image resizing

You can resize the image using the resize option.

Note: The resize only works with images, if you try to resize a non-image file, the module will ignore the resize option and will upload the file as it is.

import { Controller, Post, UseInterceptors } from "@nestjs/common";

export class AppController {
  @Post()
  @FileUpload({
    options: {
      extensions: ["jpg", "png"],
      maxFileSize: 1024 * 1024 * 5, // exprese in bytes = 5MB
      maxFiles: 5, // max files to upload at the same time
      resize: {
        myImage: { width: 100, height: 100, quality: 90 },
      },
    },
  })
  public async create(@Body() file: FileUpload) {
    // Do something with the file
  }
}

Validation

If you want to validate the files, you can use the decorators.

Note: If need to validate an array of files, you need to use each: true property from ValidationOptions.

IsFile

Check if the file is a file.

@IsFile(validationOptions?: ValidationOptions)

IsFiles

Checks an array of files, the same as @IsFile({ each: true }) but with a better name.

@IsFiles(validationOptions?: ValidationOptions)

HasExtension

Check if the file has the specified extension.

@HasExtension(extensions: string, validationOptions?: ValidationOptions)

HasMime

Check if file has mime specified.

@HasMimeFile(mimes: string[])

MinFileSize

Check if the file has the minimum size.

@MinFileSize(minSize: number, validationOptions?: ValidationOptions)

MaxFileSize

Check if the file has the maximum size.

@MaxFileSize(maxSize: number, validationOptions?: ValidationOptions)