Giter VIP home page Giter VIP logo

next-multiparty's Introduction

next-multiparty

next-multiparty is a small utility library to ease the process of file uploads with Next.js. It uses formidable under the hood, but with much less work to do and a modern API

Installation

Installation is pretty straight forward. Simply run one of the following commands to install it to your Next.js app.

yarn add next-multiparty
npm i next-multiparty
pnpm add next-multiparty

Usage

TL;DR:

  1. import { withFileUpload, getConfig } from 'next-multiparty'
  2. Simply wrap any api route with withFileUpload
  3. export const config = getConfig()
  4. You now can access req.file (if the request contained one file field) or req.files
  5. Call await req.file.toBuffer() to load the file into the memory

API

withFileUpload

The withFileUpload function is a higher-order function which should be wrapped around an api route from next.js:

import { withFileUpload } from 'next-multiparty';
export default withFileUpload(async (req, res) => {
    res.json({test: 1})
})

By default it will attach the files and files which were posted to that endpoint to the NextApiRequest (in this case req) if the method was POST, PATCH or PUT and the Content-Type header was set to multipart/form-data.

If the request includes files they will be saved to the disk in the os.tmpdir() directory. After the execution of the handler all files will be cleaned up automatically.

The following properties are added to the Request:

  • files: Array of EnhancedFile
  • file: Single EnhancedFile. Will be undefined if there are no files
  • fields: Object containing the name of the field as the key and the value of the field as the value

You can also pass a second parameter options to withFileUpload. Options is an object with the following values:

// Methods which should be allowed. Defaults to ['POST', 'PATCH', 'PUT']
allowedMethods?: HTTP_METHOD[];

// Flag whether the files should be removed after the execution of the handler. Defaults to true. You will probably not need to touch this.
cleanupFiles?: boolean;

// Options to change the behavior of formidable (e.g. max file size). Please refer to the https://github.com/node-formidable/formidable#options
formidableOptions?: formidable.Options

EnhancedFile

Basically just formidable.File but with two added helper functions:

// Loads the file asynchronously from the file system and loads it into the memory
// will throw if the file doesn't exists anymore
toBuffer: () => Promise<Buffer>

// Deletes the file from the file system if it exists
destroy: () => Promise<void>

next-multiparty's People

Contributors

cstrnt avatar duncannah avatar simonorzel26 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

next-multiparty's Issues

Error: Invalid config. Please export the config as the variable `const`

I wrapped API handler as in example with exporting const , but facing following issue:

Error: Invalid config. Please export the config as the variable `const`
    at _callee$ (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:936:21)
    at tryCatch (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:130:40)
    at Generator.invoke [as _invoke] (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:361:22)
    at Generator.next (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:186:21)
    at asyncGeneratorStep (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:12:24)
    at _next (/Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:34:9)
    at /Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:41:7
    at new Promise (<anonymous>)
    at /Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:30:12
    at /Users/user/Development/www/project/node_modules/next-multiparty/dist/next-multiparty.cjs.development.js:995:19
"next": "12.3.4",
"next-multiparty": "^0.6.3",

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.