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

Release prep #23

Merged
merged 4 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# netlify Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.1 - 2018-09-25
* A whole new Netlify Node.js API client! 🎉
* Client code was extracted from our forthcoming [2.0.0 CLI](https://www.netlify.com/blog/2018/09/10/netlify-cli-2.0-now-in-beta-/) release.
* A completely new API. Treat the 2.x.x and forward as a completely separate codebase and API.
* API calls are now derived from the [open-api](https://github.com/netlify/open-api) specification. See the [swagger-ui](https://open-api.netlify.com/#/default) and know that there is a matching API method for every operationID (the name on the right). See the README for full API docs.
* Includes a method for creating content based deploys.
45 changes: 32 additions & 13 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
# CONTRIBUTING

Contributions are always welcome, no matter how large or small. Before contributing,
please read the [code of conduct](CODE_OF_CONDUCT.md).
Contributions are always welcome, no matter how large or small. Before contributing, please read the [code of conduct](CODE_OF_CONDUCT.md).

## Setup

> Install NPM on your system: https://docs.npmjs.com/getting-started/installing-node
> Install Node.js + npm on your system: https://nodejs.org/en/download/

```sh
$ git clone https://github.com/netlify/js-client netlify-js-client
$ cd netlify-js-client
$ npm install
$ npm test
```

You can also use yarn.

## Testing

```sh
$ jasmine-node spec
```
This repo uses [ava](https://github.com/avajs/ava) for testing. Any files in the `src` directory that have a `.test.js` file extension are automatically detected and run as tests.

We also test for a few other things:

- Dependencies (used an unused)
- Linting
- Test coverage
- Must work with Windows + Unix environments.

## Pull Requests
## Architecture

We actively welcome your pull requests.
We target Node.js LTS and stable environments, and aim for basic modern browser support when possible. In order to facilitate simple contributions, we avoided any kind of build steps.

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
If you need to add new API routes, please add them to the [open-api](https://github.com/netlify/open-api) repo. This client will automatically inherent the new routes from that module.

Projects that depend heavily on this client that should be taken into consideration when making changes:

- [netlify/cli](https://github.com/netlify/cli)

## Releasing

```console
# Make changes
# Update README docs if they have changed.
# Update the changelog
$ npm version [major|minor|patch]
$ git push && git push --tags
# Create a github release with the changelog contents
$ npm publish
```

## License

Expand Down
41 changes: 23 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,32 @@ Create a new instance of the Netlify API client with the provided `accessToken`.
scheme: 'https',
host: 'api.netlify.com',
pathPrefix: '/api/v1',
globalParams: {} // parameters you want available for every request
globalParams: {} // parameters you want available for every request.
// Global params are only sent of the open-api spec specifies the provided params.
}
```

### `client.accessToken`

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`.
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`.

### `client.basePath`

A getter that returns the formatted base URL of the endpoint the client is configured to use.

### Open API Client methods

The client is dynamically generated from the [open-api](https://github.com/netlify/open-api) definition file. Each method is is named after the `operationId` name of each endpoint action. **To see list of available operations see the [open-api website](https://open-api.netlify.com/)**.
The client is dynamically generated from the [open-api](https://github.com/netlify/open-api) definition file. Each method is is named after the `operationId` name of each endpoint action. **To see list of available operations see the [open-api website](https://open-api.netlify.com/)**.

Every open-api method has the following signature:

#### `promise(response) = client.operationId([params], [opts])`

Perform a call to the given endpoint corresponding with the `operationId`. Returns promise that will resolve with the body of the response, or reject with an error with details about the request attached. Rejects if the `status` > 400. Successful response objects have `status` and `statusText` properties on their prototype.
Perform a call to the given endpoint corresponding with the `operationId`. Returns promise that will resolve with the body of the response, or reject with an error with details about the request attached. Rejects if the `status` > 400. Successful response objects have `status` and `statusText` properties on their prototype.

`params` is an object that includes any of the required or optional endpoint parameters. `params.body` should be an object which gets serialized to JSON automatically. If the endpoint accepts `binary`, `params.body` can be a Node.js readable stream.
- `params` is an object that includes any of the required or optional endpoint parameters.
- `params.body` should be an object which gets serialized to JSON automatically.
- If the endpoint accepts `binary`, `params.body` can be a Node.js readable stream.

```js
// example params
Expand All @@ -63,7 +66,7 @@ Perform a call to the given endpoint corresponding with the `operationId`. Retu
}
```

Optional `opts` can include any property you want passed to `node-fetch`. The `headers` property is merged with some `defaultHeaders`.
Optional `opts` can include any property you want passed to `node-fetch`. The `headers` property is merged with some `defaultHeaders`.

```js
// example opts
Expand All @@ -82,23 +85,25 @@ All methods are conveniently consumed with async/await:
async function getSomeData () {
// Calls may fail!
try {
const resposnse = await client.getSites()
return response
return await client.getSiteDeploy({
siteId: '1234abcd',
deploy_id: '4567'
})
} catch (e) {
// handle error
}
}
```

If the request response includes `json` in the `contentType` header, fetch will deserialize the JSON body. Otherwise the `text` of the response is returned.
If the request response includes `json` in the `contentType` header, fetch will deserialize the JSON body. Otherwise the `text` of the response is returned.

### Convenience Methods
### API Flow Methods

Some methods have been added in addition to the open API methods that make certain actions simpler to perform.

#### `promise(accessToken) = client.getAccessToken(ticket, [opts])`

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.
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.

Optional `opts` include:

Expand All @@ -121,34 +126,34 @@ async function login () {
await openBrowser(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
const accessToken = await api.getAccessToken(ticket)
// API is also set up to use the returned access token as a side effect
return accessToken
return accessToken // Save this for later so you can quickly set up an authenticated client
}
```

#### `promise(deploy) = client.deploy(siteId, buildDir, [opts])`

**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.
Sometimes need to write to a `tmpDir`.
Sometimes this method needs to write to a `tmpDir`. By default `tmpDir` is the default system default.

The following paths can be passed in the options:

- `tomlPath` (a `netlify.toml` file that includes redirect rules for the deploy)
- `configPath` (path to a `netlify.toml` file that includes redirect rules for the deploy, etc.)
- `functionsDir` (a folder with lambda functions to deploy)

Optional `opts` include:

```js
{
functionsDir: null, // path to a folder of functions to deploy
tomlPath: null, // path to a netlify.toml file to include in the deploy (e.g. redirect support for manual deploys)
configPath: null, // path to a netlify.toml file to include in the deploy (e.g. redirect support for manual deploys)
draft: false, // draft deploy or production deploy
message: undefined, // a short message to associate with the deploy
deployTimeout: 1.2e6, // 20 mins
parallelHash: 100, // number of parallel hashing calls
parallelUpload: 4, // number of files to upload in parallel
parallelUpload: 15, // number of files to upload in parallel
maxRetry: 5, // number of times to try on failed file uploads
filter: filename => { /* return false to filter a file from the deploy */ },
tmpDir: tempy.directory(), // a temporary directory to zip loose files into
filter: filepath => { /* return false to filter a file from the deploy */ },
tmpDir: tempy.directory(), // a temporary directory to zip functions into
statusCb: statusObj => {
// a callback function to receive status events
/* statusObj: {
Expand Down
4 changes: 2 additions & 2 deletions src/deploy/hash-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const pump = promisify(require('pump'))
const { hasherCtor, manifestCollectorCtor, fileFilterCtor, fileNormalizerCtor } = require('./hasher-segments')

module.exports = hashFiles
async function hashFiles(dir, tomlPath, opts) {
async function hashFiles(dir, configPath, opts) {
opts = Object.assign(
{
concurrentHash: 100,
Expand All @@ -15,7 +15,7 @@ async function hashFiles(dir, tomlPath, opts) {
)

if (!opts.filter) throw new Error('Missing filter function option')
const fileStream = walker([tomlPath, dir], { filter: opts.filter })
const fileStream = walker([configPath, dir], { filter: opts.filter })
const filter = fileFilterCtor()
const hasher = hasherCtor(opts)
const fileNormalizer = fileNormalizerCtor(opts)
Expand Down
6 changes: 3 additions & 3 deletions src/deploy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = async (api, siteId, dir, opts) => {
opts = Object.assign(
{
fnDir: null,
tomlPath: null,
configPath: null,
draft: false,
message: undefined, // API calls this the 'title'
tmpDir: tempy.directory(),
Expand All @@ -34,7 +34,7 @@ module.exports = async (api, siteId, dir, opts) => {
opts
)

const { fnDir, tomlPath, statusCb, message: title } = opts
const { fnDir, configPath, statusCb, message: title } = opts

statusCb({
type: 'hashing',
Expand All @@ -43,7 +43,7 @@ module.exports = async (api, siteId, dir, opts) => {
})

const [{ files, filesShaMap }, { functions, fnShaMap }] = await Promise.all([
hashFiles(dir, tomlPath, opts),
hashFiles(dir, configPath, opts),
hashFns(fnDir, opts)
])

Expand Down