mirror of
https://gitlab.silvrtree.co.uk/martind2000/frontexpress.git
synced 2025-02-09 21:09:16 +00:00
3 lines
8.2 KiB
JavaScript
3 lines
8.2 KiB
JavaScript
var frontexpress=function(){"use strict";function t(t){var e=void 0,r=void 0,i=void 0,n=void 0;if(t&&t.length>0){if(1===t.length)n=a(t,1)[0];else{var o=a(t,2);e=o[0],n=o[1]}n instanceof f?i=n:(n instanceof h||"function"==typeof n)&&(r=n)}return{baseUri:e,middleware:r,router:i,which:n}}var e=["GET","POST","PUT","DELETE"],r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},n=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}}(),a=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")}}(),o=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)},s=function(){function t(){i(this,t)}return n(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||"")})},h=new XMLHttpRequest;h.onreadystatechange=function(){4===h.readyState&&(200===h.status?s(h.responseText):u({status:h.status,statusText:h.statusText}))};try{h.open(i,n,!0),a&&Object.keys(a).forEach(function(t){h.setRequestHeader(t,a[t])}),o?h.send(o):h.send()}catch(t){u({errorThrown:t})}}}]),t}(),u=function(){function t(){i(this,t),this.settings={"http requester":new s,"http GET transformer":{uri:function(t){var e=t.uri,r=(t.headers,t.data);if(!r)return e;var i=e,n="";if(/^(.*)(#.*)$/.exec(e)){var o=/^(.*)(#.*)$/.exec(e),s=a(o,3);i=s[1],n=s[2]}return(i=Object.keys(r).reduce(function(t,e,i){return t+=(0===i&&-1===t.indexOf("?")?"?":"&")+e+"="+r[e]},i))+n}}},this.rules={"http requester":function(t){if("function"!=typeof t.fetch)throw new TypeError("setting http requester has no fetch method")}}}return n(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}(),h=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";i(this,t),this.name=e}return n(t,[{key:"next",value:function(){return!0}}]),t}(),d=function(){function t(e,r,n,a){i(this,t),this.router=e,this.uriPart=r,this.method=n,this.middleware=a,this.visited=!1}return n(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}(),f=function(){function a(t){i(this,a),this._baseUri=t,this._routes=[]}return n(a,[{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 h)&&"function"!=typeof t)throw new TypeError("method takes at least a middleware");return this._add(new d(this,void 0,void 0,t)),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(!t(n).middleware)throw new TypeError("method takes at least a middleware");return e.forEach(function(t){r[t.toLowerCase()].apply(r,n)}),this}},{key:"baseUri",set:function(t){if(t){if(!this._baseUri)return void(this._baseUri=t);if(r(this._baseUri)!==(void 0===t?"undefined":r(t)))throw new TypeError("router cannot mix regexp and uri")}},get:function(){return this._baseUri}}]),a}();e.forEach(function(e){var r=e.toLowerCase();f.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("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 d(this,o,e,s)),this}});var c=function(){function r(){i(this,r),this.routers=[],this.settings=new u}return n(r,[{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.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)}};var r={method:"GET",uri:window.location.pathname+window.location.search},i={status:200,statusText:"OK"},n=this._routes(),a=function(){e._callMiddlewareMethod("updated",n,r,i),t&&t(r,i)};window.onbeforeunload=function(){e._callMiddlewareMethod("exited")},document.onreadystatechange=function(){switch(document.readyState){case"loading":e._callMiddlewareMethod("entered",n,r);break;case"interactive":a()}},-1!==["interactive","complete"].indexOf(document.readyState)&&a()}},{key:"route",value:function(t){var e=new f(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 f(o),e.forEach(function(t){s[t.toLowerCase()](u)})}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=[];return this.routers.forEach(function(i){r.push.apply(r,o(i.routes(t,e)))}),r}},{key:"_callMiddlewareMethod",value:function(t,e,r,i){if("exited"===t)return void this.routers.forEach(function(t){t.visited().forEach(function(t){t.middleware.exited&&(t.middleware.exited(t.visited),t.visited=null)})});e.some(function(e){if("updated"===t&&(e.visited=r),e.middleware[t]){if(e.middleware[t](r,i),e.middleware.next&&!e.middleware.next())return!0}else if("entered"!==t){var n=!0,a=function(){n=!1};if(e.middleware(r,i,a),n)return!0}return!1})}},{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,h=this.get("http "+n+" transformer");if(h){var d=h.uri,f=h.headers,c=h.data;a=d?d({uri:a,headers:o,data:s}):a,o=f?f({uri:a,headers:o,data:s}):o,s=c?c({uri:a,headers:o,data:s}):s}this._callMiddlewareMethod("exited");var l=this._routes(a,n);this._callMiddlewareMethod("entered",l,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",l,t,r),e&&e(t,r)},function(t,e){i._callMiddlewareMethod("failed",l,t,e),r&&r(t,e)})}}]),r}();e.reduce(function(e,r){var i=r.toLowerCase();return 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 h=new f;return h[i](o,s),this.routers.push(h),this},e["http"+r.charAt(0).toUpperCase()+r.slice(1).toLowerCase()]=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},c.prototype);var l=function(){return new c};return l.Router=function(t){return new f(t)},l.Middleware=h,l}();
|
|
//# sourceMappingURL=frontexpress.min.js.map
|