oaijs / koa-oai-router

Koa Router, based on OpenAPI, Swagger and Json Schema.
105 stars 15 forks source link

How to handle file upload? #49

Closed tafel closed 5 years ago

tafel commented 5 years ago

I'm trying to manage file upload in my application. I successfully implemented your lib for all "standard" routes, but now I would like, for one single route, to add the middleware koa-body to manage file upload.

And I don't find out where to put this middleware? Can you post an example? Thanks.

amazing-gao commented 5 years ago

This is an example for my application.

# upload.yaml

/cdn/upload:
  post:
    summary: Upload file to cdn
    consumes:
      - "multipart/form-data"                   # required
    x-oai-controller:
      - file: system/upload
        handler: parser
      - file: system/upload
        handler: upload
    parameters:
      - name: file
        in: formData                            # required
        type: file                              # required
        description: file
    responses:
      default:
        description: Response model
        schema:
          $ref: "#/definitions/ResponseModel"
// system/upload.js

import busboy from 'koa-busboy';

const parser = busboy();

async function upload(ctx, next) {
  {your code...}

  const files = ctx.request.files;

  {your code...}
}

export {
  parser,
  upload,
};
tafel commented 5 years ago

Thanks. I'm probably missing somethin obvious, but I simply copied the x-oai-controller inside my route and created /ctrl/system/upload.js where /ctrl is the path for koa-oai-router-autoloader

But I have this error message:

router boot error:  Error: args must be string or object, not undefined
    at MiddlewarePlugin.loadHandler (/vagrant/app/node_modules/koa-oai-router-middleware/src/middleware.js:47:13)
    at _.each (/vagrant/app/node_modules/koa-oai-router-middleware/src/middleware.js:30:26)
    at arrayEach (/vagrant/app/node_modules/lodash/lodash.js:516:11)
    at Function.forEach (/vagrant/app/node_modules/lodash/lodash.js:9344:14)
    at MiddlewarePlugin.handler (/vagrant/app/node_modules/koa-oai-router-middleware/src/middleware.js:29:7)
    at MiddlewarePlugin.<anonymous> (/vagrant/app/node_modules/koa-oai-router/lib/plugin.js:151:29)
    at Generator.next (<anonymous>)
    at step (/vagrant/app/node_modules/koa-oai-router/lib/plugin.js:25:191)
    at /vagrant/app/node_modules/koa-oai-router/lib/plugin.js:25:361

I'm using these koa plugins:

"@koa/cors": "^2.2.2",
"koa": "^2.6.1",
"koa-bodyparser": "^4.2.1",
"koa-busboy": "^1.2.0",
"koa-graceful-shutdown": "^1.1.0",
"koa-oai-router": "^2.0.3",
"koa-oai-router-autoloader": "^1.0.2",
"koa-oai-router-middleware": "^1.1.2",
"koa-oai-router-parameters": "^1.1.2",
"koa-pino-logger": "^2.1.3",

Thanks for your help

EDIT

Just found out that I need to put middleware: "/ctrl" in the new Router configuration. But I still have an error: without x-oai-controller, my controller is called (with a simple console.log). But with it, the route returns a 404 and I can't figure out why.

EDIT 2

Ok, I found out that I was totally mixing my paths. Now it is alright and I have my files, thanks a lot!