Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wadl-client

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wadl-client - npm Package Compare versions

Comparing version 0.1.5 to 0.2.0-beta.1

4

bower.json
{
"name": "wadl-client",
"main": "wadl-client.js",
"version": "0.1.5",
"version": "0.2.0-beta.1",
"homepage": "https://github.com/rbelouin/wadl-client",

@@ -25,4 +25,4 @@ "authors": [

"dependencies": {
"pacta": "~0.5.0"
"bacon": "~0.7.19"
}
}
{
"name": "wadl-client",
"version": "0.1.5",
"version": "0.2.0-beta.1",
"description": "Generate a Javascript client for a web API providing a WADL description",

@@ -14,4 +14,4 @@ "main": "wadl-client.js",

"dependencies": {
"baconjs": "^0.7.19",
"request": "^2.34.0",
"pacta": "^0.5.0",
"xml2json": "^0.4.0"

@@ -18,0 +18,0 @@ },

@@ -10,43 +10,26 @@ var resources = resources || require("./resources.js");

it("should be able to download resources", function(done) {
var p = client.test.static.get()();
var res = client.test.static.get();
p.map(function(result) {
expect(result).toBe("OK");
res.onValue(function(data) {
expect(data).toBe("OK");
done();
});
p.mapError(function(error) {
expect(error).toBe("OK"); // make test fail if any error
done();
});
});
it("should be able to download resources with query params", function(done) {
var p = client.test.query.get()({
query: {a: 12345}
});
var res = client.test.query.get.withQuery({a: 12345})();
p.map(function(result) {
expect(result).toBe("a=12345");
res.onValue(function(data) {
expect(data).toBe("a=12345");
done();
});
p.mapError(function(error) {
expect(error).toBe("a=12345"); // make test fail if any error
done();
});
});
it("should be able to download resources with path params", function(done) {
var p = client.test.dynamic._.get("12345")();
var res = client.test.dynamic._.get.withParams(["12345"])();
p.map(function(result) {
expect(result).toBe("12345");
res.onValue(function(data) {
expect(data).toBe("12345");
done();
});
p.mapError(function(error) {
expect(error).toBe("12345"); // make test fail if any error
done();
});
});

@@ -62,119 +45,67 @@

var p = client.test.private.get()();
var res = client.test.private.get();
p.map(function(result) {
expect(result).toBe("OK");
res.onValue(function(data) {
expect(data).toBe("OK");
done();
});
p.mapError(function(error) {
expect(error).toBe("OK");
done();
});
});
it("should be able to download resources by giving specific header at sending time", function(done) {
var p = client.test.private.get()({
headers: {
var res = client.test.private.get.withHeaders({
Authorization: "12345"
}
});
})();
p.map(function(result) {
expect(result).toBe("OK");
res.onValue(function(data) {
expect(data).toBe("OK");
done();
});
p.mapError(function(error) {
expect(error).toBe("OK");
done();
});
});
it("should be able to upload resources", function(done) {
var p = client.test.upload.post()("12345");
var res = client.test.upload.post("12345");
p.map(function(result) {
expect(result).toBe("12345");
res.onValue(function(data) {
expect(data).toBe("12345");
done();
});
p.mapError(function(error) {
expect(error).toBe("12345");
done();
});
});
it("should be able to upload resources with a specific header", function(done) {
var p = client.test.private.upload.put()({
data: "12345",
headers: {
Authorization: "12345"
}
});
var res = client.test.private.upload.put.withHeaders({
Authorization: "12345"
})("12345");
p.map(function(result) {
expect(result).toBe("12345");
res.onValue(function(data) {
expect(data).toBe("12345");
done();
});
p.mapError(function(error) {
expect(error).toBe("12345");
done();
});
});
it("should be able to parse JSON resources if parseJSON setting is set to true", function(done) {
var client = WadlClient.buildClient(resources, {
host: "http://localhost:3000",
parseJSON: true
});
it("should be able to parse JSON resources if parse setting is set to true", function(done) {
var res = client.test.json.get.withParsing()();
var p = client.test.json.get()();
p.map(function(result) {
expect(result.a).toBe(1);
expect(result.b).toBe(2);
res.onValue(function(data) {
expect(data.a).toBe(1);
expect(data.b).toBe(2);
done();
});
p.mapError(function(error) {
expect(error.a).toBe(1);
expect(error.b).toBe(2);
done();
});
});
it("should be able to parse JSON resources even if Content-Type header has a charset token", function(done) {
var client = WadlClient.buildClient(resources, {
host: "http://localhost:3000",
parseJSON: true
});
var res = client.test.json2.get.withParsing()();
var p = client.test.json2.get()();
p.map(function(result) {
expect(result.a).toBe(1);
expect(result.b).toBe(2);
res.onValue(function(data) {
expect(data.a).toBe(1);
expect(data.b).toBe(2);
done();
});
p.mapError(function(error) {
expect(error.a).toBe(1);
expect(error.b).toBe(2);
done();
});
});
it("should be able to parse XML resources if parseXML setting is set to true", function(done) {
var client = WadlClient.buildClient(resources, {
host: "http://localhost:3000",
parseXML: true
});
it("should be able to parse XML resources if parse setting is set to true", function(done) {
var res = client.test.xml.get.withParsing()();
var p = client.test.xml.get()();
p.map(function(result) {
if(result.getElementsByTagName) {
var a = result.getElementsByTagName("a");
res.onValue(function(data) {
if(data.getElementsByTagName) {
var a = data.getElementsByTagName("a");
expect(a && a[0]).not.toBeUndefined();

@@ -184,51 +115,26 @@ expect(a[0].textContent).toBe("1");

else {
expect(result.a).not.toBeUndefined();
expect(result.a[0]).toBe(1);
expect(data.a).not.toBeUndefined();
expect(data.a[0]).toBe(1);
}
done();
});
p.mapError(function(error) {
expect(false).toBe(true);
done();
});
});
it("should be able to parse JSON resources if parseJSON setting is set to true, even on error", function(done) {
var client = WadlClient.buildClient(resources, {
host: "http://localhost:3000",
parseJSON: true
});
it("should be able to parse JSON resources if parse setting is set to true, even on error", function(done) {
var res = client.test.json.fail.get.withParsing()();
var p = client.test.json.fail.get()();
p.map(function(result) {
expect(result.a).toBe(1);
expect(result.b).toBe(2);
res.onError(function(data) {
expect(data.a).toBe(1);
expect(data.b).toBe(2);
done();
});
p.mapError(function(error) {
expect(error.a).toBe(1);
expect(error.b).toBe(2);
done();
});
});
it("must not fail when checking Content-Type header", function(done) {
var client = WadlClient.buildClient(resources, {
host: "http://localhost:3000",
parseJSON: true
});
var res = client.test.json3.get.withParsing()();
var p = client.test.json3.get()();
p.map(function() {
res.onValue(function() {
done();
});
p.mapError(function() {
done();
});
});
});
var WadlClient = (function() {
/* Dependency aliases (to make WadlClient work on both node and browser environments) */
var P = typeof require == "function" && require("pacta") ? require("pacta") : Promise;
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");
var WadlClient = {};
/* Utils */
var Utils = {};
var querystring = function(params) {
var pairs = [];
params = params || {};
for(var name in params) {
if(params.hasOwnProperty(name)) {
pairs.push(typeof params[name] != "undefined" ? encodeURIComponent(name) + "=" + encodeURIComponent(params[name]) : encodeURIComponent(name));
}
}
return pairs.length === 0 ? "" : "?" + pairs.join("&");
};
var any = function(values, f) {
Utils.any = function(values, f) {
for(var i in values) {

@@ -34,53 +22,62 @@ var result = f(values[i]);

var nodeResponseHeaderHasValue = function(response, header, values) {
return any(values, function(value) {
return (response.headers[header] || "").indexOf(value) >= 0;
Utils.elem = function(array, values) {
return Utils.any(values, function(value) {
return array.indexOf(value) >= 0;
});
};
var browserResponseHeaderHasValue = function(response, header, values) {
return any(values, function(value) {
return (response.getResponseHeader(header) || "").indexOf(value) >= 0;
});
Utils.parseBody = function(response, body) {
var contentType = typeof XMLHttpRequest != "undefined" ? response.getResponseHeader("Content-Type") : response.headers["content-type"];
if(Utils.elem(contentType || "", ["application/json"])) {
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"])) {
return response.responseXML ? response.responseXML : parser.toJson(body, {
object: true,
arrayNotation: true
});
}
else {
return response.responseText || body;
}
};
Utils.querystring = function(params) {
var pairs = [];
params = params || {};
for(var name in params) {
if(params.hasOwnProperty(name)) {
pairs.push(typeof params[name] != "undefined" ? encodeURIComponent(name) + "=" + encodeURIComponent(params[name]) : encodeURIComponent(name));
}
}
return pairs.length === 0 ? "" : "?" + pairs.join("&");
};
Utils.send = function(sink, data) {
sink(data);
sink(new B.End());
};
var WadlClient = {};
/* Redefine request for node environment */
var sendNodeRequest = function(options) {
var result = new P();
request(options, function(error, response, body) {
if(error) {
result.reject(error);
}
else if(response.statusCode >= 200 && response.statusCode < 300) {
if(options.parseJSON && nodeResponseHeaderHasValue(response, "content-type", ["application/json"])) {
result.resolve(JSON.parse(body));
return B.fromBinder(function(sink) {
request(options, function(error, response, body) {
if(error) {
Utils.send(sink, new B.Error(error));
}
else if(options.parseXML && nodeResponseHeaderHasValue(response, "content-type", ["text/xml", "application/rss+xml", "application/rss+xml", "application/atom+xml"])) {
result.resolve(parser.toJson(body, {
object: true,
arrayNotation: true
}));
else if(response.statusCode >= 200 && response.statusCode < 300) {
Utils.send(sink, options.parse ? Utils.parseBody(response, body) : body);
}
else {
result.resolve(body);
Utils.send(sink, new B.Error(options.parse ? Utils.parseBody(response, body) : body));
}
}
else {
if(options.parseJSON && nodeResponseHeaderHasValue(response, "content-type", ["application/json"])) {
result.reject(JSON.parse(body));
}
else if(options.parseXML && nodeResponseHeaderHasValue(response, "content-type", ["text/xml", "application/rss+xml", "application/rss+xml", "application/atom+xml"])) {
result.reject(parser.toJson(body, {
object: true,
arrayNotation: true
}));
}
else {
result.reject(body);
}
}
});
return function(){};
});
return result;
};

@@ -93,55 +90,36 @@

var result = new P();
var xhr = new XMLHttpRequest();
return B.fromBinder(function(sink) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status >= 200 && xhr.status < 300) {
if(options.parseJSON && browserResponseHeaderHasValue(xhr, "Content-Type", ["application/json"])) {
result.resolve(JSON.parse(xhr.responseText));
xhr.onreadystatechange = function() {
if(xhr.readyState == 4) {
if(xhr.status >= 200 && xhr.status < 300) {
Utils.send(sink, options.parse ? Utils.parseBody(xhr) : xhr.responseText);
}
else if(options.parseXML && browserResponseHeaderHasValue(xhr, "Content-Type", ["text/xml", "application/rss+xml", "application/rss+xml", "application/atom+xml"])) {
result.resolve(xhr.responseXML);
}
else {
result.resolve(xhr.responseText);
Utils.send(sink, new B.Error(options.parse ? Utils.parseBody(xhr) : xhr.responseText));
}
}
else {
if(options.parseJSON && browserResponseHeaderHasValue(xhr, "Content-Type", ["application/json"])) {
result.reject(JSON.parse(xhr.responseText));
}
else if(options.parseXML && browserResponseHeaderHasValue(xhr, "Content-Type", ["text/xml", "application/rss+xml", "application/rss+xml", "application/atom+xml"])) {
result.reject(xhr.responseXML);
}
else {
result.reject(xhr.responseText);
}
}
}
};
};
xhr.open(options.method || "GET", options.uri + querystring(options.qs));
xhr.open(options.method || "GET", options.uri + Utils.querystring(options.qs));
for(var name in options.headers) {
if(options.headers.hasOwnProperty(name)) {
xhr.setRequestHeader(name, options.headers[name]);
for(var name in options.headers) {
if(options.headers.hasOwnProperty(name)) {
xhr.setRequestHeader(name, options.headers[name]);
}
}
}
xhr.send(options.body);
xhr.send(options.body);
return result;
return function(){};
});
};
var sendRequest = function(options) {
var host = options.host || "";
var headers = options.headers || {};
var parseJSON = options.parseJSON || false;
var parseXML = options.parseXML || false;
return function(verb, path_template) {
return function() {
var params = Array.apply(Array, arguments);
var path = path_template.replace(/{[^}]*}/g, function(matched) {
var prepareRequest = function(verb, pathTemplate, defaultSettings) {
return function() {
var send = function(body) {
var host = send.host;
var params = Array.apply(Array, send.params);
var path = pathTemplate.replace(/{[^}]*}/g, function(matched) {
var param = params.shift();

@@ -151,19 +129,45 @@ return typeof param != "undefined" ? param : matched;

return function(data) {
var userOptions = typeof data == "object" && data;
var qs = userOptions && userOptions.query;
host = userOptions ? (userOptions.host || host) : host;
headers = userOptions ? (userOptions.headers || headers) : headers;
return (request ? sendNodeRequest : sendBrowserRequest)({
uri: host + path,
method: verb.toUpperCase(),
headers: send.headers,
qs: send.query,
parse: send.parse,
body: body
});
};
return (request ? sendNodeRequest : sendBrowserRequest)({
uri: host + path,
method: verb.toUpperCase(),
headers: headers,
qs: qs,
body: userOptions ? userOptions.data : data,
parseJSON: parseJSON,
parseXML: parseXML
});
};
send.host = defaultSettings.host || "";
send.withHost = function(host) {
send.host = host;
return send;
};
send.params = [];
send.withParams = function(params) {
send.params = params;
return send;
};
send.headers = defaultSettings.headers || {};
send.withHeaders = function(headers) {
for(var name in headers) {
send.headers[name] = headers[name];
}
return send;
};
send.query = {};
send.withQuery = function(query) {
send.query = query;
return send;
};
send.parse = defaultSettings.parse;
send.withParsing = function(parse) {
send.parse = typeof parse == "undefined" ? true : parse;
return send;
};
return send;
};

@@ -199,3 +203,7 @@ };

var method = methods[i];
node[method.verb == "DELETE" ? "remove" : method.verb.toLowerCase()] = sendRequest(settings)(method.verb, path);
Object.defineProperty(node, method.verb.toLowerCase(), {
get: prepareRequest(method.verb, path, settings || {}),
configurable: true
});
}

@@ -202,0 +210,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc