Skip to content

Commit

Permalink
Merge 6be7fba into 0d0ca2f
Browse files Browse the repository at this point in the history
  • Loading branch information
winie authored Jul 31, 2022
2 parents 0d0ca2f + 6be7fba commit 89dd2b3
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,18 @@ export default function upload(option: UploadRequestOption) {
const formData = new FormData();

if (option.data) {
Object.keys(option.data).forEach(key => {
const value = option.data[key];
Object.entries(option.data).forEach(([header, value]) => {
// support key-value array data
if (Array.isArray(value)) {
value.forEach(item => {
// { list: [ 11, 22 ] }
// formData.append('list[]', 11);
formData.append(`${key}[]`, item);
formData.append(`${header}[]`, item);
});
return;
}

formData.append(key, value as string | Blob);
formData.append(header, value as string | Blob);
});
}

Expand Down Expand Up @@ -91,11 +90,9 @@ export default function upload(option: UploadRequestOption) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
}

Object.keys(headers).forEach(h => {
if (headers[h] !== null) {
xhr.setRequestHeader(h, headers[h]);
}
});
Object.entries(headers)
.filter(([, value]) => value !== null)
.forEach(([header, value]) => xhr.setRequestHeader(header, value));

xhr.send(formData);

Expand Down

0 comments on commit 89dd2b3

Please sign in to comment.