Skip to content

Commit efe42a5

Browse files
committed
Fixed issue where original params was being modified
1 parent ab90c73 commit efe42a5

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/client.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ class Client {
1010
}
1111

1212
makeGetRequest(endpoint, params = {}) {
13-
const key = params.apiKey || params.accessToken || this.apiKey
13+
const { apiKey, accessToken, ...filteredParams } = params
14+
const key = apiKey || accessToken || this.apiKey
1415
const headers = key ? { Authorization: `Bearer ${key}` } : {}
15-
delete params.apiKey
16-
delete params.accessToken
1716

1817
return new Promise((resolve, reject) => {
19-
this.instance.get(endpoint, { params: params, headers: headers })
18+
this.instance.get(endpoint, { params: filteredParams, headers: headers })
2019
.then(response => resolve(response.data))
2120
.catch(error => {
2221
if (error.response) {
@@ -32,13 +31,12 @@ class Client {
3231
}
3332

3433
makePostRequest(endpoint, data = {}) {
35-
const key = data.apiKey || data.accessToken || this.apiKey
34+
const { apiKey, accessToken, ...filteredData } = data
35+
const key = apiKey || accessToken || this.apiKey
3636
const headers = key ? { Authorization: `Bearer ${key}` } : {}
37-
delete data.apiKey
38-
delete data.accessToken
3937

4038
return new Promise((resolve, reject) => {
41-
this.instance.post(endpoint, data, { headers: headers})
39+
this.instance.post(endpoint, filteredData, { headers: headers})
4240
.then(response => resolve(response.data))
4341
.catch(error => {
4442
reject({

test/authentication.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,21 @@ describe('authentication', () => {
8282
.catch(done)
8383
})
8484

85+
it('should not modify the original params object passed in', done => {
86+
const emailable = require('../lib/emailable')()
87+
const params = { apiKey: apiKey }
88+
89+
Promise.all([
90+
emailable.verify(email, params).then((a,b,c) => {
91+
expect(params.apiKey).to.equal(apiKey)
92+
}),
93+
94+
emailable.account(params).then(() => {
95+
expect(params.apiKey).to.equal(apiKey)
96+
})
97+
])
98+
.then(() => done())
99+
.catch(done)
100+
})
101+
85102
})

0 commit comments

Comments
 (0)