Socket
Socket
Sign inDemoInstall

elysia

Package Overview
Dependencies
Maintainers
1
Versions
410
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elysia - npm Package Compare versions

Comparing version 0.3.0-rc.4 to 0.3.0-rc.5

8

dist/index.d.ts

@@ -41,7 +41,5 @@ /// <reference types="bun-types" />

store: Instance['store'] & NewInstance['store'];
meta: Instance['meta'] & (Omit<NewInstance['meta'], typeof SCHEMA> & {
[key in typeof SCHEMA]: {
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['meta'][typeof SCHEMA][key];
};
});
meta: Instance['meta'] & (Omit<NewInstance['meta'], typeof SCHEMA> & Record<typeof SCHEMA, {
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}` ? `${Prefix}${Rest}` : key]: NewInstance['meta'][typeof SCHEMA][key];
}>);
}> : this;

@@ -48,0 +46,0 @@ 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<{

@@ -1,1 +0,1 @@

import{nanoid as e}from"nanoid";import{Raikiri as t}from"raikiri";import{mapResponse as r,mapEarlyResponse as s}from"./handler";import{permission as a}from"./fn";import{SCHEMA as n,EXPOSED as i,DEFS as o,mapQuery as h,clone as l,mergeHook as d,mergeDeep as u,createValidationError as f,getSchemaValidator as m,getResponseSchemaValidator as c,mapPathnameAndQueryRegEx as p}from"./utils";import{registerSchemaPath as v}from"./schema";import{mapErrorCode as g,mapErrorStatus as y}from"./error";import{runFn as b}from"./fn";import{deserialize as w}from"superjson";export default class H{store={};meta={[n]:Object.create(null),[o]:Object.create(null),[i]:Object.create(null)};decorators={[n]:this.meta[n],[o]:this.meta[o]};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new t;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:d(l(this.event),s)});let a=this.meta[o],i=m(s?.schema?.body??this.$schema?.body,a),h=m(s?.schema?.headers??this.$schema?.headers,a,!0),u=m(s?.schema?.params??this.$schema?.params,a),f=m(s?.schema?.query??this.$schema?.query,a),p=c(s?.schema?.response??this.$schema?.response,a);v({schema:this.meta[n],contentType:s?.schema?.contentType,hook:s,method:e,path:t,models:this.meta[o]});let g=d(l(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:i||h||u||f||p?{body:i,header:h,params:u,query:f,response:p}: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,d(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,d(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,d(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}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:m(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"},{beforeHandle:r.beforeHandle,transform:r.transform,schema:{headers:r.schema?.headers,params:r.schema?.params,query:r.schema?.query}}),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}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 0===Object.keys(this.meta[i]).length&&this.post(this.config.fn??"/~fn",e=>b(e,this.meta[i])),this.meta[i]=u(this.meta[i],"function"==typeof e?e({...this.decorators,store:this.store,permission:a}):e),this}schema(e){let t=this.meta[o];return this.$schema={body:m(e.body,t),headers:m(e?.headers,t,!0),params:m(e?.params,t),query:m(e?.query,t),response:m(e?.response,t)},this}handle=async e=>{let t;let a=Object.assign({query:{},set:{status:200,headers:{}},store:this.store,request:e},this.decorators);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=s(t,a.set))return t}let i=e.url.match(p);if(!i)throw Error("NOT_FOUND");let o=this.router.match(e.method,i[1])??this.router.match("ALL",i[1]);if(!o)throw Error("NOT_FOUND");let l=o.store,d=l.hooks;if(d?.error&&(t=d?.error),"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=h(await e.text());break;case"multipart/form-data":n={},await e.formData().then(e=>{for(let t of e.keys()){if(t in n)continue;let r=e.getAll(t);1===r.length?n[t]=r[0]:n[t]=r}});break;case"elysia/fn":n=w(await e.json())}}}if(a.body=n,a.params=o.params,i[2]&&(a.query=h(i[2])),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 r=l.validator;if(d?.error&&(t=d?.error),r.headers){let t={};for(let r in e.headers)t[r]=e.headers.get(r);if(!1===r.headers.Check(t))throw f("header",r.headers,t)}if(!1===r.params?.Check(a.params))throw f("params",r.params,a.params);if(!1===r.query?.Check(a.query))throw f("query",r.query,a.query);if(!1===r.body?.Check(n))throw f("body",r.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=s(t,a.set);if(e)return e}}let u=l.handle(a);if(u instanceof Promise&&(u=await u),!1===l.validator?.response?.Check(u))throw d?.error&&(t=d?.error),f("response",l.validator.response,u);if(d?.afterHandle.length)for(let e=0;e<d.afterHandle.length;e++){let t=d.afterHandle[e](a,u);t instanceof Promise&&(t=await t);let r=s(t,a.set);if(r)return r}return r(u,a.set)}catch(n){let r=a.set;if((!r.status||r.status<300)&&(r.status=500),t){let a=g(n.message);for(let i=0;i<t.length;i++){let o=t[i]({request:e,error:n,set:r,code:a});o instanceof Promise&&(o=await o);let h=s(o,r);if(h)return h}}return this.handleError(e,n,r)}};async handleError(e,t,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let n=this.event.error[a]({request:e,code:g(t.message),error:t,set:s});if(n instanceof Promise&&(n=await n),null!=n)return r(n,s)}return new Response("string"==typeof t.cause?t.cause:t.message,{headers:s.headers,status:y(g(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.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[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,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{H as Elysia,a as permission};
import{nanoid as e}from"nanoid";import{Raikiri as t}from"raikiri";import{mapResponse as r,mapEarlyResponse as s}from"./handler";import{permission as a}from"./fn";import{SCHEMA as n,EXPOSED as i,DEFS as o,mapQuery as h,clone as l,mergeHook as d,mergeDeep as u,createValidationError as f,getSchemaValidator as m,getResponseSchemaValidator as c,mapPathnameAndQueryRegEx as p}from"./utils";import{registerSchemaPath as v}from"./schema";import{mapErrorCode as g,mapErrorStatus as y}from"./error";import{runFn as b}from"./fn";import{deserialize as w}from"superjson";export default class H{store={};meta={[n]:Object.create(null),[o]:Object.create(null),[i]:Object.create(null)};decorators={[n]:this.meta[n],[o]:this.meta[o]};event={start:[],request:[],parse:[],transform:[],beforeHandle:[],afterHandle:[],error:[],stop:[]};server=null;$schema=null;router=new t;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:d(l(this.event),s)});let a=this.meta[o],i=m(s?.schema?.body??this.$schema?.body,a),h=m(s?.schema?.headers??this.$schema?.headers,a,!0),u=m(s?.schema?.params??this.$schema?.params,a),f=m(s?.schema?.query??this.$schema?.query,a),p=c(s?.schema?.response??this.$schema?.response,a);v({schema:this.meta[n],contentType:s?.schema?.contentType,hook:s,method:e,path:t,models:this.meta[o]});let g=d(l(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:i||h||u||f||p?{body:i,header:h,params:u,query:f,response:p}: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]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:r,handler:a,hooks:n})=>{"/"===r?this._addHandler(t,e,a,d(n,{error:s.event.error})):this._addHandler(t,`${e}${r}`,a,d(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]),this.setModel(s.meta[o]),Object.values(r.routes).forEach(({method:t,path:r,handler:s,hooks:a})=>{this._addHandler(t,r,s,d(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}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:m(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"},{beforeHandle:r.beforeHandle,transform:r.transform,schema:{headers:r.schema?.headers,params:r.schema?.params,query:r.schema?.query}}),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}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 0===Object.keys(this.meta[i]).length&&this.post(this.config.fn??"/~fn",e=>b(e,this.meta[i])),this.meta[i]=u(this.meta[i],"function"==typeof e?e({...this.decorators,store:this.store,permission:a}):e),this}schema(e){let t=this.meta[o];return this.$schema={body:m(e.body,t),headers:m(e?.headers,t,!0),params:m(e?.params,t),query:m(e?.query,t),response:m(e?.response,t)},this}handle=async e=>{let t;let a=Object.assign({query:{},set:{status:200,headers:{}},store:this.store,request:e},this.decorators);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=s(t,a.set))return t}let i=e.url.match(p);if(!i)throw Error("NOT_FOUND");let o=this.router.match(e.method,i[1])??this.router.match("ALL",i[1]);if(!o)throw Error("NOT_FOUND");let l=o.store,d=l.hooks;if(d?.error&&(t=d?.error),"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=h(await e.text());break;case"multipart/form-data":n={},await e.formData().then(e=>{for(let t of e.keys()){if(t in n)continue;let r=e.getAll(t);1===r.length?n[t]=r[0]:n[t]=r}});break;case"elysia/fn":n=w(await e.json())}}}if(a.body=n,a.params=o.params,i[2]&&(a.query=h(i[2])),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 r=l.validator;if(d?.error&&(t=d?.error),r.headers){let t={};for(let r in e.headers)t[r]=e.headers.get(r);if(!1===r.headers.Check(t))throw f("header",r.headers,t)}if(!1===r.params?.Check(a.params))throw f("params",r.params,a.params);if(!1===r.query?.Check(a.query))throw f("query",r.query,a.query);if(!1===r.body?.Check(n))throw f("body",r.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=s(t,a.set);if(e)return e}}let u=l.handle(a);if(u instanceof Promise&&(u=await u),!1===l.validator?.response?.Check(u))throw d?.error&&(t=d?.error),f("response",l.validator.response,u);if(d?.afterHandle.length)for(let e=0;e<d.afterHandle.length;e++){let t=d.afterHandle[e](a,u);t instanceof Promise&&(t=await t);let r=s(t,a.set);if(r)return r}return r(u,a.set)}catch(n){let r=a.set;if((!r.status||r.status<300)&&(r.status=500),t){let a=g(n.message);for(let i=0;i<t.length;i++){let o=t[i]({request:e,error:n,set:r,code:a});o instanceof Promise&&(o=await o);let h=s(o,r);if(h)return h}}return this.handleError(e,n,r)}};async handleError(e,t,s={headers:{}}){for(let a=0;a<this.event.error.length;a++){let n=this.event.error[a]({request:e,code:g(t.message),error:t,set:s});if(n instanceof Promise&&(n=await n),null!=n)return r(n,s)}return new Response("string"==typeof t.cause?t.cause:t.message,{headers:s.headers,status:y(g(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.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[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,mapPathnameAndQueryRegEx,mapQuery}from"./utils";export{H as Elysia,a as permission};

@@ -15,2 +15,2 @@ import { TSchema } from '@sinclair/typebox';

export declare const getSchemaValidator: (s: TSchema | string | undefined, models: Record<string, TSchema>, additionalProperties?: boolean) => TypeCheck<TSchema> | undefined;
export declare const getResponseSchemaValidator: (s: TypedSchema['response'] | undefined, models: Record<string, TSchema>, additionalProperties?: boolean) => TypeCheck<TSchema> | undefined;
export declare const getResponseSchemaValidator: (s: TypedSchema['response'] | undefined, models: Record<string, TSchema>, additionalProperties?: boolean) => TSchema | Record<string, string | TSchema> | TypeCheck<TSchema> | undefined;

@@ -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)};
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,detail:mergeDeep(o?.detail??{},t?.detail??{})}: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));"object"===i.type&&"additionalProperties"in i==!1&&(i.additionalProperties=a);try{return t.Compile(i)}catch(e){return s}};
{
"name": "elysia",
"description": "Fast, and friendly Bun web framework",
"version": "0.3.0-rc.4",
"version": "0.3.0-rc.5",
"author": {

@@ -6,0 +6,0 @@ "name": "saltyAom",

@@ -185,2 +185,3 @@ import type { Serve, Server } from 'bun'

)
if (

@@ -201,3 +202,3 @@ !hooks.schema &&

this.router.add(method, path, mainHandler)
this.router.add(method, path, mainHandler as any)
}

@@ -477,9 +478,11 @@

meta: Instance['meta'] &
(Omit<NewInstance['meta'], typeof SCHEMA> & {
[key in typeof SCHEMA]: {
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}`
? `${Prefix}${Rest}`
: key]: NewInstance['meta'][typeof SCHEMA][key]
}
})
(Omit<NewInstance['meta'], typeof SCHEMA> &
Record<
typeof SCHEMA,
{
[key in keyof NewInstance['meta'][typeof SCHEMA] as key extends `${infer Rest}`
? `${Prefix}${Rest}`
: key]: NewInstance['meta'][typeof SCHEMA][key]
}
>)
}>

@@ -499,2 +502,4 @@ : this {

this.setModel(sandbox.meta[DEFS])
Object.values(instance.routes).forEach(

@@ -579,2 +584,4 @@ ({ method, path, handler, hooks }) => {

this.setModel(sandbox.meta[DEFS])
Object.values(instance.routes).forEach(

@@ -581,0 +588,0 @@ ({ method, path, handler, hooks: localHook }) => {

@@ -41,6 +41,15 @@ import { Kind, TSchema, Type } from '@sinclair/typebox'

query: bSchema?.query ?? aSchema?.query,
response: bSchema?.response ?? aSchema?.response
response: bSchema?.response ?? aSchema?.response,
detail: mergeDeep(
// @ts-ignore
bSchema?.detail ?? {},
// @ts-ignore
aSchema?.detail ?? {}
)
} as TypedSchema)
: undefined,
transform: mergeObjectArray(a.transform ?? [], b?.transform ?? []) as any,
transform: mergeObjectArray(
a.transform ?? [],
b?.transform ?? []
) as any,
beforeHandle: mergeObjectArray(

@@ -158,2 +167,3 @@ a.beforeHandle ?? [],

const schema = models[maybeNameOrSchema]
return schema

@@ -174,3 +184,8 @@ }

return TypeCompiler.Compile(schema)
try {
return TypeCompiler.Compile(schema)
} catch (error) {
// Likely is already compile
return maybeSchemaOrRecord
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc