Skip to content

Commit d8afdc8

Browse files
committed
fix: remove all undefined/null values
Fixes #40
1 parent 9564b3b commit d8afdc8

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

dist/index.cjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ function oauth(axios, { url, ...credentials }) {
66
...credentials,
77
...moreCredentials
88
};
9-
if ("scope" in body && !body.scope) {
10-
delete body.scope;
9+
for (const key of Object.keys(body)) {
10+
if (!body[key]) {
11+
delete body[key];
12+
}
1113
}
1214
return axios({
1315
url,

dist/index.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ function oauth(axios, { url, ...credentials }) {
44
...credentials,
55
...moreCredentials
66
};
7-
if ("scope" in body && !body.scope) {
8-
delete body.scope;
7+
for (const key of Object.keys(body)) {
8+
if (!body[key]) {
9+
delete body[key];
10+
}
911
}
1012
return axios({
1113
url,

src/oauth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ export function oauth (axios, { url, ...credentials }) {
55
...moreCredentials
66
}
77

8-
// remove blank scope
9-
if ('scope' in body && !body.scope) {
10-
delete body.scope
8+
// remove all blank values
9+
for (const key of Object.keys(body)) {
10+
if (!body[key]) { delete body[key] }
1111
}
1212

1313
return axios({

src/oauth.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ describe('oauth()', function () {
7070
})
7171
})
7272

73+
test('should omit any null or undefined value', async function () {
74+
const params = {
75+
url: 'https://oauth.com/2.0/token',
76+
grant_type: 'client_credentials',
77+
client_id: undefined,
78+
client_secret: undefined,
79+
scope: null
80+
}
81+
82+
const actualConfig = {}
83+
const auth = oauth(fakeAxios(actualConfig), params)
84+
await auth()
85+
86+
assert.deepStrictEqual(actualConfig, {
87+
url: 'https://oauth.com/2.0/token',
88+
method: 'post',
89+
data: 'grant_type=client_credentials'
90+
})
91+
})
92+
7393
test('should resolve to the OAuth token response', async function () {
7494
const expectedData = { access_token: 'FAKE_TOKEN', expires_in: 5 }
7595
const auth = oauth(fakeAxios({}, expectedData), {})

0 commit comments

Comments
 (0)