Socket
Socket
Sign inDemoInstall

mappersmith

Package Overview
Dependencies
Maintainers
3
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mappersmith - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

node.js

110

build/mappersmith.js

@@ -125,3 +125,3 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Mappersmith=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

var JQueryGateway = module.exports = CreateGateway({
var JQueryGateway = CreateGateway({

@@ -137,11 +137,4 @@ init: function() {

jQueryAjax: function(config) {
jQuery.ajax(Utils.extend({url: this.url}, config)).
done(function() { this.successCallback.apply(this, arguments) }.bind(this)).
fail(function() { this.failCallback.apply(this, arguments) }.bind(this)).
always(function() { this.completeCallback.apply(this, arguments) }.bind(this));
},
get: function() {
this.jQueryAjax(this.opts);
this._jQueryAjax(this.opts);
return this;

@@ -157,3 +150,3 @@ },

},
patch: function() {

@@ -178,4 +171,11 @@ return this._performRequest('PATCH');

var defaults = {type: requestMethod, data: Utils.params(this.body)};
this.jQueryAjax(Utils.extend(defaults, this.opts));
this._jQueryAjax(Utils.extend(defaults, this.opts));
return this;
},
_jQueryAjax: function(config) {
jQuery.ajax(Utils.extend({url: this.url}, config)).
done(function() { this.successCallback.apply(this, arguments) }.bind(this)).
fail(function() { this.failCallback.apply(this, arguments) }.bind(this)).
always(function() { this.completeCallback.apply(this, arguments) }.bind(this));
}

@@ -185,2 +185,4 @@

module.exports = JQueryGateway;
},{"../create-gateway":2,"../utils":8}],6:[function(require,module,exports){

@@ -190,44 +192,7 @@ var Utils = require('../utils');

var VanillaGateway = module.exports = CreateGateway({
var VanillaGateway = CreateGateway({
configureCallbacks: function(request) {
request.onload = function() {
var data = null;
try {
if (request.status >= 200 && request.status < 400) {
if (request.getResponseHeader('Content-Type') === 'application/json') {
data = JSON.parse(request.responseText);
} else {
data = request.responseText;
}
this.successCallback(data);
} else {
this.failCallback(request);
}
} catch(e) {
this.failCallback(request);
} finally {
this.completeCallback(data, request);
}
}.bind(this);
request.onerror = function() {
this.failCallback.apply(this, arguments);
this.completeCallback.apply(this, arguments);
}.bind(this);
if (this.opts.configure) {
this.opts.configure(request);
}
},
get: function() {
var request = new XMLHttpRequest();
this.configureCallbacks(request);
this._configureCallbacks(request);
request.open('GET', this.url, true);

@@ -257,3 +222,3 @@ request.send();

var request = new XMLHttpRequest();
this.configureCallbacks(request);
this._configureCallbacks(request);

@@ -276,2 +241,39 @@ if (emulateHTTP) {

request.send.apply(request, args);
},
_configureCallbacks: function(request) {
request.onload = function() {
var data = null;
try {
if (request.status >= 200 && request.status < 400) {
if (request.getResponseHeader('Content-Type') === 'application/json') {
data = JSON.parse(request.responseText);
} else {
data = request.responseText;
}
this.successCallback(data);
} else {
this.failCallback(request);
}
} catch(e) {
this.failCallback(request);
} finally {
this.completeCallback(data, request);
}
}.bind(this);
request.onerror = function() {
this.failCallback.apply(this, arguments);
this.completeCallback.apply(this, arguments);
}.bind(this);
if (this.opts.configure) {
this.opts.configure(request);
}
}

@@ -281,2 +283,4 @@

module.exports = VanillaGateway;
},{"../create-gateway":2,"../utils":8}],7:[function(require,module,exports){

@@ -410,3 +414,3 @@ var Utils = require('./utils');

},{"./utils":8}],8:[function(require,module,exports){
var Utils = module.exports = {
var Utils = {
r20: /%20/g,

@@ -483,3 +487,5 @@ noop: function() {},

module.exports = Utils;
},{}]},{},[1])(1)
});
/*! Mappersmith - https://github.com/tulios/mappersmith - Generated by browserify */
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Mappersmith=e()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){module.exports={Utils:require("./src/utils"),Gateway:require("./src/gateway"),Mapper:require("./src/mapper"),VanillaGateway:require("./src/gateway/vanilla-gateway"),JQueryGateway:require("./src/gateway/jquery-gateway"),forge:require("./src/forge"),createGateway:require("./src/create-gateway")}},{"./src/create-gateway":2,"./src/forge":3,"./src/gateway":4,"./src/gateway/jquery-gateway":5,"./src/gateway/vanilla-gateway":6,"./src/mapper":7,"./src/utils":8}],2:[function(require,module){var Utils=require("./utils"),Gateway=require("./gateway");module.exports=function(methods){var newGateway=function(){return this.init&&this.init(),Gateway.apply(this,arguments)};return newGateway.prototype=Utils.extend({},Gateway.prototype,methods),newGateway}},{"./gateway":4,"./utils":8}],3:[function(require,module){var Mapper=require("./mapper"),VanillaGateway=require("./gateway/vanilla-gateway");module.exports=function(manifest,gateway,bodyAttr){return new Mapper(manifest,gateway||VanillaGateway,bodyAttr||"body").build()}},{"./gateway/vanilla-gateway":6,"./mapper":7}],4:[function(require,module){var Utils=require("./utils"),Gateway=function(args){this.url=args.url,this.method=args.method,this.processor=args.processor,this.params=args.params||{},this.body=args.body,this.opts=args.opts||{},this.successCallback=Utils.noop,this.failCallback=Utils.noop,this.completeCallback=Utils.noop};Gateway.prototype={call:function(){return this[this.method].apply(this,arguments),this},success:function(callback){return this.successCallback=void 0!==this.processor?function(data){callback(this.processor(data))}:callback,this},fail:function(callback){return this.failCallback=callback,this},complete:function(callback){return this.completeCallback=callback,this},shouldEmulateHTTP:function(method){return!(!this.opts.emulateHTTP||!/^(delete|put|patch)/i.test(method))},get:function(){throw new Utils.Exception("Gateway#get not implemented")},post:function(){throw new Utils.Exception("Gateway#post not implemented")},put:function(){throw new Utils.Exception("Gateway#put not implemented")},delete:function(){throw new Utils.Exception("Gateway#delete not implemented")},patch:function(){throw new Utils.Exception("Gateway#patch not implemented")}},module.exports=Gateway},{"./utils":8}],5:[function(require,module){{var Utils=require("../utils"),CreateGateway=require("../create-gateway");module.exports=CreateGateway({init:function(){if(void 0===window.jQuery)throw new Utils.Exception("JQueryGateway requires jQuery but it was not found! Change the gateway implementation or add jQuery on the page")},jQueryAjax:function(config){jQuery.ajax(Utils.extend({url:this.url},config)).done(function(){this.successCallback.apply(this,arguments)}.bind(this)).fail(function(){this.failCallback.apply(this,arguments)}.bind(this)).always(function(){this.completeCallback.apply(this,arguments)}.bind(this))},get:function(){return this.jQueryAjax(this.opts),this},post:function(){return this._performRequest("POST")},put:function(){return this._performRequest("PUT")},patch:function(){return this._performRequest("PATCH")},delete:function(){return this._performRequest("DELETE")},_performRequest:function(method){var requestMethod=method;this.shouldEmulateHTTP(method)&&(requestMethod="POST",this.body=this.body||{},"object"==typeof this.body&&(this.body._method=method),this.opts.headers=Utils.extend(this.opts.header,{"X-HTTP-Method-Override":method}));var defaults={type:requestMethod,data:Utils.params(this.body)};return this.jQueryAjax(Utils.extend(defaults,this.opts)),this}})}},{"../create-gateway":2,"../utils":8}],6:[function(require,module){{var Utils=require("../utils"),CreateGateway=require("../create-gateway");module.exports=CreateGateway({configureCallbacks:function(request){request.onload=function(){var data=null;try{request.status>=200&&request.status<400?(data="application/json"===request.getResponseHeader("Content-Type")?JSON.parse(request.responseText):request.responseText,this.successCallback(data)):this.failCallback(request)}catch(e){this.failCallback(request)}finally{this.completeCallback(data,request)}}.bind(this),request.onerror=function(){this.failCallback.apply(this,arguments),this.completeCallback.apply(this,arguments)}.bind(this),this.opts.configure&&this.opts.configure(request)},get:function(){var request=new XMLHttpRequest;this.configureCallbacks(request),request.open("GET",this.url,!0),request.send()},post:function(){this._performRequest("POST")},put:function(){this._performRequest("PUT")},patch:function(){this._performRequest("PATCH")},delete:function(){this._performRequest("DELETE")},_performRequest:function(method){var emulateHTTP=this.shouldEmulateHTTP(method),requestMethod=method,request=new XMLHttpRequest;this.configureCallbacks(request),emulateHTTP&&(this.body=this.body||{},"object"==typeof this.body&&(this.body._method=method),requestMethod="POST"),request.open(requestMethod,this.url,!0),emulateHTTP&&request.setRequestHeader("X-HTTP-Method-Override",method),request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");var args=[];void 0!==this.body&&args.push(Utils.params(this.body)),request.send.apply(request,args)}})}},{"../create-gateway":2,"../utils":8}],7:[function(require,module){var Utils=require("./utils"),Mapper=function(manifest,Gateway,bodyAttr){this.manifest=manifest,this.host=this.manifest.host,this.rules=this.manifest.rules||[],this.Gateway=Gateway,this.bodyAttr=bodyAttr};Mapper.prototype={build:function(){return Object.keys(this.manifest.resources||{}).map(function(name){return this.buildResource(name)}.bind(this)).reduce(function(context,resource){return context[resource.name]=resource.methods,context},{})},buildResource:function(resourceName){var methods=this.manifest.resources[resourceName];return Object.keys(methods).reduce(function(context,methodName){var descriptor=methods[methodName];if("string"==typeof descriptor){var compactDefinitionMethod=descriptor.match(/^(get|post|delete|put|patch):(.*)/);descriptor=null!=compactDefinitionMethod?{method:compactDefinitionMethod[1],path:compactDefinitionMethod[2]}:{method:"get",path:descriptor}}return descriptor.method=(descriptor.method||"get").toLowerCase(),context.methods[methodName]=this.newGatewayRequest(descriptor),context}.bind(this),{name:resourceName,methods:{}})},urlFor:function(path,urlParams){var params=Utils.extend({},urlParams),normalizedPath=/^\//.test(path)?path:"/"+path,host=this.host.replace(/\/$/,"");delete params[this.bodyAttr],Object.keys(params).forEach(function(key){var value=params[key],pattern="{"+key+"}";new RegExp(pattern).test(normalizedPath)&&(normalizedPath=normalizedPath.replace("{"+key+"}",value),delete params[key])});var paramsString=Utils.params(params);return 0!==paramsString.length&&(paramsString="?"+paramsString),host+normalizedPath+paramsString},newGatewayRequest:function(descriptor){var rules=this.rules.filter(function(rule){return void 0===rule.match||rule.match.test(descriptor.path)}).reduce(function(context,rule){var mergedGateway=Utils.extend(context.gateway,rule.values.gateway);return context=Utils.extend(context,rule.values),context.gateway=mergedGateway,context},{});return function(params,callback,opts){"function"==typeof params&&(opts=callback,callback=params,params=void 0),descriptor.params&&(params=Utils.extend({},descriptor.params,params)),opts=Utils.extend({},opts,rules.gateway),Utils.isObjEmpty(opts)&&(opts=void 0);var body=(params||{})[this.bodyAttr],gatewayOpts=Utils.extend({},{url:this.urlFor(descriptor.path,params),method:descriptor.method,processor:descriptor.processor||rules.processor,params:params,body:body,opts:opts});return new this.Gateway(gatewayOpts).success(callback).call()}.bind(this)}},module.exports=Mapper},{"./utils":8}],8:[function(require,module){var Utils=module.exports={r20:/%20/g,noop:function(){},isObjEmpty:function(obj){for(var key in obj)if(obj.hasOwnProperty(key))return!1;return!0},extend:function(out){out=out||{};for(var i=1;i<arguments.length;i++)if(arguments[i])for(var key in arguments[i])arguments[i].hasOwnProperty(key)&&void 0!==arguments[i][key]&&(out[key]=arguments[i][key]);return out},params:function(entry){if("object"!=typeof entry)return entry;var validKeys=function(entry){return Object.keys(entry).filter(function(key){return void 0!==entry[key]&&null!==entry[key]})},buildRecursive=function(key,value,suffix){suffix=suffix||"";var isArray=Array.isArray(value),isObject="object"==typeof value;return isArray||isObject?isArray?value.map(function(v){return buildRecursive(key,v,suffix+"[]")}).join("&"):validKeys(value).map(function(k){return buildRecursive(key,value[k],suffix+"["+k+"]")}).join("&"):encodeURIComponent(key+suffix)+"="+encodeURIComponent(value)};return validKeys(entry).map(function(key){return buildRecursive(key,entry[key])}).join("&").replace(Utils.r20,"+")},Exception:function(message){this.message=message,this.toString=function(){return"[Mappersmith] "+this.message}}}},{}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Mappersmith=e()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){module.exports={Utils:require("./src/utils"),Gateway:require("./src/gateway"),Mapper:require("./src/mapper"),VanillaGateway:require("./src/gateway/vanilla-gateway"),JQueryGateway:require("./src/gateway/jquery-gateway"),forge:require("./src/forge"),createGateway:require("./src/create-gateway")}},{"./src/create-gateway":2,"./src/forge":3,"./src/gateway":4,"./src/gateway/jquery-gateway":5,"./src/gateway/vanilla-gateway":6,"./src/mapper":7,"./src/utils":8}],2:[function(require,module){var Utils=require("./utils"),Gateway=require("./gateway");module.exports=function(methods){var newGateway=function(){return this.init&&this.init(),Gateway.apply(this,arguments)};return newGateway.prototype=Utils.extend({},Gateway.prototype,methods),newGateway}},{"./gateway":4,"./utils":8}],3:[function(require,module){var Mapper=require("./mapper"),VanillaGateway=require("./gateway/vanilla-gateway");module.exports=function(manifest,gateway,bodyAttr){return new Mapper(manifest,gateway||VanillaGateway,bodyAttr||"body").build()}},{"./gateway/vanilla-gateway":6,"./mapper":7}],4:[function(require,module){var Utils=require("./utils"),Gateway=function(args){this.url=args.url,this.method=args.method,this.processor=args.processor,this.params=args.params||{},this.body=args.body,this.opts=args.opts||{},this.successCallback=Utils.noop,this.failCallback=Utils.noop,this.completeCallback=Utils.noop};Gateway.prototype={call:function(){return this[this.method].apply(this,arguments),this},success:function(callback){return this.successCallback=void 0!==this.processor?function(data){callback(this.processor(data))}:callback,this},fail:function(callback){return this.failCallback=callback,this},complete:function(callback){return this.completeCallback=callback,this},shouldEmulateHTTP:function(method){return!(!this.opts.emulateHTTP||!/^(delete|put|patch)/i.test(method))},get:function(){throw new Utils.Exception("Gateway#get not implemented")},post:function(){throw new Utils.Exception("Gateway#post not implemented")},put:function(){throw new Utils.Exception("Gateway#put not implemented")},delete:function(){throw new Utils.Exception("Gateway#delete not implemented")},patch:function(){throw new Utils.Exception("Gateway#patch not implemented")}},module.exports=Gateway},{"./utils":8}],5:[function(require,module){var Utils=require("../utils"),CreateGateway=require("../create-gateway"),JQueryGateway=CreateGateway({init:function(){if(void 0===window.jQuery)throw new Utils.Exception("JQueryGateway requires jQuery but it was not found! Change the gateway implementation or add jQuery on the page")},get:function(){return this._jQueryAjax(this.opts),this},post:function(){return this._performRequest("POST")},put:function(){return this._performRequest("PUT")},patch:function(){return this._performRequest("PATCH")},delete:function(){return this._performRequest("DELETE")},_performRequest:function(method){var requestMethod=method;this.shouldEmulateHTTP(method)&&(requestMethod="POST",this.body=this.body||{},"object"==typeof this.body&&(this.body._method=method),this.opts.headers=Utils.extend(this.opts.header,{"X-HTTP-Method-Override":method}));var defaults={type:requestMethod,data:Utils.params(this.body)};return this._jQueryAjax(Utils.extend(defaults,this.opts)),this},_jQueryAjax:function(config){jQuery.ajax(Utils.extend({url:this.url},config)).done(function(){this.successCallback.apply(this,arguments)}.bind(this)).fail(function(){this.failCallback.apply(this,arguments)}.bind(this)).always(function(){this.completeCallback.apply(this,arguments)}.bind(this))}});module.exports=JQueryGateway},{"../create-gateway":2,"../utils":8}],6:[function(require,module){var Utils=require("../utils"),CreateGateway=require("../create-gateway"),VanillaGateway=CreateGateway({get:function(){var request=new XMLHttpRequest;this._configureCallbacks(request),request.open("GET",this.url,!0),request.send()},post:function(){this._performRequest("POST")},put:function(){this._performRequest("PUT")},patch:function(){this._performRequest("PATCH")},delete:function(){this._performRequest("DELETE")},_performRequest:function(method){var emulateHTTP=this.shouldEmulateHTTP(method),requestMethod=method,request=new XMLHttpRequest;this._configureCallbacks(request),emulateHTTP&&(this.body=this.body||{},"object"==typeof this.body&&(this.body._method=method),requestMethod="POST"),request.open(requestMethod,this.url,!0),emulateHTTP&&request.setRequestHeader("X-HTTP-Method-Override",method),request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");var args=[];void 0!==this.body&&args.push(Utils.params(this.body)),request.send.apply(request,args)},_configureCallbacks:function(request){request.onload=function(){var data=null;try{request.status>=200&&request.status<400?(data="application/json"===request.getResponseHeader("Content-Type")?JSON.parse(request.responseText):request.responseText,this.successCallback(data)):this.failCallback(request)}catch(e){this.failCallback(request)}finally{this.completeCallback(data,request)}}.bind(this),request.onerror=function(){this.failCallback.apply(this,arguments),this.completeCallback.apply(this,arguments)}.bind(this),this.opts.configure&&this.opts.configure(request)}});module.exports=VanillaGateway},{"../create-gateway":2,"../utils":8}],7:[function(require,module){var Utils=require("./utils"),Mapper=function(manifest,Gateway,bodyAttr){this.manifest=manifest,this.host=this.manifest.host,this.rules=this.manifest.rules||[],this.Gateway=Gateway,this.bodyAttr=bodyAttr};Mapper.prototype={build:function(){return Object.keys(this.manifest.resources||{}).map(function(name){return this.buildResource(name)}.bind(this)).reduce(function(context,resource){return context[resource.name]=resource.methods,context},{})},buildResource:function(resourceName){var methods=this.manifest.resources[resourceName];return Object.keys(methods).reduce(function(context,methodName){var descriptor=methods[methodName];if("string"==typeof descriptor){var compactDefinitionMethod=descriptor.match(/^(get|post|delete|put|patch):(.*)/);descriptor=null!=compactDefinitionMethod?{method:compactDefinitionMethod[1],path:compactDefinitionMethod[2]}:{method:"get",path:descriptor}}return descriptor.method=(descriptor.method||"get").toLowerCase(),context.methods[methodName]=this.newGatewayRequest(descriptor),context}.bind(this),{name:resourceName,methods:{}})},urlFor:function(path,urlParams){var params=Utils.extend({},urlParams),normalizedPath=/^\//.test(path)?path:"/"+path,host=this.host.replace(/\/$/,"");delete params[this.bodyAttr],Object.keys(params).forEach(function(key){var value=params[key],pattern="{"+key+"}";new RegExp(pattern).test(normalizedPath)&&(normalizedPath=normalizedPath.replace("{"+key+"}",value),delete params[key])});var paramsString=Utils.params(params);return 0!==paramsString.length&&(paramsString="?"+paramsString),host+normalizedPath+paramsString},newGatewayRequest:function(descriptor){var rules=this.rules.filter(function(rule){return void 0===rule.match||rule.match.test(descriptor.path)}).reduce(function(context,rule){var mergedGateway=Utils.extend(context.gateway,rule.values.gateway);return context=Utils.extend(context,rule.values),context.gateway=mergedGateway,context},{});return function(params,callback,opts){"function"==typeof params&&(opts=callback,callback=params,params=void 0),descriptor.params&&(params=Utils.extend({},descriptor.params,params)),opts=Utils.extend({},opts,rules.gateway),Utils.isObjEmpty(opts)&&(opts=void 0);var body=(params||{})[this.bodyAttr],gatewayOpts=Utils.extend({},{url:this.urlFor(descriptor.path,params),method:descriptor.method,processor:descriptor.processor||rules.processor,params:params,body:body,opts:opts});return new this.Gateway(gatewayOpts).success(callback).call()}.bind(this)}},module.exports=Mapper},{"./utils":8}],8:[function(require,module){var Utils={r20:/%20/g,noop:function(){},isObjEmpty:function(obj){for(var key in obj)if(obj.hasOwnProperty(key))return!1;return!0},extend:function(out){out=out||{};for(var i=1;i<arguments.length;i++)if(arguments[i])for(var key in arguments[i])arguments[i].hasOwnProperty(key)&&void 0!==arguments[i][key]&&(out[key]=arguments[i][key]);return out},params:function(entry){if("object"!=typeof entry)return entry;var validKeys=function(entry){return Object.keys(entry).filter(function(key){return void 0!==entry[key]&&null!==entry[key]})},buildRecursive=function(key,value,suffix){suffix=suffix||"";var isArray=Array.isArray(value),isObject="object"==typeof value;return isArray||isObject?isArray?value.map(function(v){return buildRecursive(key,v,suffix+"[]")}).join("&"):validKeys(value).map(function(k){return buildRecursive(key,value[k],suffix+"["+k+"]")}).join("&"):encodeURIComponent(key+suffix)+"="+encodeURIComponent(value)};return validKeys(entry).map(function(key){return buildRecursive(key,entry[key])}).join("&").replace(Utils.r20,"+")},Exception:function(message){this.message=message,this.toString=function(){return"[Mappersmith] "+this.message}}};module.exports=Utils},{}]},{},[1])(1)});
{
"name": "mappersmith",
"version": "0.3.0",
"version": "0.4.0",
"description": "It is a lightweight rest client mapper for javascript",

@@ -14,3 +14,6 @@ "author": "Tulio Ornelas <ornelas.tulio@gmail.com>",

"build-test": "./node_modules/.bin/browserify index.js -t rewireify -s Mappersmith -o build/mappersmith.test.js",
"release": "npm run build && ./node_modules/.bin/uglifyjs build/mappersmith.js -o build/mappersmith.min.js -c --screw-ie8 --preamble '/*! Mappersmith - https://github.com/tulios/mappersmith - Generated by browserify */'"
"release": "npm run build && ./node_modules/.bin/uglifyjs build/mappersmith.js -o build/mappersmith.min.js -c --screw-ie8 --preamble '/*! Mappersmith - https://github.com/tulios/mappersmith - Generated by browserify */'",
"test": "./node_modules/.bin/mocha test/node/*-test.js",
"debug-test": "./node_modules/.bin/mocha --debug-brk test/node/*-test.js",
"node-inspector": "./node_modules/.bin/node-inspector --web-port 7777"
},

@@ -25,2 +28,3 @@ "repository": {

"browser",
"server",
"data-mapper"

@@ -37,6 +41,10 @@ ],

"mocha": "^2.0.1",
"nock": "^0.58.0",
"node-inspector": "^0.8.3",
"rewireify": "0.0.13",
"shared-examples-for": "^0.1.3",
"sinon": "^1.12.2",
"sinon-chai": "^2.6.0",
"uglify-js": "^2.4.16"
}
}
# Mappersmith
**Mappersmith** is a lightweight, dependency-free, rest client mapper for javascript. It helps you map your API to use at the client, giving you all the flexibility you want to customize requests or write your own gateways.
**Mappersmith** is a lightweight, dependency-free, rest client mapper for javascript. It helps you map your API to use at the client and/or server, giving you all the flexibility you want to customize requests or write your own gateways.

@@ -29,4 +29,11 @@ ## Install

npm run build
npm run release # for minified version
```
## Requiring in Node.js
```javascript
var Mappersmith = require('mappersmith/node');
```
## Usage

@@ -182,3 +189,3 @@

**Mappersmith** allows you to customize the transport layer. You can use the default `Mappersmith.VanillaGateway`, the included `Mappersmith.JQueryGateway` or write your own version.
**Mappersmith** allows you to customize the transport layer. There are gateways for browser and server. You can use the default `Mappersmith.VanillaGateway` (client only), the included `Mappersmith.JQueryGateway` (client only), `NodeVanillaGateway` (server only) or write your own version. Check the list of [available gateways](#gateway-implementations) at the bottom of the readme.

@@ -278,3 +285,3 @@ #### How to write one?

## Gateway Implementations
## <a name="gateway-implementations"></a> Gateway Implementations

@@ -285,5 +292,5 @@ The gateways listed here are available through the `Mappersmith` namespace.

The default gateway - it uses plain `XMLHttpRequest`. Accepts a `configure` callback that allows you to change the request object before it is used.
__Client Only__. The default gateway - it uses plain `XMLHttpRequest`. Accepts a `configure` callback that allows you to change the request object before it is used.
#### Available methods:
Available methods:

@@ -296,3 +303,3 @@ - :ok: GET

#### Available options:
Available options:

@@ -304,5 +311,5 @@ - emulateHTTP: sends request as POST with `_method` in the body and `X-HTTP-Method-Override` header, both with requested method as value. (default `false`)

It uses `$.ajax` and accepts an object that will be merged with `defaults`. It doesn't include **jquery**, so you will need to include that in your page.
__Client Only__. It uses `$.ajax` and accepts an object that will be merged with `defaults`. It doesn't include **jquery**, so you will need to include that in your page.
#### Available methods:
Available methods:

@@ -315,12 +322,46 @@ - :ok: GET

#### Available options:
Available options:
- emulateHTTP: sends request as POST with `_method` in the body and `X-HTTP-Method-Override` header, both with request method as value. (default `false`)
### NodeVanillaGateway
__Server Only__. It uses the module `http` and accepts an object that will be merged with `defaults`.
How to access this gateway?
```javascript
var Mappersmith = require('mappersmith/node');
Mappersmith.node.NodeVanillaGateway;
```
Available methods:
- :ok: GET
- :ok: POST
- :ok: PUT
- :ok: DELETE
- :ok: PATCH
Available options:
- emulateHTTP: sends request as POST with `_method` in the body and `X-HTTP-Method-Override` header, both with request method as value. (default `false`)
## Extras
For gateways with transparent cache functionalities and different cache stores, take a look at:
[https://github.com/tulios/mappersmith-cached-gateway](https://github.com/tulios/mappersmith-cached-gateway)
## Tests
### Client
1. Build the source (`npm run build-test`)
2. Open test.html
### Server
1. `npm run test`
## Compile and release

@@ -327,0 +368,0 @@

var Utils = require('../utils');
var CreateGateway = require('../create-gateway');
var JQueryGateway = module.exports = CreateGateway({
var JQueryGateway = CreateGateway({

@@ -15,11 +15,4 @@ init: function() {

jQueryAjax: function(config) {
jQuery.ajax(Utils.extend({url: this.url}, config)).
done(function() { this.successCallback.apply(this, arguments) }.bind(this)).
fail(function() { this.failCallback.apply(this, arguments) }.bind(this)).
always(function() { this.completeCallback.apply(this, arguments) }.bind(this));
},
get: function() {
this.jQueryAjax(this.opts);
this._jQueryAjax(this.opts);
return this;

@@ -35,3 +28,3 @@ },

},
patch: function() {

@@ -56,6 +49,15 @@ return this._performRequest('PATCH');

var defaults = {type: requestMethod, data: Utils.params(this.body)};
this.jQueryAjax(Utils.extend(defaults, this.opts));
this._jQueryAjax(Utils.extend(defaults, this.opts));
return this;
},
_jQueryAjax: function(config) {
jQuery.ajax(Utils.extend({url: this.url}, config)).
done(function() { this.successCallback.apply(this, arguments) }.bind(this)).
fail(function() { this.failCallback.apply(this, arguments) }.bind(this)).
always(function() { this.completeCallback.apply(this, arguments) }.bind(this));
}
});
module.exports = JQueryGateway;
var Utils = require('../utils');
var CreateGateway = require('../create-gateway');
var VanillaGateway = module.exports = CreateGateway({
var VanillaGateway = CreateGateway({
configureCallbacks: function(request) {
request.onload = function() {
var data = null;
try {
if (request.status >= 200 && request.status < 400) {
if (request.getResponseHeader('Content-Type') === 'application/json') {
data = JSON.parse(request.responseText);
} else {
data = request.responseText;
}
this.successCallback(data);
} else {
this.failCallback(request);
}
} catch(e) {
this.failCallback(request);
} finally {
this.completeCallback(data, request);
}
}.bind(this);
request.onerror = function() {
this.failCallback.apply(this, arguments);
this.completeCallback.apply(this, arguments);
}.bind(this);
if (this.opts.configure) {
this.opts.configure(request);
}
},
get: function() {
var request = new XMLHttpRequest();
this.configureCallbacks(request);
this._configureCallbacks(request);
request.open('GET', this.url, true);

@@ -70,3 +33,3 @@ request.send();

var request = new XMLHttpRequest();
this.configureCallbacks(request);
this._configureCallbacks(request);

@@ -89,4 +52,43 @@ if (emulateHTTP) {

request.send.apply(request, args);
},
_configureCallbacks: function(request) {
request.onload = function() {
var data = null;
try {
if (request.status >= 200 && request.status < 400) {
if (request.getResponseHeader('Content-Type') === 'application/json') {
data = JSON.parse(request.responseText);
} else {
data = request.responseText;
}
this.successCallback(data);
} else {
this.failCallback(request);
}
} catch(e) {
this.failCallback(request);
} finally {
this.completeCallback(data, request);
}
}.bind(this);
request.onerror = function() {
this.failCallback.apply(this, arguments);
this.completeCallback.apply(this, arguments);
}.bind(this);
if (this.opts.configure) {
this.opts.configure(request);
}
}
});
module.exports = VanillaGateway;

@@ -1,2 +0,2 @@

var Utils = module.exports = {
var Utils = {
r20: /%20/g,

@@ -72,1 +72,3 @@ noop: function() {},

}
module.exports = Utils;

@@ -31,2 +31,6 @@ var expect = chai.expect;

});
it('don\'t expose "node" attribute', function() {
expect(Mappersmith.node).to.not.exist();
});
});

Sorry, the diff of this file is not supported yet

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