Browse Source

Merge pull request #17 from brettlangdon/bug/json.big.int

Fix json big int issue
pull/19/head
Brett Langdon 11 years ago
parent
commit
eb4923743f
8 changed files with 63 additions and 15 deletions
  1. +8
    -0
      .travis.yml
  2. +3
    -2
      bin/dogapi
  3. +3
    -2
      lib/api/screenboard.js
  4. +8
    -7
      lib/api/timeboard.js
  5. +3
    -2
      lib/client.js
  6. +7
    -0
      lib/json.js
  7. +5
    -2
      package.json
  8. +26
    -0
      test/json.js

+ 8
- 0
.travis.yml View File

@ -0,0 +1,8 @@
language: node_js
node_js:
- "0.12"
- "0.11"
- "0.10"
- "iojs"
- "iojs-v1.0.4"
- "stable"

+ 3
- 2
bin/dogapi View File

@ -1,5 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
var dogapi = require("../"); var dogapi = require("../");
var json = require("../lib/json");
var minimist = require("minimist"); var minimist = require("minimist");
var rc = require("rc"); var rc = require("rc");
@ -56,13 +57,13 @@ if(command === "now"){
if(subcommand){ if(subcommand){
dogapi[command].handleCli(subcommand, args, function(err, res){ dogapi[command].handleCli(subcommand, args, function(err, res){
if(err){ if(err){
console.error(JSON.stringify(err, null, ' '));
console.error(json.stringify(err, null, ' '));
process.exit(1); process.exit(1);
} else { } else {
if(res === ""){ if(res === ""){
res = "success"; res = "success";
} }
console.log(JSON.stringify(res, null, ' '));
console.log(json.stringify(res, null, ' '));
} }
}); });
} else { } else {


+ 3
- 2
lib/api/screenboard.js View File

@ -1,4 +1,5 @@
var client = require("../client"); var client = require("../client");
var json = require("../json");
var util = require("util"); var util = require("util");
@ -218,11 +219,11 @@ module.exports = {
} else if(subcommand === "create"){ } else if(subcommand === "create"){
var title = args._[4]; var title = args._[4];
var description = args._[5]; var description = args._[5];
var graphs = JSON.parse(args._[6]);
var graphs = json.parse(args._[6]);
var options = {}; var options = {};
if(args["tmpvars"]){ if(args["tmpvars"]){
options.templateVariables = JSON.parse(args["tmpvars"]);
options.templateVariables = json.parse(args["tmpvars"]);
} }
if(args["width"]){ if(args["width"]){
options.width = parseInt(args["width"]); options.width = parseInt(args["width"]);


+ 8
- 7
lib/api/timeboard.js View File

@ -1,4 +1,5 @@
var client = require("../client"); var client = require("../client");
var json = require("../json");
var util = require("util"); var util = require("util");
/*section: timeboard /*section: timeboard
@ -165,7 +166,7 @@ function update(dashId, title, description, graphs, templateVariables, callback)
* ``` * ```
*/ */
function remove(dashId, callback){ function remove(dashId, callback){
client.request("DELETE", util.format("/dash/%s", dashId), params, callback);
client.request("DELETE", util.format("/dash/%s", dashId), {}, callback);
} }
/*section: timeboard /*section: timeboard
@ -186,7 +187,7 @@ function remove(dashId, callback){
* ``` * ```
*/ */
function getAll(callback){ function getAll(callback){
client.request("GET", "/dash", params, callback);
client.request("GET", "/dash", {}, callback);
} }
/*section: timeboard /*section: timeboard
@ -208,7 +209,7 @@ function getAll(callback){
* ``` * ```
*/ */
function get(dashId, callback){ function get(dashId, callback){
client.request("GET", util.format("/dash/%s", dashId), params, callback);
client.request("GET", util.format("/dash/%s", dashId), {}, callback);
} }
module.exports = { module.exports = {
@ -249,10 +250,10 @@ module.exports = {
} else if(subcommand === "create"){ } else if(subcommand === "create"){
var title = args._[4]; var title = args._[4];
var description = args._[5]; var description = args._[5];
var graphs = JSON.parse(args._[6]);
var graphs = json.parse(args._[6]);
var templateVariables = []; var templateVariables = [];
if(args["tmpvars"]){ if(args["tmpvars"]){
templateVariables = JSON.parse(args["tmpvars"]);
templateVariables = json.parse(args["tmpvars"]);
} }
create(title, description, graphs, templateVariables, callback); create(title, description, graphs, templateVariables, callback);
@ -260,10 +261,10 @@ module.exports = {
var dashId = parseInt(args._[4]); var dashId = parseInt(args._[4]);
var title = args._[5]; var title = args._[5];
var description = args._[6]; var description = args._[6];
var graphs = JSON.parse(args._[7]);
var graphs = json.parse(args._[7]);
var templateVariables = []; var templateVariables = [];
if(args["tmpvars"]){ if(args["tmpvars"]){
templateVariables = JSON.parse(args["tmpvars"]);
templateVariables = json.parse(args["tmpvars"]);
} }
update(dashId, title, description, graphs, templateVariables, callback); update(dashId, title, description, graphs, templateVariables, callback);


+ 3
- 2
lib/client.js View File

@ -1,5 +1,6 @@
var extend = require("extend"); var extend = require("extend");
var https = require("https"); var https = require("https");
var json = require("./json");
var url = require("url"); var url = require("url");
var util = require("util"); var util = require("util");
@ -50,7 +51,7 @@ client.prototype.request = function(method, path, params, callback){
} }
params = params || {}; params = params || {};
var body = (typeof params["body"] === "object")? JSON.stringify(params["body"]) : params["body"];
var body = (typeof params["body"] === "object")? json.stringify(params["body"]) : params["body"];
var query = { var query = {
"api_key": this.api_key, "api_key": this.api_key,
"application_key": this.app_key, "application_key": this.app_key,
@ -97,7 +98,7 @@ client.prototype.request = function(method, path, params, callback){
res.on("end", function(){ res.on("end", function(){
var error = null; var error = null;
try{ data = JSON.parse(data); }catch(e){}
try{ data = json.parse(data); }catch(e){}
if(data["errors"]){ if(data["errors"]){
error = data["errors"]; error = data["errors"];
data = null; data = null;


+ 7
- 0
lib/json.js View File

@ -0,0 +1,7 @@
var JSONBig = require("json-bigint");
module.exports = {
stringify: JSONBig.stringify,
parse: JSONBig.parse
};

+ 5
- 2
package.json View File

@ -4,7 +4,7 @@
"description": "Datadog API Node.JS Client", "description": "Datadog API Node.JS Client",
"main": "lib/index.js", "main": "lib/index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "./node_modules/.bin/mocha ./test",
"docs": "node ./docs/create.js > index.html" "docs": "node ./docs/create.js > index.html"
}, },
"bin": { "bin": {
@ -33,13 +33,16 @@
"gitHead": "f388635a5ab4f4da25702dc0999385d437bdf2bc", "gitHead": "f388635a5ab4f4da25702dc0999385d437bdf2bc",
"dependencies": { "dependencies": {
"extend": "^2.0.0", "extend": "^2.0.0",
"json-bigint": "^0.1.4",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"rc": "^1.0.0" "rc": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"bignumber.js": "^2.0.7",
"docast": "^0.1.1", "docast": "^0.1.1",
"glob": "^5.0.3", "glob": "^5.0.3",
"js-yaml": "^3.2.7", "js-yaml": "^3.2.7",
"marked": "^0.3.3"
"marked": "^0.3.3",
"mocha": "^2.2.5"
} }
} }

+ 26
- 0
test/json.js View File

@ -0,0 +1,26 @@
var assert = require("assert");
var BigNumber = require("bignumber.js");
var json = require("../lib/json");
describe("json", function(){
describe("#parse()", function(){
it("should properly parse big integers", function(){
// DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16
var data = "{\"id\": 2868860079149422351}";
var parsed = json.parse(data);
// `parsed.id` is an instance of `BigNumber`
assert.equal(parsed.id.toString(), "2868860079149422351");
});
});
describe("#stringify()", function(){
it("should properly parse big integers", function(){
// DEV: This test case is from: https://github.com/brettlangdon/node-dogapi/issues/16
var data = {"id": new BigNumber('2868860079149422351')};
var stringified = json.stringify(data);
// Yeah, it ends up being a string and not an int, but mostly we
// want to make sure it doesn't throw an error or provide the wrong number
assert.equal(stringified, "{\"id\":\"2868860079149422351\"}");
});
});
});

Loading…
Cancel
Save