cloudflare
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -11,20 +11,2 @@ var https = require('https'); | ||
///--- Internal Helpers | ||
function _clone(object) { | ||
var keys, prop, clone = {}; | ||
assert.ok(object); | ||
keys = Object.getOwnPropertyNames(object); | ||
keys.forEach(function (k) { | ||
prop = Object.getOwnPropertyDescriptor(object, k); | ||
Object.defineProperty(clone, k, prop); | ||
}); | ||
return clone; | ||
} | ||
function validateType(type) { | ||
@@ -38,5 +20,5 @@ return (/^(A|CNAME|MX|TXT|SPF|AAAA|NS|SRV|LOC)$/).test(type); | ||
* | ||
* @class CloudFlare | ||
* @constructor | ||
* @link http://www.cloudflare.com/docs/client-api.html | ||
* @class CloudFlare | ||
* @param {String} token API token from the (My Account)[https://www.cloudflare.com/my-account] page | ||
@@ -46,5 +28,5 @@ * @param {String} email Email address associated with your CloudFlare account | ||
function CloudFlare(token, email) { | ||
this.token = token; | ||
this.email = email; | ||
this.endpoint = endpoint; | ||
this.token = token; | ||
this.email = email; | ||
this.endpoint = endpoint; | ||
} | ||
@@ -550,6 +532,10 @@ | ||
this._request("rec_new", util.mix(defaults, options, { z: domain }), function (err, res) { | ||
fn(err, res.rec.obj); | ||
if (err) { | ||
fn(err, res); | ||
} else { | ||
fn(null, res.rec.obj); | ||
} | ||
}); | ||
}; | ||
cf.rec_new = cf.addRecord =cf.addDomainRecord; | ||
cf.rec_new = cf.addRecord = cf.addDomainRecord; | ||
@@ -623,72 +609,94 @@ /** | ||
cf.createRequestData = function (action, params) { | ||
var data = { | ||
tkn: this.token, | ||
email: this.email, | ||
a: action | ||
}; | ||
var data = { | ||
tkn: this.token, | ||
email: this.email, | ||
a: action | ||
}; | ||
if (typeof params === 'object') { | ||
Object.keys(params).forEach(function (key) { | ||
data[key] = params[key]; | ||
}); | ||
} | ||
if (typeof params === 'object') { | ||
Object.keys(params).forEach(function (key) { | ||
data[key] = params[key]; | ||
}); | ||
} | ||
return data; | ||
return data; | ||
}; | ||
cf._response = function (req, res, fn) { | ||
var str = "", | ||
data; | ||
cf._request = function (action, params, fn) { | ||
var uri = url.parse(this.endpoint), | ||
client = uri.protocol === 'http:' ? http : https; | ||
res.setEncoding('utf8'); | ||
res.on('data', function (d) { | ||
str += d.toString(); | ||
}); | ||
uri.method = 'POST'; | ||
res.on('end', function (chunk) { | ||
if (chunk) { | ||
str += chunk.toString(); | ||
} | ||
var postData = this.createRequestData(action, params); | ||
postData = qs.stringify(postData).toString('utf8'); | ||
try { | ||
data = JSON.parse(str); | ||
} catch (e) { | ||
return fn(new Error("Unable to parse response"), data, res); | ||
} | ||
uri.headers = { | ||
'content-length': postData.length, | ||
'content-type': 'application/x-www-form-urlencoded; charset=utf-8' | ||
}; | ||
if ("success" === data.result) { | ||
fn(null, data.response); | ||
} else { | ||
fn(new Error(data.msg), data); | ||
} | ||
}); | ||
}; | ||
var req = client.request(uri, function (res) { | ||
var str = "", data; | ||
cf.createPostData = function (action, params) { | ||
var postData = this.createRequestData(action, params); | ||
res.on('data', function (d) { | ||
str += d.toString('utf8'); | ||
}); | ||
return qs.stringify(postData).toString('utf8') + '\n'; | ||
}; | ||
res.on('end', function () { | ||
try { | ||
data = JSON.parse(str); | ||
} catch (e) { | ||
return fn("Unable to parse response", data, res); | ||
} | ||
cf._request = function (action, params, fn) { | ||
var uri = url.parse(this.endpoint), | ||
client = uri.protocol === 'http:' ? http : https, | ||
data = this.createPostData(action, params); | ||
if ("success" === data.result) { | ||
fn(null, data.response); | ||
} else { | ||
fn(data.msg, data.response); | ||
} | ||
}); | ||
}); | ||
uri.method = 'POST'; | ||
req.write(postData + '\n'); | ||
req.end(); | ||
uri.headers = { | ||
'content-length': data.length, | ||
'content-type': 'application/x-www-form-urlencoded; charset=utf-8' | ||
}; | ||
req.on('error', function (e) { | ||
console.error(e); | ||
}); | ||
var req = client.request(uri, function (res) { | ||
cf._response(req, res, fn); | ||
}); | ||
req.write(data); | ||
req.end(); | ||
req.on('error', function (e) { | ||
console.error(e); | ||
}); | ||
}; | ||
module.exports = { | ||
CloudFlare: CloudFlare, | ||
CloudFlare: CloudFlare, | ||
createClient: function (options) { | ||
assert.equal(typeof options, 'object'); | ||
assert.equal(typeof options.token, 'string'); | ||
assert.equal(typeof options.email, 'string'); | ||
createClient: function (options) { | ||
if (arguments.length === 2) { | ||
var args = Array.prototype.slice.call(arguments); | ||
return new CloudFlare(options.token, options.email); | ||
options = { | ||
token: args[0], | ||
email: args[1] | ||
}; | ||
} | ||
assert.equal(typeof(options), 'object'); | ||
assert.equal(typeof(options.token), 'string'); | ||
assert.equal(typeof(options.email), 'string'); | ||
return new CloudFlare(options.token, options.email); | ||
} | ||
}; |
{ | ||
"name": "cloudflare", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "CloudFlare API client", | ||
@@ -5,0 +5,0 @@ "main": "lib/cloudflare.js", |
@@ -58,2 +58,17 @@ var should = require('should'); | ||
}); | ||
describe('#addDomainRecord', function () { | ||
it('should create a new A record', function (done) { | ||
server.expect('rec_new'); | ||
client.addDomainRecord('example.com', { name: 'test', content: '96.126.126.36', type: 'A' }, function (err, res) { | ||
assert.equal(err, null); | ||
res.should.have.property('auto_ttl', 1); | ||
res.should.have.property('name', 'test.example.com'); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
}); |
@@ -1,1 +0,58 @@ | ||
undefined | ||
{ | ||
"response": { | ||
"zone": { | ||
"obj": { | ||
"zone_id": "44", | ||
"user_id": "1", | ||
"zone_name": "example.com", | ||
"display_name": "example.com", | ||
"zone_status": "V", | ||
"zone_mode": "1", | ||
"host_id": "115", | ||
"zone_type": "P", | ||
"host_pubname": "Hosting Provider", | ||
"host_website": "providersite.com", | ||
"vtxt": null, | ||
"fqdns": null, | ||
"step": "4", | ||
"zone_status_class": "status-active", | ||
"zone_status_desc": "CloudFlare powered, this website will be accelerated and protected (<a class=\"modal-link-faq muted\" href=\"#\" onClick=\"cloudFlare.faq('en_US', ['CompleteActive']);return false;\">info</a>)", | ||
"ns_vanity_map": [], | ||
"orig_registrar": null, | ||
"orig_dnshost": null, | ||
"orig_ns_names": null, | ||
"props": { | ||
"dns_cname": 0, | ||
"dns_partner": 1, | ||
"dns_anon_partner": 0, | ||
"pro": 0, | ||
"expired_pro": 0, | ||
"pro_sub": 0, | ||
"ssl": 0, | ||
"expired_ssl": 0, | ||
"expired_rs_pro": 0, | ||
"reseller_pro": 0, | ||
"force_interal": 0, | ||
"ssl_needed": 0, | ||
"alexa_rank": 0 | ||
}, | ||
"confirm_code": { | ||
"zone_deactivate": "c85831462b7bf79d69ad82aba1fa02ce", | ||
"zone_dev_mode1": "fc6cacf3b4ef5bbdb9c0099a9762ae94" | ||
}, | ||
"allow": [ | ||
"analytics", | ||
"threat_control", | ||
"cf_apps", | ||
"dns_editor", | ||
"cf_settings", | ||
"page_rules", | ||
"zone_deactivate", | ||
"zone_dev_mode1" | ||
] | ||
} | ||
} | ||
}, | ||
"result": "success", | ||
"msg": null | ||
} |
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
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
312389
4194
2