Created
September 17, 2018 13:44
-
-
Save bill-kidwell/e015e1a8d9e968765a9aee9487fdcfa2 to your computer and use it in GitHub Desktop.
Sample using then-busboy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For each part, create a promise that captures the input | |
// Promises are put in the promises array so that we can wait on them all | |
const onFulfilled = (body) => { | |
let promises = []; | |
let parts = []; | |
let partPromise = null; | |
for (let partName in body) { | |
if (isFile(body[partName])) { | |
partPromise = | |
body[partName].read() | |
.then((buffer) => { | |
parts.push({ | |
name: partName, | |
'content-type': body[partName].mime, | |
raw: buffer, | |
}); | |
}) | |
.catch(err => next(err)); | |
promises.push(partPromise); | |
} else { // !isFile | |
// TODO: Assuming type is JSON until the gateway switches to using files. | |
parts.push({ | |
name: partName, | |
'content-type': 'application/json', | |
raw: Buffer.from(body[partName]), | |
}); | |
} | |
} | |
// once they are all read, we can do processing that requires all content | |
Promise.all(promises) | |
.then(() => { | |
// Validate parts based on mimetype (Validate JSON, XML) | |
}) | |
.then(() => { | |
// Check for required parts... | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment