elysia 0.2.8 → 0.2.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{mapQuery as s,clone as a,mergeHook as i,mergeDeep as n,createValidationError as
|
|
1
|
+
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{mapQuery as s,clone as a,mergeHook as i,mergeDeep as n,createValidationError as o,getSchemaValidator as h,SCHEMA as l,DEFS as d,getResponseSchemaValidator as u,mapPathnameAndQueryRegEx as f}from"./utils";import{registerSchemaPath as c}from"./schema";import{mapErrorCode as p,mapErrorStatus as m}from"./error";export default class v{store={[l]:{},[d]:{}};decorators=null;event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new e;routes=[];lazyLoadModules=[];constructor(e={}){this.config={...e}}_addHandler(e,t,r,s){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i(a(this.event),s)});let n=this.store[d],o=h(s?.schema?.body??this.$schema?.body,n),f=h(s?.schema?.headers??this.$schema?.headers,n,!0),p=h(s?.schema?.params??this.$schema?.params,n),m=h(s?.schema?.query??this.$schema?.query,n),v=u(s?.schema?.response??this.$schema?.response,n);c({schema:this.store[l],hook:s,method:e,path:t,models:this.store[d]});let g=i(a(this.event),s);g.schema||g.transform.length||g.beforeHandle.length||g.error.length||g.afterHandle.length||(g=void 0);let y={handle:r,hooks:g,validator:o||f||p||m||v?{body:o,header:f,params:p,query:m,response:v}:void 0};this.router.add(e,t,y)}onStart(e){return this.event.start.push(e),this}onRequest(e){return this.event.request.push(e),this}onParse(e){return this.event.parse.splice(this.event.parse.length-1,0,e),this}onTransform(e){return this.event.transform.push(e),this}onBeforeHandle(e){return this.event.beforeHandle.push(e),this}onAfterHandle(e){return this.event.afterHandle.push(e),this}onError(e){return this.event.error.push(e),this}onStop(e){return this.event.stop.push(e),this}on(e,t){switch(e){case"start":this.event.start.push(t);break;case"request":this.event.request.push(t);break;case"parse":this.event.parse.push(t);break;case"transform":this.event.transform.push(t);break;case"beforeHandle":this.event.beforeHandle.push(t);break;case"afterHandle":this.event.afterHandle.push(t);break;case"error":this.event.error.push(t);break;case"stop":this.event.stop.push(t)}return this}group(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:a,hooks:n})=>{"/"===r?this._addHandler(t,e,a,i(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,i(n,{error:s.event.error}))}),this}guard(e,t){let r=new v;r.store=this.store;let s=t(r);return s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),Object.values(r.routes).forEach(({method:t,path:r,handler:s,hooks:a})=>{this._addHandler(t,r,s,i(e,a))}),this}use(e){if(e instanceof Promise)return this.lazyLoadModules.push(e.then(e=>"function"==typeof e?e(this):e.default(this))),this;let t=e(this);return t instanceof Promise?(this.lazyLoadModules.push(t),this):t}get(e,t,r){return this._addHandler("GET",e,t,r),this}post(e,t,r){return this._addHandler("POST",e,t,r),this}put(e,t,r){return this._addHandler("PUT",e,t,r),this}patch(e,t,r){return this._addHandler("PATCH",e,t,r),this}delete(e,t,r){return this._addHandler("DELETE",e,t,r),this}options(e,t,r){return this._addHandler("OPTIONS",e,t,r),this}all(e,t,r){return this._addHandler("ALL",e,t,r),this}head(e,t,r){return this._addHandler("HEAD",e,t,r),this}trace(e,t,r){return this._addHandler("TRACE",e,t,r),this}connect(e,t,r){return this._addHandler("CONNECT",e,t,r),this}route(e,t,r,s){return this._addHandler(e,t,r,s),this}state(e,t){return e in this.store||(this.store[e]=t),this}decorate(e,t){return this.decorators||(this.decorators={}),e in this.decorators||(this.decorators[e]=t),this}derive(e){return this.store=n(this.store,e(()=>this.store)),this}inject(e){return this.onTransform(t=>{Object.assign(t,e(t))})}schema(e){let t=this.store[d];return this.$schema={body:h(e.body,t),headers:h(e?.headers,t,!0),params:h(e?.params,t),query:h(e?.query,t),response:h(e?.response,t)},this}handle=async e=>{let i,n;let h={status:200,headers:{}};this.decorators?((i=a(this.decorators)).request=e,i.set=h,i.store=this.store,i.query={}):i={set:h,store:this.store,request:e,query:{}};try{let a;if(this.event.request.length)for(let e=0;e<this.event.request.length;e++){let t=this.event.request[e](i);if(t instanceof Promise&&(t=await t),t=r(t,h))return t}let l=e.url.match(f);if(!l)throw Error("NOT_FOUND");let d=this.router.match(e.method,l[1])??this.router.match("ALL",l[1]),u=d?.store;if(!u)throw Error("NOT_FOUND");if("GET"!==e.method){let t=e.headers.get("content-type");if(t){let r=t.indexOf(";");if(-1!==r&&(t=t.slice(0,r)),this.event.parse.length)for(let e=0;e<this.event.parse.length;e++){let r=this.event.parse[e](i,t);if(r instanceof Promise&&(r=await r),void 0!==r){a=r;break}}if(void 0===a)switch(t){case"application/json":a=await e.json();break;case"text/plain":a=await e.text();break;case"application/x-www-form-urlencoded":a=s(await e.text())}}}i.body=a,i.params=d.params,l[2]&&(i.query=s(l[2]));let c=u.hooks;if(c?.error&&(n=c?.error),c?.transform.length)for(let e=0;e<c.transform.length;e++){let t=c.transform[e](i);t instanceof Promise&&await t}if(u.validator){let t=u.validator;if(t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw o("header",t.headers,r)}if(!1===t.params?.Check(i.params))throw o("params",t.params,i.params);if(!1===t.query?.Check(i.query))throw o("query",t.query,i.query);if(!1===t.body?.Check(a))throw o("body",t.body,a)}if(c?.beforeHandle.length)for(let e=0;e<c.beforeHandle.length;e++){let t=c.beforeHandle[e](i);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<c.afterHandle.length;e++){let r=c.afterHandle[e](i,t);r instanceof Promise&&(r=await r),r&&(t=r)}let e=r(t,i.set);if(e)return e}}let p=u.handle(i);if(p instanceof Promise&&(p=await p),!1===u.validator?.response?.Check(p))throw o("response",u.validator.response,p);if(c?.afterHandle.length)for(let e=0;e<c.afterHandle.length;e++){let t=c.afterHandle[e](i,p);t instanceof Promise&&(t=await t);let s=r(t,i.set);if(s)return s}return t(p,i.set)}catch(t){if((!h.status||h.status<300)&&(h.status=500),n){let s=p(t.message);for(let a=0;a<n.length;a++){let i=n[a]({request:e,error:t,set:h,code:s});i instanceof Promise&&(i=await i);let o=r(i,h);if(o)return o}}return this.handleError(e,t,h)}};async handleError(e,r,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let i=this.event.error[a]({request:e,code:p(r.message),error:r,set:s});if(i instanceof Promise&&(i=await i),null!=i)return t(i,s)}return new Response("string"==typeof r.cause?r.cause:r.message,{headers:s.headers,status:m(p(r.message))})}listen=(e,t)=>{if(!Bun)throw Error("Bun to run");if("string"==typeof e&&Number.isNaN(e=+e))throw Error("Port must be a numeric value");let r=this.handle,s="object"==typeof e?{...this.config.serve,...e,fetch:r}:{...this.config.serve,port:e,fetch:r},a=`$$Elysia:${s.port}`;globalThis[a]?(this.server=globalThis[a],this.server.reload(s)):globalThis[a]=this.server=Bun.serve(s);for(let e=0;e<this.event.start.length;e++)this.event.start[e](this);return t&&t(this.server),Promise.all(this.lazyLoadModules).then(()=>{this.server.pendingRequests||Bun.gc(!0)}),this};stop=async()=>{if(!this.server)throw Error("Elysia isn't running. Call `app.listen` to start the server.");this.server.stop();for(let e=0;e<this.event.stop.length;e++)await this.event.stop[e](this)};get modules(){return Promise.all(this.lazyLoadModules)}setModel(e){return Object.entries(e).forEach(([e,t])=>{e in this.store[d]||(this.store[d][e]=t)}),this}}export{Type as t}from"@sinclair/typebox";export{SCHEMA,DEFS,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{v as Elysia};
|