0.2.4 • Published 8 years ago

phastly v0.2.4

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
8 years ago

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

ParamTypeDescription
optionsObject
options.paramsObjectparameters to upload with encoding: application/x-www-form-urlencoded
options.baseUrlstringfastly api baseUrl (default: 'https://api.fastly.com')
options.endpointstringfastly api endpoint e.g. 'service/${serviceId}/version/${version}/backend' (default: '')
options.headersObjectadd to or overwrite the default headers (default: {'Fastly-Key', Accept})
options.methodstringthe http method (default: GET)
options.timeoutnumberthe 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

ParamTypeDefaultDescription
serviceIdstring
keystring
softBooleanfalsesets 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

ParamTypeDefault
urlstring
softBooleanfalse

phastly.purgeAllP(serviceId) ⇒ Promise

Instant Purge everything from a service

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

ParamType
serviceIdstring

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

ParamTypeDescription
serviceIdstring
versionnumber
dataObjectkeys 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

ParamTypeDescription
serviceIdstring
versionnumber
namestringname of backend

phastly.createServiceP(name) ⇒ Promise

Create a service

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

ParamType
namestring

phastly.updateServiceP(params) ⇒ Promise

Update a service

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

ParamTypeDescription
paramsObjectkey/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

ParamType
serviceIdstring

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

ParamTypeDescription
serviceIdstring
versionnumber
dataObjectkeys 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

ParamType
serviceIdstring
versionnumber

phastly.activateServiceVersionP(serviceId, version) ⇒ Promise

Activate the current version

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

ParamType
serviceIdstring
versionnumber

phastly.deactivateServiceVersionP(serviceId, version) ⇒ Promise

Deactivate the current version

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

ParamType
serviceIdstring
versionnumber

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

ParamType
serviceIdstring
versionnumber

phastly.lockServiceVersion(serviceId, version) ⇒ Promise

Locks the specified version

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

ParamType
serviceIdstring
versionnumber

phastly.deleteServiceP(serviceId) ⇒ Promise

Delete a service

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

ParamType
serviceIdstring

phastly.renameServiceP(serviceId, newName) ⇒ Promise

Rename a service

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

ParamType
serviceIdstring
newNamestring

phastly.filterActiveVersion(versions) ⇒ Object

helper function - get active version from a fastly version list

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

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

ParamType
serviceIdstring

phastly.getServiceByNameP(name) ⇒ Promise

Get a service by name

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

ParamTypeDescription
namestringname 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

ParamType
serviceIdstring

phastly.getServiceDomainsP(service) ⇒ Promise

List the domains within a service

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

ParamTypeDescription
servicestringid

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

ParamTypeDescription
serviceIdstring
versionnumber
dataObjectfastly 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

ParamType
serviceIdstring
versionnumber

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

Create a new Request Settings object

Kind: static method of phastly

ParamTypeDescription
serviceIdstring
versionnumber
settingsObjectfastly 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

ParamTypeDescription
serviceIdstring
versionnumber
settingsObjectfastly 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

ParamTypeDescription
keystringyour fastly api key

Documentation

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

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago