https://github.com/zesty-io/phastly
node.js functional fastly api with promises
https://github.com/zesty-io/phastly
Last synced: 11 months ago
JSON representation
node.js functional fastly api with promises
- Host: GitHub
- URL: https://github.com/zesty-io/phastly
- Owner: zesty-io
- License: apache-2.0
- Created: 2016-08-03T00:04:30.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-15T22:58:36.000Z (almost 10 years ago)
- Last Synced: 2025-05-21T00:17:15.886Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 32.2 KB
- Stars: 3
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
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:
### 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
phastly module.
* [phastly](#module_phastly)
* [.sendP(options)](#module_phastly.sendP) ⇒ Promise
* [.purgeP(serviceId, key, [soft])](#module_phastly.purgeP) ⇒ Promise
* [.purgeUrlP(url, [soft])](#module_phastly.purgeUrlP) ⇒ Promise
* [.purgeAllP(serviceId)](#module_phastly.purgeAllP) ⇒ Promise
* [.createBackendP(serviceId, version, data)](#module_phastly.createBackendP) ⇒ Promise
* [.deleteBackendP(serviceId, version, name)](#module_phastly.deleteBackendP) ⇒ Promise
* [.createServiceP(name)](#module_phastly.createServiceP) ⇒ Promise
* [.updateServiceP(params)](#module_phastly.updateServiceP) ⇒ Promise
* [.createServiceVersionP(serviceId)](#module_phastly.createServiceVersionP) ⇒ Promise
* [.updateServiceVersionP(serviceId, version, data)](#module_phastly.updateServiceVersionP) ⇒ Promise
* [.validateServiceVersionP(serviceId, version)](#module_phastly.validateServiceVersionP) ⇒ Promise
* [.activateServiceVersionP(serviceId, version)](#module_phastly.activateServiceVersionP) ⇒ Promise
* [.deactivateServiceVersionP(serviceId, version)](#module_phastly.deactivateServiceVersionP) ⇒ Promise
* [.cloneServiceVersion(serviceId, version)](#module_phastly.cloneServiceVersion) ⇒ Promise
* [.lockServiceVersion(serviceId, version)](#module_phastly.lockServiceVersion) ⇒ Promise
* [.deleteServiceP(serviceId)](#module_phastly.deleteServiceP) ⇒ Promise
* [.renameServiceP(serviceId, newName)](#module_phastly.renameServiceP) ⇒ Promise
* [.filterActiveVersion(versions)](#module_phastly.filterActiveVersion) ⇒ Object
* [.ListServicesP()](#module_phastly.ListServicesP) ⇒ Promise
* [.getServiceP(serviceId)](#module_phastly.getServiceP) ⇒ Promise
* [.getServiceByNameP(name)](#module_phastly.getServiceByNameP) ⇒ Promise
* [.getServiceDetailsP(serviceId)](#module_phastly.getServiceDetailsP) ⇒ Promise
* [.getServiceDomainsP(service)](#module_phastly.getServiceDomainsP) ⇒ Promise
* [.createDomainP(serviceId, version, data)](#module_phastly.createDomainP) ⇒ Promise
* [.checkAllDomainsP(serviceId, version)](#module_phastly.checkAllDomainsP) ⇒ Promise
* [.createRequestSettingP(serviceId, version, settings)](#module_phastly.createRequestSettingP) ⇒ Promise
* [.updateSettingsP(serviceId, version, settings)](#module_phastly.updateSettingsP) ⇒ Promise
* [.setApiKey(key)](#module_phastly.setApiKey)
### 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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_phastly)
**Returns**: Object - version information
| Param | Type | Description |
| --- | --- | --- |
| versions | Array.<Object> | of the service |
### phastly.ListServicesP() ⇒ Promise
List services
**Kind**: static method of [phastly](#module_phastly)
**Returns**: Promise - resolves to parsed api result object
### phastly.getServiceP(serviceId) ⇒ Promise
Get a service by id
**Kind**: static method of [phastly](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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](#module_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`