mappersmith
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -1,2 +0,2 @@ | ||
!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){ | ||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Mappersmith = f()}})(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){ | ||
module.exports = { | ||
@@ -47,2 +47,4 @@ Utils: require('./src/utils'), | ||
* * url: The full url of the resource, including host and query strings | ||
* * host: The resolved host | ||
* * path: The resolved path (e.g. /path?a=true&b=3) | ||
* * method: The name of the HTTP method (get, head, post, put, delete and patch) | ||
@@ -55,6 +57,9 @@ * to be used, in lower case. | ||
this.url = args.url; | ||
this.host = args.host; | ||
this.path = args.path; | ||
this.params = args.params || {}; | ||
this.method = args.method; | ||
this.body = args.body; | ||
this.processor = args.processor; | ||
this.params = args.params || {}; | ||
this.body = args.body; | ||
this.opts = args.opts || {}; | ||
@@ -87,2 +92,4 @@ | ||
url: this.url, | ||
host: this.host, | ||
path: this.path, | ||
params: this.params, | ||
@@ -309,3 +316,2 @@ timeElapsed: this.timeElapsed, | ||
this.manifest = manifest; | ||
this.host = this.manifest.host; | ||
this.rules = this.manifest.rules || []; | ||
@@ -351,15 +357,7 @@ this.Gateway = Gateway; | ||
urlFor: function(path, urlParams, host) { | ||
resolvePath: function(pathDefinition, urlParams) { | ||
// using `Utils.extend` avoids undesired changes to `urlParams` | ||
var params = Utils.extend({}, urlParams); | ||
var normalizedPath = path; | ||
var resolvedPath = pathDefinition; | ||
if (typeof host === "undefined" || host === null) host = this.host; | ||
if (host === false) host = ''; | ||
host = host.replace(/\/$/, ''); | ||
if (host !== '') { | ||
normalizedPath = /^\//.test(path) ? path : '/' + path; | ||
} | ||
// does not includes the body param into the URL | ||
@@ -372,4 +370,4 @@ delete params[this.bodyAttr]; | ||
if (new RegExp(pattern).test(normalizedPath)) { | ||
normalizedPath = normalizedPath.replace('\{' + key + '\}', value); | ||
if (new RegExp(pattern).test(resolvedPath)) { | ||
resolvedPath = resolvedPath.replace('\{' + key + '\}', value); | ||
delete params[key]; | ||
@@ -384,5 +382,11 @@ } | ||
return host + normalizedPath + paramsString; | ||
return resolvedPath + paramsString; | ||
}, | ||
resolveHost: function(value) { | ||
if (typeof value === "undefined" || value === null) value = this.manifest.host; | ||
if (value === false) value = ''; | ||
return value.replace(/\/$/, ''); | ||
}, | ||
newGatewayRequest: function(descriptor) { | ||
@@ -414,11 +418,24 @@ var rules = this.rules. | ||
var host = this.resolveHost(descriptor.host); | ||
var path = this.resolvePath(descriptor.path, params); | ||
if (host !== '') { | ||
path = /^\//.test(path) ? path : '/' + path; | ||
} | ||
var fullUrl = host + path; | ||
var body = (params || {})[this.bodyAttr]; | ||
var gatewayOpts = Utils.extend({}, { | ||
url: this.urlFor(descriptor.path, params, descriptor.host), | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
params: params, | ||
body: body, | ||
method: descriptor.method, | ||
processor: descriptor.processor || rules.processor, | ||
params: params, | ||
body: body, | ||
opts: opts | ||
}) | ||
}); | ||
@@ -425,0 +442,0 @@ return new this.Gateway(gatewayOpts). |
/*! 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.timeStart=null,this.timeEnd=null,this.timeElapsed=null,this.successCallback=Utils.noop,this.failCallback=Utils.noop,this.completeCallback=Utils.noop};Gateway.prototype={call:function(){return this.timeStart=Utils.performanceNow(),this[this.method].apply(this,arguments),this},success:function(callback){return this.successCallback=function(data,extraStats){this.timeEnd=Utils.performanceNow(),this.timeElapsed=this.timeEnd-this.timeStart,this.processor&&(data=this.processor(data));var stats=Utils.extend({url:this.url,params:this.params,timeElapsed:this.timeElapsed,timeElapsedHumanized:Utils.humanizeTimeElapsed(this.timeElapsed)},extraStats);callback(data,stats)}.bind(this),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(arguments[0])}.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=this._isContentTypeJSON(request)?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)},_isContentTypeJSON:function(request){return/application\/json/.test(request.getResponseHeader("Content-Type"))}});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,host){var params=Utils.extend({},urlParams),normalizedPath=path;("undefined"==typeof host||null===host)&&(host=this.host),host===!1&&(host=""),host=host.replace(/\/$/,""),""!==host&&(normalizedPath=/^\//.test(path)?path:"/"+path),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,descriptor.host),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){(function(process){"undefined"!=typeof window&&null!==window&&(window.performance=window.performance||{},performance.now=function(){return performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||function(){return(new Date).getTime()}}());var getNanoSeconds,loadTime,hasProcessHrtime=function(){return"undefined"!=typeof process&&null!==process&&process.hrtime};hasProcessHrtime()&&(getNanoSeconds=function(){var hr=process.hrtime();return 1e9*hr[0]+hr[1]},loadTime=getNanoSeconds());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,"+")},performanceNow:function(){return hasProcessHrtime()?(getNanoSeconds()-loadTime)/1e6:performance.now()},humanizeTimeElapsed:function(timeElapsed){return timeElapsed>=1e3?(timeElapsed/1e3).toFixed(2)+" s":timeElapsed.toFixed(2)+" ms"},Exception:function(message){this.message=message,this.toString=function(){return"[Mappersmith] "+this.message}}};module.exports=Utils}).call(this,require("_process"))},{_process:void 0}]},{},[1])(1)}); | ||
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.Mappersmith=f()}}(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,exports){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,exports){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,exports){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,exports){var Utils=require("./utils"),Gateway=function(args){this.url=args.url,this.host=args.host,this.path=args.path,this.params=args.params||{},this.method=args.method,this.body=args.body,this.processor=args.processor,this.opts=args.opts||{},this.timeStart=null,this.timeEnd=null,this.timeElapsed=null,this.successCallback=Utils.noop,this.failCallback=Utils.noop,this.completeCallback=Utils.noop};Gateway.prototype={call:function(){return this.timeStart=Utils.performanceNow(),this[this.method].apply(this,arguments),this},success:function(callback){return this.successCallback=function(data,extraStats){this.timeEnd=Utils.performanceNow(),this.timeElapsed=this.timeEnd-this.timeStart,this.processor&&(data=this.processor(data));var stats=Utils.extend({url:this.url,host:this.host,path:this.path,params:this.params,timeElapsed:this.timeElapsed,timeElapsedHumanized:Utils.humanizeTimeElapsed(this.timeElapsed)},extraStats);callback(data,stats)}.bind(this),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,exports){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(arguments[0])}.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,exports){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=this._isContentTypeJSON(request)?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)},_isContentTypeJSON:function(request){return/application\/json/.test(request.getResponseHeader("Content-Type"))}});module.exports=VanillaGateway},{"../create-gateway":2,"../utils":8}],7:[function(require,module,exports){var Utils=require("./utils"),Mapper=function(manifest,Gateway,bodyAttr){this.manifest=manifest,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:{}})},resolvePath:function(pathDefinition,urlParams){var params=Utils.extend({},urlParams),resolvedPath=pathDefinition;delete params[this.bodyAttr],Object.keys(params).forEach(function(key){var value=params[key],pattern="{"+key+"}";new RegExp(pattern).test(resolvedPath)&&(resolvedPath=resolvedPath.replace("{"+key+"}",value),delete params[key])});var paramsString=Utils.params(params);return 0!==paramsString.length&&(paramsString="?"+paramsString),resolvedPath+paramsString},resolveHost:function(value){return("undefined"==typeof value||null===value)&&(value=this.manifest.host),value===!1&&(value=""),value.replace(/\/$/,"")},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 host=this.resolveHost(descriptor.host),path=this.resolvePath(descriptor.path,params);""!==host&&(path=/^\//.test(path)?path:"/"+path);var fullUrl=host+path,body=(params||{})[this.bodyAttr],gatewayOpts=Utils.extend({},{url:fullUrl,host:host,path:path,params:params,body:body,method:descriptor.method,processor:descriptor.processor||rules.processor,opts:opts});return new this.Gateway(gatewayOpts).success(callback).call()}.bind(this)}},module.exports=Mapper},{"./utils":8}],8:[function(require,module,exports){(function(process){"undefined"!=typeof window&&null!==window&&(window.performance=window.performance||{},performance.now=function(){return performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||function(){return(new Date).getTime()}}());var getNanoSeconds,loadTime,hasProcessHrtime=function(){return"undefined"!=typeof process&&null!==process&&process.hrtime};hasProcessHrtime()&&(getNanoSeconds=function(){var hr=process.hrtime();return 1e9*hr[0]+hr[1]},loadTime=getNanoSeconds());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,"+")},performanceNow:function(){return hasProcessHrtime()?(getNanoSeconds()-loadTime)/1e6:performance.now()},humanizeTimeElapsed:function(timeElapsed){return timeElapsed>=1e3?(timeElapsed/1e3).toFixed(2)+" s":timeElapsed.toFixed(2)+" ms"},Exception:function(message){this.message=message,this.toString=function(){return"[Mappersmith] "+this.message}}};module.exports=Utils}).call(this,require("_process"))},{_process:void 0}]},{},[1])(1)}); |
# Changelog | ||
## 0.7.0 | ||
- Included host and path into stats object | ||
## 0.6.0 | ||
Improved package size | ||
Included url and params into stats object | ||
Support for alternative hosts for each resource method | ||
Ability to disable host resolution | ||
- Improved package size | ||
- Included url and params into stats object | ||
- Support for alternative hosts for each resource method | ||
- Ability to disable host resolution | ||
## 0.5.0 | ||
Measure of the time elapsed between the request and the callback invocation | ||
Stats object for success callbacks | ||
- Measure of the time elapsed between the request and the callback invocation | ||
- Stats object for success callbacks | ||
## 0.4.1 | ||
bugfix: VanillaGateway doesn't parse JSON responses | ||
- bugfix: VanillaGateway doesn't parse JSON responses | ||
## 0.4.0 | ||
Support for Node vanilla gateway | ||
- Support for Node vanilla gateway | ||
## 0.3.0 | ||
Ability to configure default parameters for resources | ||
- Ability to configure default parameters for resources | ||
## 0.2.1 | ||
bugfix: fixed typo | ||
- bugfix: fixed typo | ||
## 0.2.0 | ||
Support for emulateHTTP | ||
Support for global configurations and per url match configurations | ||
Support for POST, PUT, PATCH and DELETE methods | ||
Exposed body value to gateway | ||
Option to change the bodyAttr name | ||
Processor functions for resources | ||
Exposed request params to the gateway to allow o POST, PUT, etc | ||
Compact syntax (syntatic sugar for GET methods) | ||
- Support for emulateHTTP | ||
- Support for global configurations and per url match configurations | ||
- Support for POST, PUT, PATCH and DELETE methods | ||
- Exposed body value to gateway | ||
- Option to change the bodyAttr name | ||
- Processor functions for resources | ||
- Exposed request params to the gateway to allow o POST, PUT, etc | ||
- Compact syntax (syntatic sugar for GET methods) | ||
## 0.1.1 | ||
bugfix: fixed reference of Mapper and VanillaGateway in index.js | ||
- bugfix: fixed reference of Mapper and VanillaGateway in index.js | ||
## 0.1.0 | ||
Basic structure with Jquery and Vanilla gateways | ||
Support for GET method | ||
- Basic structure with Jquery and Vanilla gateways | ||
- Support for GET method |
{ | ||
"name": "mappersmith", | ||
"version": "0.6.0", | ||
"description": "It is a lightweight rest client mapper for javascript", | ||
"version": "0.7.0", | ||
"description": "It is a lightweight, isomorphic, dependency-free, rest client mapper for javascript", | ||
"author": "Tulio Ornelas <ornelas.tulio@gmail.com>", | ||
@@ -36,3 +36,3 @@ "contributors": [ | ||
"devDependencies": { | ||
"browserify": "^7.0.1", | ||
"browserify": "^9.0.8", | ||
"chai": "^1.10.0", | ||
@@ -47,4 +47,4 @@ "mocha": "^2.0.1", | ||
"sinon-chai": "^2.6.0", | ||
"uglify-js": "^2.4.16" | ||
"uglify-js": "^2.4.20" | ||
} | ||
} |
@@ -103,2 +103,4 @@ [![npm version](https://badge.fury.io/js/mappersmith.svg)](http://badge.fury.io/js/mappersmith) | ||
url: 'http://my.api.com/v1/books.json?language=en', | ||
host: 'http://my.api.com', | ||
path: '/v1/books.json?language=en', | ||
params: {language: 'en'}, | ||
@@ -251,2 +253,4 @@ timeElapsed: 6.745000369846821, | ||
// - this.url | ||
// - this.host | ||
// - this.path | ||
// - this.params | ||
@@ -413,3 +417,3 @@ // - this.body | ||
1. `npm run test` | ||
1. `npm test` | ||
@@ -416,0 +420,0 @@ ## Compile and release |
@@ -8,2 +8,4 @@ var Utils = require('./utils'); | ||
* * url: The full url of the resource, including host and query strings | ||
* * host: The resolved host | ||
* * path: The resolved path (e.g. /path?a=true&b=3) | ||
* * method: The name of the HTTP method (get, head, post, put, delete and patch) | ||
@@ -16,6 +18,9 @@ * to be used, in lower case. | ||
this.url = args.url; | ||
this.host = args.host; | ||
this.path = args.path; | ||
this.params = args.params || {}; | ||
this.method = args.method; | ||
this.body = args.body; | ||
this.processor = args.processor; | ||
this.params = args.params || {}; | ||
this.body = args.body; | ||
this.opts = args.opts || {}; | ||
@@ -48,2 +53,4 @@ | ||
url: this.url, | ||
host: this.host, | ||
path: this.path, | ||
params: this.params, | ||
@@ -50,0 +57,0 @@ timeElapsed: this.timeElapsed, |
@@ -12,3 +12,2 @@ var Utils = require('./utils'); | ||
this.manifest = manifest; | ||
this.host = this.manifest.host; | ||
this.rules = this.manifest.rules || []; | ||
@@ -54,15 +53,7 @@ this.Gateway = Gateway; | ||
urlFor: function(path, urlParams, host) { | ||
resolvePath: function(pathDefinition, urlParams) { | ||
// using `Utils.extend` avoids undesired changes to `urlParams` | ||
var params = Utils.extend({}, urlParams); | ||
var normalizedPath = path; | ||
var resolvedPath = pathDefinition; | ||
if (typeof host === "undefined" || host === null) host = this.host; | ||
if (host === false) host = ''; | ||
host = host.replace(/\/$/, ''); | ||
if (host !== '') { | ||
normalizedPath = /^\//.test(path) ? path : '/' + path; | ||
} | ||
// does not includes the body param into the URL | ||
@@ -75,4 +66,4 @@ delete params[this.bodyAttr]; | ||
if (new RegExp(pattern).test(normalizedPath)) { | ||
normalizedPath = normalizedPath.replace('\{' + key + '\}', value); | ||
if (new RegExp(pattern).test(resolvedPath)) { | ||
resolvedPath = resolvedPath.replace('\{' + key + '\}', value); | ||
delete params[key]; | ||
@@ -87,5 +78,11 @@ } | ||
return host + normalizedPath + paramsString; | ||
return resolvedPath + paramsString; | ||
}, | ||
resolveHost: function(value) { | ||
if (typeof value === "undefined" || value === null) value = this.manifest.host; | ||
if (value === false) value = ''; | ||
return value.replace(/\/$/, ''); | ||
}, | ||
newGatewayRequest: function(descriptor) { | ||
@@ -117,11 +114,24 @@ var rules = this.rules. | ||
var host = this.resolveHost(descriptor.host); | ||
var path = this.resolvePath(descriptor.path, params); | ||
if (host !== '') { | ||
path = /^\//.test(path) ? path : '/' + path; | ||
} | ||
var fullUrl = host + path; | ||
var body = (params || {})[this.bodyAttr]; | ||
var gatewayOpts = Utils.extend({}, { | ||
url: this.urlFor(descriptor.path, params, descriptor.host), | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
params: params, | ||
body: body, | ||
method: descriptor.method, | ||
processor: descriptor.processor || rules.processor, | ||
params: params, | ||
body: body, | ||
opts: opts | ||
}) | ||
}); | ||
@@ -128,0 +138,0 @@ return new this.Gateway(gatewayOpts). |
@@ -9,2 +9,4 @@ var expect = chai.expect; | ||
gateway, | ||
host, | ||
path, | ||
url, | ||
@@ -16,3 +18,5 @@ verb, | ||
noop = Utils.noop; | ||
url = 'http://url'; | ||
host = 'http://host'; | ||
path = '/path'; | ||
url = host + path; | ||
verb = 'get'; | ||
@@ -50,4 +54,6 @@ methodStub = sinon.stub(Gateway.prototype, verb); | ||
url: url, | ||
host: host, | ||
path: path, | ||
params: {a: 1}, | ||
method: verb, | ||
params: {a: 1}, | ||
body: 'some-value', | ||
@@ -60,3 +66,3 @@ opts: {b: 2} | ||
['url', 'method', 'params', 'body', 'opts'].forEach(function(attr) { | ||
['url', 'host', 'path', 'params', 'method', 'body', 'opts'].forEach(function(attr) { | ||
it(attr, function() { | ||
@@ -96,3 +102,9 @@ expect(gateway[attr]).to.equal(args[attr]); | ||
params = {a: 1, b: false}; | ||
gateway = new Gateway({url: url, params: params, method: verb}); | ||
gateway = new Gateway({ | ||
url: url, | ||
host: host, | ||
path: path, | ||
params: params, | ||
method: verb | ||
}); | ||
}); | ||
@@ -135,2 +147,4 @@ | ||
url: url, | ||
host: host, | ||
path: path, | ||
params: params, | ||
@@ -150,2 +164,4 @@ timeElapsed: gateway.timeElapsed, | ||
url: url, | ||
host: host, | ||
path: path, | ||
params: params, | ||
@@ -164,2 +180,4 @@ timeElapsed: gateway.timeElapsed, | ||
url: url, | ||
host: host, | ||
path: path, | ||
params: params, | ||
@@ -166,0 +184,0 @@ timeElapsed: gateway.timeElapsed, |
@@ -73,6 +73,2 @@ var expect = chai.expect; | ||
it('holds a reference to host', function() { | ||
expect(mapper).to.have.property('host', manifest.host); | ||
}); | ||
it('holds a reference to bodyAttr', function() { | ||
@@ -108,4 +104,6 @@ mapper = new Mapper(manifest, gateway, 'data'); | ||
var method, | ||
host, | ||
fullUrl, | ||
path, | ||
resolvedPath, | ||
params, | ||
@@ -116,4 +114,6 @@ callback; | ||
method = 'get'; | ||
fullUrl = 'http://full-url/path'; | ||
path = 'path'; | ||
host = mapper.resolveHost(); | ||
path = '/path'; | ||
resolvedPath = mapper.resolvePath(path, {a: true}); | ||
fullUrl = host + path; | ||
params = {a: true}; | ||
@@ -129,3 +129,4 @@ callback = Utils.noop; | ||
it('returns a configured gateway', function() { | ||
var request = mapper.newGatewayRequest({method: method, path: path}); | ||
var request = mapper.newGatewayRequest({method: method, host: host, path: path}); | ||
fullUrl = host + resolvedPath; | ||
@@ -136,3 +137,5 @@ expect(request(params, callback)).to.be.an.instanceof(gateway); | ||
expect(gateway).to.have.been.calledWith({ | ||
url: fullUrl + '?a=true', | ||
url: fullUrl, | ||
host: host, | ||
path: resolvedPath, | ||
method: method, | ||
@@ -143,5 +146,42 @@ params: params | ||
describe('with host false', function() { | ||
shared.examplesFor('path with host "false"', function() { | ||
it('keeps the configured path', function() { | ||
var request = mapper.newGatewayRequest({method: method, host: host, path: path}); | ||
expect(request(callback)).to.be.an.instanceof(gateway); | ||
expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
expect(gateway.prototype.call).to.have.been.called; | ||
expect(gateway).to.have.been.calledWith({ | ||
url: path, | ||
host: '', | ||
path: path, | ||
method: method | ||
}); | ||
}); | ||
}); | ||
beforeEach(function() { | ||
host = false; | ||
}); | ||
describe('and path without leading slash', function() { | ||
beforeEach(function() { | ||
path = 'path'; //without leading slash | ||
}); | ||
shared.shouldBehaveLike('path with host "false"'); | ||
}); | ||
describe('and path with leading slash', function() { | ||
beforeEach(function() { | ||
path = '/path'; | ||
}); | ||
shared.shouldBehaveLike('path with host "false"'); | ||
}); | ||
}); | ||
describe('without params', function() { | ||
it('considers callback as the first argument', function() { | ||
var request = mapper.newGatewayRequest({method: method, path: path}); | ||
var request = mapper.newGatewayRequest({method: method, host: host, path: path}); | ||
@@ -153,2 +193,4 @@ expect(request(callback)).to.be.an.instanceof(gateway); | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
method: method | ||
@@ -160,4 +202,8 @@ }); | ||
it('considers opts as the second argument', function() { | ||
var request = mapper.newGatewayRequest({method: method, path: path}); | ||
var opts = {jsonp: true}; | ||
var request = mapper.newGatewayRequest({ | ||
method: method, | ||
host: host, | ||
path: path | ||
}); | ||
@@ -169,2 +215,4 @@ expect(request(callback, opts)).to.be.an.instanceof(gateway); | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
method: method, | ||
@@ -181,7 +229,11 @@ opts: opts | ||
params[mapper.bodyAttr] = 'some-value'; | ||
var request = mapper.newGatewayRequest({method: method, path: path});; | ||
fullUrl = host + resolvedPath; | ||
var request = mapper.newGatewayRequest({method: method, host: host, path: path}); | ||
var result = { | ||
url: fullUrl + '?a=true', | ||
method: method, | ||
params: params | ||
url: fullUrl, | ||
host: host, | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
} | ||
@@ -203,3 +255,3 @@ | ||
path = manifest.resources.Book.all.path; | ||
fullUrl = manifest.host + path; | ||
fullUrl = host + path; | ||
}); | ||
@@ -209,3 +261,7 @@ | ||
it('always merge with gateway opts and processor', function() { | ||
var request = mapper.newGatewayRequest({method: method, path: path}); | ||
var request = mapper.newGatewayRequest({ | ||
method: method, | ||
host: host, | ||
path: path | ||
}); | ||
@@ -215,2 +271,4 @@ expect(request(callback)).to.be.an.instanceof(gateway); | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
method: method, | ||
@@ -259,3 +317,7 @@ opts: opts.gateway, | ||
it('merges both rules, using natural precedence for prioritization', function() { | ||
var request = mapper.newGatewayRequest({method: method, path: path}); | ||
var request = mapper.newGatewayRequest({ | ||
method: method, | ||
host: host, | ||
path: path | ||
}); | ||
@@ -265,2 +327,4 @@ expect(request(callback)).to.be.an.instanceof(gateway); | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
method: method, | ||
@@ -274,8 +338,13 @@ opts: Utils.extend({}, opts.gateway, optsMatch.gateway), | ||
describe('with default params', function() { | ||
var descriptor, request; | ||
var descriptor, request, host, path; | ||
beforeEach(function() { | ||
descriptor = manifest.resources.Photo.byYear; | ||
descriptor.host = 'http://other-host'; | ||
descriptor.method = method; | ||
request = mapper.newGatewayRequest(descriptor); | ||
host = mapper.resolveHost(descriptor.host); | ||
path = mapper.resolvePath(descriptor.path, descriptor.params); | ||
fullUrl = host + path; | ||
}); | ||
@@ -285,9 +354,9 @@ | ||
it('uses the configured default parameters', function() { | ||
fullUrl = mapper.urlFor(descriptor.path, descriptor.params); | ||
expect(request(callback)).to.be.an.instanceof(gateway); | ||
expect(gateway).to.have.been.calledWith({ | ||
url: fullUrl, | ||
method: descriptor.method, | ||
params: descriptor.params | ||
host: host, | ||
path: path, | ||
params: descriptor.params, | ||
method: descriptor.method | ||
}); | ||
@@ -301,3 +370,4 @@ }); | ||
var mergedParams = Utils.extend({}, descriptor.params, methodParams); | ||
fullUrl = mapper.urlFor(descriptor.path, mergedParams); | ||
path = mapper.resolvePath(descriptor.path, mergedParams); | ||
fullUrl = host + path; | ||
@@ -307,2 +377,4 @@ expect(request(methodParams, callback)).to.be.an.instanceof(gateway); | ||
url: fullUrl, | ||
host: host, | ||
path: path, | ||
method: descriptor.method, | ||
@@ -317,90 +389,58 @@ params: mergedParams | ||
describe('#urlFor', function() { | ||
describe('without params and query string', function() { | ||
describe('host and path with "/"', function() { | ||
it('returns host and path', function() { | ||
mapper.host = mapper.host + '/'; | ||
expect(mapper.urlFor('/path')).to.equals('http://full-url/path'); | ||
}); | ||
describe('#host', function() { | ||
describe('with manifest host', function() { | ||
beforeEach(function() { | ||
mapper.manifest.host = 'http://some-host/'; | ||
}); | ||
describe('host and path without "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('path')).to.equals('http://full-url/path'); | ||
}); | ||
it('removes the trailing slash if configured', function() { | ||
expect(mapper.resolveHost()).to.equals('http://some-host'); | ||
}); | ||
}); | ||
describe('host with "/" and path without', function() { | ||
it('returns host and path', function() { | ||
mapper.host = mapper.host + '/'; | ||
expect(mapper.urlFor('path')).to.equals('http://full-url/path'); | ||
}); | ||
describe('with an alternative host', function() { | ||
beforeEach(function() { | ||
mapper.manifest.host = 'http://some-host/'; | ||
}); | ||
describe('host without "/" and path with', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('/path')).to.equals('http://full-url/path'); | ||
}); | ||
it('removes the trailing slash if configured', function() { | ||
expect(mapper.resolveHost('http://other-host/')).to.equals('http://other-host'); | ||
}); | ||
describe('explicit empty host with "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('/path', null, '')).to.equals('/path'); | ||
}); | ||
it('returns a blank string when value is "false"', function() { | ||
expect(mapper.resolveHost(false)).to.equals(''); | ||
}); | ||
}); | ||
}); | ||
describe('explicit empty host without "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('path', null, '')).to.equals('path'); | ||
}); | ||
describe('#path', function() { | ||
describe('without params and query string', function() { | ||
it('returns resolved path', function() { | ||
expect(mapper.resolvePath('/path')).to.equals('/path'); | ||
}); | ||
describe('explicit empty host, specified by false, with "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('/path', null, false)).to.equals('/path'); | ||
}); | ||
}); | ||
describe('explicit empty host, specified by false, without "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('path', null, false)).to.equals('path'); | ||
}); | ||
}); | ||
describe('explicit host with "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('/path', null, 'http://alt-url')).to.equals('http://alt-url/path'); | ||
}); | ||
}); | ||
describe('explicit host without "/"', function() { | ||
it('returns host and path', function() { | ||
expect(mapper.urlFor('path', null, 'http://alt-url')).to.equals('http://alt-url/path'); | ||
}); | ||
}); | ||
}); | ||
describe('with bodyAttr in params', function() { | ||
it('does not include into the URL', function() { | ||
it('does not include into the resolved path', function() { | ||
var params = {}; | ||
params[mapper.bodyAttr] = 'some-value'; | ||
expect(mapper.urlFor('/path', params)).to.equals('http://full-url/path'); | ||
expect(mapper.resolvePath('/path', params)).to.equals('/path'); | ||
}); | ||
}); | ||
describe('with params in the path', function() { | ||
it('replaces params and returns host and path', function() { | ||
expect(mapper.urlFor('{a}/{b}', {a: 1, b: 2})).to.equals('http://full-url/1/2'); | ||
describe('with params in the definition', function() { | ||
it('replaces params and returns resolved path', function() { | ||
expect(mapper.resolvePath('{a}/{b}', {a: 1, b: 2})).to.equals('1/2'); | ||
}); | ||
}); | ||
describe('with query string in the path', function() { | ||
it('includes query string and returns host and path', function() { | ||
expect(mapper.urlFor('path', {a: 1, b: 2})).to.equals('http://full-url/path?a=1&b=2'); | ||
describe('with query string in the definition', function() { | ||
it('includes query string into the resolved path', function() { | ||
expect(mapper.resolvePath('path', {a: 1, b: 2})).to.equals('path?a=1&b=2'); | ||
}); | ||
}); | ||
describe('with query string and params in the path', function() { | ||
it('includes query string, replaces params and returns host and path', function() { | ||
expect(mapper.urlFor('{a}', {a: 1, b: 2})).to.equals('http://full-url/1?b=2'); | ||
describe('with query string and params in the definition', function() { | ||
it('includes query string and replaces params into the resolved path', function() { | ||
expect(mapper.resolvePath('{a}', {a: 1, b: 2})).to.equals('1?b=2'); | ||
}); | ||
@@ -446,3 +486,3 @@ }); | ||
var path = manifest.resources.Book.all.path; | ||
var url = mapper.urlFor(path); | ||
var url = mapper.resolveHost() + path; | ||
@@ -452,2 +492,4 @@ result.Book.all(callback); | ||
url: url, | ||
host: mapper.resolveHost(), | ||
path: path, | ||
method: method | ||
@@ -463,3 +505,4 @@ }); | ||
var params = {b: 2}; | ||
var url = mapper.urlFor(path, params); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = mapper.resolveHost() + resolvedPath; | ||
@@ -469,4 +512,6 @@ result.Book.all(params, callback); | ||
url: url, | ||
method: method, | ||
params: params | ||
host: mapper.resolveHost(), | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -481,3 +526,4 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
var params = {id: 3}; | ||
var url = mapper.urlFor(path, params); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = mapper.resolveHost() + resolvedPath; | ||
@@ -487,4 +533,6 @@ result.Book.byId(params, callback); | ||
url: url, | ||
method: method, | ||
params: params | ||
host: mapper.resolveHost(), | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -499,3 +547,4 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
var params = {id: 3, d: 4}; | ||
var url = mapper.urlFor(path, params); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = mapper.resolveHost() + resolvedPath; | ||
@@ -505,4 +554,6 @@ result.Book.byId(params, callback); | ||
url: url, | ||
method: method, | ||
params: params | ||
host: mapper.resolveHost(), | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -514,3 +565,3 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
describe('with params in the path and query string and alternate empty host', function() { | ||
describe('with params in the path, query string and an alternative empty host', function() { | ||
it('calls the gateway with the configured values', function() { | ||
@@ -521,9 +572,14 @@ var path = manifest.resources.Book.byUrl.path; | ||
var params = {url: paramUrl}; | ||
var url = mapper.urlFor(path, params, host); | ||
var resolvedHost = mapper.resolveHost(host); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = resolvedHost + resolvedPath; | ||
result.Book.byUrl(params, callback); | ||
expect(gateway).to.have.been.calledWith({ | ||
url: url, | ||
method: method, | ||
params: params | ||
host: resolvedHost, | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -534,3 +590,3 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
describe('with params in the path and query string and alternate host', function() { | ||
describe('with params in the path, query string and an alternate host', function() { | ||
it('calls the gateway with the configured values', function() { | ||
@@ -540,9 +596,14 @@ var path = manifest.resources.Book.AltById.path; | ||
var params = {id: 3, d: 4}; | ||
var url = mapper.urlFor(path, params, host); | ||
var resolvedHost = mapper.resolveHost(host); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = resolvedHost + resolvedPath; | ||
result.Book.AltById(params, callback); | ||
expect(gateway).to.have.been.calledWith({ | ||
url: url, | ||
method: method, | ||
params: params | ||
host: resolvedHost, | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -556,3 +617,3 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
var path = manifest.resources.Photo.add.path; | ||
var url = mapper.urlFor(path); | ||
var url = mapper.resolveHost() + path; | ||
method = 'post'; | ||
@@ -563,2 +624,4 @@ | ||
url: url, | ||
host: mapper.resolveHost(), | ||
path: path, | ||
method: method | ||
@@ -573,3 +636,3 @@ }); | ||
var path = manifest.resources.Book.archived; | ||
var url = mapper.urlFor(path); | ||
var url = mapper.resolveHost() + path; | ||
@@ -579,2 +642,4 @@ result.Book.archived(callback); | ||
url: url, | ||
host: mapper.resolveHost(), | ||
path: path, | ||
method: method | ||
@@ -588,3 +653,4 @@ }); | ||
var params = {author: 'Daniel'}; | ||
var url = mapper.urlFor(path, params); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = mapper.resolveHost() + resolvedPath; | ||
@@ -594,4 +660,6 @@ result.Book.archived(params, callback); | ||
url: url, | ||
method: method, | ||
params: params | ||
host: mapper.resolveHost(), | ||
path: resolvedPath, | ||
params: params, | ||
method: method | ||
}); | ||
@@ -603,3 +671,2 @@ expect(gateway.prototype.success).to.have.been.calledWith(callback); | ||
describe('resource definition compact syntax', function() { | ||
it('parses HTTP method and URL', function() { | ||
@@ -613,3 +680,3 @@ var compactDefinition = manifest.resources.Book.byCategory; | ||
var url = mapper.urlFor(path); | ||
var url = mapper.resolveHost() + path; | ||
@@ -619,2 +686,4 @@ result.Book.byCategory(callback); | ||
url: url, | ||
host: mapper.resolveHost(), | ||
path: path, | ||
method: method | ||
@@ -642,4 +711,7 @@ }); | ||
result.Book.test(); | ||
var path = '/v1/books.json'; | ||
expect(gateway).to.have.been.calledWith({ | ||
url: mapper.urlFor('/v1/books.json'), | ||
url: mapper.resolveHost() + path, | ||
host: mapper.resolveHost(), | ||
path: path, | ||
method: methodName | ||
@@ -657,3 +729,4 @@ }); | ||
var params = {id: 3}; | ||
var url = mapper.urlFor(path, params); | ||
var resolvedPath = mapper.resolvePath(path, params); | ||
var url = mapper.resolveHost() + resolvedPath; | ||
@@ -663,4 +736,6 @@ result.Photo.byId(params, callback); | ||
url: url, | ||
host: mapper.resolveHost(), | ||
path: resolvedPath, | ||
params: params, | ||
method: method, | ||
params: params, | ||
processor: processor | ||
@@ -667,0 +742,0 @@ }); |
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
650032
17255
431