-
-
Notifications
You must be signed in to change notification settings - Fork 109
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
AlvesJorge
wants to merge
1
commit into
fastify:main
Choose a base branch
from
AlvesJorge:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to await this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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: { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?