mirror of
https://gitlab.silvrtree.co.uk/martind2000/frontexpress.git
synced 2025-02-11 13:09:16 +00:00
3 lines
10 KiB
JavaScript
3 lines
10 KiB
JavaScript
var frontexpress=function(){"use strict";var t=["GET","POST","PUT","DELETE"],e=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},r=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}}(),i=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")}}(),n=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)},a=function(){function t(){e(this,t)}return r(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}(),o=function(){function t(){e(this,t),this.settings={"http requester":new a,"http GET transformer":{uri:function(t){var e=t.uri,r=(t.headers,t.data);if(!r)return e;var n=e,a="",o=/^(.*)(#.*)$/.exec(e);if(o){var s=/^(.*)(#.*)$/.exec(e),u=i(s,3);n=u[1],a=u[2]}return n=Object.keys(r).reduce(function(t,e,i){return t+=""+(0===i&&t.indexOf("?")===-1?"?":"&")+e+"="+r[e]},n),n+a}}},this.rules={"http requester":function(t){if("function"!=typeof t.fetch)throw new TypeError("setting http requester has no fetch method")}}}return r(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}(),s=function(){function t(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";e(this,t),this.name=r}return r(t,[{key:"next",value:function(){return!0}}]),t}(),u=function(){function t(r,i,n,a){e(this,t),this.router=r,this.uriPart=i,this.method=n,this.middleware=a,this.visited=!1}return r(t,[{key:"uri",get:function(){if(this.uriPart||this.method)return this.uriPart instanceof RegExp?this.uriPart:this.router.baseUri instanceof RegExp?this.router.baseUri:this.router.baseUri&&this.uriPart?(this.router.baseUri.trim()+this.uriPart.trim()).replace(/\/{2,}/,"/"):this.router.baseUri?this.router.baseUri.trim():this.uriPart}}]),t}(),d=function(){function i(t){e(this,i),t&&(this._baseUri=t),this._routes=[]}return r(i,[{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.uri&&!r.method)return!0;if(r.method!==e)return!1;if(!r.uri)return!0;var i=t,n=i.indexOf("?");n>=0&&(i=i.slice(0,n));var a=i.indexOf("#");return a>=0&&(i=i.slice(0,a)),r.uri instanceof RegExp?i.match(r.uri):r.uri===i})}},{key:"visited",value:function(){return this._routes.filter(function(t){return t.visited})}},{key:"use",value:function(t){if(!(t instanceof s)&&"function"!=typeof t)throw new TypeError("use method takes at least a middleware");return this._add(new u(this,void 0,void 0,t)),this}},{key:"all",value:function(){for(var e=arguments.length,r=Array(e),i=0;i<e;i++)r[i]=arguments[i];if(0===r.length)throw new TypeError("use all method takes at least a middleware");var n=void 0;if(n=1===r.length?r[0]:r[1],!(n instanceof s)&&"function"!=typeof n)throw new TypeError("use all method takes at least a middleware");var a=!0,o=!1,u=void 0;try{for(var d,h=t[Symbol.iterator]();!(a=(d=h.next()).done);a=!0){var l=d.value;this[l.toLowerCase()].apply(this,r)}}catch(t){o=!0,u=t}finally{try{!a&&h.return&&h.return()}finally{if(o)throw u}}return this}},{key:"baseUri",set:function(t){if(t){if(!this._baseUri)return void(this._baseUri=t);if(this._baseUri instanceof RegExp)throw new TypeError("the router already contains a regexp uri "+this._baseUri.toString()+" It cannot be mixed with "+t.toString());if(t instanceof RegExp)throw new TypeError("the router already contains an uri "+this._baseUri.toString()+" It cannot be mixed with regexp "+t.toString())}},get:function(){return this._baseUri}}]),i}(),h=!0,l=!1,f=void 0;try{for(var c,v=function(){var t=c.value,e=t.toLowerCase();d.prototype[e]=function(){for(var r=arguments.length,i=Array(r),n=0;n<r;n++)i[n]=arguments[n];if(0===i.length)throw new TypeError("use "+e+" method takes at least a middleware");var a=void 0,o=void 0;if(1===i.length?o=i[0]:(a=i[0],o=i[1]),!(o instanceof s)&&"function"!=typeof o)throw new TypeError("use "+e+" method takes at least a middleware");if(a&&this._baseUri&&this._baseUri instanceof RegExp)throw new TypeError("router contains a regexp cannot mix with route uri/regexp");return this._add(new u(this,a,t,o)),this}},y=t[Symbol.iterator]();!(h=(c=y.next()).done);h=!0)v()}catch(t){l=!0,f=t}finally{try{!h&&y.return&&y.return()}finally{if(l)throw f}}var w=function(){function i(){e(this,i),this.routers=[],this.isDOMLoaded=!1,this.isDOMReady=!1,this.settings=new o}return r(i,[{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 d(t);return this.routers.push(e),e}},{key:"use",value:function(){for(var e=arguments.length,r=Array(e),i=0;i<e;i++)r[i]=arguments[i];var n="use method takes at least a middleware or a router",a=void 0,o=void 0,u=void 0,h=void 0;if(!r||0===r.length)throw new TypeError(n);if(1===r.length?h=r[0]:(a=r[0],h=r[1]),h instanceof d)u=h,u.baseUri=a;else{if(!(h instanceof s||"function"==typeof h))throw new TypeError(n);o=h,u=new d(a);var l=!0,f=!1,c=void 0;try{for(var v,y=t[Symbol.iterator]();!(l=(v=y.next()).done);l=!0){var w=v.value;u[w.toLowerCase()](o)}}catch(t){f=!0,c=t}finally{try{!l&&y.return&&y.return()}finally{if(f)throw c}}}return this.routers.push(u),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,a=!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,n(d.routes(t,e)))}}catch(t){a=!0,o=t}finally{try{!i&&u.return&&u.return()}finally{if(a)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,x=void 0;try{for(var b,T=p.visited()[Symbol.iterator]();!(m=(b=T.next()).done);m=!0){var _=b.value;_.middleware.exited&&(_.middleware.exited(_.visited),_.visited=null)}}catch(t){g=!0,x=t}finally{try{!m&&T.return&&T.return()}finally{if(g)throw x}}}}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)})}}]),i}();t.reduce(function(t,e){var r=e.toLowerCase();t[r]=function(){for(var t=arguments.length,e=Array(t),i=0;i<t;i++)e[i]=arguments[i];var n=void 0,a=void 0,o=void 0;if(!e||0===e.length)throw new TypeError(r+" method takes at least a middleware "+("get"===r?"or a string":""));if(1===e.length){if(o=e[0],"get"===r&&"string"==typeof o)return this.settings.get(o)}else n=e[0],o=e[1];if(!(o instanceof s)&&"function"!=typeof o)throw new TypeError(r+" method takes at least a middleware");a=o;var u=new d;return u[r](n,a),this.routers.push(u),this};var i="http"+e.charAt(0).toUpperCase()+e.slice(1).toLowerCase();return t[i]=function(t,r,i){var n=t.uri,a=t.headers,o=t.data,s=t.history;return n||(n=t),this._fetch({uri:n,method:e,headers:a,data:o,history:s},r,i)},t},w.prototype);var p=function(){return new w};return p.Router=function(t){return new d(t)},p.Middleware=s,p}();
|
|
//# sourceMappingURL=frontexpress.min.js.map
|