Skip to content

docs: Update README to use module imports #581

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ npm i @fastify/multipart
## Usage

```js
const fastify = require('fastify')()
const fs = require('node:fs')
const { pipeline } = require('node:stream/promises')
import fastify from "fastify"
import fs from "node:fs"
import { pipeline } from "node:stream";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlvesJorge

Was /promises removed intentionally?

import multipart from "@fastify/multipart";

fastify.register(require('@fastify/multipart'))
fastify.register(multipart)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to await this

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I want to understand for my own personal use as well.


fastify.post('/', async function (req, reply) {
// process a single file
Expand Down Expand Up @@ -72,7 +73,7 @@ If you cannot control the order of the placed fields, be sure to read `data.fiel
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(multipart, {
limits: {
fieldNameSize: 100, // Max field name size in bytes
fieldSize: 100, // Max field value size in bytes
Expand Down Expand Up @@ -225,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/main/docs/Reference/Hooks.md#manage-errors-from-a-hook) by Fastify.

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

fastify.post('/upload/files', async function (req, reply) {
const uploadValue = await req.body.upload.toBuffer() // access files
Expand All @@ -243,7 +244,7 @@ fastify.post('/upload/files', async function (req, reply) {
Request body key-value pairs can be assigned directly using `attachFieldsToBody: 'keyValues'`. Field values, including file buffers, will be attached to the body object.

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

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

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

fastify.post('/upload/files', async function (req, reply) {
const fooValue = req.body.foo.value // other fields
Expand All @@ -276,7 +277,7 @@ async function onFile(part) {
part.value = decoded // set `part.value` to specify the request body value
}

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

fastify.post('/upload/files', async function (req, reply) {
const uploadValue = req.body.upload // access file as base64 string
Expand All @@ -293,7 +294,7 @@ If you try to read from a stream and pipe to a new file, you will obtain an empt
When the `attachFieldsToBody` parameter is set to `'keyValues'`, JSON Schema validation on the body will behave similarly to `application/json` and [`application/x-www-form-urlencoded`](https://github.com/fastify/fastify-formbody) content types. Additionally, uploaded files will be attached to the body as `Buffer` objects.

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

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

fastify.post('/upload/files', {
schema: {
Expand Down Expand Up @@ -377,16 +378,17 @@ The shared schema, that is added, will look like this:
If you want to use `@fastify/multipart` with `@fastify/swagger` and `@fastify/swagger-ui` you must add a new type called `isFile` and use a custom instance of a validator compiler [Docs](https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#validator-compiler).

```js

const fastify = require('fastify')({
import fastify from "fastify"
import multipart from "@fastify/multipart";
const fastify = fastify({
// ...
ajv: {
// Adds the file plugin to help @fastify/swagger schema generation
plugins: [require('@fastify/multipart').ajvFilePlugin]
plugins: [multipart.ajvFilePlugin]
}
})

fastify.register(require("@fastify/multipart"), {
fastify.register(multipart, {
attachFieldsToBody: true,
});

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

fastify.post('/upload/files', {
schema: {
Expand Down