Skip to content

Commit

Permalink
Feat: add async-busboy for multipart/formdata enctype
Browse files Browse the repository at this point in the history
  • Loading branch information
darkturtle committed Dec 29, 2016
1 parent daec4cd commit c0124ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

const debug = require('debug')('method-override')
const methods = require('methods')
const asyncBusboy = require('async-busboy')

const ALLOWED_METHODS = 'POST'
const HTTP_METHOD_OVERRIDE_HEADER = "X-HTTP-Method-Override"

let asyncBusboyBody = null

/**
* Method Override:
*
Expand Down Expand Up @@ -57,7 +60,7 @@ function methodOverride(getter, options) {
? ALLOWED_METHODS.split(' ')
: options.methods

return (ctx, next) => {
return async (ctx, next) => {
const req = ctx.request
let method
let val
Expand All @@ -69,7 +72,10 @@ function methodOverride(getter, options) {
return next()
}

val = get(req, ctx.response)
asyncBusboyBody = null
ctx.req.getAsyncBusboyBody = async () => (asyncBusboyBody || await asyncBusboy(ctx.req))

val = await get(req, ctx.response, ctx.req)
method = Array.isArray(val) ? val[0] : val

// replace
Expand Down Expand Up @@ -102,8 +108,15 @@ function createGetter(str) {
function createQueryOrBodyGetter(key) {
return queryOrBodyGetter

function queryOrBodyGetter(req) {
return req.query[key] || (req.body && req.body[key])
async function queryOrBodyGetter(req, ...args) {
const method = req.query[key] || (req.body && req.body[key])
if (method) {
return method
} else {
const koaReq = args[1]
asyncBusboyBody = await asyncBusboy(koaReq)
return asyncBusboyBody.fields[key]
}
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"middleware"
],
"dependencies": {
"async-busboy": "^0.3.3",
"debug": "*",
"methods": "*"
},
Expand Down

0 comments on commit c0124ca

Please sign in to comment.