Skip to content

Commit

Permalink
Remove field property from output; update example code
Browse files Browse the repository at this point in the history
  • Loading branch information
FlameWolf committed Nov 13, 2024
1 parent 7fa0656 commit ffdac35
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 35 deletions.
98 changes: 66 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?: <string>, // Only when using DiscStorage
stream?: <Readable> // Only when using StreamStorage
data?: <Buffer> // Only when using BufferStorage
error?: <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?: <string>, // Only when using DiscStorage
stream?: <Readable> // Only when using StreamStorage
data?: <Buffer> // Only when using BufferStorage
error?: <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}`);
}
);
```

Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit ffdac35

Please sign in to comment.