frontexpress/frontexpress.min.js
2017-01-14 15:02:50 +01:00

3 lines
9.2 KiB
JavaScript

var frontexpress=function(){"use strict";function t(t){var e=void 0,r=void 0,i=void 0,a=void 0;if(t&&t.length>0){if(1===t.length){var o=n(t,1);a=o[0]}else{var s=n(t,2);e=s[0],a=s[1]}a instanceof l?i=a:(a instanceof u||"function"==typeof a)&&(r=a)}return{baseUri:e,middleware:r,router:i,which:a}}var e=["GET","POST","PUT","DELETE"],r=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},i=function(){function t(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}return function(e,r,i){return r&&t(e.prototype,r),i&&t(e,i),e}}(),n=function(){function t(t,e){var r=[],i=!0,n=!1,a=void 0;try{for(var o,s=t[Symbol.iterator]();!(i=(o=s.next()).done)&&(r.push(o.value),!e||r.length!==e);i=!0);}catch(t){n=!0,a=t}finally{try{!i&&s.return&&s.return()}finally{if(n)throw a}}return r}return function(e,r){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),a=function(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)},o=function(){function t(){r(this,t)}return i(t,[{key:"fetch",value:function(t,e,r){var i=t.method,n=t.uri,a=t.headers,o=t.data,s=function(r){e(t,{status:200,statusText:"OK",responseText:r})},u=function(e){var i=e.status,n=e.statusText,a=e.errorThrown;r(t,{status:i,statusText:n,errorThrown:a,errors:"HTTP "+i+" "+(n?n:"")})},d=new XMLHttpRequest;d.onreadystatechange=function(){4===d.readyState&&(200===d.status?s(d.responseText):u({status:d.status,statusText:d.statusText}))};try{if(d.open(i,n,!0),a){var h=!0,l=!1,f=void 0;try{for(var c,v=Object.keys(a)[Symbol.iterator]();!(h=(c=v.next()).done);h=!0){var y=c.value;d.setRequestHeader(y,a[y])}}catch(t){l=!0,f=t}finally{try{!h&&v.return&&v.return()}finally{if(l)throw f}}}o?d.send(o):d.send()}catch(t){u({errorThrown:t})}}}]),t}(),s=function(){function t(){r(this,t),this.settings={"http requester":new o,"http GET transformer":{uri:function(t){var e=t.uri,r=(t.headers,t.data);if(!r)return e;var i=e,a="",o=/^(.*)(#.*)$/.exec(e);if(o){var s=/^(.*)(#.*)$/.exec(e),u=n(s,3);i=u[1],a=u[2]}return i=Object.keys(r).reduce(function(t,e,i){return t+=""+(0===i&&t.indexOf("?")===-1?"?":"&")+e+"="+r[e]},i),i+a}}},this.rules={"http requester":function(t){if("function"!=typeof t.fetch)throw new TypeError("setting http requester has no fetch method")}}}return i(t,[{key:"set",value:function(t,e){var r=this.rules[t];r&&r(e),this.settings[t]=e}},{key:"get",value:function(t){return this.settings[t]}}]),t}(),u=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";r(this,t),this.name=e}return i(t,[{key:"next",value:function(){return!0}}]),t}(),d=function(){function t(e,i,n,a){r(this,t),this.router=e,this.uriPart=i,this.method=n,this.middleware=a,this.visited=!1}return i(t,[{key:"uri",get:function(){if(this.uriPart||this.method){if(this.uriPart instanceof RegExp)return this.uriPart;if(this.router.baseUri instanceof RegExp)return this.router.baseUri;if(this.router.baseUri){var t=this.router.baseUri.trim();return this.uriPart?(t+this.uriPart.trim()).replace(/\/{2,}/,"/"):t}return this.uriPart}}}]),t}(),h="method takes at least a middleware",l=function(){function n(t){r(this,n),this._baseUri=t,this._routes=[]}return i(n,[{key:"_add",value:function(t){return this._routes.push(t),this}},{key:"routes",value:function(t,e){return this._routes.filter(function(r){if(r.method&&r.method!==e)return!1;if(!r.uri||!t)return!0;var i=/^(.*)\?.*#.*|(.*)(?=\?|#)|(.*[^\?#])$/.exec(t),n=i[1]||i[2]||i[3];return r.uri instanceof RegExp?n.match(r.uri):r.uri===n})}},{key:"visited",value:function(){return this._routes.filter(function(t){return t.visited})}},{key:"use",value:function(t){if(!(t instanceof u)&&"function"!=typeof t)throw new TypeError(h);return this._add(new d(this,void 0,void 0,t)),this}},{key:"all",value:function(){for(var r=arguments.length,i=Array(r),n=0;n<r;n++)i[n]=arguments[n];var a=t(i),o=a.middleware;if(!o)throw new TypeError(h);var s=!0,u=!1,d=void 0;try{for(var l,f=e[Symbol.iterator]();!(s=(l=f.next()).done);s=!0){var c=l.value;this[c.toLowerCase()].apply(this,i)}}catch(t){u=!0,d=t}finally{try{!s&&f.return&&f.return()}finally{if(u)throw d}}return this}},{key:"baseUri",set:function(t){if(this._baseUri)throw new TypeError("base uri is already set");this._baseUri=t},get:function(){return this._baseUri}}]),n}(),f=!0,c=!1,v=void 0;try{for(var y,w=function(){var e=y.value,r=e.toLowerCase();l.prototype[r]=function(){for(var r=arguments.length,i=Array(r),n=0;n<r;n++)i[n]=arguments[n];var a=t(i),o=a.baseUri,s=a.middleware;if(!s)throw new TypeError(h);if(o&&this._baseUri&&this._baseUri instanceof RegExp)throw new TypeError("router cannot mix uri/regexp");return this._add(new d(this,o,e,s)),this}},p=e[Symbol.iterator]();!(f=(y=p.next()).done);f=!0)w()}catch(t){c=!0,v=t}finally{try{!f&&p.return&&p.return()}finally{if(c)throw v}}var m=function(){function n(){r(this,n),this.routers=[],this.isDOMLoaded=!1,this.isDOMReady=!1,this.settings=new s}return i(n,[{key:"set",value:function(){for(var t=arguments.length,e=Array(t),r=0;r<t;r++)e[r]=arguments[r];if(1===e.length)return this.settings.get([e]);var i=e[0],n=e[1];return this.settings.set(i,n),this}},{key:"listen",value:function(t){var e=this;window.onbeforeunload=function(){e._callMiddlewareMethod("exited")},window.onpopstate=function(t){if(t.state){var r=t.state,i=r.request,n=r.response,a=e._routes(i.uri,i.method);e._callMiddlewareMethod("entered",a,i),e._callMiddlewareMethod("updated",a,i,n)}},document.onreadystatechange=function(){var r={method:"GET",uri:window.location.pathname+window.location.search},i={status:200,statusText:"OK"},n=e._routes();"loading"!==document.readyState||e.isDOMLoaded?"interactive"!==document.readyState||e.isDOMReady||(e.isDOMLoaded||(e.isDOMLoaded=!0,e._callMiddlewareMethod("entered",n,r)),e.isDOMReady=!0,e._callMiddlewareMethod("updated",n,r,i),t&&t(r,i)):(e.isDOMLoaded=!0,e._callMiddlewareMethod("entered",n,r))}}},{key:"route",value:function(t){var e=new l(t);return this.routers.push(e),e}},{key:"use",value:function(){for(var r=arguments.length,i=Array(r),n=0;n<r;n++)i[n]=arguments[n];var a=t(i),o=a.baseUri,s=a.router,u=a.middleware;if(s)s.baseUri=o;else{if(!u)throw new TypeError("method takes at least a middleware or a router");s=new l(o);var d=!0,h=!1,f=void 0;try{for(var c,v=e[Symbol.iterator]();!(d=(c=v.next()).done);d=!0){var y=c.value;s[y.toLowerCase()](u)}}catch(t){h=!0,f=t}finally{try{!d&&v.return&&v.return()}finally{if(h)throw f}}}return this.routers.push(s),this}},{key:"_routes",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:window.location.pathname+window.location.search,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"GET",r=[],i=!0,n=!1,o=void 0;try{for(var s,u=this.routers[Symbol.iterator]();!(i=(s=u.next()).done);i=!0){var d=s.value;r.push.apply(r,a(d.routes(t,e)))}}catch(t){n=!0,o=t}finally{try{!i&&u.return&&u.return()}finally{if(n)throw o}}return r}},{key:"_callMiddlewareMethod",value:function(t,e,r,i){if("exited"!==t){var n=!0,a=!1,o=void 0;try{for(var s,u=e[Symbol.iterator]();!(n=(s=u.next()).done);n=!0){var d=s.value;if("updated"===t&&(d.visited=r),d.middleware[t]){if(d.middleware[t](r,i),d.middleware.next&&!d.middleware.next())break}else if("entered"!==t){var h=!0,l=function(){h=!1};if(d.middleware(r,i,l),h)break}}}catch(t){a=!0,o=t}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}}else{var f=!0,c=!1,v=void 0;try{for(var y,w=this.routers[Symbol.iterator]();!(f=(y=w.next()).done);f=!0){var p=y.value,m=!0,g=!1,b=void 0;try{for(var x,M=p.visited()[Symbol.iterator]();!(m=(x=M.next()).done);m=!0){var T=x.value;T.middleware.exited&&(T.middleware.exited(T.visited),T.visited=null)}}catch(t){g=!0,b=t}finally{try{!m&&M.return&&M.return()}finally{if(g)throw b}}}}catch(t){c=!0,v=t}finally{try{!f&&w.return&&w.return()}finally{if(c)throw v}}}}},{key:"_fetch",value:function(t,e,r){var i=this,n=t.method,a=t.uri,o=t.headers,s=t.data,u=t.history,d=this.get("http "+n+" transformer");d&&(a=d.uri?d.uri({uri:a,headers:o,data:s}):a,o=d.headers?d.headers({uri:a,headers:o,data:s}):o,s=d.data?d.data({uri:a,headers:o,data:s}):s),this._callMiddlewareMethod("exited");var h=this._routes(a,n);this._callMiddlewareMethod("entered",h,t),this.settings.get("http requester").fetch(t,function(t,r){u&&window.history.pushState({request:t,response:r},u.title,u.uri),i._callMiddlewareMethod("updated",h,t,r),e&&e(t,r)},function(t,e){i._callMiddlewareMethod("failed",h,t,e),r&&r(t,e)})}}]),n}();e.reduce(function(e,r){var i=r.toLowerCase();e[i]=function(){for(var e=arguments.length,r=Array(e),n=0;n<e;n++)r[n]=arguments[n];var a=t(r),o=a.baseUri,s=a.middleware,u=a.which;if("get"===i&&"string"==typeof u)return this.settings.get(u);if(!s)throw new TypeError("method takes a middleware "+("get"===i?"or a string":""));var d=new l;return d[i](o,s),this.routers.push(d),this};var n="http"+r.charAt(0).toUpperCase()+r.slice(1).toLowerCase();return e[n]=function(t,e,i){var n=t.uri,a=t.headers,o=t.data,s=t.history;return n||(n=t),this._fetch({uri:n,method:r,headers:a,data:o,history:s},e,i)},e},m.prototype);var g=function(){return new m};return g.Router=function(t){return new l(t)},g.Middleware=u,g}();
//# sourceMappingURL=frontexpress.min.js.map