tatethurston / TwirpScript

A protobuf RPC framework for JavaScript and TypeScript
MIT License
141 stars 14 forks source link

Allow specifying a custom rpcTransport in ClientConfiguration #189

Closed jacobwgillespie closed 2 years ago

jacobwgillespie commented 2 years ago

Hey! 👋

This PR adds the ability to provide an rpcTransport directly at a client call site, so that the client can be changed dynamically for that specific call:

const hat = await MakeHat({inches: 12}, {rpcTransport: myCustomTransport})

I'm using TwirpScript with Cloudflare Workers and am additionally using Twirp to communicate between Workers and Durable Objects. Durable Objects provide a custom fetch client for communicating with that specific object, so I need a way to dynamically choose the transport per call:

async function example(env) {
  const id = env.OBJECT.idFromName('example')
  const stub = env.OBJECT.get(id)
  return await MakeHat({inches: 12}, {rpcTransport: stub.fetch})
}
tatethurston commented 2 years ago

Thanks for the PR @jacobwgillespie. This looks great, and is a cool use case.

jacobwgillespie commented 2 years ago

I got the same test failure locally, it looks the test believes clientcompat is returning an empty string? Running clientcompat directly did print the expected output.

jacobwgillespie commented 2 years ago

Ah, I think I found my local issue - the test calls clientcompat, which assumes it's installed globally. Changing it to const child = spawnSync("./clientcompat -client=./test", ... works, though I assume that binary is only for macOS and wouldn't run in Actions.

Currently the output of stderr is /bin/sh: clientcompat: command not found.

tatethurston commented 2 years ago

I can look into that failing test case. This PR looks great as is.

tatethurston commented 2 years ago

Yeah clientcompat is a vendored go get of https://github.com/twitchtv/twirp/tree/main/clientcompat