powerbi-router
Version:
Router for Microsoft Power BI. Given an http method and url pattern call the matching handler with the request and response object. Syntax matches common libraries such as express and restify.
2 lines • 8.58 kB
JavaScript
/*! powerbi-router v0.1.5 | (c) 2016 Microsoft Corporation MIT */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports["powerbi-router"]=e():t["powerbi-router"]=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var i=r[n]={exports:{},id:n,loaded:!1};return t[n].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";var n=r(1),i=function(){function t(t){this.handlers=t,this.getRouteRecognizer=new n,this.patchRouteRecognizer=new n,this.postRouteRecognizer=new n,this.putRouteRecognizer=new n,this.deleteRouteRecognizer=new n}return t.prototype.get=function(t,e){return this.registerHandler(this.getRouteRecognizer,"GET",t,e),this},t.prototype.patch=function(t,e){return this.registerHandler(this.patchRouteRecognizer,"PATCH",t,e),this},t.prototype.post=function(t,e){return this.registerHandler(this.postRouteRecognizer,"POST",t,e),this},t.prototype.put=function(t,e){return this.registerHandler(this.putRouteRecognizer,"PUT",t,e),this},t.prototype["delete"]=function(t,e){return this.registerHandler(this.deleteRouteRecognizer,"DELETE",t,e),this},t.prototype.registerHandler=function(t,e,r,n){var i=function(t){var e=new a;return Promise.resolve(n(t,e)).then(function(t){return e})};t.add([{path:r,handler:i}]);var o={test:function(r){if(r.method!==e)return!1;var n=t.recognize(r.url);if(void 0===n)return!1;var i=n[0];return r.params=i.params,r.queryParams=n.queryParams,r.handler=i.handler,!0},handle:function(t){return t.handler(t)}};this.handlers.addHandler(o)},t}();e.Router=i;var a=function(){function t(){this.statusCode=200,this.headers={},this.body=null}return t.prototype.send=function(t,e){this.statusCode=t,this.body=e},t}();e.Response=a},function(t,e,r){var n;(function(t){(function(){"use strict";function i(t,e,r){this.path=t,this.matcher=e,this.delegate=r}function a(t){this.routes={},this.children={},this.target=t}function o(t,e,r){return function(n,a){var s=t+n;return a?void a(o(s,e,r)):new i(t+n,e,r)}}function s(t,e,r){for(var n=0,i=0;i<t.length;i++)n+=t[i].path.length;e=e.substr(n);var a={path:e,handler:r};t.push(a)}function h(t,e,r,n){var i=e.routes;for(var a in i)if(i.hasOwnProperty(a)){var o=t.slice();s(o,a,i[a]),e.children[a]?h(o,e.children[a],r,n):r.call(n,o)}}function u(t){return"[object Array]"===Object.prototype.toString.call(t)}function c(t){this.string=t}function l(t){this.name=t}function p(t){this.name=t}function d(){}function f(t,e,r){"/"===t.charAt(0)&&(t=t.substr(1));var n=t.split("/"),i=new Array(n.length);r.val="";for(var a=0;a<n.length;a++){var o,s=n[a];(o=s.match(/^:([^\/]+)$/))?(i[a]=new l(o[1]),e.push(o[1]),r.val+="3"):(o=s.match(/^\*([^\/]+)$/))?(i[a]=new p(o[1]),r.val+="1",e.push(o[1])):""===s?(i[a]=new d,r.val+="2"):(i[a]=new c(s),r.val+="4")}return r.val=+r.val,i}function v(t){this.charSpec=t,this.nextStates=[],this.charSpecs={},this.regex=void 0,this.handlers=void 0,this.specificity=void 0}function g(t){return t.sort(function(t,e){return e.specificity.val-t.specificity.val})}function y(t,e){for(var r=[],n=0,i=t.length;n<i;n++){var a=t[n];r=r.concat(a.match(e))}return r}function m(t){this.queryParams=t||{}}function w(t,e,r){var n=t.handlers,i=t.regex,a=e.match(i),o=1,s=new m(r);s.length=n.length;for(var h=0;h<n.length;h++){for(var u=n[h],c=u.names,l={},p=0;p<c.length;p++)l[c[p]]=a[o++];s[h]={handler:u.handler,params:l,isDynamic:!!c.length}}return s}function x(t){t=t.replace(/\+/gm,"%20");var e;try{e=decodeURIComponent(t)}catch(r){e=""}return e}i.prototype={to:function(t,e){var r=this.delegate;if(r&&r.willAddRoute&&(t=r.willAddRoute(this.matcher.target,t)),this.matcher.add(this.path,t),e){if(0===e.length)throw new Error("You must have an argument in the function passed to `to`");this.matcher.addChild(this.path,t,e,this.delegate)}return this}},a.prototype={add:function(t,e){this.routes[t]=e},addChild:function(t,e,r,n){var i=new a(e);this.children[t]=i;var s=o(t,i,n);n&&n.contextEntered&&n.contextEntered(e,s),r(s)}};var R=function(t,e){var r=new a;t(o("",r,this.delegate)),h([],r,function(t){e?e(this,t):this.add(t)},this)},C=["/",".","*","+","?","|","(",")","[","]","{","}","\\"],S=new RegExp("(\\"+C.join("|\\")+")","g");c.prototype={eachChar:function(t){for(var e,r=this.string,n=0;n<r.length;n++)e=r.charAt(n),t=t.put({invalidChars:void 0,repeat:!1,validChars:e});return t},regex:function(){return this.string.replace(S,"\\$1")},generate:function(){return this.string}},l.prototype={eachChar:function(t){return t.put({invalidChars:"/",repeat:!0,validChars:void 0})},regex:function(){return"([^/]+)"},generate:function(t){return t[this.name]}},p.prototype={eachChar:function(t){return t.put({invalidChars:"",repeat:!0,validChars:void 0})},regex:function(){return"(.+)"},generate:function(t){return t[this.name]}},d.prototype={eachChar:function(t){return t},regex:function(){return""},generate:function(){return""}},v.prototype={get:function(t){if(this.charSpecs[t.validChars])return this.charSpecs[t.validChars];for(var e=this.nextStates,r=0;r<e.length;r++){var n=e[r],i=n.charSpec.validChars===t.validChars;if(i=i&&n.charSpec.invalidChars===t.invalidChars)return this.charSpecs[t.validChars]=n,n}},put:function(t){var e;return(e=this.get(t))?e:(e=new v(t),this.nextStates.push(e),t.repeat&&e.nextStates.push(e),e)},match:function(t){for(var e,r,n,i=this.nextStates,a=[],o=0;o<i.length;o++)e=i[o],r=e.charSpec,"undefined"!=typeof(n=r.validChars)?n.indexOf(t)!==-1&&a.push(e):"undefined"!=typeof(n=r.invalidChars)&&n.indexOf(t)===-1&&a.push(e);return a}};var b=Object.create||function(t){function e(){}return e.prototype=t,new e};m.prototype=b({splice:Array.prototype.splice,slice:Array.prototype.slice,push:Array.prototype.push,length:0,queryParams:null});var A=function(){this.rootState=new v,this.names={}};A.prototype={add:function(t,e){for(var r,n=this.rootState,i="^",a={},o=new Array(t.length),s=[],h=!0,u=0;u<t.length;u++){var c=t[u],l=[],p=f(c.path,l,a);s=s.concat(p);for(var v=0;v<p.length;v++){var g=p[v];g instanceof d||(h=!1,n=n.put({invalidChars:void 0,repeat:!1,validChars:"/"}),i+="/",n=g.eachChar(n),i+=g.regex())}var y={handler:c.handler,names:l};o[u]=y}h&&(n=n.put({invalidChars:void 0,repeat:!1,validChars:"/"}),i+="/"),n.handlers=o,n.regex=new RegExp(i+"$"),n.specificity=a,(r=e&&e.as)&&(this.names[r]={segments:s,handlers:o})},handlersFor:function(t){var e=this.names[t];if(!e)throw new Error("There is no route named "+t);for(var r=new Array(e.handlers.length),n=0;n<e.handlers.length;n++)r[n]=e.handlers[n];return r},hasRoute:function(t){return!!this.names[t]},generate:function(t,e){var r=this.names[t],n="";if(!r)throw new Error("There is no route named "+t);for(var i=r.segments,a=0;a<i.length;a++){var o=i[a];o instanceof d||(n+="/",n+=o.generate(e))}return"/"!==n.charAt(0)&&(n="/"+n),e&&e.queryParams&&(n+=this.generateQueryString(e.queryParams,r.handlers)),n},generateQueryString:function(t,e){var r=[],n=[];for(var i in t)t.hasOwnProperty(i)&&n.push(i);n.sort();for(var a=0;a<n.length;a++){i=n[a];var o=t[i];if(null!=o){var s=encodeURIComponent(i);if(u(o))for(var h=0;h<o.length;h++){var c=i+"[]="+encodeURIComponent(o[h]);r.push(c)}else s+="="+encodeURIComponent(o),r.push(s)}}return 0===r.length?"":"?"+r.join("&")},parseQueryString:function(t){for(var e=t.split("&"),r={},n=0;n<e.length;n++){var i,a=e[n].split("="),o=x(a[0]),s=o.length,h=!1;1===a.length?i="true":(s>2&&"[]"===o.slice(s-2)&&(h=!0,o=o.slice(0,s-2),r[o]||(r[o]=[])),i=a[1]?x(a[1]):""),h?r[o].push(i):r[o]=i}return r},recognize:function(t){var e,r,n,i=[this.rootState],a={},o=!1;if(n=t.indexOf("?"),n!==-1){var s=t.substr(n+1,t.length);t=t.substr(0,n),a=this.parseQueryString(s)}for(t=decodeURI(t),"/"!==t.charAt(0)&&(t="/"+t),e=t.length,e>1&&"/"===t.charAt(e-1)&&(t=t.substr(0,e-1),o=!0),r=0;r<t.length&&(i=y(i,t.charAt(r)),i.length);r++);var h=[];for(r=0;r<i.length;r++)i[r].handlers&&h.push(i[r]);i=g(h);var u=h[0];if(u&&u.handlers)return o&&"(.+)$"===u.regex.source.slice(-5)&&(t+="/"),w(u,t,a)}},A.prototype.map=R,A.VERSION="0.1.11";var P=A;r(3).amd?(n=function(){return P}.call(e,r,e,t),!(void 0!==n&&(t.exports=n))):"undefined"!=typeof t&&t.exports?t.exports=P:"undefined"!=typeof this&&(this.RouteRecognizer=P)}).call(this)}).call(e,r(2)(t))},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children=[],t.webpackPolyfill=1),t}},function(t,e){t.exports=function(){throw new Error("define cannot be used indirect")}}])});