From 2f18fbf6963d427fccad7a9c976e6648a5dee5f7 Mon Sep 17 00:00:00 2001 From: winie Date: Sun, 10 Jul 2022 01:04:44 +0200 Subject: [PATCH 1/2] refactor: Optimizing access to header values in default request When iterating over header values with array functions, the header values are passed directly to the array function. --- src/request.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/request.ts b/src/request.ts index 898847d0..1000f61e 100644 --- a/src/request.ts +++ b/src/request.ts @@ -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); }); } @@ -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(([header, value]) => value !== null) + .forEach(([header, value]) => xhr.setRequestHeader(header, value)); xhr.send(formData); From 6be7fba917625c749180d7c767e2ae042e65cc52 Mon Sep 17 00:00:00 2001 From: winie Date: Sun, 31 Jul 2022 22:25:07 +0200 Subject: [PATCH 2/2] fix: Removes the unused parameter "header" when filtering headers without values --- src/request.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/request.ts b/src/request.ts index 1000f61e..e80e10cb 100644 --- a/src/request.ts +++ b/src/request.ts @@ -91,7 +91,7 @@ export default function upload(option: UploadRequestOption) { } Object.entries(headers) - .filter(([header, value]) => value !== null) + .filter(([, value]) => value !== null) .forEach(([header, value]) => xhr.setRequestHeader(header, value)); xhr.send(formData);