netlify 8.0.2 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -49
- package/package.json +14 -26
- package/src/index.js +9 -11
- package/src/methods/body.js +1 -3
- package/src/methods/index.js +7 -11
- package/src/methods/response.js +5 -7
- package/src/methods/retry.js +3 -5
- package/src/methods/url.js +3 -5
- package/src/open_api.js +7 -0
- package/src/operations.js +11 -16
- package/dist/main.js +0 -2
- package/dist/main.js.map +0 -1
package/README.md
CHANGED
|
@@ -8,39 +8,32 @@ A Netlify [OpenAPI](https://github.com/netlify/open-api) client that works in th
|
|
|
8
8
|
## Usage
|
|
9
9
|
|
|
10
10
|
```js
|
|
11
|
-
|
|
11
|
+
import { NetlifyAPI } from 'netlify'
|
|
12
12
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
const sites = await client.listSites()
|
|
16
|
-
return sites
|
|
17
|
-
}
|
|
13
|
+
const client = new NetlifyAPI('1234myAccessToken')
|
|
14
|
+
const sites = await client.listSites()
|
|
18
15
|
```
|
|
19
16
|
|
|
20
17
|
## Using OpenAPI operations
|
|
21
18
|
|
|
22
19
|
```js
|
|
23
|
-
|
|
20
|
+
import { NetlifyAPI } from 'netlify'
|
|
24
21
|
|
|
25
22
|
const client = new NetlifyAPI('1234myAccessToken')
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const sites = await client.listSites()
|
|
24
|
+
// Fetch sites
|
|
25
|
+
const sites = await client.listSites()
|
|
30
26
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
// Create a site. Notice `body` here for sending OpenAPI body
|
|
28
|
+
const site = await client.createSite({
|
|
29
|
+
body: {
|
|
30
|
+
name: `my-awesome-site`,
|
|
31
|
+
// ... https://open-api.netlify.com/#/default/createSite
|
|
32
|
+
},
|
|
33
|
+
})
|
|
38
34
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
site_id: siteId,
|
|
42
|
-
})
|
|
43
|
-
}
|
|
35
|
+
// Delete site. Notice `site_id` is a path parameter https://open-api.netlify.com/#/default/deleteSite
|
|
36
|
+
await client.deleteSite({ site_id: siteId })
|
|
44
37
|
```
|
|
45
38
|
|
|
46
39
|
## API
|
|
@@ -124,16 +117,14 @@ const opts = {
|
|
|
124
117
|
All operations are conveniently consumed with async/await:
|
|
125
118
|
|
|
126
119
|
```js
|
|
127
|
-
|
|
120
|
+
try {
|
|
121
|
+
const siteDeploy = await client.getSiteDeploy({
|
|
122
|
+
siteId: '1234abcd',
|
|
123
|
+
deploy_id: '4567',
|
|
124
|
+
})
|
|
128
125
|
// Calls may fail!
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
siteId: '1234abcd',
|
|
132
|
-
deploy_id: '4567',
|
|
133
|
-
})
|
|
134
|
-
} catch (error) {
|
|
135
|
-
// handle error
|
|
136
|
-
}
|
|
126
|
+
} catch (error) {
|
|
127
|
+
// handle error
|
|
137
128
|
}
|
|
138
129
|
```
|
|
139
130
|
|
|
@@ -162,19 +153,15 @@ const opts = {
|
|
|
162
153
|
See the [authenticating](https://www.netlify.com/docs/api/#authenticating) docs for more context.
|
|
163
154
|
|
|
164
155
|
```js
|
|
165
|
-
|
|
166
|
-
const open = require('open') // installed with 'npm i open'
|
|
156
|
+
import open from 'open'
|
|
167
157
|
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// API is also set up to use the returned access token as a side effect
|
|
176
|
-
return accessToken // Save this for later so you can quickly set up an authenticated client
|
|
177
|
-
}
|
|
158
|
+
const ticket = await client.createTicket({ clientId: CLIENT_ID })
|
|
159
|
+
// Open browser for authentication
|
|
160
|
+
await open(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
|
|
161
|
+
|
|
162
|
+
// API is also set up to use the returned access token as a side effect
|
|
163
|
+
// Save this for later so you can quickly set up an authenticated client
|
|
164
|
+
const accessToken = await client.getAccessToken(ticket)
|
|
178
165
|
```
|
|
179
166
|
|
|
180
167
|
## Proxy support
|
|
@@ -183,7 +170,7 @@ const login = async () => {
|
|
|
183
170
|
`http.Agent` that can handle your situation as `agent` option:
|
|
184
171
|
|
|
185
172
|
```js
|
|
186
|
-
|
|
173
|
+
import HttpsProxyAgent from 'https-proxy-agent'
|
|
187
174
|
|
|
188
175
|
const proxyUri = 'http(s)://[user:password@]proxyhost:port'
|
|
189
176
|
const agent = new HttpsProxyAgent(proxyUri)
|
|
@@ -195,11 +182,6 @@ const client = new NetlifyAPI('1234myAccessToken', { agent })
|
|
|
195
182
|
Support for site deployment has been removed from this package in version 7.0.0. You should consider using the
|
|
196
183
|
[`deploy` command](https://cli.netlify.com/commands/deploy/) of Netlify CLI.
|
|
197
184
|
|
|
198
|
-
## UMD Builds
|
|
199
|
-
|
|
200
|
-
A UMD build is provided for your convenience, however browser support is still experimental. Contributions to improve
|
|
201
|
-
browser support are welcome.
|
|
202
|
-
|
|
203
185
|
## Contributing
|
|
204
186
|
|
|
205
187
|
See [CONTRIBUTING.md](CONTRIBUTING.md) for more info on how to make contributions to this project.
|
package/package.json
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "netlify",
|
|
3
3
|
"description": "Netlify Node.js API client",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "10.0.0",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"files": [
|
|
6
7
|
"src/**/*.js",
|
|
7
|
-
"!src/**/*.test.js"
|
|
8
|
-
"dist/main.js",
|
|
9
|
-
"dist/main.js.map"
|
|
8
|
+
"!src/**/*.test.js"
|
|
10
9
|
],
|
|
10
|
+
"exports": "./src/index.js",
|
|
11
11
|
"main": "./src/index.js",
|
|
12
|
-
"unpkg": "./dist/main.js",
|
|
13
|
-
"umd:main": "./dist/main.js",
|
|
14
12
|
"scripts": {
|
|
15
|
-
"prepublishOnly": "npm ci &&
|
|
16
|
-
"test": "
|
|
13
|
+
"prepublishOnly": "npm ci && npm test",
|
|
14
|
+
"test": "run-s format test:dev",
|
|
17
15
|
"format": "run-s format:check-fix:*",
|
|
18
16
|
"format:lint": "eslint --ignore-path .gitignore --fix --cache --format=codeframe --max-warnings=0 \"src/**/*.js\"",
|
|
19
17
|
"format:prettier": "prettier --ignore-path .gitignore --write --loglevel warn \"src/**/*.js\" \"*.{js,md,yml,json}\" \"!package-lock.json\"",
|
|
@@ -26,12 +24,11 @@
|
|
|
26
24
|
"format:fix:prettier": "cross-env-shell prettier --write $npm_package_config_prettier",
|
|
27
25
|
"test:dev": "ava",
|
|
28
26
|
"test:ci": "nyc -r lcovonly -r text -r json ava",
|
|
29
|
-
"update-snapshots": "ava -u"
|
|
30
|
-
"build": "webpack"
|
|
27
|
+
"update-snapshots": "ava -u"
|
|
31
28
|
},
|
|
32
29
|
"config": {
|
|
33
|
-
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,tests,.github}/**/*.{js,md,html}\" \"*.{js,md,html}\" \".*.{js,md,html}\"",
|
|
34
|
-
"prettier": "--ignore-path .gitignore --loglevel=warn \"{src,tests,.github}/**/*.{js,md,yml,json,html}\" \"*.{js,yml,json,html}\" \".*.{js,yml,json,html}\" \"!package-lock.json\" \"!CHANGELOG.md\""
|
|
30
|
+
"eslint": "--ignore-path .gitignore --cache --format=codeframe --max-warnings=0 \"{src,tests,.github}/**/*.{cjs,mjs,js,md,html}\" \"*.{cjs,mjs,js,md,html}\" \".*.{cjs,mjs,js,md,html}\"",
|
|
31
|
+
"prettier": "--ignore-path .gitignore --loglevel=warn \"{src,tests,.github}/**/*.{cjs,mjs,js,md,yml,json,html}\" \"*.{cjs,mjs,js,yml,json,html}\" \".*.{cjs,mjs,js,yml,json,html}\" \"!package-lock.json\" \"!CHANGELOG.md\""
|
|
35
32
|
},
|
|
36
33
|
"husky": {
|
|
37
34
|
"hooks": {
|
|
@@ -62,7 +59,7 @@
|
|
|
62
59
|
"node client"
|
|
63
60
|
],
|
|
64
61
|
"dependencies": {
|
|
65
|
-
"@netlify/open-api": "^2.
|
|
62
|
+
"@netlify/open-api": "^2.6.0",
|
|
66
63
|
"lodash.camelcase": "^4.3.0",
|
|
67
64
|
"micro-api-client": "^3.3.0",
|
|
68
65
|
"node-fetch": "^2.6.1",
|
|
@@ -71,31 +68,22 @@
|
|
|
71
68
|
"qs": "^6.9.6"
|
|
72
69
|
},
|
|
73
70
|
"devDependencies": {
|
|
74
|
-
"@
|
|
75
|
-
"
|
|
76
|
-
"@babel/preset-env": "^7.12.17",
|
|
77
|
-
"@babel/runtime": "^7.12.18",
|
|
78
|
-
"@netlify/eslint-config-node": "^3.3.3",
|
|
79
|
-
"ava": "^2.4.0",
|
|
80
|
-
"babel-loader": "^8.2.2",
|
|
71
|
+
"@netlify/eslint-config-node": "^3.3.10",
|
|
72
|
+
"ava": "^3.0.0",
|
|
81
73
|
"from2-string": "^1.1.0",
|
|
82
74
|
"husky": "^4.3.8",
|
|
83
75
|
"nock": "^13.0.0",
|
|
84
76
|
"npm-run-all": "^4.1.5",
|
|
85
77
|
"nyc": "^15.1.0",
|
|
86
|
-
"uuid": "^8.3.2"
|
|
87
|
-
"webpack": "^4.46.0",
|
|
88
|
-
"webpack-cli": "^4.5.0"
|
|
78
|
+
"uuid": "^8.3.2"
|
|
89
79
|
},
|
|
90
80
|
"engines": {
|
|
91
|
-
"node": ">=
|
|
81
|
+
"node": "^12.20.0 || ^14.14.0 || >=16.0.0"
|
|
92
82
|
},
|
|
93
83
|
"ava": {
|
|
94
84
|
"files": [
|
|
95
85
|
"src/**/*.test.js"
|
|
96
86
|
],
|
|
97
|
-
"compileEnhancements": false,
|
|
98
|
-
"babel": false,
|
|
99
87
|
"verbose": true
|
|
100
88
|
}
|
|
101
89
|
}
|
package/src/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
const pWaitFor = require('p-wait-for')
|
|
1
|
+
import pWaitFor from 'p-wait-for'
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { getMethods } from './methods/index.js'
|
|
4
|
+
import { openApiSpec } from './open_api.js'
|
|
5
|
+
import { getOperations } from './operations.js'
|
|
6
6
|
|
|
7
|
-
class NetlifyAPI {
|
|
7
|
+
export class NetlifyAPI {
|
|
8
8
|
constructor(firstArg, secondArg) {
|
|
9
9
|
// variadic arguments
|
|
10
10
|
const [accessTokenInput, opts = {}] = typeof firstArg === 'object' ? [null, firstArg] : [firstArg, secondArg]
|
|
@@ -12,9 +12,9 @@ class NetlifyAPI {
|
|
|
12
12
|
// default opts
|
|
13
13
|
const {
|
|
14
14
|
userAgent = 'netlify/js-client',
|
|
15
|
-
scheme =
|
|
16
|
-
host =
|
|
17
|
-
pathPrefix =
|
|
15
|
+
scheme = openApiSpec.schemes[0],
|
|
16
|
+
host = openApiSpec.host,
|
|
17
|
+
pathPrefix = openApiSpec.basePath,
|
|
18
18
|
accessToken = accessTokenInput,
|
|
19
19
|
globalParams = {},
|
|
20
20
|
agent,
|
|
@@ -89,6 +89,4 @@ const DEFAULT_TICKET_POLL = 1e3
|
|
|
89
89
|
// 1 hour
|
|
90
90
|
const DEFAULT_TICKET_TIMEOUT = 3.6e6
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
module.exports.methods = getOperations()
|
|
92
|
+
export const methods = getOperations()
|
package/src/methods/body.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Handle request body
|
|
2
|
-
const addBody = function (body, parameters, opts) {
|
|
2
|
+
export const addBody = function (body, parameters, opts) {
|
|
3
3
|
if (!body) {
|
|
4
4
|
return opts
|
|
5
5
|
}
|
|
@@ -28,5 +28,3 @@ const isBinaryBody = function (parameters) {
|
|
|
28
28
|
const isBodyParam = function ({ schema }) {
|
|
29
29
|
return schema && schema.format === 'binary'
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
module.exports = { addBody }
|
package/src/methods/index.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
// Webpack will sometimes export default exports in different places
|
|
3
|
-
const fetch = nodeFetch.default || nodeFetch
|
|
1
|
+
import fetch from 'node-fetch'
|
|
4
2
|
|
|
5
|
-
|
|
3
|
+
import { getOperations } from '../operations.js'
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
import { addBody } from './body.js'
|
|
6
|
+
import { parseResponse, getFetchError } from './response.js'
|
|
7
|
+
import { shouldRetry, waitForRetry, MAX_RETRY } from './retry.js'
|
|
8
|
+
import { getUrl } from './url.js'
|
|
11
9
|
|
|
12
10
|
// For each OpenAPI operation, add a corresponding method.
|
|
13
11
|
// The `operationId` is the method name.
|
|
14
|
-
const getMethods = function ({ basePath, defaultHeaders, agent, globalParams }) {
|
|
12
|
+
export const getMethods = function ({ basePath, defaultHeaders, agent, globalParams }) {
|
|
15
13
|
const operations = getOperations()
|
|
16
14
|
const methods = operations.map((method) => getMethod({ method, basePath, defaultHeaders, agent, globalParams }))
|
|
17
15
|
return Object.assign({}, ...methods)
|
|
@@ -90,5 +88,3 @@ const makeRequest = async function (url, opts) {
|
|
|
90
88
|
return { error: errorA }
|
|
91
89
|
}
|
|
92
90
|
}
|
|
93
|
-
|
|
94
|
-
module.exports = { getMethods }
|
package/src/methods/response.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { JSONHTTPError, TextHTTPError } from 'micro-api-client'
|
|
2
|
+
import omit from 'omit.js'
|
|
3
3
|
|
|
4
4
|
// Read and parse the HTTP response
|
|
5
|
-
const parseResponse = async function (response) {
|
|
5
|
+
export const parseResponse = async function (response) {
|
|
6
6
|
const responseType = getResponseType(response)
|
|
7
7
|
const textResponse = await response.text()
|
|
8
8
|
|
|
@@ -38,12 +38,10 @@ const parseJsonResponse = function (response, textResponse, responseType) {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const getFetchError = function (error, url, opts) {
|
|
42
|
-
const data = omit(opts, ['Authorization'])
|
|
41
|
+
export const getFetchError = function (error, url, opts) {
|
|
42
|
+
const data = omit.default(opts, ['Authorization'])
|
|
43
43
|
error.name = 'FetchError'
|
|
44
44
|
error.url = url
|
|
45
45
|
error.data = data
|
|
46
46
|
return error
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
module.exports = { parseResponse, getFetchError }
|
package/src/methods/retry.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// We retry:
|
|
2
2
|
// - when receiving a rate limiting response
|
|
3
3
|
// - on network failures due to timeouts
|
|
4
|
-
const shouldRetry = function ({ response = {}, error = {} }) {
|
|
4
|
+
export const shouldRetry = function ({ response = {}, error = {} }) {
|
|
5
5
|
return isRetryStatus(response) || RETRY_ERROR_CODES.has(error.code)
|
|
6
6
|
}
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@ const isRetryStatus = function ({ status }) {
|
|
|
9
9
|
return typeof status === 'number' && (status === RATE_LIMIT_STATUS || String(status).startsWith('5'))
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
const waitForRetry = async function (response) {
|
|
12
|
+
export const waitForRetry = async function (response) {
|
|
13
13
|
const delay = getDelay(response)
|
|
14
14
|
await sleep(delay)
|
|
15
15
|
}
|
|
@@ -37,9 +37,7 @@ const sleep = function (ms) {
|
|
|
37
37
|
const DEFAULT_RETRY_DELAY = 5e3
|
|
38
38
|
const MIN_RETRY_DELAY = 1e3
|
|
39
39
|
const SECS_TO_MSECS = 1e3
|
|
40
|
-
const MAX_RETRY = 5
|
|
40
|
+
export const MAX_RETRY = 5
|
|
41
41
|
const RATE_LIMIT_STATUS = 429
|
|
42
42
|
const RATE_LIMIT_HEADER = 'X-RateLimit-Reset'
|
|
43
43
|
const RETRY_ERROR_CODES = new Set(['ETIMEDOUT', 'ECONNRESET'])
|
|
44
|
-
|
|
45
|
-
module.exports = { shouldRetry, waitForRetry, MAX_RETRY }
|
package/src/methods/url.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import camelCase from 'lodash.camelcase'
|
|
2
|
+
import queryString from 'qs'
|
|
3
3
|
|
|
4
4
|
// Replace path parameters and query parameters in the URI, using the OpenAPI
|
|
5
5
|
// definition
|
|
6
|
-
const getUrl = function ({ path, parameters }, basePath, requestParams) {
|
|
6
|
+
export const getUrl = function ({ path, parameters }, basePath, requestParams) {
|
|
7
7
|
const url = `${basePath}${path}`
|
|
8
8
|
const urlA = addPathParams(url, parameters, requestParams)
|
|
9
9
|
const urlB = addQueryParams(urlA, parameters, requestParams)
|
|
@@ -45,5 +45,3 @@ const getRequestParam = function (param, requestParams, name) {
|
|
|
45
45
|
throw new Error(`Missing required ${name} '${param.name}'`)
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
|
|
49
|
-
module.exports = { getUrl }
|
package/src/open_api.js
ADDED
package/src/operations.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import omit from 'omit.js'
|
|
2
|
+
|
|
3
|
+
import { openApiSpec } from './open_api.js'
|
|
3
4
|
|
|
4
5
|
// Retrieve all OpenAPI operations
|
|
5
|
-
const getOperations = function () {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return { ...operation, verb: method, path, parameters }
|
|
14
|
-
})
|
|
15
|
-
}),
|
|
16
|
-
)
|
|
6
|
+
export const getOperations = function () {
|
|
7
|
+
return Object.entries(openApiSpec.paths).flatMap(([path, pathItem]) => {
|
|
8
|
+
const operations = omit.default(pathItem, ['parameters'])
|
|
9
|
+
return Object.entries(operations).map(([method, operation]) => {
|
|
10
|
+
const parameters = getParameters(pathItem.parameters, operation.parameters)
|
|
11
|
+
return { ...operation, verb: method, path, parameters }
|
|
12
|
+
})
|
|
13
|
+
})
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
const getParameters = function (pathParameters = [], operationParameters = []) {
|
|
@@ -24,5 +21,3 @@ const getParameters = function (pathParameters = [], operationParameters = []) {
|
|
|
24
21
|
const addParameter = function (parameters, param) {
|
|
25
22
|
return { ...parameters, [param.in]: { ...parameters[param.in], [param.name]: param } }
|
|
26
23
|
}
|
|
27
|
-
|
|
28
|
-
module.exports = { getOperations }
|