UNPKG

@luukgoossen/elysia-procedures

Version:

tRPC style procedures and actions with TypeBox validation

3 lines (2 loc) 2.03 kB
import{b as I,c as W,d as X}from"./action.js";var V=new WeakMap,_=(E,F)=>`${E}:[${F.join(",")}]`;class Y{_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)=>W({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 L=await this._handler(E);return F?.setAttribute("procedure.cache","unavailable"),F?.end(performance.timeOrigin+performance.now()),L}let G=_(this._id,await this._keys(E)),H=V.get(E.ctx.request)??new Map;if(H.has(G)){let L=H.get(G);return F?.setAttribute("procedure.cache.hit",!0),F?.end(performance.timeOrigin+performance.now()),L}let R=await this._handler(E);return H.set(G,R??null),V.set(E.ctx.request,H),F?.setAttribute("procedure.cache.hit",!1),F?.end(performance.timeOrigin+performance.now()),R})}class N{_state;constructor(E){this._state=E}_apply=(E)=>{return new N({...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 Y(E,this._state.name,this._state.config,this._state.keys);this._state.middlewares=[...this._state.middlewares,F]}return new Z(this._state)}}class Z{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 X({params:this.params,query:this.query,body:this.body,output:void 0,middlewares:this.middlewares,name:E,details:F})}}var x=(E,F,G={})=>new N({params:F?.params,query:F?.query,body:F?.body,middlewares:F?.middlewares??[],name:E,config:G});export{x as createProcedure,N as ProcedureBuilder,Z as Procedure,Y as Middleware}; export{x as a};