1 | # netlify/js-client
|
2 |
|
3 | [![npm version][npm-img]][npm] [![build status][build-img]][build]
|
4 | [![coverage][coverage-img]][coverage] [![dependencies][david-img]][david] [![downloads][dl-img]][dl]
|
5 |
|
6 | A Netlify [OpenAPI](https://github.com/netlify/open-api) client that works in the browser and Node.js.
|
7 |
|
8 | ## Usage
|
9 |
|
10 | ```js
|
11 | const NetlifyAPI = require('netlify')
|
12 | const client = new NetlifyAPI('1234myAccessToken')
|
13 | const sites = await client.listSites()
|
14 | ```
|
15 |
|
16 | ## Using OpenAPI operations
|
17 |
|
18 | ```js
|
19 | const NetlifyAPI = require('netlify')
|
20 | const client = new NetlifyAPI('1234myAccessToken')
|
21 |
|
22 | // Fetch sites
|
23 | const sites = await client.listSites()
|
24 |
|
25 | // Create a site. Notice `body` here for sending OpenAPI body
|
26 | const site = await client.createSite({
|
27 | body: {
|
28 | name: `my-awesome-site`
|
29 | // ... https://open-api.netlify.com/#/default/createSite
|
30 | }
|
31 | })
|
32 |
|
33 | // Delete site. Notice `site_id` is a path parameter https://open-api.netlify.com/#/default/deleteSite
|
34 | await client.deleteSite({
|
35 | site_id: siteId
|
36 | })
|
37 | ```
|
38 |
|
39 | ## API
|
40 |
|
41 | ### `client = new NetlifyAPI([accessToken], [opts])`
|
42 |
|
43 | Create a new instance of the Netlify API client with the provided `accessToken`.
|
44 |
|
45 | `accessToken` is optional. Without it, you can't make authorized requests.
|
46 |
|
47 | `opts` includes:
|
48 |
|
49 | ```js
|
50 | {
|
51 | userAgent: 'netlify/js-client',
|
52 | scheme: 'https',
|
53 | host: 'api.netlify.com',
|
54 | pathPrefix: '/api/v1',
|
55 | accessToken: '1234myAccessToken',
|
56 | agent: undefined, // e.g. HttpsProxyAgent
|
57 | globalParams: {} // parameters you want available for every request.
|
58 | // Global params are only sent of the OpenAPI spec specifies the provided params.
|
59 | }
|
60 | ```
|
61 |
|
62 | ### `client.accessToken`
|
63 |
|
64 | A setter/getter that returns the `accessToken` that the client is configured to use. You can set this after the class is instantiated, and all subsequent calls will use the newly set `accessToken`.
|
65 |
|
66 | ### `client.basePath`
|
67 |
|
68 | A getter that returns the formatted base URL of the endpoint the client is configured to use.
|
69 |
|
70 | ### OpenAPI Client methods
|
71 |
|
72 | The client is dynamically generated from the [OpenAPI](https://github.com/netlify/open-api) definition file. Each method is is named after the `operationId` name of each operation. **To see a list of available operations, please see the [OpenAPI website](https://open-api.netlify.com/)**.
|
73 |
|
74 | Every OpenAPI operation has the following signature:
|
75 |
|
76 | #### `response = await client.operationId([params], [opts])`
|
77 |
|
78 | Performs a call to the given endpoint corresponding with the `operationId`. Returns a promise resolved with the body of the response, or rejected with an error with the details about the request attached. Rejects if the `status` > 400.
|
79 |
|
80 | - `params` is an object that includes any of the required or optional endpoint parameters.
|
81 | - `params.body` should be an object which gets serialized to JSON automatically. Any object can live here but refer to the OpenAPI specification for allowed fields in a particular request body. It can also be a function returning an object.
|
82 | - If the endpoint accepts `binary`, `params.body` can be a Node.js readable stream or a function returning one (e.g. `() => fs.createReadStream('./foo')`). Using a function is recommended.
|
83 |
|
84 | ```js
|
85 | // example params
|
86 | {
|
87 | any_param_needed,
|
88 | paramsCanAlsoBeCamelCase,
|
89 | body: {
|
90 | an: 'arbitrary js object'
|
91 | }
|
92 | }
|
93 | ```
|
94 |
|
95 | Optional `opts` can include any property you want passed to [`node-fetch`](https://github.com/bitinn/node-fetch). The `headers` property is merged with some `defaultHeaders`.
|
96 |
|
97 | ```js
|
98 | // example opts
|
99 | {
|
100 | headers: { // Default headers
|
101 | 'User-agent': 'netlify-js-client',
|
102 | accept: 'application/json'
|
103 | }
|
104 | // any other properties for node-fetch
|
105 | }
|
106 | ```
|
107 |
|
108 | All operations are conveniently consumed with async/await:
|
109 |
|
110 | ```js
|
111 | async function getSomeData() {
|
112 | // Calls may fail!
|
113 | try {
|
114 | return await client.getSiteDeploy({
|
115 | siteId: '1234abcd',
|
116 | deploy_id: '4567'
|
117 | })
|
118 | } catch (e) {
|
119 | // handle error
|
120 | }
|
121 | }
|
122 | ```
|
123 |
|
124 | If the response includes `json` in the `contentType` header, fetch will deserialize the JSON body. Otherwise the `text` of the response is returned.
|
125 |
|
126 | ### API Flow Methods
|
127 |
|
128 | Some methods have been added in addition to the open API operations that make certain actions simpler to perform.
|
129 |
|
130 | #### `accessToken = await client.getAccessToken(ticket, [opts])`
|
131 |
|
132 | Pass in a [`ticket`](https://open-api.netlify.com/#model-ticket) and get back an `accessToken`. Call this with the response from a `client.createTicket({ client_id })` call. Automatically sets the `accessToken` to `this.accessToken` and returns `accessToken` for the consumer to save for later.
|
133 |
|
134 | Optional `opts` include:
|
135 |
|
136 | ```js
|
137 | {
|
138 | poll: 1000, // number of ms to wait between polling
|
139 | timeout: 3.6e6 // number of ms to wait before timing out
|
140 | }
|
141 | ```
|
142 |
|
143 | See the [authenticating](https://www.netlify.com/docs/api/#authenticating) docs for more context.
|
144 |
|
145 | ```js
|
146 | // example:
|
147 | async function login() {
|
148 | const ticket = await api.createTicket({
|
149 | clientId: CLIENT_ID
|
150 | })
|
151 | // Open browser for authentication
|
152 | await openBrowser(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
|
153 | const accessToken = await api.getAccessToken(ticket)
|
154 | // API is also set up to use the returned access token as a side effect
|
155 | return accessToken // Save this for later so you can quickly set up an authenticated client
|
156 | }
|
157 | ```
|
158 |
|
159 | #### `deploy = await client.deploy(siteId, buildDir, [opts])`
|
160 |
|
161 | **Node.js only**: Pass in a `siteId`, a `buildDir` (the folder you want to deploy) and an options object to deploy the contents of that folder.
|
162 | Sometimes this method needs to write to a `tmpDir`. By default `tmpDir` is a folder in the system temporary directory.
|
163 |
|
164 | The following paths can be passed in the options:
|
165 |
|
166 | - `configPath` (path to a `netlify.toml` file that includes redirect rules for the deploy, etc.)
|
167 | - `fnDir` (a folder with lambda functions to deploy)
|
168 |
|
169 | Optional `opts` include:
|
170 |
|
171 | ```js
|
172 | {
|
173 | fnDir: null, // path to a folder of functions to deploy
|
174 | configPath: null, // path to a netlify.toml file to include in the deploy (e.g. redirect support for manual deploys)
|
175 | draft: false, // draft deploy or production deploy
|
176 | message: undefined, // a short message to associate with the deploy
|
177 | deployTimeout: 1.2e6, // 20 mins
|
178 | parallelHash: 100, // number of parallel hashing calls
|
179 | parallelUpload: 15, // number of files to upload in parallel
|
180 | maxRetry: 5, // number of times to try on failed file uploads
|
181 | filter: filepath => { /* return false to filter a file from the deploy */ },
|
182 | tmpDir: tempy.directory(), // a temporary directory to zip functions into
|
183 | statusCb: statusObj => {
|
184 | // a callback function to receive status events
|
185 | /* statusObj: {
|
186 | type: name-of-step
|
187 | msg: msg to print
|
188 | phase: [start, progress, stop]
|
189 | } */
|
190 | // See https://github.com/netlify/cli/blob/v2.0.0-beta.3/src/commands/deploy.js#L161-L195
|
191 | // for an example of how this can be used.
|
192 | }
|
193 | }
|
194 | ```
|
195 |
|
196 | ## Proxy support
|
197 |
|
198 | **Node.js only**: If this client is used behind a corporate proxy, you can pass an `HttpsProxyAgent` or any other `http.Agent` that can handle your situation as `agent` option:
|
199 |
|
200 | ```js
|
201 | const HttpsProxyAgent = require('https-proxy-agent')
|
202 |
|
203 | const proxyUri = 'http(s)://[user:password@]proxyhost:port'
|
204 | const agent = new HttpsProxyAgent(proxyUri)
|
205 | const client = new NetlifyAPI('1234myAccessToken', { agent })
|
206 | ```
|
207 |
|
208 | ## UMD Builds
|
209 |
|
210 | A UMD build is provided for your convenience, however browser support is still experimental. Contributions to improve browser support are welcome.
|
211 |
|
212 | ## Contributing
|
213 |
|
214 | See [CONTRIBUTING.md](CONTRIBUTING.md) for more info on how to make contributions to this project.
|
215 |
|
216 | ## License
|
217 |
|
218 | MIT. See [LICENSE](LICENSE) for more details.
|
219 |
|
220 | [npm-img]: https://img.shields.io/npm/v/netlify.svg
|
221 | [npm]: https://npmjs.org/package/netlify
|
222 | [build-img]: https://github.com/netlify/js-client/workflows/Build/badge.svg
|
223 | [build]: https://github.com/netlify/js-client/actions
|
224 | [dl-img]: https://img.shields.io/npm/dm/netlify.svg
|
225 | [dl]: https://npmjs.org/package/netlify
|
226 | [coverage-img]: https://codecov.io/gh/netlify/js-client/branch/master/graph/badge.svg
|
227 | [coverage]: https://codecov.io/gh/netlify/js-client
|
228 | [david-img]: https://david-dm.org/netlify/js-client/status.svg
|
229 | [david]: https://david-dm.org/netlify/js-client
|