netlify 9.0.0 → 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 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
- const NetlifyAPI = require('netlify')
11
+ import { NetlifyAPI } from 'netlify'
12
12
 
13
- const listNetlifySites = async function () {
14
- const client = new NetlifyAPI('1234myAccessToken')
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
- const NetlifyAPI = require('netlify')
20
+ import { NetlifyAPI } from 'netlify'
24
21
 
25
22
  const client = new NetlifyAPI('1234myAccessToken')
26
23
 
27
- const listCreateAndDeleteSite = async function () {
28
- // Fetch sites
29
- const sites = await client.listSites()
24
+ // Fetch sites
25
+ const sites = await client.listSites()
30
26
 
31
- // Create a site. Notice `body` here for sending OpenAPI body
32
- const site = await client.createSite({
33
- body: {
34
- name: `my-awesome-site`,
35
- // ... https://open-api.netlify.com/#/default/createSite
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
- // Delete site. Notice `site_id` is a path parameter https://open-api.netlify.com/#/default/deleteSite
40
- await client.deleteSite({
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
- const getSomeData = async () => {
120
+ try {
121
+ const siteDeploy = await client.getSiteDeploy({
122
+ siteId: '1234abcd',
123
+ deploy_id: '4567',
124
+ })
128
125
  // Calls may fail!
129
- try {
130
- return await client.getSiteDeploy({
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
- // example:
166
- const open = require('open') // installed with 'npm i open'
156
+ import open from 'open'
167
157
 
168
- const login = async () => {
169
- const ticket = await client.createTicket({
170
- clientId: CLIENT_ID,
171
- })
172
- // Open browser for authentication
173
- await open(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
174
- const accessToken = await client.getAccessToken(ticket)
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
- const HttpsProxyAgent = require('https-proxy-agent')
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": "9.0.0",
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 && run-s test build",
16
- "test": "npm run format && npm run test:dev",
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": {
@@ -71,21 +68,14 @@
71
68
  "qs": "^6.9.6"
72
69
  },
73
70
  "devDependencies": {
74
- "@babel/core": "^7.12.17",
75
- "@babel/plugin-transform-runtime": "^7.12.17",
76
- "@babel/preset-env": "^7.12.17",
77
- "@babel/runtime": "^7.12.18",
78
- "@netlify/eslint-config-node": "^3.3.8",
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
81
  "node": "^12.20.0 || ^14.14.0 || >=16.0.0"
@@ -94,8 +84,6 @@
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
- const dfn = require('@netlify/open-api')
2
- const pWaitFor = require('p-wait-for')
1
+ import pWaitFor from 'p-wait-for'
3
2
 
4
- const { getMethods } = require('./methods')
5
- const { getOperations } = require('./operations')
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 = dfn.schemes[0],
16
- host = dfn.host,
17
- pathPrefix = dfn.basePath,
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
- module.exports = NetlifyAPI
93
-
94
- module.exports.methods = getOperations()
92
+ export const methods = getOperations()
@@ -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 }
@@ -1,17 +1,15 @@
1
- const nodeFetch = require('node-fetch')
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
- const { getOperations } = require('../operations')
3
+ import { getOperations } from '../operations.js'
6
4
 
7
- const { addBody } = require('./body')
8
- const { parseResponse, getFetchError } = require('./response')
9
- const { shouldRetry, waitForRetry, MAX_RETRY } = require('./retry')
10
- const { getUrl } = require('./url')
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 }
@@ -1,8 +1,8 @@
1
- const { JSONHTTPError, TextHTTPError } = require('micro-api-client')
2
- const omit = require('omit.js').default
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 }
@@ -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 }
@@ -1,9 +1,9 @@
1
- const camelCase = require('lodash.camelcase')
2
- const queryString = require('qs')
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 }
@@ -0,0 +1,7 @@
1
+ import { createRequire } from 'module'
2
+
3
+ // TODO: remove once Node.js supports JSON imports with pure ES modules without
4
+ // any experimental flags
5
+ const require = createRequire(import.meta.url)
6
+
7
+ export const openApiSpec = require('@netlify/open-api')
package/src/operations.js CHANGED
@@ -1,10 +1,11 @@
1
- const { paths } = require('@netlify/open-api')
2
- const omit = require('omit.js').default
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
- return Object.entries(paths).flatMap(([path, pathItem]) => {
7
- const operations = omit(pathItem, ['parameters'])
6
+ export const getOperations = function () {
7
+ return Object.entries(openApiSpec.paths).flatMap(([path, pathItem]) => {
8
+ const operations = omit.default(pathItem, ['parameters'])
8
9
  return Object.entries(operations).map(([method, operation]) => {
9
10
  const parameters = getParameters(pathItem.parameters, operation.parameters)
10
11
  return { ...operation, verb: method, path, parameters }
@@ -20,5 +21,3 @@ const getParameters = function (pathParameters = [], operationParameters = []) {
20
21
  const addParameter = function (parameters, param) {
21
22
  return { ...parameters, [param.in]: { ...parameters[param.in], [param.name]: param } }
22
23
  }
23
-
24
- module.exports = { getOperations }