@micahjonas/switchboard
Version:
Switchboard, a small fetch api router useful for cloudflare workers. Based on Trouter.
3 lines (2 loc) • 3.59 kB
JavaScript
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("trouter")):"function"==typeof define&&define.amd?define(["trouter"],t):(n||self).switchboard=t(n.trouter)}(this,function(n){function t(n){return n&&"object"==typeof n&&"default"in n?n:{default:n}}function r(n,t){return(r=Object.setPrototypeOf||function(n,t){return n.__proto__=t,n})(n,t)}function o(n,t,e){if(!n.s){if(e instanceof u){if(!e.s)return void(e.o=o.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=e;var r=n.o;r&&r(n)}}var i=function(n){var t,i;function h(t){return void 0===t&&(t={}),n.call(this,t)||this}i=n,(t=h).prototype=Object.create(i.prototype),t.prototype.constructor=t,r(t,i);var a=h.prototype;return a.handleRequest=function(n){try{var t,r,i,h,a=new URL(n.url),l=this.find(n.method,a.pathname),v=l.handlers,d=l.params;return Promise.resolve(c(function(){function a(n){return t?n:new Response(JSON.stringify(e,null," "),{status:400,statusText:"No response generated"})}var l,p=!0,y=!1,b=s(function(){return c(function(){return r=function(n){var t;if("undefined"!=typeof Symbol){if(Symbol.asyncIterator&&null!=(t=n[Symbol.asyncIterator]))return t.call(n);if(Symbol.iterator&&null!=(t=n[Symbol.iterator]))return t.call(n)}throw new TypeError("Object is not async iterable")}(v),function(n,t,e){for(var r;;){var i=n();if(f(i)&&(i=i.v),!i)return c;if(i.then){r=0;break}var c=e();if(c&&c.then){if(!f(c)){r=1;break}c=c.s}if(t){var s=t();if(s&&s.then&&!f(s)){r=2;break}}}var h=new u,a=o.bind(null,h,2);return(0===r?i.then(v):1===r?c.then(l):s.then(d)).then(void 0,a),h;function l(r){c=r;do{if(t&&(s=t())&&s.then&&!f(s))return void s.then(d).then(void 0,a);if(!(i=n())||f(i)&&!i.v)return void o(h,1,c);if(i.then)return void i.then(v).then(void 0,a);f(c=e())&&(c=c.v)}while(!c||!c.then);c.then(l).then(void 0,a)}function v(n){n?(c=e())&&c.then?c.then(l).then(void 0,a):l(c):o(h,1,c)}function d(){(i=n())?i.then?i.then(v).then(void 0,a):v(i):o(h,1,c)}}(function(){function n(n){function e(n){return!t&&(h=n,!p)}return p=i.done,i=n,t?e(!t&&i.value):Promise.resolve(!t&&i.value).then(e)}return t?!!n(!t&&r.next()):!!Promise.resolve(!t&&r.next()).then(n)},function(){return!!(p=!0)},function(){return Promise.resolve(h(n,d)).then(function(n){if(n&&n instanceof Response)return t=1,n})})},function(n){y=!0,l=n})},function(n,e){function o(r){if(t)return r;if(n)throw e;return e}var i=s(function(){var n=function(){if(!p&&null!=r.return)return Promise.resolve(r.return()).then(function(){})}();if(n&&n.then)return n.then(function(){})},function(n,t){if(y)throw l;if(n)throw t;return t});return i&&i.then?i.then(o):o(i)});return b&&b.then?b.then(a):a(b)},function(n){return new Response(JSON.stringify(n,null," "),{status:500,statusText:"Worker failed"})}))}catch(n){return Promise.reject(n)}},a.sendJsonResponse=function(n){return new Response(JSON.stringify(n),{headers:{"content-type":"text/html;charset=UTF-8"}})},h}(t(n).default),u=function(){function n(){}return n.prototype.then=function(t,e){var r=new n,i=this.s;if(i){var u=1&i?t:e;if(u){try{o(r,1,u(this.v))}catch(n){o(r,2,n)}return r}return this}return this.o=function(n){try{var i=n.v;1&n.s?o(r,1,t?t(i):i):e?o(r,1,e(i)):o(r,2,i)}catch(n){o(r,2,n)}},r},n}();function f(n){return n instanceof u&&1&n.s}function c(n,t){try{var e=n()}catch(n){return t(n)}return e&&e.then?e.then(void 0,t):e}function s(n,t){try{var e=n()}catch(n){return t(!0,n)}return e&&e.then?e.then(t.bind(null,!1),t.bind(null,!0)):t(!1,e)}return i});
//# sourceMappingURL=switchboard.umd.js.map