omermecitoglu / next-openapi-route-handler

a Next.js plugin to generate OpenAPI documentation from route handlers
MIT License
5 stars 1 forks source link

Request for Encoding for FormData #2

Closed gvzq closed 2 days ago

gvzq commented 1 week ago

This is a neat library. I would love to see more extensibility. For example, I would also add the file encodings for multipart/form-data schemas. Is currently a way to accomplish this?

"requestBody": {
          "required": true,
          "content": {
            "multipart/form-data": {
              "schema": {
                "type": "object",
                "properties": {
                  "file": {
                    "type": "string",
                    "format": "binary"
                  }
                },
                "required": [
                  "file"
                ],
                "additionalProperties": false
              }
            }
          }
        },

Ideally, exporting a path that looks like this:

"requestBody": {
  "required": true,
  "content": {
    "multipart/form-data": {
      "schema": {
        "type": "object",
        "properties": {
          "file": {
            "type": "string",
            "format": "binary"
          }
        },
        "required": [
          "file"
        ],
        "additionalProperties": false
      },
      "encoding": {
        "file": {
          "contentType": [
            "application/pdf",
            "image/jpeg",
            "image/png",
            "image/bmp",
            "image/webp",
            "text/plain"
          ]
        }
      }
    }
  }
}
omermecitoglu commented 1 week ago

It supports form-data!

export const { PUT } = createRoute({
  operationId: "uploadFile",
  method: "PUT",
  summary: "Upload a file",
  description: "Uploads a file to the storage service",
  tags: ["Upload"],
  requestBody: z.object({
    file: z.instanceof(File).describe("File object to be uploaded"),
  }),
  hasFormData: true,
  action: async ({ body }) => {
    const file = body.file; // this is a File object
  },
  responses: {
    201: { description: "File uploaded successfully", content: FileDTO },
  },
});