frontexpress/frontexpress.min.js

3 lines
11 KiB
JavaScript

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.frontexpress=t()}(this,function(){"use strict";var e=["GET","HEAD","POST","PUT","DELETE","CONNECT","OPTIONS","TRACE","PATCH"],t=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},r=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}}(),i=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)},n=function(){function e(){t(this,e)}return r(e,[{key:"fetch",value:function(e,t,r){var i=this,n=e.method,a=e.uri,o=e.headers,s=e.data,u=function(r){t(e,{status:200,statusText:"OK",responseText:r})},d=function(t){var n=t.status,a=t.statusText,o=t.errorThrown,s=i._analyzeErrors({status:n,statusText:a,errorThrown:o});r(e,{status:n,statusText:a,errorThrown:o,errors:s})},l=new XMLHttpRequest;l.onreadystatechange=function(){4===l.readyState&&(200===l.status?u(l.responseText):d({status:l.status,statusText:l.statusText}))};try{if(l.open(n,a,!0),o){var f=!0,h=!1,c=void 0;try{for(var v,y=Object.keys(o)[Symbol.iterator]();!(f=(v=y.next()).done);f=!0){var w=v.value;l.setRequestHeader(w,o[w])}}catch(e){h=!0,c=e}finally{try{!f&&y.return&&y.return()}finally{if(h)throw c}}}s?l.send(s):l.send()}catch(e){d({errorThrown:e})}}},{key:"_analyzeErrors",value:function(e){if(e.errorThrown){if("SyntaxError"===e.errorThrown.name)return"Problem during data decoding [JSON]";if("TimeoutError"===e.errorThrown.name)return"Server is taking too long to reply";if("AbortError"===e.errorThrown.name)return"Request cancelled on server";if("NetworkError"===e.errorThrown.name)return"A network error occurred";throw e.errorThrown}return 0===e.status?"Server access problem. Check your network connection":401===e.status?"Your session has expired, Please reconnect. [code: 401]":404===e.status?"Page not found on server. [code: 404]":500===e.status?"Internal server error. [code: 500]":"Unknown error. "+(e.statusText?e.statusText:"")}}]),e}(),a=function(){function e(){t(this,e),this.settings={"http requester":new n,"http GET transformer":{uri:function(e){var t=e.uri,r=(e.headers,e.data);if(!r)return t;var i="",n=t,a=t.indexOf("#");return a>=1&&(n=t.slice(0,a),i=t.slice(a,t.length)),n=Object.keys(r).reduce(function(e,t,i){return e+=0===i&&e.indexOf("?")===-1?"?":"&",e+=t+"="+r[t]},n),n+i},data:function(e){e.uri,e.headers,e.data}}},this.rules={"http requester":function(e){if("function"!=typeof e.fetch)throw new TypeError("setting http requester has no fetch method")}}}return r(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}(),o=function(){function e(){var r=arguments.length<=0||void 0===arguments[0]?"":arguments[0];t(this,e),this.name=r}return r(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}(),s=function(){function e(r,i,n,a){t(this,e),this.router=r,this.uriPart=i,this.method=n,this.middleware=a,this.visited=!1}return r(e,[{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}}]),e}(),u=function(){function i(e){t(this,i),e&&(this._baseUri=e),this._routes=[]}return r(i,[{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.uri&&!r.method)return!0;if(r.method!==t)return!1;if(!r.uri)return!0;var i=e,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(e){return e.visited})}},{key:"use",value:function(e){if(!(e instanceof o)&&"function"!=typeof e)throw new TypeError("use method takes at least a middleware");return this._add(new s(this,void 0,void 0,e)),this}},{key:"all",value:function(){for(var t=arguments.length,r=Array(t),i=0;i<t;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 o)&&"function"!=typeof n)throw new TypeError("use all method takes at least a middleware");var a=!0,s=!1,u=void 0;try{for(var d,l=e[Symbol.iterator]();!(a=(d=l.next()).done);a=!0){var f=d.value;this[f.toLowerCase()].apply(this,r)}}catch(e){s=!0,u=e}finally{try{!a&&l.return&&l.return()}finally{if(s)throw u}}return this}},{key:"baseUri",set:function(e){if(e){if(!this._baseUri)return void(this._baseUri=e);if(this._baseUri instanceof RegExp)throw new TypeError("the router already contains a regexp uri "+this._baseUri.toString()+" It cannot be mixed with "+e.toString());if(e instanceof RegExp)throw new TypeError("the router already contains an uri "+this._baseUri.toString()+" It cannot be mixed with regexp "+e.toString())}},get:function(){return this._baseUri}}]),i}(),d=!0,l=!1,f=void 0;try{for(var h,c=function(){var e=h.value,t=e.toLowerCase();u.prototype[t]=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 "+t+" method takes at least a middleware");var a=void 0,u=void 0;if(1===i.length?u=i[0]:(a=i[0],u=i[1]),!(u instanceof o)&&"function"!=typeof u)throw new TypeError("use "+t+" 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 s(this,a,e,u)),this}},v=e[Symbol.iterator]();!(d=(h=v.next()).done);d=!0)c()}catch(e){l=!0,f=e}finally{try{!d&&v.return&&v.return()}finally{if(l)throw f}}var y=function(){function n(){t(this,n),this.routers=[],this.isDOMLoaded=!1,this.isDOMReady=!1,this.settings=new a}return r(n,[{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){var i=[t];return this.settings.get(i)}var n=t[0],a=t[1];return this.settings.set(n,a),this}},{key:"listen",value:function(e){var t=this;window.onbeforeunload=function(){t._callMiddlewareExited()},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._callMiddlewareEntered(a,i),t._callMiddlewareUpdated(a,i,n)}},document.onreadystatechange=function(){var r={method:"GET",uri:window.location.pathname+window.location.search},i={status:200,statusText:"OK"},n=t._routes();"loading"!==document.readyState||t.isDOMLoaded?"interactive"!==document.readyState||t.isDOMReady||(t.isDOMLoaded||(t.isDOMLoaded=!0,t._callMiddlewareEntered(n,r)),t.isDOMReady=!0,t._callMiddlewareUpdated(n,r,i),e&&e(r,i)):(t.isDOMLoaded=!0,t._callMiddlewareEntered(n,r))}}},{key:"route",value:function(e){var t=new u(e);return this.routers.push(t),t}},{key:"use",value:function(){for(var t=arguments.length,r=Array(t),i=0;i<t;i++)r[i]=arguments[i];if(0===r.length)throw new TypeError("use method takes at least a middleware or a router");var n=void 0,a=void 0,s=void 0,d=void 0;if(1===r.length?d=r[0]:(n=r[0],d=r[1]),!(d instanceof o||"function"==typeof d||d instanceof u))throw new TypeError("use method takes at least a middleware or a router");if(d instanceof u)s=d,s.baseUri=n;else{a=d,s=new u(n);var l=!0,f=!1,h=void 0;try{for(var c,v=e[Symbol.iterator]();!(l=(c=v.next()).done);l=!0){var y=c.value;s[y.toLowerCase()](a)}}catch(e){f=!0,h=e}finally{try{!l&&v.return&&v.return()}finally{if(f)throw h}}}return this.routers.push(s),this}},{key:"_routes",value:function(){var e=arguments.length<=0||void 0===arguments[0]?window.location.pathname+window.location.search:arguments[0],t=arguments.length<=1||void 0===arguments[1]?"GET":arguments[1],r=[],n=!0,a=!1,o=void 0;try{for(var s,u=this.routers[Symbol.iterator]();!(n=(s=u.next()).done);n=!0){var d=s.value,l=d.routes(e,t);r.push.apply(r,i(l))}}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}return r}},{key:"_callMiddlewareEntered",value:function(e,t){var r=!0,i=!1,n=void 0;try{for(var a,o=e[Symbol.iterator]();!(r=(a=o.next()).done);r=!0){var s=a.value;if(s.middleware.entered&&s.middleware.entered(t),s.middleware.next&&!s.middleware.next())break}}catch(e){i=!0,n=e}finally{try{!r&&o.return&&o.return()}finally{if(i)throw n}}}},{key:"_callMiddlewareUpdated",value:function(e,t,r){var i=!0,n=!1,a=void 0;try{for(var o,s=e[Symbol.iterator]();!(i=(o=s.next()).done);i=!0){var u=o.value;if(u.visited=t,u.middleware.updated){if(u.middleware.updated(t,r),u.middleware.next&&!u.middleware.next())break}else{var d=!0,l=function(){d=!1};if(u.middleware(t,r,l),d)break}}}catch(e){n=!0,a=e}finally{try{!i&&s.return&&s.return()}finally{if(n)throw a}}}},{key:"_callMiddlewareExited",value:function(){var e=!0,t=!1,r=void 0;try{for(var i,n=this.routers[Symbol.iterator]();!(e=(i=n.next()).done);e=!0){var a=i.value,o=a.visited(),s=!0,u=!1,d=void 0;try{for(var l,f=o[Symbol.iterator]();!(s=(l=f.next()).done);s=!0){var h=l.value;h.middleware.exited&&(h.middleware.exited(h.visited),h.visited=null)}}catch(e){u=!0,d=e}finally{try{!s&&f.return&&f.return()}finally{if(u)throw d}}}}catch(e){t=!0,r=e}finally{try{!e&&n.return&&n.return()}finally{if(t)throw r}}}},{key:"_callMiddlewareFailed",value:function(e,t,r){var i=!0,n=!1,a=void 0;try{for(var o,s=e[Symbol.iterator]();!(i=(o=s.next()).done);i=!0){var u=o.value;if(u.middleware.failed){if(u.middleware.failed(t,r),u.middleware.next&&!u.middleware.next())break}else{var d=!0,l=function(){d=!1};if(u.middleware(t,r,l),d)break}}}catch(e){n=!0,a=e}finally{try{!i&&s.return&&s.return()}finally{if(n)throw a}}}},{key:"_fetch",value:function(e,t,r){var i=this,n=e.method,a=e.uri,o=e.headers,s=e.data,u=e.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._callMiddlewareExited();var l=this._routes(a,n);this._callMiddlewareEntered(l,e),this.settings.get("http requester").fetch(e,function(e,r){u&&window.history.pushState({request:e,response:r},u.title,u.uri),i._callMiddlewareUpdated(l,e,r),t&&t(e,r)},function(e,t){i._callMiddlewareFailed(l,e,t),r&&r(e,t)})}}]),n}();e.reduce(function(e,t){var r=t.toLowerCase();e[r]=function(){for(var e=arguments.length,t=Array(e),i=0;i<e;i++)t[i]=arguments[i];if("get"===r){if(0===t.length)throw new TypeError(r+" method takes at least a string or a middleware");if(1===t.length){var n=t[0];if("string"==typeof n)return this.settings.get(n)}}else if(0===t.length)throw new TypeError(r+" method takes at least a middleware");var a=void 0,s=void 0,d=void 0;if(1===t.length?d=t[0]:(a=t[0],d=t[1]),!(d instanceof o)&&"function"!=typeof d)throw new TypeError(r+" method takes at least a middleware");var l=new u;return s=d,l[r](a,s),this.routers.push(l),this};var i="http"+t.charAt(0).toUpperCase()+t.slice(1).toLowerCase();return e[i]=function(e,r,i){var n=e.uri,a=e.headers,o=e.data,s=e.history;return n||(n=e),this._fetch({uri:n,method:t,headers:a,data:o,history:s},r,i)},e},y.prototype);var w=function(){return new y};return w.Router=function(e){return new u(e)},w.Middleware=function(e){return new o(e)},w});
//# sourceMappingURL=frontexpress.min.js.map