Skip to content

climba03003/fastify-formidable

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

Bumps [eslint-config-standard-with-typescript](https://github.com/standard/eslint-config-standard-with-typescript) from 36.1.1 to 37.0.0.
- [Release notes](https://github.com/standard/eslint-config-standard-with-typescript/releases)
- [Changelog](https://github.com/standard/eslint-config-standard-with-typescript/blob/master/CHANGELOG.md)
- [Commits](standard/eslint-config-standard-with-typescript@v36.1.1...v37.0.0)

---
updated-dependencies:
- dependency-name: eslint-config-standard-with-typescript
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
c04e882

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
July 20, 2022 18:19
lib
July 20, 2023 16:19
July 20, 2023 16:19
July 10, 2021 00:41
October 26, 2021 18:19
July 10, 2021 00:41
July 20, 2022 18:19
July 10, 2021 00:41
October 26, 2021 18:19
June 9, 2022 14:42
December 7, 2022 14:40

fastify-formidable

Continuous Integration Package Manager CI NPM version GitHub package.json version GitHub

This plugin add a handy parser for multipart/form-data by using formidable and provide a better integration between multipart/form-data and fastify-swagger

Install

npm install fastify-formidable --save

yarn add fastify-formidable

Usage

import FastifyFormidable, { kFileSavedPaths, kIsMultipart, kIsMultipartParsed } from 'fastify-formidable'

fastify.register(FastifyFormidable)

fastify.post('/', async function(request, reply) {
  // you need to call the parser if you do not pass any option through plugin registration
  await request.parseMultipart()

  // access files
  request.files

  // access body
  // note that file fields will exist in body and it will becomes the file path saved on disk
  request.body

  // access all the files path
  request[kFileSavedPaths]

  // check if it is multipart
  if( request[kIsMultipart] === true ) {}

  // check if it is already parsed
  if ( request[kIsMultipartParsed] === true ) {}
})

// add content type parser which will automatic parse all `multipart/form-data` found
fastify.register(FastifyFormidable, {
  addContentTypeParser: true
})

// add `preValidation` hook which will automatic parse all `multipart/form-data` found
fastify.register(FastifyFormidable, {
  addHooks: true
})

Options

options.formidable

The options which will be directly passed to formidable.

import FastifyFormidable from 'fastify-formidable'

fastify.register(FastifyFormidable, {
  formidable: {
    maxFileSize: 1000,
    // this folder will be automatic created by this plugin
    uploadDir: '/'
  }
})

See: formidable

options.removeFilesFromBody

This options will add a preHandler hooks to remove files from body.

import FastifyFormidable from 'fastify-formidable'

fastify.register(FastifyFormidable, {
  removeFilesFromBody: true
})

Integration

It is a known limitation for fastify-multipart integrate with fastify-swagger and this plugin provide a relatively simple solution for the integration.

import Fastify from 'fastify'
import FastifyFormidable, { ajvBinaryFormat } from 'fastify-formidable'
import FastifySwagger from 'fastify-swagger'

const fastify = Fastify({
  ajv: {
    plugins: [ ajvBinaryFormat ]
  }
})

fastify.register(FastifyFormidable, {
  addContentTypeParser: true
})

fastify.register(FastifySwagger)