@luukgoossen/elysia-procedures
Version:
tRPC style procedures and actions with TypeBox validation
2 lines (1 loc) • 2.91 kB
JavaScript
var{defineProperty:K,getOwnPropertyNames:W,getOwnPropertyDescriptor:X}=Object,Y=Object.prototype.hasOwnProperty;var O=new WeakMap,Z=(z)=>{var D=O.get(z),E;if(D)return D;if(D=K({},"__esModule",{value:!0}),z&&typeof z==="object"||typeof z==="function")W(z).map((F)=>!Y.call(D,F)&&K(D,F,{get:()=>z[F],enumerable:!(E=X(z,F))||E.enumerable}));return O.set(z,D),D};var _=(z,D)=>{for(var E in D)K(z,E,{get:D[E],enumerable:!0,configurable:!0,set:(F)=>D[E]=()=>F})};var $={};_($,{ActionBuilder:()=>L,Action:()=>M});module.exports=Z($);var H=require("@sinclair/typebox/value");var R=require("@sinclair/typebox"),S=(z)=>z.replace(/(?:^\w|[A-Z]|\b\w)/g,(D,E)=>E===0?D.toLowerCase():D.toUpperCase()).replace(/\s+/g,""),J=(z,D)=>R.Type.Object({...D.properties,...z?z.properties:{}},{additionalProperties:!1});var G=require("@elysiajs/opentelemetry");class L{_state;constructor(z){this._state=z}_apply=(z)=>{return new L({...this._state,...z})};params=(z)=>{let D=J(this._state.params,z);return this._apply({params:D})};query=(z)=>{let D=J(this._state.query,z);return this._apply({query:D})};body=(z)=>{let D=J(this._state.body,z);return this._apply({body:D})};output=(z)=>this._apply({output:z});build=(z)=>{return new M({handler:z,...this._state})}}class M{_handler;_middlewares;name;details;params;query;body;output;constructor(z){this._handler=z.handler,this._middlewares=z.middlewares,this.name=z.name,this.details=z.details,this.params=z.params,this.query=z.query,this.body=z.body,this.output=z.output}get docs(){return{params:this.params,query:this.query,body:this.body,response:this.output,detail:{summary:this.name,operationId:S(this.name),...this.details}}}handle=async(z)=>G.record(`Action: ${this.name}`,{attributes:{type:"handle",action:this.name}},async()=>{let{params:D,query:E,body:F,...I}=z;return await this._execute(I,{params:D,query:E,body:F})});run=async(z,D)=>G.record(`Action: ${this.name}`,{attributes:{type:"run",action:this.name}},async()=>{let{params:E,query:F,body:I}=D;await G.record("Validate Inputs",{attributes:{type:"input",action:this.name}},async()=>{if(this.params)E=this.params?H.Value.Parse(this.params,D.params):D.params;if(this.query)F=this.query?H.Value.Parse(this.query,D.query):D.query;if(this.body)I=this.body?H.Value.Parse(this.body,D.body):D.body});let N=await this._execute(z,{params:E,query:F,body:I});if(!this.output)return N;let U=this.output;return G.record("Validate Output",{attributes:{type:"output",action:this.name}},()=>H.Value.Parse(U,N))});_execute=async(z,D)=>{for(let E of this._middlewares)await G.record(`Middleware: ${E.name}`,{attributes:{type:"middleware",action:this.name}},async()=>{let F=await E.execute({params:D.params,query:D.query,body:D.body,ctx:z});if(F)z={...z,...F}});return await G.record("Main Handler",{attributes:{type:"handler",action:this.name}},async()=>{return await this._handler({params:D.params,query:D.query,body:D.body,ctx:z})})}}