@fastify/multipart
Advanced tools
Comparing version 7.6.0 to 7.6.1
@@ -11,16 +11,21 @@ 'use strict' | ||
fastify.post('/upload/files', { | ||
schema: { | ||
body: { | ||
type: 'object', | ||
required: ['myField'], | ||
properties: { | ||
myField: { $ref: '#mySharedSchema' } | ||
fastify.post( | ||
'/upload/files', | ||
{ | ||
schema: { | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
type: 'object', | ||
required: ['myField'], | ||
properties: { | ||
myField: { $ref: '#mySharedSchema' } | ||
} | ||
} | ||
} | ||
}, | ||
function (req, reply) { | ||
console.log({ body: req.body }) | ||
reply.send('done') | ||
} | ||
}, function (req, reply) { | ||
console.log({ body: req.body }) | ||
reply.send('done') | ||
}) | ||
) | ||
@@ -27,0 +32,0 @@ fastify.listen({ port: 3000 }, err => { |
@@ -36,2 +36,4 @@ import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy"; | ||
tmpUploads: Array<string> | null; | ||
/** This will get populated as soon as a call to `saveRequestFiles` gets resolved. Avoiding any future duplicate work */ | ||
savedRequestFiles: Array<fastifyMultipart.SavedMultipartFile> | null; | ||
} | ||
@@ -38,0 +40,0 @@ |
17
index.js
@@ -10,3 +10,3 @@ 'use strict' | ||
const path = require('path') | ||
const hexoid = require('hexoid') | ||
const { generateId } = require('./lib/generateId') | ||
const util = require('util') | ||
@@ -210,2 +210,3 @@ const createError = require('@fastify/error') | ||
fastify.decorateRequest('tmpUploads', null) | ||
fastify.decorateRequest('savedRequestFiles', null) | ||
@@ -227,4 +228,2 @@ // legacy | ||
const toID = hexoid() | ||
function isMultipart () { | ||
@@ -530,2 +529,6 @@ return this.raw[kMultipart] || false | ||
async function saveRequestFiles (options) { | ||
// Checks if this has already been run | ||
if (this.savedRequestFiles) { | ||
return this.savedRequestFiles | ||
} | ||
let files | ||
@@ -541,11 +544,11 @@ if (attachFieldsToBody === true) { | ||
} | ||
const requestFiles = [] | ||
this.savedRequestFiles = [] | ||
const tmpdir = (options && options.tmpdir) || os.tmpdir() | ||
this.tmpUploads = [] | ||
for await (const file of files) { | ||
const filepath = path.join(tmpdir, toID() + path.extname(file.filename)) | ||
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename)) | ||
const target = createWriteStream(filepath) | ||
try { | ||
await pump(file.file, target) | ||
requestFiles.push({ ...file, filepath }) | ||
this.savedRequestFiles.push({ ...file, filepath }) | ||
this.tmpUploads.push(filepath) | ||
@@ -558,3 +561,3 @@ } catch (err) { | ||
return requestFiles | ||
return this.savedRequestFiles | ||
} | ||
@@ -561,0 +564,0 @@ |
{ | ||
"name": "@fastify/multipart", | ||
"version": "7.6.0", | ||
"version": "7.6.1", | ||
"description": "Multipart plugin for Fastify", | ||
@@ -11,5 +11,6 @@ "main": "index.js", | ||
"@fastify/error": "^3.0.0", | ||
"@fastify/swagger": "^8.3.1", | ||
"@fastify/swagger-ui": "^1.8.0", | ||
"end-of-stream": "^1.4.4", | ||
"fastify-plugin": "^4.0.0", | ||
"hexoid": "^1.0.0", | ||
"secure-json-parse": "^2.4.0", | ||
@@ -20,5 +21,6 @@ "stream-wormhole": "^1.1.0" | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@types/node": "^18.0.6", | ||
"@types/node": "^20.1.0", | ||
"@typescript-eslint/eslint-plugin": "^5.30.7", | ||
"@typescript-eslint/parser": "^5.30.7", | ||
"benchmark": "^2.1.4", | ||
"climem": "^1.0.3", | ||
@@ -25,0 +27,0 @@ "concat-stream": "^2.0.0", |
@@ -300,2 +300,3 @@ # @fastify/multipart | ||
schema: { | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
@@ -335,2 +336,3 @@ type: 'object', | ||
schema: { | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
@@ -377,2 +379,53 @@ type: 'object', | ||
### JSON Schema with Swagger | ||
If you want to use `@fastify/multipart` with `@fastify/swagger` and `@fastify/swagger-ui` you must add a new type called `isFile` and use custom instance of validator compiler [Docs](https://www.fastify.io/docs/latest/Reference/Validation-and-Serialization/#validator-compiler). | ||
```js | ||
const ajvFilePlugin = (ajv, options = {}) => { | ||
return ajv.addKeyword({ | ||
keyword: "isFile", | ||
compile: (_schema, parent, _it) => { | ||
parent.type = "file"; | ||
delete parent.isFile; | ||
return () => true; | ||
}, | ||
}); | ||
}; | ||
const fastify = require('fastify')({ | ||
// ... | ||
ajv: { | ||
// add the new ajv plugin | ||
plugins: [/*...*/ ajvFilePlugin] | ||
} | ||
}) | ||
const opts = { | ||
attachFieldsToBody: true, | ||
}; | ||
fastify.register(require(".."), opts); | ||
fastify.post( | ||
"/upload/files", | ||
{ | ||
schema: { | ||
consumes: ["multipart/form-data"], | ||
body: { | ||
type: "object", | ||
required: ["myField"], | ||
properties: { | ||
myField: { isFile: true }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
function (req, reply) { | ||
console.log({ body: req.body }); | ||
reply.send("done"); | ||
} | ||
); | ||
``` | ||
### JSON Schema non-file field | ||
@@ -439,2 +492,3 @@ When sending fields with the body (`attachFieldsToBody` set to true), the field might look like this in the `request.body`: | ||
schema: { | ||
consumes: ['multipart/form-data'], | ||
body: { | ||
@@ -441,0 +495,0 @@ type: 'object', |
Sorry, the diff of this file is not supported yet
179927
42
5097
539
9
22
21
19
+ Added@fastify/swagger@^8.3.1
+ Added@fastify/swagger-ui@^1.8.0
+ Added@fastify/accept-negotiator@1.1.0(transitive)
+ Added@fastify/send@2.1.0(transitive)
+ Added@fastify/static@6.12.0(transitive)
+ Added@fastify/swagger@8.15.0(transitive)
+ Added@fastify/swagger-ui@1.10.2(transitive)
+ Added@lukeed/ms@2.0.2(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@2.0.1(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addeddebug@4.3.6(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedfast-decode-uri-component@1.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@8.1.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjson-schema-resolver@2.0.0(transitive)
+ Addedmime@3.0.0(transitive)
+ Addedminimatch@5.1.6(transitive)
+ Addedms@2.1.2(transitive)
+ Addedopenapi-types@12.1.3(transitive)
+ Addedp-limit@3.1.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedrfdc@1.4.1(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addedyaml@2.5.0(transitive)
+ Addedyocto-queue@0.1.0(transitive)
- Removedhexoid@^1.0.0
- Removedhexoid@1.0.0(transitive)