You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 17, 2022. It is now read-only.
Hi! Super excited about this library, gRPC was too unapproachable no its own but this library has a much smaller footprint which is awesome. One thing that stuck out though was your service definitions. They look a tad awkward. Messing around, I figured out how to convert handlers into definitions using typescript object traversal. I was wondering if this could be useful in some capacity.
import{NodeHttpTransport}from'@improbable-eng/grpc-web-node-http-transport'import{ModuleRpcCommon}from'rpc_ts/lib/common'import{ModuleRpcServer}from'rpc_ts/lib/server'import{ModuleRpcProtocolServer}from'rpc_ts/lib/protocol/server'import{ModuleRpcProtocolClient}from'rpc_ts/lib/protocol/client'typeArgumentTypes<FextendsFunction>=Fextends(...args: infer A)=>any ? A : nevertypeThenArg<T>=TextendsPromise<infer U> ? U : TtypeServiceHandlers={[fnName: string]: (...args: any[])=>any}typeConvertHandlersToDefinitions<IextendsServiceHandlers>={[KinkeyofI]: {request: ArgumentTypes<I[K]>[0]response: ThenArg<ReturnType<I[K]>>}}constcreateServiceDefinitions=<TextendsServiceHandlers>(serviceHandlers: T): ConvertHandlersToDefinitions<T>=>{constdefinitions: any=Object.entries(serviceHandlers).reduce((acc,[key,fn])=>{acc[key]={request: {},response: {}}returnacc},{})returndefinitions}// Definition of the RPC serviceconsthelloServiceHandlers={getHello: async(request: {language: string})=>{const{ language }=requestif(language==='Spanish')return{text: 'Hola'}thrownewModuleRpcServer.ServerRpcError(ModuleRpcCommon.RpcErrorType.notFound,`language '${language}' not found`)}}// Implementation of an RPC serverimport*asexpressfrom'express'import*ashttpfrom'http'constapp=express()exportconsthelloServiceDefinition=createServiceDefinitions(helloServiceHandlers)app.use(ModuleRpcProtocolServer.registerRpcRoutes(helloServiceDefinition,helloServiceHandlers))constserver=http.createServer(app).listen(4000,()=>{console.log('server started')})
Personally I like the idea of defining my protocols once instead of a type and then a handler. However, if you think defnitions should be the source of truth (like gRPC) then you could define a type only in typescript and pass that around instead of this definitions object that also holds the definition types
Hi! Super excited about this library, gRPC was too unapproachable no its own but this library has a much smaller footprint which is awesome. One thing that stuck out though was your service definitions. They look a tad awkward. Messing around, I figured out how to convert handlers into definitions using typescript object traversal. I was wondering if this could be useful in some capacity.
Personally I like the idea of defining my protocols once instead of a type and then a handler. However, if you think defnitions should be the source of truth (like gRPC) then you could define a type only in typescript and pass that around instead of this definitions object that also holds the definition types