Comparing version 0.5.0-exp-230428.1644 to 0.5.0-exp-230428.1647
@@ -1,1 +0,1 @@ | ||
import{nanoid as e}from"nanoid";import{Memoirist as t}from"memoirist";import{mapResponse as r}from"./handler";import{SCHEMA as s,EXPOSED as a,DEFS as o,mergeHook as i,getSchemaValidator as h,getResponseSchemaValidator as n,mergeDeep as u}from"./utils";import{registerSchemaPath as d}from"./schema";import{mapErrorCode as l,mapErrorStatus as c}from"./error";import{composeGeneralHandler as p,composeHandler as m}from"./compose";import{ws as f}from"./ws";export default class v{config;store={};meta={[s]:Object.create(null),[o]:Object.create(null),[a]:Object.create(null)};decorators={};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new t;routes=[];_s=new Map;wsRouter;lazyLoadModules=[];constructor(e){this.config={fn:"/~fn",...e},this.fetch=p(this)}_addHandler(e,t,r,a,{allowMeta:u=!1}={allowMeta:!1}){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i({...this.event},a)});let l=this.meta[o];if(a?.type)switch(a.type){case"text":a.type="text/plain";break;case"json":a.type="application/json";break;case"formdata":a.type="multipart/form-data";break;case"urlencoded":a.type="application/x-www-form-urlencoded"}d({schema:this.meta[s],contentType:a?.type,hook:a,method:e,path:t,models:this.meta[o]});let c={body:h(a?.schema?.body??this.$schema?.body,l),headers:h(a?.schema?.headers??this.$schema?.headers,l,!0),params:h(a?.schema?.params??this.$schema?.params,l),query:h(a?.schema?.query??this.$schema?.query,l),response:n(a?.schema?.response??this.$schema?.response,l)},f=i(this.event,a),v=m({method:e,hooks:f,validator:c,handler:r,handleError:this.handleError,meta:u?this.meta:void 0});-1===t.indexOf(":")&&-1===t.indexOf("*")&&(this._s.has(e)||this._s.set(e,new Map),this._s.get(e).set(t,v)),this.router.add(e,t,v),this.fetch=p(this)}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,this.wsRouter&&r.use(f());let s=t(r);return this.decorators=u(this.decorators,r.decorators),s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:a,handler:o,hooks:h})=>{let n="/"===a?e:`${e}${a}`,u=r.wsRouter?.find("subscribe",n);if(u){let e=r.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(n,e[2])}this._addHandler(t,n,o,i(h,{error:s.event.error}))}),r.wsRouter&&this.wsRouter&&r.wsRouter.history.forEach(([t,r,s])=>{"/"===r?this.wsRouter?.add(t,e,s):this.wsRouter?.add(t,`${e}${r}`,s)}),this}guard(e,t){let r=new v;r.store=this.store,this.wsRouter&&r.use(f());let s=t(r);return this.decorators=u(this.decorators,r.decorators),s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:a,handler:o,hooks:h})=>{let n=r.wsRouter?.find("subscribe",a);if(n){let e=r.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(a,e[2])}this._addHandler(t,a,o,i(e,{...h,error:h.error?Array.isArray(h.error)?[...h.error,...s.event.error]:[h.error,...s.event.error]:s.event.error}))}),r.wsRouter&&this.wsRouter&&r.wsRouter.history.forEach(([e,t,r])=>{this.wsRouter?.add(e,t,r)}),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}if(e,t){return e?this.use(t):this}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}ws(t,r){if(!this.wsRouter)throw Error("Can't find WebSocket. Please register WebSocket plugin first by importing 'elysia/ws'");return this.wsRouter.add("subscribe",t,r),this.get(t,t=>{if(!this.server?.upgrade(t.request,{headers:"function"==typeof r.headers?r.headers(t):r.headers,data:{...t,id:e(),message:h(r.schema?.body,this.meta[o]),transformMessage:r.transform?Array.isArray(r.transformMessage)?r.transformMessage:[r.transformMessage]:[]}}))return t.set.status=400,"Expected a websocket connection"},{parse:r.parse,beforeHandle:r.beforeHandle,transform:r.transform,schema:{headers:r.schema?.headers,params:r.schema?.params,query:r.schema?.query}}),this}route(e,t,r,{config:s,...a}={config:{allowMeta:!1}}){return this._addHandler(e,t,r,a,s),this}state(e,t){return e in this.store||(this.store[e]=t),this}decorate(e,t){return e in this.decorators||(this.decorators[e]=t),this}derive(e){return"AsyncFunction"===e.constructor.name?this.onTransform(async t=>{Object.assign(t,await e(t))}):this.onTransform(t=>{Object.assign(t,e(t))})}fn(e){return this.use(async()=>{let{fn:t}=await import("@elysiajs/fn");return t({app:this,value:e,path:this.config.fn})})}schema(e){let t=this.meta[o];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=>this.fetch(e);handleError=async(e,t,s={headers:{}})=>{for(let a=0;a<this.event.error.length;a++){let o=this.event.error[a]({request:e,code:l(t.message),error:t,set:s});if(o instanceof Promise&&(o=await o),null!=o)return r(o,s)}return new Response("string"==typeof t.cause?t.cause:t.message,{headers:s.headers,status:c(l(t.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.fetch,s="object"==typeof e?{...this.config.serve,...e,fetch:r}:{...this.config.serve,port:e,fetch:r};if("production"!==process.env.ENV){let e=`$$Elysia:${s.port}`;globalThis[e]?(this.server=globalThis[e],this.server.reload(s)):globalThis[e]=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(()=>{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.meta[o]||(this.meta[o][e]=t)}),this}}export{t}from"./custom-types";export{ws}from"./ws";export{SCHEMA,DEFS,EXPOSED,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,getPath,getPathAndQuery,getResponseSchemaValidator}from"./utils";export{ElysiaError,ValidationError}from"./validation";export{v as Elysia}; | ||
import{nanoid as e}from"nanoid";import{Memoirist as t}from"memoirist";import{mapResponse as r}from"./handler";import{SCHEMA as s,EXPOSED as a,DEFS as o,mergeHook as i,getSchemaValidator as h,getResponseSchemaValidator as n,mergeDeep as u}from"./utils";import{registerSchemaPath as d}from"./schema";import{mapErrorCode as l,mapErrorStatus as c}from"./error";import{composeGeneralHandler as p,composeHandler as m}from"./compose";import{ws as f}from"./ws";export default class v{config;store={};meta={[s]:Object.create(null),[o]:Object.create(null),[a]:Object.create(null)};decorators={};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new t;routes=[];_s=new Map;wsRouter;lazyLoadModules=[];constructor(e){this.config={fn:"/~fn",...e},this.fetch=p(this)}_addHandler(e,t,r,a,{allowMeta:u=!1}={allowMeta:!1}){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:i({...this.event},a)});let l=this.meta[o];if(a?.type)switch(a.type){case"text":a.type="text/plain";break;case"json":a.type="application/json";break;case"formdata":a.type="multipart/form-data";break;case"urlencoded":a.type="application/x-www-form-urlencoded"}d({schema:this.meta[s],contentType:a?.type,hook:a,method:e,path:t,models:this.meta[o]});let c={body:h(a?.schema?.body??this.$schema?.body,l),headers:h(a?.schema?.headers??this.$schema?.headers,l,!0),params:h(a?.schema?.params??this.$schema?.params,l),query:h(a?.schema?.query??this.$schema?.query,l),response:n(a?.schema?.response??this.$schema?.response,l)},f=i(this.event,a),v=m({method:e,hooks:f,validator:c,handler:r,handleError:this.handleError,meta:u?this.meta:void 0});-1===t.indexOf(":")&&-1===t.indexOf("*")&&(this._s.has(e)||this._s.set(e,new Map),this._s.get(e).set(t,v)),this.router.add(e,t,v),this.fetch=p(this)}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,this.wsRouter&&r.use(f());let s=t(r);return this.decorators=u(this.decorators,r.decorators),s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:a,handler:o,hooks:h})=>{let n="/"===a?e:`${e}${a}`,u=r.wsRouter?.find("subscribe",n);if(u){let e=r.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(n,e[2])}this._addHandler(t,n,o,i(h,{error:s.event.error}))}),r.wsRouter&&this.wsRouter&&r.wsRouter.history.forEach(([t,r,s])=>{"/"===r?this.wsRouter?.add(t,e,s):this.wsRouter?.add(t,`${e}${r}`,s)}),this}guard(e,t){let r=new v;r.store=this.store,this.wsRouter&&r.use(f());let s=t(r);return this.decorators=u(this.decorators,r.decorators),s.event.request.length&&(this.event.request=[...this.event.request,...s.event.request]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:a,handler:o,hooks:h})=>{let n=r.wsRouter?.find("subscribe",a);if(n){let e=r.wsRouter.history.find(([e,t])=>a===t);if(!e)return;return this.ws(a,e[2])}this._addHandler(t,a,o,i(e,{...h,error:h.error?Array.isArray(h.error)?[...h.error,...s.event.error]:[h.error,...s.event.error]:s.event.error}))}),r.wsRouter&&this.wsRouter&&r.wsRouter.history.forEach(([e,t,r])=>{this.wsRouter?.add(e,t,r)}),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}if(e,t){return e?this.use(t):this}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}ws(t,r){if(!this.wsRouter)throw Error("Can't find WebSocket. Please register WebSocket plugin first by importing 'elysia/ws'");return this.wsRouter.add("subscribe",t,r),this.get(t,t=>{if(!this.server?.upgrade(t.request,{headers:"function"==typeof r.headers?r.headers(t):r.headers,data:{...t,id:e(),message:h(r.schema?.body,this.meta[o]),transformMessage:r.transform?Array.isArray(r.transformMessage)?r.transformMessage:[r.transformMessage]:[]}}))return t.set.status=400,"Expected a websocket connection"},{parse:r.parse,beforeHandle:r.beforeHandle,transform:r.transform,schema:{headers:r.schema?.headers,params:r.schema?.params,query:r.schema?.query}}),this}route(e,t,r,{config:s,...a}={config:{allowMeta:!1}}){return this._addHandler(e,t,r,a,s),this}state(e,t){return e in this.store||(this.store[e]=t),this}decorate(e,t){return e in this.decorators||(this.decorators[e]=t),this}derive(e){return"AsyncFunction"===e.constructor.name?this.onTransform(async t=>{Object.assign(t,await e(t))}):this.onTransform(t=>{Object.assign(t,e(t))})}fn(e){return this.use(async()=>{let{fn:t}=await import("@elysiajs/fn");return t({app:this,value:e,path:this.config.fn})})}schema(e){let t=this.meta[o];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=>this.fetch(e);handleError=async(e,t,s={headers:{}})=>{for(let a=0;a<this.event.error.length;a++){let o=this.event.error[a]({request:e,code:l(t.message),error:t,set:s});if(o instanceof Promise&&(o=await o),null!=o)return r(o,s)}return new Response("string"==typeof t.cause?t.cause:t.message,{headers:s.headers,status:c(l(t.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.fetch,s="object"==typeof e?{...this.config.serve,...e,fetch:r}:{...this.config.serve,port:e,fetch:r};if("production"!==process.env.ENV){let e=`$$Elysia:${s.port}`;globalThis[e]?(this.server=globalThis[e],this.server.reload(s)):globalThis[e]=this.server=Bun.serve(s)}else 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(()=>{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.meta[o]||(this.meta[o][e]=t)}),this}}export{t}from"./custom-types";export{ws}from"./ws";export{SCHEMA,DEFS,EXPOSED,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,getPath,getPathAndQuery,getResponseSchemaValidator}from"./utils";export{ElysiaError,ValidationError}from"./validation";export{v as Elysia}; |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.5.0-exp-230428.1644", | ||
"version": "0.5.0-exp-230428.1647", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "saltyAom", |
@@ -2298,2 +2298,4 @@ import type { Serve, Server } from 'bun' | ||
} | ||
} else { | ||
this.server = Bun.serve(serve) | ||
} | ||
@@ -2300,0 +2302,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
214605
5612