From 6a20876b6ed3ecadb998a5fbe3fc83595ffeb1f7 Mon Sep 17 00:00:00 2001 From: BlackYoup Date: Mon, 24 Oct 2016 10:29:59 +0200 Subject: [PATCH] make xml2json an optionnal dependency --- package.json | 3 +-- wadl-client.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 0168f3a..1fd0bdb 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,7 @@ "license": "BSD-3-Clause", "dependencies": { "baconjs": "^0.7.70", - "request": "^2.60.0", - "xml2json": "^0.7.1" + "request": "^2.60.0" }, "devDependencies": { "body": "^5.1.0", diff --git a/wadl-client.js b/wadl-client.js index 5547eb1..7b662a3 100644 --- a/wadl-client.js +++ b/wadl-client.js @@ -2,7 +2,14 @@ var WadlClient = (function() { /* Dependency aliases (to make WadlClient work on both node and browser environments) */ var B = typeof require == "function" && require("baconjs") ? require("baconjs") : Bacon; var request = typeof XMLHttpRequest != "undefined" ? null : require("request"); - var parser = typeof XMLHttpRequest != "undefined" ? null : require("xml2json"); + + // If you want xml2json support, add it as a dependency of your project + var parser; + try{ + parser = typeof XMLHttpRequest != "undefined" ? null : require("xml2json"); + } catch(err){ + parser = null; + } /* Utils */ var Utils = {}; @@ -32,6 +39,10 @@ var WadlClient = (function() { return JSON.parse(typeof response.responseText != "undefined" ? response.responseText : body); } else if(Utils.elem(contentType || "", ["text/xml", "application/rss+xml", "application/rdf+xml", "application/atom+xml"])) { + if(!response.responseXML && !parser){ + throw new Error("Parser is undefined. Please add xml2json as a dependency in your project"); + } + return response.responseXML ? response.responseXML : parser.toJson(body, { object: true, arrayNotation: true