Comparing version 0.3.0-exp-230222.2210 to 0.3.0-exp-240224.141
/// <reference types="bun-types" /> | ||
import { Elysia } from '.'; | ||
import type { TypedRoute } from './types'; | ||
import type { Elysia, TypedRoute, DEFS, SCHEMA, TypedSchema } from '.'; | ||
export interface Context<Route extends TypedRoute = TypedRoute, Store extends Elysia['store'] = Elysia['store']> { | ||
@@ -10,2 +9,6 @@ request: Request; | ||
store: Store; | ||
[SCHEMA]: TypedSchema; | ||
[DEFS]: { | ||
[index: string]: Record<string, unknown>; | ||
}; | ||
set: { | ||
@@ -12,0 +15,0 @@ headers: Record<string, string>; |
/// <reference types="bun-types" /> | ||
import type { Serve, Server } from 'bun'; | ||
import { SCHEMA, EXPOSED, DEFS, exposePermission } from './utils'; | ||
import { permission, type Permission } from './fn'; | ||
import { SCHEMA, EXPOSED, DEFS } from './utils'; | ||
import type { Context } from './context'; | ||
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, MergeIfNotNull, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, ElysiaRoute, MaybePromise, IsNever } from './types'; | ||
import type { Handler, BeforeRequestHandler, TypedRoute, ElysiaInstance, ElysiaConfig, HTTPMethod, InternalRoute, BodyParser, ErrorHandler, TypedSchema, LocalHook, LocalHandler, LifeCycle, LifeCycleEvent, LifeCycleStore, VoidLifeCycle, AfterRequestHandler, IsAny, OverwritableTypeRoute, MergeSchema, ListenCallback, NoReturnHandler, ElysiaRoute, MaybePromise, IsNever } from './types'; | ||
import { type TSchema } from '@sinclair/typebox'; | ||
@@ -10,2 +11,3 @@ export default class Elysia<Instance extends ElysiaInstance = ElysiaInstance> { | ||
store: Instance['store']; | ||
meta: Instance['meta']; | ||
protected decorators: ElysiaInstance['request']; | ||
@@ -33,15 +35,18 @@ event: LifeCycleStore<Instance>; | ||
schema: Instance['schema']; | ||
meta: Omit<Instance['meta'], typeof SCHEMA> & ElysiaInstance['meta']; | ||
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<{ | ||
request: Instance['request'] & NewInstance['request']; | ||
schema: Instance['schema'] & NewInstance['schema']; | ||
store: Instance['store'] & (Omit<NewInstance['store'], typeof SCHEMA> & { | ||
store: Instance['store'] & NewInstance['store']; | ||
meta: Instance['meta'] & (Omit<NewInstance['meta'], typeof SCHEMA> & { | ||
[key in typeof SCHEMA]: { | ||
[key in keyof NewInstance['store'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['store'][typeof SCHEMA][key]; | ||
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['meta'][typeof SCHEMA][key]; | ||
}; | ||
}); | ||
}> : this; | ||
guard<Schema extends TypedSchema = {}, NewElysia extends Elysia<any> = Elysia<any>>(hook: LocalHook<Schema, Instance>, run: (group: Elysia<{ | ||
guard<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, NewElysia extends Elysia<any> = Elysia<any>>(hook: LocalHook<Schema, Instance>, run: (group: Elysia<{ | ||
request: Instance['request']; | ||
store: Instance['store']; | ||
schema: Omit<MergeIfNotNull<Schema, Instance['schema']>, typeof SCHEMA>; | ||
schema: Schema & Instance['schema']; | ||
meta: Instance['meta']; | ||
}>) => NewElysia): NewElysia extends Elysia<infer NewInstance> ? Elysia<NewInstance & Instance> : this; | ||
@@ -51,13 +56,13 @@ use<NewElysia extends MaybePromise<Elysia<any>> = Elysia<any>, Params extends Elysia = Elysia<any>, LazyLoadElysia extends never | ElysiaInstance = never>(plugin: MaybePromise<(app: Params extends Elysia<infer ParamsInstance> ? IsAny<ParamsInstance> extends true ? this : Params : Params) => MaybePromise<NewElysia>> | Promise<{ | ||
}>): IsNever<LazyLoadElysia> extends false ? Elysia<LazyLoadElysia & Instance> : NewElysia extends Elysia<infer NewInstance> ? IsNever<NewInstance> extends true ? Elysia<Instance> : Elysia<NewInstance & Instance> : NewElysia extends Promise<Elysia<infer NewInstance>> ? Elysia<NewInstance & Instance> : this; | ||
get<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'GET', Schema, Instance, Path, Response>; | ||
post<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'POST', Schema, Instance, Path, Response>; | ||
put<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PUT', Schema, Instance, Path, Response>; | ||
patch<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PATCH', Schema, Instance, Path, Response>; | ||
delete<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'DELETE', Schema, Instance, Path, Response>; | ||
options<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'OPTIONS', Schema, Instance, Path, Response>; | ||
all<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'ALL', Schema, Instance, Path, Response>; | ||
head<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'HEAD', Schema, Instance, Path, Response>; | ||
trace<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'TRACE', Schema, Instance, Path, Response>; | ||
connect<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'CONNECT', Schema, Instance, Path, Response>; | ||
route<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, Method extends HTTPMethod = HTTPMethod, Path extends string = string, Response = unknown>(method: Method, path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<Method, Schema, Instance, Path, Response>; | ||
get<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'GET', Schema, Instance, Path, Response>; | ||
post<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'POST', Schema, Instance, Path, Response>; | ||
put<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PUT', Schema, Instance, Path, Response>; | ||
patch<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'PATCH', Schema, Instance, Path, Response>; | ||
delete<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'DELETE', Schema, Instance, Path, Response>; | ||
options<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'OPTIONS', Schema, Instance, Path, Response>; | ||
all<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'ALL', Schema, Instance, Path, Response>; | ||
head<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'HEAD', Schema, Instance, Path, Response>; | ||
trace<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'TRACE', Schema, Instance, Path, Response>; | ||
connect<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Path extends string = string, Response = unknown>(path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<'CONNECT', Schema, Instance, Path, Response>; | ||
route<Schema extends TypedSchema<Exclude<keyof Instance['meta'][typeof DEFS], number | symbol>> = {}, Method extends HTTPMethod = HTTPMethod, Path extends string = string, Response = unknown>(method: Method, path: Path, handler: LocalHandler<Schema, Instance, Path, Response>, hook?: LocalHook<Schema, Instance, Path>): ElysiaRoute<Method, Schema, Instance, Path, Response>; | ||
state<Key extends string | number | symbol = keyof Instance['store'], Value = Instance['store'][keyof Instance['store']], NewInstance = Elysia<{ | ||
@@ -69,2 +74,3 @@ store: Instance['store'] & { | ||
schema: Instance['schema']; | ||
meta: Instance['meta']; | ||
}>>(name: Key, value: Value): NewInstance; | ||
@@ -77,2 +83,3 @@ decorate<Name extends string, Value = any, NewInstance = Elysia<{ | ||
schema: Instance['schema']; | ||
meta: Instance['meta']; | ||
}>>(name: Name, value: Value): NewInstance; | ||
@@ -85,15 +92,15 @@ inject<Returned extends Object = Object>(transform: (context: Context<{}, Instance['store']> & Instance['request']) => Returned extends { | ||
schema: Instance['schema']; | ||
meta: Instance['meta']; | ||
}>; | ||
fn<T extends Record<string, unknown> | ((app: Instance['request'] & { | ||
store: Instance['store']; | ||
permission: typeof exposePermission; | ||
permission: Permission; | ||
}) => Record<string, unknown>) = Record<string, unknown> | ((app: Instance['request'] & { | ||
store: Instance['store']; | ||
permission: typeof exposePermission; | ||
permission: Permission; | ||
}) => Record<string, unknown>)>(value: T): Elysia<{ | ||
store: Instance['store'] & { | ||
[EXPOSED]: T extends (store: any) => infer Returned ? Returned : T; | ||
}; | ||
store: Instance['store']; | ||
request: Instance['request']; | ||
schema: Instance['schema']; | ||
meta: Instance['meta'] & Record<typeof EXPOSED, T extends (store: any) => infer Returned ? Returned : T>; | ||
}>; | ||
@@ -104,2 +111,3 @@ schema<Schema extends TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>> = TypedSchema<Exclude<keyof Instance['store'][typeof DEFS], number | symbol>>, NewInstance = Elysia<{ | ||
schema: MergeSchema<Schema, Instance['schema']>; | ||
meta: Instance['meta']; | ||
}>>(schema: Schema): NewInstance; | ||
@@ -112,10 +120,9 @@ handle: (request: Request) => Promise<Response>; | ||
setModel<Recorder extends Record<string, TSchema>>(record: Recorder): Elysia<{ | ||
store: Instance['store'] & { | ||
[Defs in typeof DEFS]: Recorder; | ||
}; | ||
store: Instance['store']; | ||
request: Instance['request']; | ||
schema: Instance['schema']; | ||
meta: Instance['meta'] & Record<typeof DEFS, Recorder>; | ||
}>; | ||
} | ||
export { Elysia }; | ||
export { Elysia, permission }; | ||
export { Type as t } from '@sinclair/typebox'; | ||
@@ -122,0 +129,0 @@ export { SCHEMA, DEFS, EXPOSED, createValidationError, getSchemaValidator, mergeDeep, mergeHook, mergeObjectArray, mapPathnameAndQueryRegEx, mapQuery } from './utils'; |
@@ -1,1 +0,1 @@ | ||
import{Raikiri as e}from"raikiri";import{mapResponse as t,mapEarlyResponse as r}from"./handler";import{SCHEMA as s,EXPOSED as a,DEFS as n,mapQuery as i,clone as o,mergeHook as h,mergeDeep as l,createValidationError as u,getSchemaValidator as d,getResponseSchemaValidator as f,mapPathnameAndQueryRegEx as p,exposePermission as c}from"./utils";import{registerSchemaPath as m}from"./schema";import{mapErrorCode as v,mapErrorStatus as g}from"./error";import{serialize as y,deserialize as b}from"superjson";export default class H{store={[s]:{},[n]:{},[a]:{}};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,a){t=t.startsWith("/")?t:`/${t}`,this.routes.push({method:e,path:t,handler:r,hooks:h(o(this.event),a)});let i=this.store[n],l=d(a?.schema?.body??this.$schema?.body,i),u=d(a?.schema?.headers??this.$schema?.headers,i,!0),p=d(a?.schema?.params??this.$schema?.params,i),c=d(a?.schema?.query??this.$schema?.query,i),v=f(a?.schema?.response??this.$schema?.response,i);m({schema:this.store[s],hook:a,method:e,path:t,models:this.store[n]});let g=h(o(this.event),a);g.schema||g.transform.length||g.beforeHandle.length||g.error.length||g.afterHandle.length||(g=void 0);let y={handle:r,hooks:g,validator:l||u||p||c||v?{body:l,header:u,params:p,query:c,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:n})=>{"/"===r?this._addHandler(t,e,a,h(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,h(n,{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,h(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[a]).length&&this.post(this.config.fn??"/~fn",e=>{let t=this.store[a],r=[],s=e.body;e:for(let n=0;n<s.length;n++){let i=s[n],o=t;if(!Array.isArray(i.n)){r.push(Error("Invalid procedure"));continue e}for(let t=0;t<i.n.length;t++){if(!(o=o[i.n[t]])){r.push(Error("Invalid procedure"));continue e}if(a in o){if(!1==o.allow){r.push(Error("Forbidden"));continue e}if(!0!==o.allow){try{let s=o.allow({...e,key:i.n.slice(t+1).join("."),params:i.p??null});if(s instanceof Error){r.push(s);continue e}}catch(e){r.push(e);continue e}o=o.value}}}"function"==typeof o?void 0!==i.p?Array.isArray(i.p)&&1===i.p.length?r.push(o(i.p[0])):r.push(o(...i.p)):r.push(o()):r.push(Error("Invalid procedure"))}return Promise.all(r).then(y)}),this.store[a]=l(this.store[a],"function"==typeof e?e({...this.decorators,store:this.store,permission:c}):e),this}schema(e){let t=this.store[n];return this.$schema={body:d(e.body,t),headers:d(e?.headers,t,!0),params:d(e?.params,t),query:d(e?.query,t),response:d(e?.response,t)},this}handle=async e=>{let s;let a=o(this.decorators);a.request=e;try{let n;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 o=e.url.match(p);if(!o)throw Error("NOT_FOUND");let h=this.router.match(e.method,o[1])??this.router.match("ALL",o[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){n=r;break}}if(void 0===n)switch(t){case"application/json":n=await e.json();break;case"text/plain":n=await e.text();break;case"application/x-www-form-urlencoded":n=i(await e.text());break;case"elysia/fn":n=b(await e.json())}}}a.body=n,a.params=h.params,o[2]&&(a.query=i(o[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(n))throw u("body",t.body,n)}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(n){let t=a.set;if((!t.status||t.status<300)&&(t.status=500),s){let a=v(n.message);for(let i=0;i<s.length;i++){let o=s[i]({request:e,error:n,set:t,code:a});o instanceof Promise&&(o=await o);let h=r(o,t);if(h)return h}}return this.handleError(e,n,t)}};async handleError(e,r,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let n=this.event.error[a]({request:e,code:v(r.message),error:r,set:s});if(n instanceof Promise&&(n=await n),null!=n)return t(n,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}; | ||
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 u,createValidationError as d,getSchemaValidator as f,getResponseSchemaValidator as c,mapPathnameAndQueryRegEx as m}from"./utils";import{registerSchemaPath as p}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={};meta={[a]:Object.create(null),[n]:Object.create(null),[i]:Object.create(null)};decorators={query:{},set:{status:200,headers:{}},store:this.store,[a]:this.meta[a],[n]:this.meta[n]};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.meta[n],o=f(s?.schema?.body??this.$schema?.body,i),u=f(s?.schema?.headers??this.$schema?.headers,i,!0),d=f(s?.schema?.params??this.$schema?.params,i),m=f(s?.schema?.query??this.$schema?.query,i),v=c(s?.schema?.response??this.$schema?.response,i);p({schema:this.meta[a],hook:s,method:e,path:t,models:this.meta[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||u||d||m||v?{body:o,header:u,params:d,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 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.meta[i]).length&&this.post(this.config.fn??"/~fn",e=>y(e,this.meta[i])),this.meta[i]=u(this.meta[i],"function"==typeof e?e({...this.decorators,store:this.store,permission:s}):e),this}schema(e){let t=this.meta[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(m);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 u=l.hooks;if(u?.error&&(s=u?.error),u?.transform.length)for(let e=0;e<u.transform.length;e++){let t=u.transform[e](a);t instanceof Promise&&await t}if(l.validator){let t=l.validator;if(u?.error&&(s=u?.error),t.headers){let r={};for(let t in e.headers)r[t]=e.headers.get(t);if(!1===t.headers.Check(r))throw d("header",t.headers,r)}if(!1===t.params?.Check(a.params))throw d("params",t.params,a.params);if(!1===t.query?.Check(a.query))throw d("query",t.query,a.query);if(!1===t.body?.Check(i))throw d("body",t.body,i)}if(u?.beforeHandle.length)for(let e=0;e<u.beforeHandle.length;e++){let t=u.beforeHandle[e](a);if(t instanceof Promise&&(t=await t),null!=t){for(let e=0;e<u.afterHandle.length;e++){let r=u.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 u?.error&&(s=u?.error),d("response",l.validator.response,f);if(u?.afterHandle.length)for(let e=0;e<u.afterHandle.length;e++){let t=u.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.meta[n]||(this.meta[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}; |
@@ -7,11 +7,12 @@ import { TSchema } from '@sinclair/typebox'; | ||
export declare const registerSchemaPath: ({ schema, path, method, hook, models }: { | ||
schema: OpenAPIV2.PathsObject; | ||
schema: Partial<OpenAPIV2.PathsObject>; | ||
path: string; | ||
method: HTTPMethod; | ||
hook?: LocalHook<import("./types").TypedSchema<string>, import("./types").ElysiaInstance<{ | ||
store: Record<any, any> & Record<typeof import("./utils").SCHEMA, {}> & Record<typeof import("./utils").DEFS, {}> & Record<typeof import("./utils").EXPOSED, {}>; | ||
store: Record<any, any>; | ||
request: {}; | ||
schema: {}; | ||
meta: Record<typeof import("./utils").SCHEMA, {}> & Record<typeof import("./utils").DEFS, {}> & Record<typeof import("./utils").EXPOSED, {}>; | ||
}>, string, import("./types").MergeSchema<import("./types").TypedSchema<string>, {}>> | undefined; | ||
models: Record<string, TSchema>; | ||
}) => void; |
@@ -11,18 +11,25 @@ /// <reference types="bun-types" /> | ||
export type ObjectValues<T extends object> = T[keyof T]; | ||
export interface ElysiaInstance<Instance extends { | ||
store?: Record<any, any> & Record<typeof SCHEMA, Partial<OpenAPIV2.PathsObject>> & Record<typeof DEFS, { | ||
export type ElysiaInstance<Instance extends { | ||
store?: Record<any, any>; | ||
request?: Record<any, any>; | ||
schema?: TypedSchema; | ||
meta?: Record<typeof SCHEMA, Partial<OpenAPIV2.PathsObject>> & Record<typeof DEFS, { | ||
[x in string]: TSchema; | ||
}> & Record<typeof EXPOSED, Record<string, Record<string, unknown>>>; | ||
request?: Record<any, any>; | ||
schema?: TypedSchema; | ||
} = { | ||
store: Record<any, any> & Record<typeof SCHEMA, {}> & Record<typeof DEFS, {}> & Record<typeof EXPOSED, {}>; | ||
store: Record<any, any>; | ||
request: {}; | ||
schema: {}; | ||
}> { | ||
request: Instance['request'] extends undefined ? Record<typeof SCHEMA, {}> : Instance['request']; | ||
meta: Record<typeof SCHEMA, {}> & Record<typeof DEFS, {}> & Record<typeof EXPOSED, {}>; | ||
}> = { | ||
request: Instance['request']; | ||
store: Instance['store'] extends undefined ? {} : Instance['store']; | ||
schema: Instance['schema'] extends undefined ? TypedSchema : Instance['schema']; | ||
} | ||
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request']) => Route['response'] extends (models: Record<string, TSchema>) => TSchema ? undefined extends ReturnType<Route['response']> ? MaybePromise<CatchResponse> | Response : MaybePromise<ReturnType<Route['response']>> | Response : undefined extends Route['response'] ? MaybePromise<CatchResponse> | Response : MaybePromise<Route['response']> | Response; | ||
meta: Instance['meta']; | ||
}; | ||
export type Handler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance, CatchResponse = Route['response']> = (context: Context<Route, Instance['store']> & Instance['request'] & { | ||
a: Instance; | ||
r: Route; | ||
merged: MergeSchema<Instance['schema'], Route>; | ||
}) => Route['response'] extends (models: Record<string, TSchema>) => TSchema ? undefined extends ReturnType<Route['response']> ? MaybePromise<CatchResponse> | Response : MaybePromise<ReturnType<Route['response']>> | Response : undefined extends Route['response'] ? MaybePromise<CatchResponse> | Response : MaybePromise<Route['response']> | Response; | ||
export type NoReturnHandler<Route extends TypedRoute = TypedRoute, Instance extends ElysiaInstance = ElysiaInstance> = (context: Context<Route, Instance['store']> & Instance['request']) => void | Promise<void>; | ||
@@ -69,13 +76,13 @@ export type LifeCycleEvent = 'start' | 'request' | 'parse' | 'transform' | 'beforeHandle' | 'afterHandle' | 'error' | 'stop'; | ||
} | ||
export type UnwrapSchema<Schema extends TSchema | undefined | string, Instance extends ElysiaInstance = ElysiaInstance, Fallback = unknown> = Schema extends string ? Instance['store'][typeof DEFS] extends { | ||
export type UnwrapSchema<Schema extends TSchema | undefined | string, Definitions extends ElysiaInstance['meta'][typeof DEFS] = {}, Fallback = unknown> = Schema extends string ? Definitions extends { | ||
[name in Schema]: infer NamedSchema extends TSchema; | ||
} ? Static<NamedSchema> : Fallback : Schema extends TSchema ? Static<NonNullable<Schema>> : Fallback; | ||
export type TypedSchemaToRoute<Schema extends TypedSchema, Instance extends ElysiaInstance> = { | ||
body: UnwrapSchema<Schema['body'], Instance>; | ||
headers: UnwrapSchema<Schema['headers'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: Schema['response'] extends TSchema | string ? UnwrapSchema<Schema['response'], Instance> : Schema['response'] extends { | ||
export type TypedSchemaToRoute<Schema extends TypedSchema<any>, Definitions extends ElysiaInstance['meta'][typeof DEFS]> = { | ||
body: UnwrapSchema<Schema['body'], Definitions>; | ||
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: Schema['response'] extends TSchema | string ? UnwrapSchema<Schema['response'], Definitions> : Schema['response'] extends { | ||
[k in string]: TSchema | string; | ||
} ? UnwrapSchema<ObjectValues<Schema['response']>, Instance> : unknown; | ||
} ? UnwrapSchema<ObjectValues<Schema['response']>, Definitions> : unknown; | ||
}; | ||
@@ -96,3 +103,3 @@ export type AnyTypedSchema = { | ||
}; | ||
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, Typed extends AnyTypedSchema = TypedSchemaToRoute<Schema, Instance>> = Handler<Typed['params'] extends {} ? Omit<Typed, 'response'> & { | ||
export type HookHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, Typed extends AnyTypedSchema = TypedSchemaToRoute<Schema, Instance['meta'][typeof DEFS]>> = Handler<Typed['params'] extends {} ? Omit<Typed, 'response'> & { | ||
response: void | Typed['response']; | ||
@@ -107,3 +114,3 @@ } : Omit<Omit<Typed, 'response'> & { | ||
export type PickInOrder<A, B> = A extends NonNullable<A> ? A : B; | ||
export type MergeSchema<A extends TypedSchema, B extends TypedSchema> = { | ||
export type MergeSchema<A extends TypedSchema<any>, B extends TypedSchema<any>> = { | ||
body: PickInOrder<PickInOrder<A['body'], B['body']>, undefined>; | ||
@@ -124,3 +131,3 @@ headers: PickInOrder<PickInOrder<A['headers'], B['headers']>, undefined>; | ||
} | ||
export type RouteToSchema<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, FinalSchema extends MergeSchema<Schema, Instance['schema']> = MergeSchema<Schema, Instance['schema']>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToRoute<FinalSchema, Instance> : Omit<TypedSchemaToRoute<FinalSchema, Instance>, 'params'> & { | ||
export type RouteToSchema<Schema extends TypedSchema, InstanceSchema extends ElysiaInstance['schema'], Definitions extends ElysiaInstance['meta'][typeof DEFS], Path extends string = string, FinalSchema extends MergeSchema<Schema, InstanceSchema> = MergeSchema<Schema, InstanceSchema>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToRoute<FinalSchema, Definitions> : Omit<TypedSchemaToRoute<FinalSchema, Definitions>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string>; | ||
@@ -130,32 +137,31 @@ }; | ||
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']; | ||
schema: Instance['schema']; | ||
meta: Instance['meta'] & Record<typeof SCHEMA, { | ||
[path in Path]: { | ||
[method in Method]: TypedRouteToEden<Schema, Instance['schema'], Path> extends infer FinalSchema extends AnyTypedSchema ? Omit<FinalSchema, 'response'> & { | ||
response: undefined extends FinalSchema['response'] ? { | ||
'200': CatchResponse; | ||
} : FinalSchema['response']; | ||
} : never; | ||
}; | ||
}; | ||
schema: Instance['schema']; | ||
}>; | ||
}>; | ||
export type TypedRouteToEden<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance<any> = ElysiaInstance, Path extends string = string, FinalSchema extends MergeSchema<Schema, Instance['schema']> = MergeSchema<Schema, Instance['schema']>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToEden<FinalSchema, Instance> : Omit<TypedSchemaToEden<FinalSchema, Instance>, 'params'> & { | ||
export type TypedRouteToEden<Schema extends TypedSchema = TypedSchema, InstanceSchema extends TypedSchema<string> = ElysiaInstance['schema'], Path extends string = string, FinalSchema extends MergeSchema<Schema, InstanceSchema> = MergeSchema<Schema, InstanceSchema>> = FinalSchema['params'] extends NonNullable<Schema['params']> ? TypedSchemaToEden<FinalSchema, InstanceSchema> : Omit<TypedSchemaToEden<FinalSchema, InstanceSchema>, 'params'> & { | ||
params: Record<ExtractPath<Path>, string>; | ||
}; | ||
export type TypedSchemaToEden<Schema extends TypedSchema, Instance extends ElysiaInstance> = { | ||
body: UnwrapSchema<Schema['body'], Instance>; | ||
headers: UnwrapSchema<Schema['headers'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Instance> extends infer Result extends Record<string, any> ? Result : undefined; | ||
export type TypedSchemaToEden<Schema extends TypedSchema, Definitions extends ElysiaInstance['meta'][typeof DEFS]> = { | ||
body: UnwrapSchema<Schema['body'], Definitions>; | ||
headers: UnwrapSchema<Schema['headers'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
query: UnwrapSchema<Schema['query'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
params: UnwrapSchema<Schema['params'], Definitions> extends infer Result extends Record<string, any> ? Result : undefined; | ||
response: Schema['response'] extends TSchema | string ? { | ||
'200': UnwrapSchema<Schema['response'], Instance>; | ||
'200': UnwrapSchema<Schema['response'], Definitions>; | ||
} : Schema['response'] extends { | ||
[x in string]: TSchema | string; | ||
} ? { | ||
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Instance>; | ||
[key in keyof Schema['response']]: UnwrapSchema<Schema['response'][key], Definitions>; | ||
} : unknown; | ||
}; | ||
export type LocalHandler<Schema extends TypedSchema = TypedSchema, Instance extends ElysiaInstance = ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Handler<RouteToSchema<Schema, Instance, Path>, Instance, CatchResponse>; | ||
export type LocalHandler<Schema extends TypedSchema, Instance extends ElysiaInstance, Path extends string = string, CatchResponse = unknown> = Handler<RouteToSchema<Schema, Instance['schema'], Instance['meta'][typeof DEFS], Path>, Instance, CatchResponse>; | ||
export interface TypedRoute { | ||
@@ -229,8 +235,9 @@ body?: unknown; | ||
export type IsNever<T> = [T] extends [never] ? true : false; | ||
export type FunctionalKeys<T, Prefix extends string = ''> = { | ||
[K in keyof T]-?: T[K] extends Function ? `${Prefix}${K}` : never; | ||
}[keyof T] | { | ||
[K in keyof T]-?: T[K] extends object ? FunctionalKeys<T[K], `${Prefix}${K}.`> : never; | ||
export type FunctionProperties<T> = { | ||
[K in keyof T as T[K] extends Record<any, any> | ((...args: any[]) => any) ? IsAny<T[K]> extends true ? never : K : never]: T[K] extends (...args: any[]) => any ? T[K] : T[K] extends Record<string, any> ? FunctionProperties<T[K]> : never; | ||
}; | ||
export type JoinKeys<T, Prefix extends string = ''> = { | ||
[K in keyof T]-?: T[K] extends string | Function ? '' extends Prefix ? K : K extends string ? `${Prefix}.${K}` : never : K extends string ? JoinKeys<T[K], '' extends Prefix ? K : `${Prefix}.${K}`> : never; | ||
}[keyof T]; | ||
export type ConnectedKeysType<T, K extends string> = K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? ConnectedKeysType<T[Key], Rest> : never : K extends keyof T ? T[K] extends (...args: any) => any ? ReturnType<T[K]> : never : never; | ||
export type ConnectedKeysType<T, K extends string> = K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? ConnectedKeysType<T[Key], Rest> : never : K extends keyof T ? T[K] extends (...args: infer A) => any ? A : never : never; | ||
export type Prettify<T> = { | ||
@@ -237,0 +244,0 @@ [K in keyof T]: T[K]; |
@@ -1,5 +0,4 @@ | ||
/// <reference types="bun-types" /> | ||
import { TSchema } from '@sinclair/typebox'; | ||
import { TypeCheck } from '@sinclair/typebox/compiler'; | ||
import type { DeepMergeTwoTypes, LifeCycleStore, LocalHook, TypedSchema, RegisteredHook, ConnectedKeysType, FunctionalKeys } from './types'; | ||
import type { DeepMergeTwoTypes, LifeCycleStore, LocalHook, TypedSchema, RegisteredHook } from './types'; | ||
export declare const SCHEMA: unique symbol; | ||
@@ -17,17 +16,1 @@ export declare const DEFS: unique symbol; | ||
export declare const getResponseSchemaValidator: (s: TypedSchema['response'] | undefined, models: Record<string, TSchema>, additionalProperties?: boolean) => TypeCheck<TSchema> | undefined; | ||
export declare const exposePermission: <T, Key = T extends Record<any, any> ? FunctionalKeys<T, ""> : never>({ value, allow }: { | ||
value: T; | ||
allow?: boolean | ((context: { | ||
request: Request; | ||
key: Key; | ||
params: Key extends string ? ConnectedKeysType<T, Key> : unknown; | ||
}) => unknown) | undefined; | ||
}) => { | ||
value: T; | ||
allow?: boolean | ((context: { | ||
request: Request; | ||
key: Key; | ||
params: Key extends string ? ConnectedKeysType<T, Key> : unknown; | ||
}) => unknown) | undefined; | ||
[EXPOSED]: true; | ||
}; |
@@ -1,1 +0,1 @@ | ||
import{Kind as e,Type as r}from"@sinclair/typebox";import{TypeCompiler as t}from"@sinclair/typebox/compiler";export const SCHEMA=Symbol("schema");export const DEFS=Symbol("definitions");export const EXPOSED=Symbol("exposed");export const mergeObjectArray=(e,r)=>[...Array.isArray(e)?e:[e],...Array.isArray(r)?r:[r]];export const mergeHook=(e,r)=>{let t="schema"in e?e.schema:null,o=r&&"schema"in r?r.schema:null;return{schema:t||o?{body:o?.body??t?.body,header:o?.headers??t?.headers,params:o?.params??t?.params,query:o?.query??t?.query,response:o?.response??t?.response}:void 0,transform:mergeObjectArray(e.transform??[],r?.transform??[]),beforeHandle:mergeObjectArray(e.beforeHandle??[],r?.beforeHandle??[]),afterHandle:mergeObjectArray(e.afterHandle??[],r?.afterHandle??[]),error:mergeObjectArray(e.error??[],r?.error??[])}};export const clone=e=>e;export const mapPathnameAndQueryRegEx=/:\/\/[^/]+([^#?]+)(?:\?([^#]+))?/;export const mapQuery=e=>{let r={},t=e.split("&");for(let e=0;e<t.length;e++){let o=t[e],n=o.indexOf("="),a=o.slice(n+1);a.includes("%")&&(a=decodeURIComponent(a)),r[o.slice(0,n)]=a}return r};let o=e=>e&&"object"==typeof e&&!Array.isArray(e);export const mergeDeep=(e,r)=>{let t=Object.assign({},e);return o(e)&&o(r)&&Object.keys(r).forEach(n=>{o(r[n])&&n in e?t[n]=mergeDeep(e[n],r[n]):Object.assign(t,{[n]:r[n]})}),t};export const createValidationError=(e,r,t)=>{let o=r.Errors(t).next().value;return Error("VALIDATION",{cause:`Invalid ${e}: '${o?.path?.slice(1)||"root"}'. ${o.message}`})};export const getSchemaValidator=(e,r,o=!1)=>{if(!e||"string"==typeof e&&!(e in r))return;let n="string"==typeof e?r[e]:e;return"object"===n.type&&"additionalProperties"in n==!1&&(n.additionalProperties=o),t.Compile(n)};export const getResponseSchemaValidator=(o,n,a=!1)=>{if(!o||"string"==typeof o&&!(o in n))return;let s="string"==typeof o?n[o]:o,i=e in s?s:r.Union(Object.keys(s).map(e=>{let r=s[e];if("string"==typeof r){if(r in n){let e=n[r];return e}return}return r}).filter(e=>e));return"object"===i.type&&"additionalProperties"in i==!1&&(i.additionalProperties=a),t.Compile(i)};export const exposePermission=({value:e,allow:r=!0})=>({[EXPOSED]:!0,value:e,allow:r}); | ||
import{Kind as e,Type as r}from"@sinclair/typebox";import{TypeCompiler as t}from"@sinclair/typebox/compiler";export const SCHEMA=Symbol("schema");export const DEFS=Symbol("definitions");export const EXPOSED=Symbol("exposed");export const mergeObjectArray=(e,r)=>[...Array.isArray(e)?e:[e],...Array.isArray(r)?r:[r]];export const mergeHook=(e,r)=>{let t="schema"in e?e.schema:null,o=r&&"schema"in r?r.schema:null;return{schema:t||o?{body:o?.body??t?.body,header:o?.headers??t?.headers,params:o?.params??t?.params,query:o?.query??t?.query,response:o?.response??t?.response}:void 0,transform:mergeObjectArray(e.transform??[],r?.transform??[]),beforeHandle:mergeObjectArray(e.beforeHandle??[],r?.beforeHandle??[]),afterHandle:mergeObjectArray(e.afterHandle??[],r?.afterHandle??[]),error:mergeObjectArray(e.error??[],r?.error??[])}};export const clone=e=>e;export const mapPathnameAndQueryRegEx=/:\/\/[^/]+([^#?]+)(?:\?([^#]+))?/;export const mapQuery=e=>{let r={},t=e.split("&");for(let e=0;e<t.length;e++){let o=t[e],n=o.indexOf("="),a=o.slice(n+1);a.includes("%")&&(a=decodeURIComponent(a)),r[o.slice(0,n)]=a}return r};let o=e=>e&&"object"==typeof e&&!Array.isArray(e);export const mergeDeep=(e,r)=>{let t=Object.assign({},e);return o(e)&&o(r)&&Object.keys(r).forEach(n=>{o(r[n])&&n in e?t[n]=mergeDeep(e[n],r[n]):Object.assign(t,{[n]:r[n]})}),t};export const createValidationError=(e,r,t)=>{let o=r.Errors(t).next().value;return Error("VALIDATION",{cause:`Invalid ${e}: '${o?.path?.slice(1)||"root"}'. ${o.message}`})};export const getSchemaValidator=(e,r,o=!1)=>{if(!e||"string"==typeof e&&!(e in r))return;let n="string"==typeof e?r[e]:e;return"object"===n.type&&"additionalProperties"in n==!1&&(n.additionalProperties=o),t.Compile(n)};export const getResponseSchemaValidator=(o,n,a=!1)=>{if(!o||"string"==typeof o&&!(o in n))return;let s="string"==typeof o?n[o]:o,i=e in s?s:r.Union(Object.keys(s).map(e=>{let r=s[e];if("string"==typeof r){if(r in n){let e=n[r];return e}return}return r}).filter(e=>e));return"object"===i.type&&"additionalProperties"in i==!1&&(i.additionalProperties=a),t.Compile(i)}; |
115
package.json
{ | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.3.0-exp-230222.2210", | ||
"author": { | ||
"name": "saltyAom", | ||
"url": "https://github.com/SaltyAom", | ||
"email": "saltyaom@gmail.com" | ||
}, | ||
"main": "./dist/index.js", | ||
"exports": { | ||
"require": "./dist/index.js", | ||
"import": "./dist/index.js", | ||
"node": "./dist/index.js", | ||
"default": "./dist/index.js" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/elysiajs/elysia" | ||
}, | ||
"bugs": "https://github.com/elysiajs/elysia/issues", | ||
"homepage": "https://github.com/elysiajs/elysia", | ||
"keywords": [ | ||
"bun", | ||
"http", | ||
"web", | ||
"server" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "bun wiptest", | ||
"dev": "bun run --hot example/http.ts", | ||
"build": "rimraf dist && swc src -d dist && tsc --project tsconfig.esm.json", | ||
"release": "npm run build && npm run test && npm publish" | ||
}, | ||
"dependencies": { | ||
"@sinclair/typebox": "^0.25.21", | ||
"openapi-types": "^12.1.0", | ||
"raikiri": "^0.0.0-beta.5", | ||
"superjson": "^1.12.2" | ||
}, | ||
"devDependencies": { | ||
"@swc/cli": "^0.1.60", | ||
"@swc/core": "^1.3.32", | ||
"@types/node": "^18.11.18", | ||
"@typescript-eslint/eslint-plugin": "^5.48.2", | ||
"@typescript-eslint/parser": "^5.48.2", | ||
"bun-types": "^0.5.0", | ||
"eslint": "^8.32.0", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.9.4" | ||
} | ||
"name": "elysia", | ||
"description": "Fast, and friendly Bun web framework", | ||
"version": "0.3.0-exp-240224.141", | ||
"author": { | ||
"name": "saltyAom", | ||
"url": "https://github.com/SaltyAom", | ||
"email": "saltyaom@gmail.com" | ||
}, | ||
"main": "./dist/index.js", | ||
"exports": { | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.js", | ||
"node": "./dist/index.js" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/elysiajs/elysia" | ||
}, | ||
"bugs": "https://github.com/elysiajs/elysia/issues", | ||
"homepage": "https://github.com/elysiajs/elysia", | ||
"keywords": [ | ||
"bun", | ||
"http", | ||
"web", | ||
"server" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "bun wiptest", | ||
"dev": "bun run --hot example/http.ts", | ||
"build": "rimraf dist && swc src -d dist && tsc --project tsconfig.esm.json", | ||
"release": "npm run build && npm run test && npm publish" | ||
}, | ||
"dependencies": { | ||
"@sinclair/typebox": "^0.25.21", | ||
"openapi-types": "^12.1.0", | ||
"raikiri": "^0.0.0-beta.8", | ||
"superjson": "^1.12.2" | ||
}, | ||
"devDependencies": { | ||
"@swc/cli": "^0.1.60", | ||
"@swc/core": "^1.3.32", | ||
"@types/node": "^18.11.18", | ||
"@typescript-eslint/eslint-plugin": "^5.48.2", | ||
"@typescript-eslint/parser": "^5.48.2", | ||
"bun-types": "^0.5.0", | ||
"eslint": "^8.32.0", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.9.4" | ||
}, | ||
"peerDependencies": { | ||
"@sinclair/typebox": ">= 0.25.21", | ||
"openapi-types": ">= 12.0.0" | ||
}, | ||
"peerDependenciesMeta": { | ||
"@sinclair/typebox": { | ||
"optional": true | ||
}, | ||
"openapi-types": { | ||
"optional": true | ||
} | ||
} | ||
} |
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
474
52884
6
19
Updatedraikiri@^0.0.0-beta.8