From c79719b593a21db56cc7b1f10cac321d45c9cfbb Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 19 May 2025 14:31:39 +0200 Subject: [PATCH 1/2] update readme to use module imports --- README.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 278aa842..196837b5 100644 --- a/README.md +++ b/README.md @@ -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"; +import multipart from "@fastify/multipart"; -fastify.register(require('@fastify/multipart')) +fastify.register(multipart) fastify.post('/', async function (req, reply) { // process a single file @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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: { @@ -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: { @@ -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, }); @@ -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: { From 996c1494d66124ebd05bccb5f48e188e23a212a3 Mon Sep 17 00:00:00 2001 From: AlvesJorge <60895482+AlvesJorge@users.noreply.github.com> Date: Fri, 22 Aug 2025 10:59:16 +0200 Subject: [PATCH 2/2] docs(readme): undo change to node:streams import Signed-off-by: AlvesJorge <60895482+AlvesJorge@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 196837b5..a28ecf59 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ npm i @fastify/multipart ```js import fastify from "fastify" import fs from "node:fs" -import { pipeline } from "node:stream"; +import { pipeline } from "node:stream/promises"; import multipart from "@fastify/multipart"; fastify.register(multipart)