Skip to content
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

Rename multipartIterator to parts #147

Merged
merged 2 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fastify.post('/', async function (req, reply) {

```js
fastify.post('/upload/raw/any', async function (req, reply) {
const parts = await req.multipartIterator()
const parts = await req.parts()
for await (const part of parts) {
if (part.file) {
await pump(part.file, fs.createWriteStream(part.filename))
Expand Down
2 changes: 1 addition & 1 deletion examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fastify.post('/upload/stream/files', async function (req, reply) {
})

fastify.post('/upload/raw/any', async function (req, reply) {
const parts = await req.multipartIterator()
const parts = await req.parts()
for await (const part of parts) {
if (part.file) {
await pump(part.file, fs.createWriteStream(part.filename))
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function fastifyMultipart (fastify, options = {}, done) {
})
}
fastify.addHook('preValidation', async function (req, reply) {
for await (const part of req.multipartIterator()) {
for await (const part of req.parts()) {
req.body = part.fields
if (part.file) {
if (options.onFile) {
Expand Down Expand Up @@ -164,7 +164,12 @@ function fastifyMultipart (fastify, options = {}, done) {

fastify.addContentTypeParser('multipart', setMultipart)
fastify.decorateRequest(kMultipartHandler, handleMultipart)

fastify.decorateRequest('parts', getMultipartIterator)
// keeping multipartIterator to avoid bumping a major
Copy link
Member

Choose a reason for hiding this comment

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

wonder if this could be done by using some kind of getter that would be emitting warnings when used. although that might require support on fastify level

Copy link
Member

Choose a reason for hiding this comment

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

maybe just a wrapping function would suffice here, actually

Copy link
Member Author

Choose a reason for hiding this comment

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

This has been released for so little that I don't think we should bother with a full blown deprecation.

// TODO remove on 4.x
fastify.decorateRequest('multipartIterator', getMultipartIterator)

fastify.decorateRequest('isMultipart', isMultipart)
fastify.decorateRequest('tmpUploads', [])

Expand Down
8 changes: 4 additions & 4 deletions test/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('should parse forms', function (t) {
fastify.register(multipart)

fastify.post('/', async function (req, reply) {
for await (const part of req.multipartIterator()) {
for await (const part of req.parts()) {
if (part.file) {
t.equal(part.fieldname, 'upload')
t.equal(part.filename, 'README.md')
Expand Down Expand Up @@ -136,7 +136,7 @@ test('should group parts with the same name to an array', function (t) {
fastify.register(multipart)

fastify.post('/', async function (req, reply) {
const parts = await req.multipartIterator()
const parts = await req.parts()
for await (const part of parts) {
t.ok(part)
if (Array.isArray(part.fields.upload)) {
Expand Down Expand Up @@ -386,7 +386,7 @@ test('should throw error due to fieldsLimit (Max number of non-file fields (Defa

fastify.post('/', async function (req, reply) {
try {
for await (const part of req.multipartIterator({ limits: { fields: 1 } })) {
for await (const part of req.parts({ limits: { fields: 1 } })) {
t.ok(part)
}
reply.code(200).send()
Expand Down Expand Up @@ -436,7 +436,7 @@ test('should throw error due to partsLimit (The max number of parts (fields + fi

fastify.post('/', async function (req, reply) {
try {
for await (const part of req.multipartIterator({ limits: { parts: 1 } })) {
for await (const part of req.parts({ limits: { parts: 1 } })) {
t.ok(part)
}
reply.code(200).send()
Expand Down