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.2.1 to 0.3.0

31

build/mappersmith.js

@@ -307,5 +307,6 @@ !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 descriptor = methods[methodName];
// Compact Syntax
if (typeof(descriptor) === 'string') {
var compactDefinitionMethod = descriptor.match( /^(get|post|delete|put|patch):(.*)/ )
var compactDefinitionMethod = descriptor.match(/^(get|post|delete|put|patch):(.*)/);
if (compactDefinitionMethod != null) {

@@ -319,10 +320,4 @@ descriptor = {method: compactDefinitionMethod[1], path: compactDefinitionMethod[2]};

var httpMethod = (descriptor.method || 'get').toLowerCase();
context.methods[methodName] = this.newGatewayRequest(
httpMethod,
descriptor.path,
descriptor.processor
);
descriptor.method = (descriptor.method || 'get').toLowerCase();
context.methods[methodName] = this.newGatewayRequest(descriptor);
return context;

@@ -360,5 +355,7 @@

newGatewayRequest: function(method, path, processor) {
newGatewayRequest: function(descriptor) {
var rules = this.rules.
filter(function(rule) { return rule.match === undefined || rule.match.test(path) }).
filter(function(rule) {
return rule.match === undefined || rule.match.test(descriptor.path)
}).
reduce(function(context, rule) {

@@ -378,2 +375,6 @@ var mergedGateway = Utils.extend(context.gateway, rule.values.gateway);

if (!!descriptor.params) {
params = Utils.extend({}, descriptor.params, params);
}
opts = Utils.extend({}, opts, rules.gateway);

@@ -384,5 +385,5 @@ if(Utils.isObjEmpty(opts)) opts = undefined;

var gatewayOpts = Utils.extend({}, {
url: this.urlFor(path, params),
method: method,
processor: processor || rules.processor,
url: this.urlFor(descriptor.path, params),
method: descriptor.method,
processor: descriptor.processor || rules.processor,
params: params,

@@ -389,0 +390,0 @@ body: body,

/*! 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}}var httpMethod=(descriptor.method||"get").toLowerCase();return context.methods[methodName]=this.newGatewayRequest(httpMethod,descriptor.path,descriptor.processor),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(method,path,processor){var rules=this.rules.filter(function(rule){return void 0===rule.match||rule.match.test(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),opts=Utils.extend({},opts,rules.gateway),Utils.isObjEmpty(opts)&&(opts=void 0);var body=(params||{})[this.bodyAttr],gatewayOpts=Utils.extend({},{url:this.urlFor(path,params),method:method,processor: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");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)});
{
"name": "mappersmith",
"version": "0.2.1",
"version": "0.3.0",
"description": "It is a lightweight rest client mapper for javascript",

@@ -5,0 +5,0 @@ "author": "Tulio Ornelas <ornelas.tulio@gmail.com>",

@@ -96,2 +96,31 @@ # Mappersmith

#### Default parameters
It is possible to configure default parameters for your resources, just use the key `params` in the definition. It will replace params in the URL or include query strings, for example, imagine that our manifest has the method __byYear__ in the resource __Photo__:
```javascript
...
Photo: {
byYear: {
path: '/v1/photos/{year}.json',
params: {year: new Date().getFullYear(), category: 'cats'}
}
}
...
```
If we call it without any params and `new Date().getFullYear()` is 2015, it will generate the following URL:
```javascript
Client.Photo.byYear();
// http://my.api.com/v1/photos/2015.json?category=cats
```
And, of course, we can override the defaults:
```javascript
Client.Photo.byYear({category: 'dogs'});
// http://my.api.com/v1/photos/2015.json?category=dogs
```
#### Message Body

@@ -139,3 +168,3 @@

Book: {
all: 'get:/v1/books.json', // The same as {method: 'GET', path: '/v1/books.json'}
all: 'get:/v1/books.json', // The same as {method: 'GET', path: '/v1/books.json'}
byId: '/v1/books/{id}.json' // The default is GET, as always

@@ -142,0 +171,0 @@ },

@@ -34,5 +34,6 @@ var Utils = require('./utils');

var descriptor = methods[methodName];
// Compact Syntax
if (typeof(descriptor) === 'string') {
var compactDefinitionMethod = descriptor.match( /^(get|post|delete|put|patch):(.*)/ )
var compactDefinitionMethod = descriptor.match(/^(get|post|delete|put|patch):(.*)/);
if (compactDefinitionMethod != null) {

@@ -46,10 +47,4 @@ descriptor = {method: compactDefinitionMethod[1], path: compactDefinitionMethod[2]};

var httpMethod = (descriptor.method || 'get').toLowerCase();
context.methods[methodName] = this.newGatewayRequest(
httpMethod,
descriptor.path,
descriptor.processor
);
descriptor.method = (descriptor.method || 'get').toLowerCase();
context.methods[methodName] = this.newGatewayRequest(descriptor);
return context;

@@ -87,5 +82,7 @@

newGatewayRequest: function(method, path, processor) {
newGatewayRequest: function(descriptor) {
var rules = this.rules.
filter(function(rule) { return rule.match === undefined || rule.match.test(path) }).
filter(function(rule) {
return rule.match === undefined || rule.match.test(descriptor.path)
}).
reduce(function(context, rule) {

@@ -105,2 +102,6 @@ var mergedGateway = Utils.extend(context.gateway, rule.values.gateway);

if (!!descriptor.params) {
params = Utils.extend({}, descriptor.params, params);
}
opts = Utils.extend({}, opts, rules.gateway);

@@ -111,5 +112,5 @@ if(Utils.isObjEmpty(opts)) opts = undefined;

var gatewayOpts = Utils.extend({}, {
url: this.urlFor(path, params),
method: method,
processor: processor || rules.processor,
url: this.urlFor(descriptor.path, params),
method: descriptor.method,
processor: descriptor.processor || rules.processor,
params: params,

@@ -116,0 +117,0 @@ body: body,

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

}
},
byYear: {
path: '/v1/photos/{year}.json',
params: {year: 2015, category: 'cats'}
}

@@ -121,3 +125,3 @@ }

it('returns a configured gateway', function() {
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});

@@ -136,3 +140,3 @@ expect(request(params, callback)).to.be.an.instanceof(gateway);

it('considers callback as the first argument', function() {
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});

@@ -150,3 +154,3 @@ expect(request(callback)).to.be.an.instanceof(gateway);

it('considers opts as the second argument', function() {
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});
var opts = {jsonp: true};

@@ -170,3 +174,3 @@

params[mapper.bodyAttr] = 'some-value';
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});;
var result = {

@@ -197,3 +201,3 @@ url: fullUrl + '?a=true',

it('always merge with gateway opts and processor', function() {
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});

@@ -246,3 +250,3 @@ expect(request(callback)).to.be.an.instanceof(gateway);

it('merges both rules, using natural precedence for prioritization', function() {
var request = mapper.newGatewayRequest(method, path);
var request = mapper.newGatewayRequest({method: method, path: path});

@@ -258,2 +262,40 @@ expect(request(callback)).to.be.an.instanceof(gateway);

});
describe('with default params', function() {
var descriptor, request;
beforeEach(function() {
descriptor = manifest.resources.Photo.byYear;
descriptor.method = method;
request = mapper.newGatewayRequest(descriptor);
});
describe('without params in method call', function() {
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
});
});
});
describe('with params in method call', function() {
it('merges with the given params', function() {
var methodParams = {category: 'dogs'};
var mergedParams = Utils.extend({}, descriptor.params, methodParams);
fullUrl = mapper.urlFor(descriptor.path, mergedParams);
expect(request(methodParams, callback)).to.be.an.instanceof(gateway);
expect(gateway).to.have.been.calledWith({
url: fullUrl,
method: descriptor.method,
params: mergedParams
});
});
});
});
});

@@ -260,0 +302,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