# phastly
functional fastly api with promises. A simple, minimal node.js fastly api wrapper. Sends the requests out and gives you back promises which resolve to the parsed object.

This library seeks to be developer friendly by providing promises and mapping each endpoint to a function for you, hopefully resulting in less time reading documentation: less fat-fingering of URLs, less forgetting http verbs.

For information on request parameters and response formats, please read: <https://docs.fastly.com/api/>

Currently in development and does not immediately seek to cover all endpoints but open to it. Pull requests and feature requests welcome.

Tested with node4+

## Style - Why do most functions end in "P"?

This denotes a promise is being returned. It is an opinionated style that I've adopted because unless you are using an IDE with really good type hinting you don't always know when a promise is being returned or not. And unlike other javascript types promises cannot and should not be silently coerced into their resolved value. Functions returning promises are always treated differently so this naming convention makes this behavior obvious.

## Usage

If set, phastly will automatically use the environment variable `process.env.FASTLY_API_KEY`. Or set/change the key with function `setApiKey()`

This is a great project for populating env variables from a file: <https://www.npmjs.com/package/dotenv>

### Promises

```js
import * as fastly from 'phastly'
// or
// const fastly = require('phastly')

function setupP(name) {
  return fastly.createServiceP(name)
  .then((service) => {
    // use service here
  })
}
```

### Async/Await
```js
import * as fastly from 'phastly'

async function setupP(name) {
  let service = await fastly.createServiceP(name)

  const backendData = {
    name: service.name,
    address: `foobarbaz.storage.googleapis.com`,
    hostname: `foobarbaz.storage.googleapis.com`,
    port: 443,
  }

  const p1 = fastly.createBackendP(service.id, 1, backendData)

  const domainData = {
    name: `foobarbaz.global.ssl.fastly.net`,
    comment: 'generated by my app'
  }

  const p2 = fastly.createDomainP(service.id, 1, domainData)

  const settingsData = {
    'general.default_host': `foobarbaz.storage.googleapis.com`
  }

  const p3 = fastly.updateSettingsP(service.id, 1, settingsData)

  await Promise.all([p1, p2, p3])

  return fastly.activateServiceVersionP(service.id, 1)
}
```

## API Reference
{{#module name="phastly"}}
{{>body~}}
{{>member-index~}}
{{>separator~}}
{{>members~}}
{{/module}}

## Documentation

To update `README.md` make changes in `doc/README.md.hbs` and/or `src/index.js` and run `doc/generate`
