cloudflare
Advanced tools
Comparing version 1.1.1 to 2.0.0
111
index.js
'use strict'; | ||
var url = require('url'); | ||
var spdy = require('spdy'); | ||
var got = require('got'); | ||
var prototypal = require('es-class'); | ||
var pkg = require('./package.json'); | ||
var auto = require('autocreate'); | ||
var ips = require('./lib/ips'); | ||
var zones = require('./lib/zones'); | ||
var purge = require('./lib/purge'); | ||
var dns = require('./lib/dns'); | ||
var Client = require('./lib/Client'); | ||
/** | ||
* Stub for paginated responses. | ||
* | ||
* Can be expanded later allow for getting the next page of results. | ||
*/ | ||
function PaginatedResponse(result, info) { | ||
this.result = result; | ||
this.page = info.page; | ||
this.perPage = info.per_page; | ||
this.totalPages = info.total_pages; | ||
this.count = info.count; | ||
this.total = info.total_count; | ||
} | ||
var resources = { | ||
dnsRecords: require('./lib/resources/DNSRecords'), | ||
ips: require('./lib/resources/IPs'), | ||
zones: require('./lib/resources/Zones') | ||
}; | ||
module.exports = prototypal({ | ||
static: { | ||
RequestError: got.RequestError, | ||
ReadError: got.ReadError, | ||
ParseError: got.ParseError, | ||
HTTPError: got.HTTPError, | ||
MaxRedirectError: got.MaxRedirectError, | ||
PaginatedResponse: PaginatedResponse | ||
}, | ||
constructor: function (opts) { | ||
opts = opts || {}; | ||
var Cloudflare = auto(prototypal({ | ||
constructor: function (auth) { | ||
var client = new Client({ | ||
email: auth && auth.email, | ||
key: auth && auth.key, | ||
h2: true | ||
}); | ||
var spdyAgent = opts.h2 ? spdy.createAgent({ | ||
host: 'api.cloudflare.com', | ||
port: 443, | ||
protocol: ['h2'] | ||
}).once('error', function (err) { | ||
/* istanbul ignore next */ | ||
this.emit('error', err); | ||
}) : undefined; | ||
Object.defineProperty(this, '_client', { | ||
value: client, | ||
writable: false, | ||
enumerable: false, | ||
configurable: false | ||
}); | ||
this._got = function (endpoint, options) { | ||
options = options || {}; | ||
var uri = url.resolve('https://api.cloudflare.com/client/v4/', endpoint); | ||
return got(uri, { | ||
json: true, | ||
timeout: options.timeout || 1E4, | ||
retries: options.retries, | ||
method: options.method, | ||
query: options.query, | ||
body: options.body, | ||
headers: { | ||
'user-agent': 'cloudflare/' + pkg.version + ' node/' + process.versions.node, | ||
'X-Auth-Key': opts.key, | ||
'X-Auth-Email': opts.email, | ||
'Content-Type': 'application/json' | ||
}, | ||
agent: spdyAgent | ||
Object.keys(resources).forEach(function (resource) { | ||
Object.defineProperty(this, resource, { | ||
value: resources[resource](this._client), | ||
writable: true, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
}; | ||
}, | ||
_paginateResponse: function (result, info) { | ||
return new PaginatedResponse(result, info); | ||
}, | ||
readIPs: ips.read, | ||
browseZones: zones.browse, | ||
readZone: zones.read, | ||
editZone: zones.edit, | ||
addZone: zones.add, | ||
checkZoneActivation: zones.activationCheck, | ||
deleteZone: zones.delete, | ||
deleteCache: purge.delete, | ||
browseDNS: dns.browse, | ||
readDNS: dns.read, | ||
editDNS: dns.edit, | ||
deleteDNS: dns.delete, | ||
addDNS: dns.add | ||
}); | ||
}, this); | ||
} | ||
})); | ||
module.exports.IPRanges = ips.IPRanges; | ||
module.exports.Zone = zones.Zone; | ||
module.exports.DNSRecord = dns.DNSRecord; | ||
module.exports = Cloudflare; |
@@ -5,2 +5,3 @@ { | ||
"author": "Terin Stock <terinjokes@gmail.com>", | ||
"version": "2.0.0", | ||
"bugs": { | ||
@@ -10,15 +11,18 @@ "url": "https://github.com/cloudflare/node-cloudflare/issues" | ||
"dependencies": { | ||
"autocreate": "^1.1.0", | ||
"es-class": "^2.1.1", | ||
"got": "^6.3.0", | ||
"lodash": "^4.13.1", | ||
"object-assign": "^4.1.0", | ||
"spdy": "^3.3.2", | ||
"url-join": "^1.1.0", | ||
"verymodel": "^3.6.0" | ||
"url-pattern": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.17.0", | ||
"coveralls": "^2.11.9", | ||
"nock": "^9.0.2", | ||
"intelli-espower-loader": "^1.0.1", | ||
"mocha": "^3.1.2", | ||
"nyc": "^10.0.0", | ||
"power-assert": "^1.4.2", | ||
"semantic-release": "^6.3.2", | ||
"testdouble": "^1.9.0", | ||
"xo": "^0.17.1" | ||
@@ -38,3 +42,3 @@ }, | ||
"scripts": { | ||
"test": "xo && nyc --reporter=lcov -- ava", | ||
"test": "xo && nyc --reporter=lcov --reporter=text-summary mocha --require intelli-espower-loader --recursive test", | ||
"coverage": "cat ./coverage/lcov.info | coveralls", | ||
@@ -49,8 +53,10 @@ "semantic-release": "semantic-release pre && npm publish && semantic-release post" | ||
"xo": { | ||
"space": true | ||
"space": true, | ||
"rules": { | ||
"unicorn/filename-case": 0 | ||
} | ||
}, | ||
"release": { | ||
"branch": "1.x" | ||
}, | ||
"version": "1.1.1" | ||
} | ||
"publishConfig": { | ||
"tag": "next" | ||
} | ||
} |
256
README.md
@@ -1,231 +0,73 @@ | ||
# CloudFlare API Client for Node.js | ||
# Cloudflare Node.js bindings | ||
An edible "BREAD" (Browse, Read, Edit, Add, and Delete) API Client for the | ||
CloudFlare v4 Client API. | ||
[Cloudflare v4 API][cf-api] bindings for Node.js, providing a sourdough | ||
"BREAD" (Browse, Read, Edit, Add, and Delete) interface. | ||
With this API client, you'll get the following features: | ||
[cf-api]: https://api.cloudflare.com/ | ||
* Promise-based API, allowing for easy async/await usage in harmony JavaScript | ||
engines, and with JavaScript transpilers. | ||
* Effortlessly connects over HTTP/2. | ||
With these bindings, you'll get the following features: | ||
* A Promise-based API. With modern versions of Node.js, this can be | ||
leveraged for async/await and generator support. | ||
* Effortless connections over HTTP/2. | ||
* Automatic handling of Gzip/Deflate compression. | ||
* Expontential backoff and retry in the presence of error. | ||
## Node.js Support | ||
Node.js v4 and greater are supported. | ||
This CloudFlare API client supports Node.js v4 and greater. | ||
## Configuration | ||
## Usage | ||
Set your account email address and API key. The API key can be found on | ||
the [My Account][my-account] page in the Cloudflare dashboard. | ||
### Creating a Client | ||
[my-account]: https://www.cloudflare.com/a/account | ||
```javascript | ||
var CFClient = require('cloudflare'); | ||
var client = new CFClient({ | ||
email: 'you@example.com', | ||
key: 'deadbeef' | ||
var cf = require('cloudflare')({ | ||
email: 'you@example.com', | ||
key: 'your Cloudflare API key' | ||
}); | ||
``` | ||
### Zones | ||
## API Overview | ||
#### `Zone` | ||
Every resource is accessed via your `cf` instance: | ||
* *API Reference*: [Zone properties](https://api.cloudflare.com/#zone-properties) | ||
```javascript | ||
// cf.{ RESOURCE_NAME }.{ METHOD_NAME } | ||
``` | ||
A normalized representation of a CloudFlare Zone. Properties that were | ||
`snake_cased` have been aliased to `camelCased`. TODO: document all the known | ||
properties. | ||
Every resource method returns a promise, which can be chained or used | ||
with async/await. | ||
#### `Zone.create(properties): Zone` | ||
```javascript | ||
cf.zones.read('023e105f4ecef8ad9ca31a8372d0c353').then(function (resp) { | ||
return resp.result.status; | ||
}); | ||
* properties: The properties to set on the new `Zone` object. | ||
* *Returns*: A new `Zone` object. | ||
Creates a new `Zone` object with the provided properties. The returned object | ||
can be used with the below methods that accept a `Zone` object. | ||
#### `Zone.is(z): boolean` | ||
* z: The other object that may, or may not, be a `Zone` object. | ||
* *Returns*: `true` if the parameter is a `Zone` object, otherwise `false`. | ||
#### `browseZones([query, [options]]): Promise<PaginatedResponse<Zone[]>>` | ||
* query: An object to pass filter and sorting parameters to the API | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a list of `Zone` objects wrapped in a `PaginatedResponse` | ||
* *API Reference*: [List zones](https://api.cloudflare.com/#zone-list-zones) | ||
Retrives the list of `Zone` objects, optionally filtered and sorted by `query` options. | ||
#### `readZone(z_id, [options]): Promise<Zone>` | ||
* z_id: The string Zone id | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a `Zone` | ||
* *API Reference*: [Zone details](https://api.cloudflare.com/#zone-zone-details) | ||
Retrives the `Zone` for the zone identifier `z_id`. | ||
#### `editZone(z, [options]): Promise<Zone>` | ||
* z: `Zone` object | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a `Zone`. | ||
* *API Reference*: [Edit Zone Properties](https://api.cloudflare.com/#zone-edit-zone-properties) | ||
Saves the modifications to the `Zone` object. | ||
**Note**: The CloudFlare API currently doesn't support patching multiple | ||
properties at once. This method executes the modifications in series, but if an | ||
error occurs, there are no guarantees which properties would have been applied. | ||
#### `addZone(z, [jumpstart, [options]]): Promise<Zone>` | ||
* z: `Zone` object | ||
* jumpstart: `true` to automatically fetch existing DNS records | ||
(default). `false` to disable this behavior. | ||
* options: *see Request Options* | ||
* *API Reference*: [Create a zone](https://api.cloudflare.com/#zone-create-a-zone) | ||
Add a zone to an account or organization. | ||
#### `checkZoneActivation(z): Promise<{id: string}>` | ||
* z: `Zone` object | ||
* *API Reference*: [Initiate another zone activation check](https://api.cloudflare.com/#zone-initiate-another-zone-activation-check) | ||
Initiate another zone activation check. | ||
#### `deleteZone(z, [options]): Promise<{id: string}>` | ||
* z: `Zone` object or string zone id | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a tombstone stub | ||
* *API Reference*: [Delete a zone](https://api.cloudflare.com/#zone-delete-a-zone) | ||
Deletes the `Zone` from CloudFlare. A tombstone stub is returned. | ||
#### `deleteCache(z, query, [options]): Promise<bool>` | ||
* z: `Zone` object or string zone id | ||
* query: The (required) purge options for the API: | ||
* `purge_everything`: Delete all of the zone's content from the CloudFlare | ||
cache. **Note**: This may cause an increase in connections to your server | ||
after performing this action. | ||
* `files`: An array of URLs that should be removed from cache. | ||
* `tags`: Removes assets from cache that were served with a Cache-Tag header | ||
that matches a string in this array. | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to `true`. | ||
* *API Reference*: | ||
[Purge all files](https://api.cloudflare.com/#zone-purge-all-files) and | ||
[Purge individual files by URL and Cache-Tags](https://api.cloudflare.com/#zone-purge-individual-files-by-url-and-cache-tags) | ||
Purges content from CloudFlare's cache. Please note that `query` is required for | ||
this API call. | ||
### DNS | ||
The CloudFlare API client makes it easy to manage DNS records for domains on CloudFlare. | ||
#### `DNSRecord` | ||
* *API Reference*: [DNS Records for a Zone](https://api.cloudflare.com/#dns-records-for-a-zone-properties) | ||
A normalized representation of a CloudFlare DNS Record type. Properties that | ||
were `snake_cased` have been aliased to be `camelCased`. | ||
#### `DNSRecord.create(properties): DNSRecord` | ||
* properties: The properties to set on the new `DNSRecord` object. | ||
* *Returns*: A new `DNSRecord` object. | ||
Creates a new `DNSRecord` object with the provided properties. The returned | ||
object can be used with the below methods that accept a `DNSRecord` object. | ||
#### `DNSRecord.is(rr): boolean` | ||
* rr: The other object that may, or may not, be a `DNSRecord` object. | ||
* *Returns*: `true` if the parameter is a `DNSRecord` object, otherwise `false`. | ||
#### `browseDNS(z, [query, [options]]): Promise<PaginatedResponse<DNSRecord[]>>` | ||
* z: `Zone` object or string zone id | ||
* query: An object to pass filter and sorting parameters to the API | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a list of `DNSRecord` objects wrapped in a `PaginatedResponse` | ||
* *API Reference*: [List DNS Records](https://api.cloudflare.com/#dns-records-for-a-zone-list-dns-records) | ||
Retrives the listing of `DNSRecord` objects for a `Zone` (or zone id). | ||
#### `readDNS(dns_id, z, [options]): Promise<DNSRecord>` | ||
* dns_id: The string DNS record id | ||
* z: `Zone` object or string zone id | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a `DNSRecord` | ||
* *API Reference*: [DNS record details](https://api.cloudflare.com/#dns-records-for-a-zone-dns-record-details) | ||
Retrive the `DNSRecord` for an identifier `did` from `Zone` (or zone id). | ||
#### `editDNS(d, [options]): Promise<DNSRecord>` | ||
* d: The `DNSRecord` to be saved | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a new `DNSRecord` | ||
* *API Reference*: [Update DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-update-dns-record) | ||
Save the modifications to the `DNSRecord`. | ||
#### `addDNS(d, [options]): Promise<DNSRecord>` | ||
* d: The `DNSRecord` to be added | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a new `DNSRecord` | ||
* *API Reference*: [Create DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-create-dns-record) | ||
Create a DNS Record corresponding to the provided `DNSRecord`. A new `DNSRecord` | ||
with an id field is returned. | ||
#### `deleteDNS(d, [options]): Promise<{id: string}>` | ||
* d: The `DNSRecord` to be deleted | ||
* options: *see Request Options* | ||
* *Returns*: A Promise that resolves to a tombstone stub | ||
* *API Reference*: [Delete DNS record](https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record) | ||
Deletes the `DNSRecord` from the DNS. A tombstone stub is returned. | ||
### CloudFlare IPs | ||
#### `IPRanges` | ||
* *API Reference*: [CloudFlare IPs properties](https://api.cloudflare.com/#cloudflare-ips-properties) | ||
Normalized representation of the CloudFlare IP Ranges. | ||
``` | ||
IPRanges{ | ||
IPv4CIDRs: [ | ||
'199.27.128.0/21' | ||
], | ||
IPv6CIDRs: [ | ||
'2400:cb00::/32' | ||
] | ||
// where supported | ||
async function getZoneStatus(id) { | ||
var resp = await cf.zones.read('023e105f4ecef8ad9ca31a8372d0c353'); | ||
return resp.result.status; | ||
} | ||
``` | ||
#### `IPRanges.is(ip): boolean` | ||
### Available resources and methods | ||
* ip: The other object that may, or may not, be an `IPRanges` object. | ||
* *Returns*: `true` if the parameter is a `IPRanges` object, otherwise `false`. | ||
* dnsRecords | ||
* `browse()` | ||
* `read(dnsRecordId)` | ||
* `edit(dnsRecordId, params)` | ||
* `add(params)` | ||
* `del(dnsRecordId)` | ||
* ips | ||
* `browse()` | ||
* zones | ||
* `browse()` | ||
* `read(zoneId)` | ||
* `edit(zoneId, params)` | ||
* `add(params)` | ||
* `del(zoneId)` | ||
* `activationCheck(zoneId)` | ||
* `purgeCache(zoneId, params)` | ||
#### `readIPs(): Promise<IPRanges>` | ||
* *Returns*: A Promise that resolves to `IPRanges` | ||
* *API Reference*: [CloudFlare IPs](https://api.cloudflare.com/#cloudflare-ips) | ||
Retrieves the CloudFlare Edge IP addresses. These are the IP addresses used by | ||
CloudFlare to connect to your server. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14
23315
7
8
692
74
1
+ Addedautocreate@^1.1.0
+ Addedobject-assign@^4.1.0
+ Addedurl-pattern@^1.0.3
+ Addedautocreate@1.2.0(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedurl-pattern@1.0.3(transitive)
- Removedlodash@^4.13.1
- Removedverymodel@^3.6.0
- Removedhoek@3.0.44.3.1(transitive)
- Removedisemail@2.2.1(transitive)
- Removedjoi@7.3.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedmoment@2.30.1(transitive)
- Removedtopo@2.1.1(transitive)
- Removedverymodel@3.6.0(transitive)