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
// or// const fastly = require('phastly') { return fastly }
Async/Await
{ let service = await fastly const backendData = name: servicename address: `foobarbaz.storage.googleapis.com` hostname: `foobarbaz.storage.googleapis.com` port: 443 const p1 = fastly const domainData = name: `foobarbaz.global.ssl.fastly.net` comment: 'generated by my app' const p2 = fastly const settingsData = 'general.default_host': `foobarbaz.storage.googleapis.com` const p3 = fastly await Promiseallp1 p2 p3 return fastly}
API Reference
phastly module.
- phastly
- .sendP(options) ⇒
Promise
- .purgeP(serviceId, key, [soft]) ⇒
Promise
- .purgeUrlP(url, [soft]) ⇒
Promise
- .purgeAllP(serviceId) ⇒
Promise
- .createBackendP(serviceId, version, data) ⇒
Promise
- .deleteBackendP(serviceId, version, name) ⇒
Promise
- .createServiceP(name) ⇒
Promise
- .updateServiceP(params) ⇒
Promise
- .createServiceVersionP(serviceId) ⇒
Promise
- .updateServiceVersionP(serviceId, version, data) ⇒
Promise
- .validateServiceVersionP(serviceId, version) ⇒
Promise
- .activateServiceVersionP(serviceId, version) ⇒
Promise
- .deactivateServiceVersionP(serviceId, version) ⇒
Promise
- .cloneServiceVersion(serviceId, version) ⇒
Promise
- .lockServiceVersion(serviceId, version) ⇒
Promise
- .deleteServiceP(serviceId) ⇒
Promise
- .renameServiceP(serviceId, newName) ⇒
Promise
- .filterActiveVersion(versions) ⇒
Object
- .ListServicesP() ⇒
Promise
- .getServiceP(serviceId) ⇒
Promise
- .getServiceByNameP(name) ⇒
Promise
- .getServiceDetailsP(serviceId) ⇒
Promise
- .getServiceDomainsP(service) ⇒
Promise
- .createDomainP(serviceId, version, data) ⇒
Promise
- .checkAllDomainsP(serviceId, version) ⇒
Promise
- .createRequestSettingP(serviceId, version, settings) ⇒
Promise
- .updateSettingsP(serviceId, version, settings) ⇒
Promise
- .setApiKey(key)
- .sendP(options) ⇒
Promise
phastly.sendP(options) ⇒ 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) |
Promise
phastly.purgeP(serviceId, key, [soft]) ⇒ 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 |
Promise
phastly.purgeUrlP(url, [soft]) ⇒ 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 |
Promise
phastly.purgeAllP(serviceId) ⇒ Instant Purge everything from a service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
Promise
phastly.createBackendP(serviceId, version, data) ⇒ 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 |
Promise
phastly.deleteBackendP(serviceId, version, name) ⇒ 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 |
Promise
phastly.createServiceP(name) ⇒ Create a service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
name | string |
Promise
phastly.updateServiceP(params) ⇒ 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 |
Promise
phastly.createServiceVersionP(serviceId) ⇒ Create a version for a particular service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
Promise
phastly.updateServiceVersionP(serviceId, version, data) ⇒ 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 |
Promise
phastly.validateServiceVersionP(serviceId, version) ⇒ 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 |
Promise
phastly.activateServiceVersionP(serviceId, version) ⇒ Activate the current version
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
version | number |
Promise
phastly.deactivateServiceVersionP(serviceId, version) ⇒ Deactivate the current version
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
version | number |
Promise
phastly.cloneServiceVersion(serviceId, version) ⇒ 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 |
Promise
phastly.lockServiceVersion(serviceId, version) ⇒ Locks the specified version
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
version | number |
Promise
phastly.deleteServiceP(serviceId) ⇒ Delete a service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
Promise
phastly.renameServiceP(serviceId, newName) ⇒ Rename a service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
newName | string |
Object
phastly.filterActiveVersion(versions) ⇒ 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 |
Promise
phastly.ListServicesP() ⇒ List services
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Promise
phastly.getServiceP(serviceId) ⇒ Get a service by id
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
Promise
phastly.getServiceByNameP(name) ⇒ 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 |
Promise
phastly.getServiceDetailsP(serviceId) ⇒ List detailed information on a specified service
Kind: static method of phastly
Returns: Promise
- resolves to parsed api result object
Param | Type |
---|---|
serviceId | string |
Promise
phastly.getServiceDomainsP(service) ⇒ List the domains within a service
Kind: static method of phastly
Returns: Promise
- resolves to deserialized response
Param | Type | Description |
---|---|---|
service | string |
id |
Promise
phastly.createDomainP(serviceId, version, data) ⇒ 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 |
Promise
phastly.checkAllDomainsP(serviceId, version) ⇒ 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 |
Promise
phastly.createRequestSettingP(serviceId, version, settings) ⇒ 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, ...} |
Promise
phastly.updateSettingsP(serviceId, version, settings) ⇒ 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