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

Commit 2edd766

Browse files
authored
Merge pull request #242 from netlify/lint/no-mutating-assign
Add `fp/no-mutating-assigns` ESLint rule
2 parents fb946b9 + dd52bee commit 2edd766

File tree

5 files changed

+29
-31
lines changed

5 files changed

+29
-31
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module.exports = {
1212
'fp/no-class': 0,
1313
'fp/no-let': 0,
1414
'fp/no-loops': 0,
15-
'fp/no-mutating-assign': 0,
1615
'fp/no-mutating-methods': 0,
1716
'fp/no-mutation': 0,
1817
'fp/no-this': 0,

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ const dfn = require('@netlify/open-api')
22
const pWaitFor = require('p-wait-for')
33

44
const deploy = require('./deploy')
5-
const { addMethods } = require('./methods')
5+
const { getMethods } = require('./methods')
66
const { getOperations } = require('./operations')
77

88
class NetlifyAPI {
99
constructor(accessToken, opts) {
10-
addMethods(this)
11-
1210
// variadic arguments
1311
if (typeof accessToken === 'object') {
1412
opts = accessToken
@@ -36,6 +34,10 @@ class NetlifyAPI {
3634
this.globalParams = opts.globalParams
3735
this.accessToken = opts.accessToken
3836
this.agent = opts.agent
37+
38+
const methods = getMethods(this)
39+
// eslint-disable-next-line fp/no-mutating-assign
40+
Object.assign(this, methods)
3941
}
4042

4143
get accessToken() {

src/methods/index.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,34 @@ const { getUrl } = require('./url.js')
1111

1212
// For each OpenAPI operation, add a corresponding method.
1313
// The `operationId` is the method name.
14-
const addMethods = function (NetlifyApi) {
15-
const methods = getMethods(NetlifyApi)
16-
Object.assign(NetlifyApi, methods)
17-
}
18-
19-
const getMethods = function (NetlifyApi) {
14+
const getMethods = function ({ basePath, defaultHeaders, agent, globalParams }) {
2015
const operations = getOperations()
21-
const methods = operations.map((method) => getMethod(method, NetlifyApi))
16+
const methods = operations.map((method) => getMethod({ method, basePath, defaultHeaders, agent, globalParams }))
2217
return Object.assign({}, ...methods)
2318
}
2419

25-
const getMethod = function (method, NetlifyApi) {
20+
const getMethod = function ({ method, basePath, defaultHeaders, agent, globalParams }) {
2621
return {
2722
[method.operationId](params, opts) {
28-
return callMethod(method, NetlifyApi, params, opts)
23+
return callMethod({ method, basePath, defaultHeaders, agent, globalParams, params, opts })
2924
},
3025
}
3126
}
3227

33-
const callMethod = async function (method, NetlifyApi, params, opts) {
34-
const requestParams = { ...NetlifyApi.globalParams, ...params }
35-
const url = getUrl(method, NetlifyApi, requestParams)
36-
const response = await makeRequestOrRetry({ url, method, NetlifyApi, requestParams, opts })
28+
const callMethod = async function ({ method, basePath, defaultHeaders, agent, globalParams, params, opts }) {
29+
const requestParams = { ...globalParams, ...params }
30+
const url = getUrl(method, basePath, requestParams)
31+
const response = await makeRequestOrRetry({ url, method, defaultHeaders, agent, requestParams, opts })
3732

3833
const parsedResponse = await parseResponse(response)
3934
return parsedResponse
4035
}
4136

42-
const getOpts = function ({ verb, parameters }, NetlifyApi, { body }, opts) {
37+
const getOpts = function ({ method: { verb, parameters }, defaultHeaders, agent, requestParams: { body }, opts }) {
4338
const optsA = addHttpMethod(verb, opts)
44-
const optsB = addDefaultHeaders(NetlifyApi, optsA)
39+
const optsB = addDefaultHeaders(defaultHeaders, optsA)
4540
const optsC = addBody(body, parameters, optsB)
46-
const optsD = addAgent(NetlifyApi, optsC)
41+
const optsD = addAgent(agent, optsC)
4742
return optsD
4843
}
4944

@@ -53,21 +48,21 @@ const addHttpMethod = function (verb, opts) {
5348
}
5449

5550
// Assign default HTTP headers
56-
const addDefaultHeaders = function (NetlifyApi, opts) {
57-
return { ...opts, headers: { ...NetlifyApi.defaultHeaders, ...opts.headers } }
51+
const addDefaultHeaders = function (defaultHeaders, opts) {
52+
return { ...opts, headers: { ...defaultHeaders, ...opts.headers } }
5853
}
5954

6055
// Assign fetch agent (like for example HttpsProxyAgent) if there is one
61-
const addAgent = function (NetlifyApi, opts) {
62-
if (NetlifyApi.agent) {
63-
return { ...opts, agent: NetlifyApi.agent }
56+
const addAgent = function (agent, opts) {
57+
if (agent) {
58+
return { ...opts, agent }
6459
}
6560
return opts
6661
}
6762

68-
const makeRequestOrRetry = async function ({ url, method, NetlifyApi, requestParams, opts }) {
63+
const makeRequestOrRetry = async function ({ url, method, defaultHeaders, agent, requestParams, opts }) {
6964
for (let index = 0; index <= MAX_RETRY; index++) {
70-
const optsA = getOpts(method, NetlifyApi, requestParams, opts)
65+
const optsA = getOpts({ method, defaultHeaders, agent, requestParams, opts })
7166
const { response, error } = await makeRequest(url, optsA)
7267

7368
if (shouldRetry({ response, error }) && index !== MAX_RETRY) {
@@ -93,4 +88,4 @@ const makeRequest = async function (url, opts) {
9388
}
9489
}
9590

96-
module.exports = { addMethods }
91+
module.exports = { getMethods }

src/methods/response.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ const parseJsonResponse = function (response, textResponse, responseType) {
4040

4141
const getFetchError = function (error, url, opts) {
4242
const data = omit(opts, ['Authorization'])
43-
Object.assign(error, { name: 'FetchError', url, data })
43+
error.name = 'FetchError'
44+
error.url = url
45+
error.data = data
4446
return error
4547
}
4648

src/methods/url.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const queryString = require('qs')
33

44
// Replace path parameters and query parameters in the URI, using the OpenAPI
55
// definition
6-
const getUrl = function ({ path, parameters }, NetlifyApi, requestParams) {
7-
const url = `${NetlifyApi.basePath}${path}`
6+
const getUrl = function ({ path, parameters }, basePath, requestParams) {
7+
const url = `${basePath}${path}`
88
const urlA = addPathParams(url, parameters, requestParams)
99
const urlB = addQueryParams(urlA, parameters, requestParams)
1010
return urlB

0 commit comments

Comments
 (0)