Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit d2d95bc

Browse files
authored
Improve accessToken logic (#228)
1 parent 5402768 commit d2d95bc

File tree

7 files changed

+24
-33
lines changed

7 files changed

+24
-33
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = {
1111
'no-magic-numbers': 0,
1212
'no-param-reassign': 0,
1313
'fp/no-class': 0,
14-
'fp/no-delete': 0,
1514
'fp/no-let': 0,
1615
'fp/no-loops': 0,
1716
'fp/no-mutating-assign': 0,

package-lock.json

Lines changed: 7 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@
6969
"@netlify/zip-it-and-ship-it": "^1.3.12",
7070
"backoff": "^2.5.0",
7171
"clean-deep": "^3.3.0",
72-
"filter-obj": "^2.0.1",
7372
"flush-write-stream": "^2.0.0",
7473
"folder-walker": "^3.2.0",
7574
"from2-array": "0.0.4",
7675
"hasha": "^5.0.0",
7776
"lodash.camelcase": "^4.3.0",
78-
"lodash.get": "^4.4.2",
79-
"lodash.set": "^4.3.2",
8077
"micro-api-client": "^3.3.0",
8178
"node-fetch": "^2.2.0",
79+
"omit.js": "^2.0.2",
8280
"p-map": "^3.0.0",
8381
"p-wait-for": "^3.1.0",
8482
"parallel-transform": "^1.1.0",

src/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const dfn = require('@netlify/open-api')
2-
const get = require('lodash.get')
3-
const set = require('lodash.set')
42
const pWaitFor = require('p-wait-for')
53

64
const deploy = require('./deploy')
@@ -41,15 +39,24 @@ class NetlifyAPI {
4139
}
4240

4341
get accessToken() {
44-
return (get(this, 'defaultHeaders.Authorization') || '').replace('Bearer ', '') || null
42+
const {
43+
defaultHeaders: { Authorization },
44+
} = this
45+
if (typeof Authorization !== 'string' || !Authorization.startsWith('Bearer ')) {
46+
return null
47+
}
48+
49+
return Authorization.replace('Bearer ', '')
4550
}
4651

4752
set accessToken(token) {
48-
if (token) {
49-
set(this, 'defaultHeaders.Authorization', `Bearer ${token}`)
50-
} else {
53+
if (!token) {
54+
// eslint-disable-next-line fp/no-delete
5155
delete this.defaultHeaders.Authorization
56+
return
5257
}
58+
59+
this.defaultHeaders.Authorization = `Bearer ${token}`
5360
}
5461

5562
get basePath() {

src/methods/response.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
2+
const omit = require('omit.js').default
23

34
// Read and parse the HTTP response
45
const parseResponse = async function (response) {
@@ -38,8 +39,7 @@ const parseJsonResponse = function (response, textResponse, responseType) {
3839
}
3940

4041
const getFetchError = function (error, url, opts) {
41-
const data = { ...opts }
42-
delete data.Authorization
42+
const data = omit(opts, ['Authorization'])
4343
Object.assign(error, { name: 'FetchError', url, data })
4444
return error
4545
}

src/operations.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { paths } = require('@netlify/open-api')
2-
3-
const { omit } = require('./utils/omit')
2+
const omit = require('omit.js').default
43

54
// Retrieve all OpenAPI operations
65
const getOperations = function () {

src/utils/omit.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)