Skip to content

Instantly share code, notes, and snippets.

@bill-kidwell
Created September 17, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-kidwell/e015e1a8d9e968765a9aee9487fdcfa2 to your computer and use it in GitHub Desktop.
Save bill-kidwell/e015e1a8d9e968765a9aee9487fdcfa2 to your computer and use it in GitHub Desktop.
Sample using then-busboy
// 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