Skip to content

docs: update references to old fastify-* modules #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# fastify-multipart
# @fastify/multipart

![CI](https://github.com/fastify/fastify-multipart/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/fastify-multipart.svg?style=flat)](https://www.npmjs.com/package/fastify-multipart)
[![NPM version](https://img.shields.io/npm/v/@fastify/multipart.svg?style=flat)](https://www.npmjs.com/package/@fastify/multipart)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-multipart/badge.svg)](https://snyk.io/test/github/fastify/fastify-multipart)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

Expand All @@ -18,7 +18,7 @@ Under the hood it uses [`@fastify/busboy`](https://github.com/fastify/busboy).

## Install
```sh
npm i --save fastify-multipart
npm i --save @fastify/multipart
```

## Usage
Expand All @@ -33,7 +33,7 @@ const path = require('path')
const { pipeline } = require('stream')
const pump = util.promisify(pipeline)

fastify.register(require('fastify-multipart'))
fastify.register(require('@fastify/multipart'))

fastify.post('/', async function (req, reply) {
// process a single file
Expand Down Expand Up @@ -70,15 +70,15 @@ fastify.listen(3000, err => {
})
```

**Note** about `data.fields`: `busboy` consumes the multipart in serial order (stream). Therefore, the order of form fields is *VERY IMPORTANT* to how `fastify-multipart` can display the fields to you.
**Note** about `data.fields`: `busboy` consumes the multipart in serial order (stream). Therefore, the order of form fields is *VERY IMPORTANT* to how `@fastify/multipart` can display the fields to you.
We would recommend you place the value fields first before any of the file fields.
It will ensure your fields are accessible before it starts consuming any files.
If you cannot control the order of the placed fields, be sure to read `data.fields` *AFTER* consuming the stream, or it will only contain the fields parsed at that moment.

You can also pass optional arguments to `@fastify/busboy` when registering with Fastify. This is useful for setting limits on the content that can be uploaded. A full list of available options can be found in the [`@fastify/busboy` documentation](https://github.com/fastify/busboy#busboy-methods).

```js
fastify.register(require('fastify-multipart'), {
fastify.register(require('@fastify/multipart'), {
limits: {
fieldNameSize: 100, // Max field name size in bytes
fieldSize: 100, // Max field value size in bytes
Expand Down Expand Up @@ -226,7 +226,7 @@ fastify.post('/upload/file', async function (req, reply) {
This allows you to parse all fields automatically and assign them to the `request.body`. By default files are accumulated in memory (Be careful!) to buffer objects. Uncaught errors are [handled](https://github.com/fastify/fastify/blob/master/docs/Hooks.md#manage-errors-from-a-hook) by Fastify.

```js
fastify.register(require('fastify-multipart'), { attachFieldsToBody: true })
fastify.register(require('@fastify/multipart'), { attachFieldsToBody: true })

fastify.post('/upload/files', async function (req, reply) {
const uploadValue = await req.body.upload.toBuffer() // access files
Expand All @@ -244,7 +244,7 @@ async function onFile(part) {
await pump(part.file, fs.createWriteStream(part.filename))
}

fastify.register(require('fastify-multipart'), { attachFieldsToBody: true, onFile })
fastify.register(require('@fastify/multipart'), { attachFieldsToBody: true, onFile })

fastify.post('/upload/files', async function (req, reply) {
const fooValue = req.body.foo.value // other fields
Expand All @@ -264,7 +264,7 @@ const opts = {
attachFieldsToBody: true,
sharedSchemaId: '#mySharedSchema'
}
fastify.register(require('fastify-multipart'), opts)
fastify.register(require('@fastify/multipart'), opts)

fastify.post('/upload/files', {
schema: {
Expand Down Expand Up @@ -366,7 +366,7 @@ const opts = {
attachFieldsToBody: true,
sharedSchemaId: '#mySharedSchema'
}
fastify.register(require('fastify-multipart'), opts)
fastify.register(require('@fastify/multipart'), opts)

fastify.post('/upload/files', {
schema: {
Expand Down Expand Up @@ -402,7 +402,7 @@ fastify.post('/upload/files', {

## Access all errors

We export all custom errors via a server decorator `fastify.multipartErrors`. This is useful if you want to react to specific errors. They are derived from [fastify-error](https://github.com/fastify/fastify-error) and include the correct `statusCode` property.
We export all custom errors via a server decorator `fastify.multipartErrors`. This is useful if you want to react to specific errors. They are derived from [@fastify/error](https://github.com/fastify/fastify-error) and include the correct `statusCode` property.

```js
fastify.post('/upload/files', async function (req, reply) {
Expand Down
6 changes: 3 additions & 3 deletions callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const concat = require('concat-stream')
const fs = require('fs')
const pump = require('pump')

fastify.register(require('fastify-multipart'))
fastify.register(require('@fastify/multipart'))

fastify.post('/', function (req, reply) {
// you can use this request's decorator to check if the request is multipart
Expand Down Expand Up @@ -68,7 +68,7 @@ You can also pass optional arguments to busboy when registering with fastify. Th
This behavior is inherited from [busboy](https://github.com/mscdex/busboy).

```js
fastify.register(require('fastify-multipart'), {
fastify.register(require('@fastify/multipart'), {
limits: {
fieldNameSize: 100, // Max field name size in bytes
fieldSize: 1000000, // Max field value size in bytes
Expand Down Expand Up @@ -132,7 +132,7 @@ const options = {
limit: { /*...*/ } // You can the limit options in any case
}

fastify.register(require('fastify-multipart'), options)
fastify.register(require('@fastify/multipart'), options)

fastify.post('/', function (req, reply) {
console.log(req.body)
Expand Down