Skip to main content
Module

x/alosaur_lite/dist/mod.js

Lightweight version of Alosaur. Specially designed to work with deno deploy.
Latest
File
var g;(function(t){t.Get="GET",t.Post="POST",t.Put="PUT",t.Delete="DELETE",t.Path="PATCH",t.Options="OPTIONS"})(g||(g={}));var p;(function(t){t.Cookie="cookie",t.Context="context",t.Response="response",t.Request="request",t.Query="query",t.RouteParam="route-param",t.Body="body"})(p||(p={}));var d=class{constructor(e){this.request=e}};var f={};function i(){return f.routingControllersMetadataArgsStorage||(f.routingControllersMetadataArgsStorage=new y),f.routingControllersMetadataArgsStorage}var y=class{constructor(){this.controllers=[];this.actions=[];this.params=[]}};var m=(t,e)=>!e||t===e,b=t=>t.split("/").reduce((e,n,r)=>{if(/:[A-Za-z1-9]{1,}/.test(n)){let a=n.replace(":","");e.push({i:r,el:a})}return e},[]),k=t=>t.replace(/\/\:[^/]{1,}/gi,"/[^/]{1,}").replace(/\//g,"\\/"),C=(t,e,n)=>t.filter(r=>r.route.includes("/:")&&m(r.method,n)).find(r=>new RegExp("^"+k(r.route)+"$").test(e)),A=(t,e,n)=>t.find(r=>m(r.method,n)&&r.route===e),P=(t,e,n)=>t.filter(r=>r.regexpRoute&&m(r.method.toString(),n)).find(r=>{let a=new RegExp("^"+r.route);return a.test(e)&&r.regexpRoute.test(e.replace(a,""))});function w(t){return R(t).pathname}var x=new Map;function R(t){return x.has(t)||x.set(t,new URL(t)),x.get(t)}function M(t,e,n){let r=w(n),a={},o=A(t,r,e);if(o||(o=P(t,r,e)),!o&&(o=C(t,r,e),o)){let u=b(o.route),s=r.split("/");u.forEach(c=>{a[c.el]=s[c.i]})}return o?{controllerObject:o.controllerObject,actionObject:o.actionObject,target:o.target,action:o.action,actionMetadata:o.actionMetadata,params:o.params,routeParams:a}:null}async function S(t,e){if(e.params.length==0)return[];let n=[],r=e.params.sort((a,o)=>a.index-o.index);for(let a=0;a<r.length;a++){let o=r[a];switch(o.type){case"query":let u=_(t.request.url);if(u&&o.name){let s=u.get(o.name);n.push(s||void 0)}else n.push(void 0);break;case"cookie":case"body":break;case"request":n.push(t.request);break;case"response":n.push(t.response);break;case"context":n.push(t);break;case"route-param":e.routeParams&&o.name?n.push(e.routeParams[o.name]):n.push(void 0);break;default:n.push(void 0);break}}return n}function _(t){let e=t.split("?")[1];if(!!e)return new URLSearchParams(e)}var j=class extends Response{constructor(){super(...arguments);this.__isContentResult__=!0}};function l(t,e=200){let n=new Headers,r=t;switch(typeof t){case"object":case"boolean":case"number":n.set("content-type","application/json; charset=utf-8"),r=JSON.stringify(t);break;default:n.set("content-type","text/html; charset=UTF-8");break}return new j(r,{status:e,headers:n})}async function Q(t,e,n=200){let r=O();return await r.getBody(t,e,r)&&l(await r.getBody(t,e,r),n)}async function F(t,{root:e,index:n,baseRoute:r}){let o=R(t.url).pathname;if(r){let s=new RegExp(`^${r}`);if(s.test(o))o=o.replace(s,"");else return null}let u;try{o[0]==="/"&&(o=o.replace("/","")),(o[o.length-1]==="/"||o==="")&&(o+=n);let c=new URL(o,e).toString();if(u=await fetch(new Request(c,t)),u.status===404)return null}catch(s){return null}return u}function O(){return window.viewRenderConfig}var v=class{constructor(e){this.settings=e;this.classes=[];this.viewRenderConfig=void 0;this._routes=[];this.metadata=i(),V(this.metadata,this.classes,n=>this._routes.push(n))}async handleRequest(e){let n=new d(e);if(this._staticConfig){let a=await F(e,this._staticConfig);if(a)return a}let r=M(this._routes,e.method,e.url);if(r!==null){let a=await S(n,r),o=await r.target[r.action](...a);if(o)return o.__isContentResult__||o instanceof Response?o:l(o)}return l("Not found",404)}useStatic(e){e&&!this._staticConfig&&(this._staticConfig=e)}useViewRender(e){e&&!this.viewRenderConfig&&(this.viewRenderConfig=e,window.viewRenderConfig=e)}};function V(t,e=[],n){t.controllers.forEach(r=>{let a=t.actions.filter(s=>s.target===r.target),o=t.params.filter(s=>s.target===r.target);r.actions=a;let u=new r.object;e.push(u),a.forEach(s=>{s.controller=r;let c="";r.route&&(c+=r.route);let h=s.route instanceof RegExp?s.route:void 0;!h&&s.route&&(c+=s.route),c===""&&(c="/");let E={route:c,regexpRoute:h,target:u,actionObject:s.object,controllerObject:r.target,actionMetadata:s,action:s.method,method:s.type,params:o.filter(U=>U.method===s.method)};n(E)})})}function gt(t){return function(e){i().controllers.push({type:"default",object:e,target:e,route:t})}}function pt(t){return function(e,n){i().actions.push({type:g.Get,object:e,target:e.constructor,method:n,route:t})}}function lt(t){return function(e,n){i().actions.push({type:g.Post,object:e,target:e.constructor,method:n,route:t})}}function dt(t){return function(e,n){i().actions.push({type:g.Put,object:e,target:e.constructor,method:n,route:t})}}function ft(t){return function(e,n){i().actions.push({type:g.Path,object:e,target:e.constructor,method:n,route:t})}}function mt(t){return function(e,n){i().actions.push({type:g.Delete,object:e,target:e.constructor,method:n,route:t})}}function Rt(){return function(t,e,n){i().params.push({type:p.Context,target:t.constructor,method:e,index:n})}}function xt(t){return function(e,n,r){i().params.push({type:p.RouteParam,target:e.constructor,method:n,index:r,name:t})}}function ht(t){return function(e,n,r){i().params.push({type:p.Query,target:e.constructor,method:n,index:r,name:t})}}export{v as App,l as Content,j as ContentResponse,gt as Controller,Rt as Ctx,mt as Delete,pt as Get,d as HttpContext,y as MetadataArgsStorage,xt as Param,p as ParamType,ft as Patch,lt as Post,dt as Put,ht as QueryParam,g as RequestMethod,Q as View,i as getMetadataArgsStorage,O as getViewRenderConfig,V as registerControllers,F as sendStaticFiles};