1.0.0 • Published 3 years ago

@trubavuong/fastify-multipart v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

fastify-multipart

A wrapper of fastify-multipart with confident mimetype which accumulates whole file in memory.

Note

This plugin has some characteristics:

  1. Loads whole file in memory, so don't use this plugin if you want to upload large files!
  2. Assign all fields to the body, so you can use JSON schema to validate them
  3. MIME type only works with binaries, such as images, videos, documents...

Install

$ npm install @trubavuong/fastify-multipart

Usage

const fastify = require('fastify');
const plugin = require('@trubavuong/fastify-multipart');

const app = fastify();
app.register(plugin);

JSON schema

You should write your own JSON schema to fit your need. Here is an example of the req.body:

{
  name: {
    fieldname: 'name',
    value: 'Vuong Tru',
  },
  avatar: {
    fieldname: 'avatar',
    filename: 'image.jpg',
    encoding: '7bit',
    mimetype: 'image/jpeg', // original mimetype
    mime: 'image/jpeg',     // corrected mimetype, added by this plugin
    ext: 'jpg',             // corrected extension, added by this plugin
    toBuffer: <function>
  }
}