phastly

0.2.4 • Public • Published

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

import * as fastly from 'phastly'
// or
// const fastly = require('phastly')
 
function setupP(name) {
  return fastly.createServiceP(name)
  .then((service) => {
    // use service here
  })
}

Async/Await

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

phastly module.

phastly.sendP(options) ⇒ Promise

Wrapper to send a fastly api request. Use this if the endpoint you need hasn't been mapped to a function.

Kind: static method of phastly
Returns: Promise - resolving to response

Param Type Description
options Object
options.params Object parameters to upload with encoding: application/x-www-form-urlencoded
options.baseUrl string fastly api baseUrl (default: 'https://api.fastly.com')
options.endpoint string fastly api endpoint e.g. 'service/${serviceId}/version/${version}/backend' (default: '')
options.headers Object add to or overwrite the default headers (default: {'Fastly-Key', Accept})
options.method string the http method (default: GET)
options.timeout number the connection timeout in ms (default: 5000)

phastly.purgeP(serviceId, key, [soft]) ⇒ Promise

Instant Purge a particular service of items tagged with a Surrogate Key. Soft Purging sets an object's TTL to 0s, forcing revalidation. For best results, Soft Purging should be used in conjuction with stale_while_revalidate and stale_if_error.

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Default Description
serviceId string
key string
[soft] Boolean false sets an object's TTL to 0s

phastly.purgeUrlP(url, [soft]) ⇒ Promise

Instant Purge an individual URL. Soft Purging sets an object's TTL to 0s, forcing revalidation. For best results, Soft Purging should be used in conjuction with stale_while_revalidate and stale_if_error.

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Default
url string
[soft] Boolean false

phastly.purgeAllP(serviceId) ⇒ Promise

Instant Purge everything from a service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string

phastly.createBackendP(serviceId, version, data) ⇒ Promise

Create a backend for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
serviceId string
version number
data Object keys are backend object keys

phastly.deleteBackendP(serviceId, version, name) ⇒ Promise

Delete the backend for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
serviceId string
version number
name string name of backend

phastly.createServiceP(name) ⇒ Promise

Create a service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
name string

phastly.updateServiceP(params) ⇒ Promise

Update a service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
params Object key/values of paramters to update

phastly.createServiceVersionP(serviceId) ⇒ Promise

Create a version for a particular service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string

phastly.updateServiceVersionP(serviceId, version, data) ⇒ Promise

Update a particular version for a particular service.

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
serviceId string
version number
data Object keys are service object keys

phastly.validateServiceVersionP(serviceId, version) ⇒ Promise

Validate the version for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.activateServiceVersionP(serviceId, version) ⇒ Promise

Activate the current version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.deactivateServiceVersionP(serviceId, version) ⇒ Promise

Deactivate the current version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.cloneServiceVersion(serviceId, version) ⇒ Promise

Clone the current configuration into a new version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.lockServiceVersion(serviceId, version) ⇒ Promise

Locks the specified version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.deleteServiceP(serviceId) ⇒ Promise

Delete a service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string

phastly.renameServiceP(serviceId, newName) ⇒ Promise

Rename a service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
newName string

phastly.filterActiveVersion(versions) ⇒ Object

helper function - get active version from a fastly version list

Kind: static method of phastly
Returns: Object - version information

Param Type Description
versions Array.<Object> of the service

phastly.ListServicesP() ⇒ Promise

List services

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

phastly.getServiceP(serviceId) ⇒ Promise

Get a service by id

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string

phastly.getServiceByNameP(name) ⇒ Promise

Get a service by name

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
name string name of service

phastly.getServiceDetailsP(serviceId) ⇒ Promise

List detailed information on a specified service

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string

phastly.getServiceDomainsP(service) ⇒ Promise

List the domains within a service

Kind: static method of phastly
Returns: Promise - resolves to deserialized response

Param Type Description
service string id

phastly.createDomainP(serviceId, version, data) ⇒ Promise

Create a domain for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
serviceId string
version number
data Object fastly domain object

phastly.checkAllDomainsP(serviceId, version) ⇒ Promise

Check all domains' DNS for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type
serviceId string
version number

phastly.createRequestSettingP(serviceId, version, settings) ⇒ Promise

Create a new Request Settings object

Kind: static method of phastly

Param Type Description
serviceId string
version number
settings Object fastly request settings object: {hash_keys, action, ...}

phastly.updateSettingsP(serviceId, version, settings) ⇒ Promise

Update the settings for a particular service and version

Kind: static method of phastly
Returns: Promise - resolves to parsed api result object

Param Type Description
serviceId string
version number
settings Object fastly settings object e.g. {general.default_host, general.default_ttl, ...}

phastly.setApiKey(key)

set or change the fastly api key

Kind: static method of phastly

Param Type Description
key string your fastly api key

Documentation

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

Package Sidebar

Install

npm i phastly

Weekly Downloads

3

Version

0.2.4

License

Apache-2.0

Last publish

Collaborators

  • chrishiestand