Skip to content

Commit

Permalink
fix: allow for synchronous building of file requests (#211)
Browse files Browse the repository at this point in the history
This issue was caused by an oversight in adding an `await` statement within a `forEach`
loop, which will result in asynchronous operations happening without the synchronous
control you'd expect from async/await.

This solves the issue by simply converting the loops to "for of" loops.

Note that the AirBnb style guide we follow prohibits the use of "for of" loops and that
ESLint discourages "await" statements in any kind of loop. However, to adherence to the
rules would require a significant refactor that is not current worth the effort. So for
now, we are ignoring the rules.

Signed-off-by: Dustin Popp <dpopp07@gmail.com>
  • Loading branch information
dpopp07 committed Oct 21, 2022
1 parent e8c5edf commit dcce4ea
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/request-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,29 @@ export class RequestWrapper {

// Form params
if (formData) {
Object.keys(formData).forEach((key) => {
const values = Array.isArray(formData[key]) ? formData[key] : [formData[key]];
for (const key of Object.keys(formData)) { // eslint-disable-line
let values = Array.isArray(formData[key]) ? formData[key] : [formData[key]];
// Skip keys with undefined/null values or empty object value
values
.filter((v) => v != null && !isEmptyObject(v))
.forEach(async (value) => {
// Special case of empty file object
if (
Object.prototype.hasOwnProperty.call(value, 'contentType') &&
!Object.prototype.hasOwnProperty.call(value, 'data')
) {
return;
}

values = values.filter((v) => v != null && !isEmptyObject(v));

for (let value of values) { // eslint-disable-line
// Ignore special case of empty file object
if (
!Object.prototype.hasOwnProperty.call(value, 'contentType') ||
Object.prototype.hasOwnProperty.call(value, 'data')
) {
if (isFileWithMetadata(value)) {
const fileObj = await buildRequestFileObject(value);
const fileObj = await buildRequestFileObject(value); // eslint-disable-line
multipartForm.append(key, fileObj.value, fileObj.options);
} else {
if (typeof value === 'object' && !isFileData(value)) {
value = JSON.stringify(value);
}
multipartForm.append(key, value);
}
});
});
}
}
}
}

// Path params
Expand Down

0 comments on commit dcce4ea

Please sign in to comment.