UNPKG

@luukgoossen/elysia-procedures

Version:

tRPC style procedures and actions with TypeBox validation

2 lines (1 loc) 2 kB
var x={};$(x,{createProcedure:()=>v,ProcedureBuilder:()=>L,Procedure:()=>V,Middleware:()=>R});module.exports=_(x);var X=new WeakMap,j=(E,F)=>`${E}:[${F.join(",")}]`;class R{_id=crypto.randomUUID();_handler;_keys;name;config;constructor(E,F,G,H){this._handler=E,this._keys=H,this.name=F,this.config=G}execute=async(E)=>Y({name:this.config.tracing?.name??this.name,op:"procedure.middleware",startTime:performance.timeOrigin+performance.now(),attributes:{"procedure.type":"middleware","procedure.name":this.name,...this.config.tracing?.attributes}},async(F)=>{if(!this._keys){let N=await this._handler(E);return F?.setAttribute("procedure.cache","unavailable"),F?.end(performance.timeOrigin+performance.now()),N}let G=j(this._id,await this._keys(E)),H=X.get(E.ctx.request)??new Map;if(H.has(G)){let N=H.get(G);return F?.setAttribute("procedure.cache.hit",!0),F?.end(performance.timeOrigin+performance.now()),N}let W=await this._handler(E);return H.set(G,W??null),X.set(E.ctx.request,H),F?.setAttribute("procedure.cache.hit",!1),F?.end(performance.timeOrigin+performance.now()),W})}class L{_state;constructor(E){this._state=E}_apply=(E)=>{return new L({...this._state,...E})};params=(E)=>{let F=I(this._state.params,E);return this._apply({params:F})};query=(E)=>{let F=I(this._state.query,E);return this._apply({query:F})};body=(E)=>{let F=I(this._state.body,E);return this._apply({body:F})};cache=(E)=>this._apply({keys:E});build=(E)=>{if(E){let F=new R(E,this._state.name,this._state.config,this._state.keys);this._state.middlewares=[...this._state.middlewares,F]}return new V(this._state)}}class V{params;query;body;middlewares;constructor(E){this.params=E.params,this.query=E.query,this.body=E.body,this.middlewares=E.middlewares}createAction=(E,F)=>{return new Z({params:this.params,query:this.query,body:this.body,output:void 0,middlewares:this.middlewares,name:E,details:F})}}var v=(E,F,G={})=>new L({params:F?.params,query:F?.query,body:F?.body,middlewares:F?.middlewares??[],name:E,config:G});