Skip to content

Commit

Permalink
fix: remove all undefined/null values
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
orgads committed Nov 10, 2024
1 parent 9564b3b commit d8afdc8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
6 changes: 4 additions & 2 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
20 changes: 20 additions & 0 deletions src/oauth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), {})
Expand Down

0 comments on commit d8afdc8

Please sign in to comment.