diff --git a/README.md b/README.md index 22e81fa..21eaba7 100644 --- a/README.md +++ b/README.md @@ -39,52 +39,86 @@ const postCreateSchema = { You will find that neither `@fastify/multipart` nor `fastify-multer` will process this schema correctly, unless you add a `preValidation` hook to convert your request body into the correct schema. I created Formzilla to solve this exact problem. ```tsx -import fastify, { FastifyInstance, FastifyRequest, FastifyReply, FastifyPluginOptions } from "fastify"; -import fastifySwagger from "@fastify/swagger"; +import fastify, { FastifyInstance } from "fastify"; import formDataParser from "formzilla"; +import fastifySwagger from "@fastify/swagger"; +import fastifySwaggerUi from "@fastify/swagger-ui"; +const postCreateSchema = { + consumes: ["multipart/form-data"], + body: { + type: "object", + properties: { + content: { + type: "string" + }, + media: { + type: "string", + format: "binary" + }, + poll: { + type: "object", + properties: { + first: { type: "string" }, + second: { type: "string" } + }, + required: ["first", "second"] + } + } + } +}; const server: FastifyInstance = fastify({ logger: true }); +server.register(formDataParser); server.register(fastifySwagger, { - routePrefix: "/swagger", - exposeRoute: true, + mode: "dynamic", openapi: { info: { - title: "Formzilla Demo", + title: "Fastify Playground", + description: "Testing Fastify features", version: "1.0.0" } } }); -server.register(formDataParser); -server.register( - async (instance: FastifyInstance, options: FastifyPluginOptions) => { - instance.post( - "/create", +server.register(fastifySwaggerUi, { + routePrefix: "/swagger" +}); +server.register(async (instance, options) => { + instance.post("/create-post", { + schema: postCreateSchema, + handler: (request, reply) => { + console.log(request.body); + /* + request.body will look like this: { - schema: postCreateSchema - }, - (request: FastifyRequest, reply: FastifyReply) => { - console.log(request.body); - /* - request.body will look like this: - { - content: "Test.", - poll: { first: "Option 1", second: "Option 2" }, - media: { - fileName: "flame-wolf.png", - encoding: "7bit", - mimeType: "image/png", - path?: , // Only when using DiscStorage - stream?: // Only when using StreamStorage - data?: // Only when using BufferStorage - error?: // Only if any errors occur during processing - } + content: "Test.", + poll: { first: "Option 1", second: "Option 2" }, + media: { + originalName: "flame-wolf.png", + encoding: "7bit", + mimeType: "image/png", + path?: , // Only when using DiscStorage + stream?: // Only when using StreamStorage + data?: // Only when using BufferStorage + error?: // Only if any errors occur during processing } - */ - reply.status(200).send(); } - ); + */ + reply.status(200).send(); + } + }); +}); +server.listen( + { + port: +(process.env.PORT as string) || 1024, + host: process.env.HOST || "::" }, - { prefix: "/posts" } + (err, address) => { + if (err) { + console.log(err.message); + process.exit(1); + } + console.log(`Listening on ${address}`); + } ); ``` diff --git a/index.js b/index.js index 6864c6d..6ebfae0 100644 --- a/index.js +++ b/index.js @@ -48,6 +48,7 @@ const formDataParser = async (instance, options) => { const fileFields = Object.create(null); for (const file of files) { const field = file.field; + delete file.field; const fileProp = fileFields[field]; if (!fileProp) { fileFields[field] = file; diff --git a/package-lock.json b/package-lock.json index 85c8624..6f8da81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "formzilla", - "version": "3.4.0", + "version": "3.4.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "formzilla", - "version": "3.4.0", + "version": "3.4.1", "license": "ISC", "dependencies": { "busboy": "^1.6.0" diff --git a/package.json b/package.json index 57bb301..c901a23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "formzilla", - "version": "3.4.0", + "version": "3.4.1", "description": "Fastify plugin for parsing multipart/form data", "main": "index.js", "scripts": {