Skip to content

fix: remove all undefined/null values #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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