mirror of
https://gitlab.silvrtree.co.uk/martind2000/frontexpress.git
synced 2025-01-11 03:35:09 +00:00
3 lines
9.3 KiB
JavaScript
3 lines
9.3 KiB
JavaScript
var frontexpress=function(){"use strict";function e(e,t){if(t.method&&t.method!==e.method)return!1;if(!t.uri||!e.uri)return!0;var r=/^(.*)\?.*#.*|(.*)(?=\?|#)|(.*[^\?#])$/.exec(e.uri),n=r[1]||r[2]||r[3];if(t.uri instanceof RegExp)return null!==n.match(t.uri);if(-1!==t.uri.indexOf(":")){for(var i=function(e){return!isNaN(parseFloat(e))&&isFinite(e)?Number.isInteger(e)?Number.parseInt(e,10):Number.parseFloat(e):e},a=[],u=/:([^\/\?]+)\??/g,o=u.exec(t.uri);null!=o;)a.push(o[1]),o=u.exec(t.uri);var s=t.uri.replace(/\/:[^\/]+\?/g,"(?:/([^/]+))?").replace(/:[^\/]+/g,"([^/]+)").replace("/","\\/"),f=n.match(new RegExp("^"+s+"$"));return!!f&&(e.params=Object.assign(e.params,a.reduce(function(e,t,r){var n=f[r+1];return n&&(n=-1!==n.indexOf(",")?n.split(",").map(function(e){return i(e)}):n=i(n)),e[t]=n,e},{})),!0)}return t.uri===n}function t(e,t){if("function"!=typeof e)throw new TypeError(t)}function r(e){var t,r,n,i,a=void 0,u=void 0,s=void 0,h=void 0,c=void 0;return 1===e.length?(t=e,r=o(t,1),c=r[0]):(n=e,i=o(n,2),a=i[0],c=i[1]),c instanceof d?s=c:c instanceof f||"function"==typeof c?u=c:c&&c.plugin&&"function"==typeof c.plugin&&(h=c),{baseUri:a,middleware:u,router:s,plugin:h,which:c}}var n=["GET","POST","PUT","PATCH","DELETE"],i="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},a=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},u=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),o=function(){function e(e,t){var r=[],n=!0,i=!1,a=void 0;try{for(var u,o=e[Symbol.iterator]();!(n=(u=o.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){i=!0,a=e}finally{try{!n&&o.return&&o.return()}finally{if(i)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")}}(),s=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)},f=function(){function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"";a(this,e),this.name=t}return u(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,i){a(this,e),this.router=t,this.uriPart=r,this.method=n,this.middleware=i,this.visited=!1}return u(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}(),d=function(){function e(t){a(this,e),this._baseUri=t,this._routes=[]}return u(e,[{key:"_add",value:function(e){return this._routes.push(e),this}},{key:"routes",value:function(e,t){t.params=t.params||{};var r=e.get("route matcher");return this._routes.filter(function(e){return r(t,e)})}},{key:"visited",value:function(){return this._routes.filter(function(e){return e.visited})}},{key:"use",value:function(e){if(!(e instanceof f)&&"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 e=this,t=arguments.length,i=Array(t),a=0;a<t;a++)i[a]=arguments[a];if(!r(i).middleware)throw new TypeError("method takes at least a middleware");return n.forEach(function(t){e[t.toLowerCase()].apply(e,i)}),this}},{key:"baseUri",set:function(e){if(e){if(!this._baseUri)return void(this._baseUri=e);if(i(this._baseUri)!==(void 0===e?"undefined":i(e)))throw new TypeError("router cannot mix regexp and uri")}},get:function(){return this._baseUri}}]),e}();n.forEach(function(e){var t=e.toLowerCase();d.prototype[t]=function(){for(var t=arguments.length,n=Array(t),i=0;i<t;i++)n[i]=arguments[i];var a=r(n),u=a.baseUri,o=a.middleware;if(!o)throw new TypeError("method takes at least a middleware");if(u&&this._baseUri&&this._baseUri instanceof RegExp)throw new TypeError("router cannot mix uri/regexp");return this._add(new h(this,u,e,o)),this}});var c=function(){function e(){a(this,e)}return u(e,[{key:"fetch",value:function(e,t,r){var n=e.method,i=e.uri,a=e.headers,u=e.data,o=function(r){t(e,{status:200,statusText:"OK",responseText:r})},s=function(t){var n=t.status,i=t.statusText,a=t.errorThrown;r(e,{status:n,statusText:i,errorThrown:a,errors:"HTTP "+n+" "+(i||"")})},f=new XMLHttpRequest;f.onreadystatechange=function(){4===f.readyState&&(200===f.status?o(f.responseText):s({status:f.status,statusText:f.statusText}))};try{f.open(n,i,!0),a&&Object.keys(a).forEach(function(e){f.setRequestHeader(e,a[e])}),u?f.send(u):f.send()}catch(e){s({errorThrown:e})}}}]),e}(),l={uri:function(e){var t=e.uri,r=(e.headers,e.data);if(!r)return t;var n=t,i="";if(/^(.*)(#.*)$/.exec(t)){var a=/^(.*)(#.*)$/.exec(t),u=o(a,3);n=u[1],i=u[2]}return(n=Object.keys(r).reduce(function(e,t,n){return e+=(0===n&&-1===e.indexOf("?")?"?":"&")+t+"="+r[t]},n))+i}},p=function(){function r(){a(this,r),this.settings={"http requester":new c,"http GET transformer":l,"route matcher":e},this.rules={"http requester":function(e){t(e.fetch,"setting http requester has no fetch function")},"http GET transformer":function(e){if(!e||!e.uri&&!e.headers&&!e.data)throw new TypeError("setting http transformer one of functions: uri, headers, data is missing")},"route matcher":function(e){t(e,"setting route matcher is not a function")}}}return u(r,[{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]}}]),r}(),v=function(){function e(){a(this,e),this.routers=[],this.settings=new p,this.plugins=[]}return u(e,[{key:"set",value:function(){for(var e,t=arguments.length,r=Array(t),n=0;n<t;n++)r[n]=arguments[n];return 1===r.length?this.settings.get([r]):((e=this.settings).set.apply(e,r),this)}},{key:"listen",value:function(e){var t=this,r={method:"GET",uri:window.location.pathname+window.location.search},n={status:200,statusText:"OK"},i=this._routes(r);this._callMiddlewareMethod("entered",i,r),window.onpopstate=function(e){if(e.state){var r=e.state,n=r.request,i=r.response;["exited","entered","updated"].forEach(function(e){return t._callMiddlewareMethod(e,t._routes(n),n,i)})}},window.onbeforeunload=function(){t._callMiddlewareMethod("exited")};var a=function(){t.plugins.forEach(function(e){return e.plugin(t)}),t._callMiddlewareMethod("updated",i,r,n),e&&e(r,n)};document.onreadystatechange=function(){"interactive"===document.readyState&&a()},-1!==["interactive","complete"].indexOf(document.readyState)&&a()}},{key:"route",value:function(e){var t=new d(e);return this.routers.push(t),t}},{key:"use",value:function(){for(var e=arguments.length,t=Array(e),i=0;i<e;i++)t[i]=arguments[i];var a=r(t),u=a.baseUri,o=a.router,s=a.middleware,f=a.plugin;if(f)this.plugins.push(f);else{if(o)o.baseUri=u;else{if(!s)throw new TypeError("method takes at least a middleware or a router");o=new d(u),n.forEach(function(e){o[e.toLowerCase()](s)})}this.routers.push(o)}return this}},{key:"_routes",value:function(e){var t=this;return this.routers.reduce(function(r,n){return r.push.apply(r,s(n.routes(t,e))),r},[])}},{key:"_callMiddlewareMethod",value:function(e,t,r,n){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,n),t.middleware.next&&!t.middleware.next())return!0}else if("entered"!==e){var i=!0,a=function(){i=!1};if(t.middleware(r,n,a),i)return!0}return!1})}},{key:"_fetch",value:function(e,t,r){var n=this,i=e.method,a=e.uri,u=e.headers,o=e.data,s=e.history,f=this.get("http "+i+" transformer");if(f){var h=f.uri,d=f.headers,c=f.data;e.uri=h?h({uri:a,headers:u,data:o}):a,e.headers=d?d({uri:a,headers:u,data:o}):u,e.data=c?c({uri:a,headers:u,data:o}):o}this._callMiddlewareMethod("exited");var l=this._routes(e);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),n._callMiddlewareMethod("updated",l,e,r),t&&t(e,r)},function(e,t){n._callMiddlewareMethod("failed",l,e,t),r&&r(e,t)})}}]),e}();n.reduce(function(e,t){var n=t.toLowerCase();return e[n]=function(){for(var e=arguments.length,t=Array(e),i=0;i<e;i++)t[i]=arguments[i];var a=r(t),u=a.baseUri,o=a.middleware,s=a.which;if("get"===n&&"string"==typeof s)return this.settings.get(s);if(!o)throw new TypeError("method takes a middleware "+("get"===n?"or a string":""));var f=new d;return f[n](u,o),this.routers.push(f),this},e["http"+t.charAt(0).toUpperCase()+t.slice(1).toLowerCase()]=function(e,r,n){var i=e.uri,a=e.headers,u=e.data,o=e.history;return i||(i=e),this._fetch({uri:i,method:t,headers:a,data:u,history:o},r,n)},e},v.prototype);var y=function(){return new v};return y.Router=function(e){return new d(e)},y.Middleware=f,y}();
|
|
//# sourceMappingURL=frontexpress.min.js.map
|