@fastify/multipart
Advanced tools
Comparing version 7.6.1 to 7.7.0
'use strict' | ||
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')({ | ||
// ... | ||
logger: true, | ||
ajv: { | ||
plugins: [ajvFilePlugin] | ||
// Adds the file plugin to help @fastify/swagger schema generation | ||
plugins: [require('..').ajvFilePlugin] | ||
} | ||
@@ -22,4 +15,6 @@ }) | ||
}) | ||
fastify.register(require('fastify-swagger')) | ||
fastify.register(require('@fastify/swagger-ui')) | ||
fastify.post( | ||
@@ -26,0 +21,0 @@ '/upload/files', |
@@ -208,2 +208,7 @@ import { Busboy, BusboyConfig, BusboyFileStream } from "@fastify/busboy"; | ||
/** | ||
* Adds a new type `isFile` to help @fastify/swagger generate the correct schema. | ||
*/ | ||
export function ajvFilePlugin(ajv: any): void; | ||
export const fastifyMultipart: FastifyMultipartPlugin; | ||
@@ -210,0 +215,0 @@ export { fastifyMultipart as default }; |
23
index.js
@@ -26,3 +26,3 @@ 'use strict' | ||
const FieldsLimitError = createError('FST_FIELDS_LIMIT', 'reach fields limit', 413) | ||
const RequestFileTooLargeError = createError('FST_REQ_FILE_TOO_LARGE', 'request file too large, please check multipart config', 413) | ||
const RequestFileTooLargeError = createError('FST_REQ_FILE_TOO_LARGE', 'request file too large', 413) | ||
const PrototypeViolationError = createError('FST_PROTO_VIOLATION', 'prototype property is not allowed as field name', 400) | ||
@@ -630,2 +630,22 @@ const InvalidMultipartContentTypeError = createError('FST_INVALID_MULTIPART_CONTENT_TYPE', 'the request is not multipart', 406) | ||
/** | ||
* Adds a new type `isFile` to help @fastify/swagger generate the correct schema. | ||
*/ | ||
function ajvFilePlugin (ajv) { | ||
return ajv.addKeyword({ | ||
keyword: 'isFile', | ||
compile: (_schema, parent) => { | ||
// Updates the schema to match the file type | ||
parent.type = 'string' | ||
parent.format = 'binary' | ||
delete parent.isFile | ||
return (field /* MultipartFile */) => !!field.file | ||
}, | ||
error: { | ||
message: 'should be a file' | ||
} | ||
}) | ||
} | ||
/** | ||
* These export configurations enable JS and TS developers | ||
@@ -640,1 +660,2 @@ * to consumer fastify in whatever way best suits their needs. | ||
module.exports.fastifyMultipart = fastifyMultipart | ||
module.exports.ajvFilePlugin = ajvFilePlugin |
{ | ||
"name": "@fastify/multipart", | ||
"version": "7.6.1", | ||
"version": "7.7.0", | ||
"description": "Multipart plugin for Fastify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -382,24 +382,14 @@ # @fastify/multipart | ||
```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] | ||
// Adds the file plugin to help @fastify/swagger schema generation | ||
plugins: [require('@fastify/multipart').ajvFilePlugin] | ||
} | ||
}) | ||
const opts = { | ||
fastify.register(require("@fastify/multipart"), { | ||
attachFieldsToBody: true, | ||
}; | ||
fastify.register(require(".."), opts); | ||
}); | ||
@@ -406,0 +396,0 @@ fastify.post( |
183263
44
5230
529
22
20