frontexpress/frontexpress.min.js

3 lines
8.4 KiB
JavaScript

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