From d8afdc8be0f60aab47fb1d3d2cf7f4bedbda2549 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Sun, 10 Nov 2024 12:20:57 +0200 Subject: [PATCH] fix: remove all undefined/null values Fixes #40 --- dist/index.cjs | 6 ++++-- dist/index.mjs | 6 ++++-- src/oauth.js | 6 +++--- src/oauth.test.js | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/dist/index.cjs b/dist/index.cjs index 88653e0..307f5f7 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -6,8 +6,10 @@ function oauth(axios, { url, ...credentials }) { ...credentials, ...moreCredentials }; - if ("scope" in body && !body.scope) { - delete body.scope; + for (const key of Object.keys(body)) { + if (!body[key]) { + delete body[key]; + } } return axios({ url, diff --git a/dist/index.mjs b/dist/index.mjs index 151604a..9768790 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -4,8 +4,10 @@ function oauth(axios, { url, ...credentials }) { ...credentials, ...moreCredentials }; - if ("scope" in body && !body.scope) { - delete body.scope; + for (const key of Object.keys(body)) { + if (!body[key]) { + delete body[key]; + } } return axios({ url, diff --git a/src/oauth.js b/src/oauth.js index 2279c08..12ed588 100644 --- a/src/oauth.js +++ b/src/oauth.js @@ -5,9 +5,9 @@ export function oauth (axios, { url, ...credentials }) { ...moreCredentials } - // remove blank scope - if ('scope' in body && !body.scope) { - delete body.scope + // remove all blank values + for (const key of Object.keys(body)) { + if (!body[key]) { delete body[key] } } return axios({ diff --git a/src/oauth.test.js b/src/oauth.test.js index b76e4ca..ddbcb26 100644 --- a/src/oauth.test.js +++ b/src/oauth.test.js @@ -70,6 +70,26 @@ describe('oauth()', function () { }) }) + test('should omit any null or undefined value', async function () { + const params = { + url: 'https://oauth.com/2.0/token', + grant_type: 'client_credentials', + client_id: undefined, + client_secret: undefined, + scope: null + } + + const actualConfig = {} + const auth = oauth(fakeAxios(actualConfig), params) + await auth() + + assert.deepStrictEqual(actualConfig, { + url: 'https://oauth.com/2.0/token', + method: 'post', + data: 'grant_type=client_credentials' + }) + }) + test('should resolve to the OAuth token response', async function () { const expectedData = { access_token: 'FAKE_TOKEN', expires_in: 5 } const auth = oauth(fakeAxios({}, expectedData), {})