@middy/http-multipart-body-parser
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -167,2 +167,22 @@ const { invoke } = require('../../test-helpers') | ||
}) | ||
test('It should parse an array from a multipart/form-data request', async () => { | ||
const handler = middy((event, context, cb) => { | ||
cb(null, event.body) // propagates the body as a response | ||
}) | ||
handler.use(httpMultipartBodyParser()) | ||
const event = { | ||
headers: { | ||
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryppsQEwf2BVJeCe0M' | ||
}, | ||
body: 'LS0tLS0tV2ViS2l0Rm9ybUJvdW5kYXJ5cHBzUUV3ZjJCVkplQ2UwTQ0KQ29udGVudC1EaXNwb3NpdGlvbjogZm9ybS1kYXRhOyBuYW1lPSJmb29bXSINCg0Kb25lDQotLS0tLS1XZWJLaXRGb3JtQm91bmRhcnlwcHNRRXdmMkJWSmVDZTBNDQpDb250ZW50LURpc3Bvc2l0aW9uOiBmb3JtLWRhdGE7IG5hbWU9ImZvb1tdIg0KDQp0d28NCi0tLS0tLVdlYktpdEZvcm1Cb3VuZGFyeXBwc1FFd2YyQlZKZUNlME0tLQ==', | ||
isBase64Encoded: true | ||
} | ||
const response = await invoke(handler, event) | ||
expect(Object.keys(response)).toContain('foo') | ||
expect(response.foo.length).toEqual(2) | ||
}) | ||
}) |
13
index.js
@@ -58,2 +58,3 @@ const BusBoy = require('busboy') | ||
file.on('end', () => { | ||
attachment.truncated = file.truncated | ||
attachment.content = Buffer.concat(chunks) | ||
@@ -63,3 +64,13 @@ multipartData[fieldname] = attachment | ||
}) | ||
.on('field', (fieldname, value) => { multipartData[fieldname] = value }) | ||
.on('field', (fieldname, value) => { | ||
const matches = fieldname.match(/(.+)\[(.*)]$/) | ||
if (!matches) { | ||
multipartData[fieldname] = value | ||
} else { | ||
if (!multipartData[matches[1]]) { | ||
multipartData[matches[1]] = [] | ||
} | ||
multipartData[matches[1]].push(value) | ||
} | ||
}) | ||
.on('finish', () => resolve(multipartData)) | ||
@@ -66,0 +77,0 @@ .on('error', err => reject(err)) |
{ | ||
"name": "@middy/http-multipart-body-parser", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Http event normalizer middleware for the middy framework", | ||
@@ -51,6 +51,6 @@ "engines": { | ||
"devDependencies": { | ||
"@middy/core": "^1.0.0", | ||
"@middy/core": "^1.1.0", | ||
"es6-promisify": "^6.0.2" | ||
}, | ||
"gitHead": "6d3b35f67dbfda8c8e7ae2885d267687181b2e1d" | ||
"gitHead": "464119c7819be60f5dd85cce7648dd2db6723a55" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17290
240