elysia
Advanced tools
Comparing version 0.3.0-exp-230222.1829 to 0.3.0-exp-230222.1921
@@ -89,5 +89,3 @@ /// <reference types="bun-types" /> | ||
}) => Record<string, unknown>)>(value: T): Elysia<{ | ||
store: Instance['store'] & { | ||
[EXPOSED]: T extends (store: any) => infer Returned ? Returned : T; | ||
}; | ||
store: Instance['store'] & Record<typeof EXPOSED, T extends (store: any) => infer Returned ? Returned : T>; | ||
request: Instance['request']; | ||
@@ -115,6 +113,5 @@ schema: Instance['schema']; | ||
export { Elysia, permission }; | ||
export { SCHEMA, DEFS, EXPOSED }; | ||
export { Type as t } from '@sinclair/typebox'; | ||
export { createValidationError, getSchemaValidator, mergeDeep, mergeHook, mergeObjectArray, mapPathnameAndQueryRegEx, mapQuery } from './utils'; | ||
export { SCHEMA, DEFS, EXPOSED, createValidationError, getSchemaValidator, mergeDeep, mergeHook, mergeObjectArray, mapPathnameAndQueryRegEx, mapQuery } from './utils'; | ||
export type { Context, PreContext } from './context'; | ||
export type { Handler, RegisteredHook, BeforeRequestHandler, TypedRoute, OverwritableTypeRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, ComposedHandler, InternalRoute, BodyParser, ErrorHandler, ErrorCode, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, AfterRequestHandler, HookHandler, TypedSchemaToRoute, UnwrapSchema, LifeCycleStore, VoidLifeCycle, SchemaValidator, ElysiaRoute, ExtractPath, IsPathParameter, IsAny, IsNever, UnknownFallback, WithArray, ObjectValues, PickInOrder, MaybePromise, MergeIfNotNull } from './types'; |
@@ -1,1 +0,1 @@ | ||
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{permission as s}from"./fn";import{SCHEMA as a,EXPOSED as i,DEFS as n,mapQuery as o,clone as h,mergeHook as l,mergeDeep as d,createValidationError as u,getSchemaValidator as f,getResponseSchemaValidator as c,mapPathnameAndQueryRegEx as p}from"./utils";import{registerSchemaPath as m}from"./schema";import{mapErrorCode as v,mapErrorStatus as g}from"./error";import{runFn as y}from"./fn";import{deserialize as b}from"superjson";export default class H{store={[a]:{},[n]:{},[i]:Object.create(null)};decorators={query:{},set:{status:200,headers:{}},store:this.store};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new e;routes=[];lazyLoadModules=[];constructor(e){this.config={fn:"/~fn",...e}}_addHandler(e,t,r,s){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:l(h(this.event),s)});let i=this.store[n],o=f(s?.schema?.body??this.$schema?.body,i),d=f(s?.schema?.headers??this.$schema?.headers,i,!0),u=f(s?.schema?.params??this.$schema?.params,i),p=f(s?.schema?.query??this.$schema?.query,i),v=c(s?.schema?.response??this.$schema?.response,i);m({schema:this.store[a],hook:s,method:e,path:t,models:this.store[n]});let g=l(h(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||d||u||p||v?{body:o,header:d,params:u,query:p,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 H;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:i})=>{"/"===r?this._addHandler(t,e,a,l(i,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,l(i,{error:s.event.error}))}),this}guard(e,t){let r=new H;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,l(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 e in this.decorators||(this.decorators[e]=t),this}inject(e){return this.onTransform(t=>{Object.assign(t,e(t))})}fn(e){return 0===Object.keys(this.store[i]).length&&this.post(this.config.fn??"/~fn",e=>y(e,this.store[i])),this.store[i]=d(this.store[i],"function"==typeof e?e({...this.decorators,store:this.store,permission:s}):e),this}schema(e){let t=this.store[n];return this.$schema={body:f(e.body,t),headers:f(e?.headers,t,!0),params:f(e?.params,t),query:f(e?.query,t),response:f(e?.response,t)},this}handle=async e=>{let s;let a=h(this.decorators);a.request=e;try{let i;if(this.event.request.length)for(let e=0;e<this.event.request.length;e++){let t=this.event.request[e](a);if(t instanceof Promise&&(t=await t),t=r(t,a.set))return t}let n=e.url.match(p);if(!n)throw Error("NOT_FOUND");let h=this.router.match(e.method,n[1])??this.router.match("ALL",n[1]);if(!h)throw Error("NOT_FOUND");let l=h.store;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](a,t);if(r instanceof Promise&&(r=await r),void 0!==r){i=r;break}}if(void 0===i)switch(t){case"application/json":i=await e.json();break;case"text/plain":i=await e.text();break;case"application/x-www-form-urlencoded":i=o(await e.text());break;case"elysia/fn":i=b(await e.json())}}}a.body=i,a.params=h.params,n[2]&&(a.query=o(n[2]));let d=l.hooks;if(d?.error&&(s=d?.error),d?.transform.length)for(let e=0;e<d.transform.length;e++){let t=d.transform[e](a);t instanceof Promise&&await t}if(l.validator){let t=l.validator;if(d?.error&&(s=d?.error),t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw u("header",t.headers,r)}if(!1===t.params?.Check(a.params))throw u("params",t.params,a.params);if(!1===t.query?.Check(a.query))throw u("query",t.query,a.query);if(!1===t.body?.Check(i))throw u("body",t.body,i)}if(d?.beforeHandle.length)for(let e=0;e<d.beforeHandle.length;e++){let t=d.beforeHandle[e](a);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<d.afterHandle.length;e++){let r=d.afterHandle[e](a,t);r instanceof Promise&&(r=await r),r&&(t=r)}let e=r(t,a.set);if(e)return e}}let f=l.handle(a);if(f instanceof Promise&&(f=await f),!1===l.validator?.response?.Check(f))throw d?.error&&(s=d?.error),u("response",l.validator.response,f);if(d?.afterHandle.length)for(let e=0;e<d.afterHandle.length;e++){let t=d.afterHandle[e](a,f);t instanceof Promise&&(t=await t);let s=r(t,a.set);if(s)return s}return t(f,a.set)}catch(i){let t=a.set;if((!t.status||t.status<300)&&(t.status=500),s){let a=v(i.message);for(let n=0;n<s.length;n++){let o=s[n]({request:e,error:i,set:t,code:a});o instanceof Promise&&(o=await o);let h=r(o,t);if(h)return h}}return this.handleError(e,i,t)}};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:v(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:g(v(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[n]||(this.store[n][e]=t)}),this}}export{Type as t}from"@sinclair/typebox";export{createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{H as Elysia,s as permission,a as SCHEMA,n as DEFS,i as EXPOSED}; | ||
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{permission as s}from"./fn";import{SCHEMA as a,EXPOSED as i,DEFS as n,mapQuery as o,clone as h,mergeHook as l,mergeDeep as d,createValidationError as u,getSchemaValidator as f,getResponseSchemaValidator as c,mapPathnameAndQueryRegEx as p}from"./utils";import{registerSchemaPath as m}from"./schema";import{mapErrorCode as v,mapErrorStatus as g}from"./error";import{runFn as y}from"./fn";import{deserialize as b}from"superjson";export default class H{store={[a]:{},[n]:{},[i]:Object.create(null)};decorators={query:{},set:{status:200,headers:{}},store:this.store};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new e;routes=[];lazyLoadModules=[];constructor(e){this.config={fn:"/~fn",...e}}_addHandler(e,t,r,s){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:l(h(this.event),s)});let i=this.store[n],o=f(s?.schema?.body??this.$schema?.body,i),d=f(s?.schema?.headers??this.$schema?.headers,i,!0),u=f(s?.schema?.params??this.$schema?.params,i),p=f(s?.schema?.query??this.$schema?.query,i),v=c(s?.schema?.response??this.$schema?.response,i);m({schema:this.store[a],hook:s,method:e,path:t,models:this.store[n]});let g=l(h(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||d||u||p||v?{body:o,header:d,params:u,query:p,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 H;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:i})=>{"/"===r?this._addHandler(t,e,a,l(i,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,l(i,{error:s.event.error}))}),this}guard(e,t){let r=new H;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,l(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 e in this.decorators||(this.decorators[e]=t),this}inject(e){return this.onTransform(t=>{Object.assign(t,e(t))})}fn(e){return 0===Object.keys(this.store[i]).length&&this.post(this.config.fn??"/~fn",e=>y(e,this.store[i])),this.store[i]=d(this.store[i],"function"==typeof e?e({...this.decorators,store:this.store,permission:s}):e),this}schema(e){let t=this.store[n];return this.$schema={body:f(e.body,t),headers:f(e?.headers,t,!0),params:f(e?.params,t),query:f(e?.query,t),response:f(e?.response,t)},this}handle=async e=>{let s;let a=h(this.decorators);a.request=e;try{let i;if(this.event.request.length)for(let e=0;e<this.event.request.length;e++){let t=this.event.request[e](a);if(t instanceof Promise&&(t=await t),t=r(t,a.set))return t}let n=e.url.match(p);if(!n)throw Error("NOT_FOUND");let h=this.router.match(e.method,n[1])??this.router.match("ALL",n[1]);if(!h)throw Error("NOT_FOUND");let l=h.store;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](a,t);if(r instanceof Promise&&(r=await r),void 0!==r){i=r;break}}if(void 0===i)switch(t){case"application/json":i=await e.json();break;case"text/plain":i=await e.text();break;case"application/x-www-form-urlencoded":i=o(await e.text());break;case"elysia/fn":i=b(await e.json())}}}a.body=i,a.params=h.params,n[2]&&(a.query=o(n[2]));let d=l.hooks;if(d?.error&&(s=d?.error),d?.transform.length)for(let e=0;e<d.transform.length;e++){let t=d.transform[e](a);t instanceof Promise&&await t}if(l.validator){let t=l.validator;if(d?.error&&(s=d?.error),t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw u("header",t.headers,r)}if(!1===t.params?.Check(a.params))throw u("params",t.params,a.params);if(!1===t.query?.Check(a.query))throw u("query",t.query,a.query);if(!1===t.body?.Check(i))throw u("body",t.body,i)}if(d?.beforeHandle.length)for(let e=0;e<d.beforeHandle.length;e++){let t=d.beforeHandle[e](a);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<d.afterHandle.length;e++){let r=d.afterHandle[e](a,t);r instanceof Promise&&(r=await r),r&&(t=r)}let e=r(t,a.set);if(e)return e}}let f=l.handle(a);if(f instanceof Promise&&(f=await f),!1===l.validator?.response?.Check(f))throw d?.error&&(s=d?.error),u("response",l.validator.response,f);if(d?.afterHandle.length)for(let e=0;e<d.afterHandle.length;e++){let t=d.afterHandle[e](a,f);t instanceof Promise&&(t=await t);let s=r(t,a.set);if(s)return s}return t(f,a.set)}catch(i){let t=a.set;if((!t.status||t.status<300)&&(t.status=500),s){let a=v(i.message);for(let n=0;n<s.length;n++){let o=s[n]({request:e,error:i,set:t,code:a});o instanceof Promise&&(o=await o);let h=r(o,t);if(h)return h}}return this.handleError(e,i,t)}};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:v(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:g(v(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[n]||(this.store[n][e]=t)}),this}}export{Type as t}from"@sinclair/typebox";export{SCHEMA,DEFS,EXPOSED,createValidationError,getSchemaValidator,mergeDeep,mergeHook,mergeObjectArray,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{H as Elysia,s as permission}; |
@@ -125,13 +125,11 @@ /// <reference types="bun-types" /> | ||
request: Instance['request']; | ||
store: Instance['store'] & { | ||
[SCHEMA]: { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance, Path> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? { | ||
'200': CatchResponse; | ||
} : FinalSchema['response']; | ||
} : never; | ||
}; | ||
store: Instance['store'] & Record<typeof SCHEMA, { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance, Path> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? { | ||
'200': CatchResponse; | ||
} : FinalSchema['response']; | ||
} : never; | ||
}; | ||
}; | ||
}>; | ||
schema: Instance['schema']; | ||
@@ -138,0 +136,0 @@ }>; |
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.3.0-exp-230222.1829", | ||
"version": "0.3.0-exp-230222.1921", | ||
"author": { | ||
@@ -17,3 +17,3 @@ "name": "saltyAom", | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"types": "./src/index.ts", | ||
"repository": { | ||
@@ -20,0 +20,0 @@ "type": "git", |
60200
453