A TypeScript module for interfacing with our Haskell-based backend.
npm install https://github.com/1protocol/bridge-client --save
Take a look at the examples
folder. Note that users are responsible for opening a WebSocket connection and passing a compatible
object to BridgeClient.make
. Consider using a reconnecting WebSocket implementation, such as
this project.
Our back-end codebase relies on Haskell's type safety to prevent errors of all kinds. Naturally, we want our front-end RPC callers to use the same types as our back-end RPC handlers. Since we're using a different language (TypeScript) to build out the front-end, however, and since all data is text-serialized when it goes over the network, we can't just copy our request and response types from back-end to front-end.
Fortunately, Haskell magic lets us generate RPC functions and types for the TypeScript front-end to use. This requires only some up-front work to create the right boilerplate. Now whenever the back-end API changes, our RPC functions can be automatically re-generated from templates—with no room for manual translation errors.
In more concrete terms, let's say that the back-end had the following API:
- Route
foo
from aFooRequest
to a singleFooResponse
. - Route
bar
from aBarRequest
to a singleBarResponse
. - Route
baz
from aBazRequest
to streamingBazResponse
.
After we generate this module, it will contain these three functions (roughly):
foo (webSocket: WebSocket, request: FooRequest): Promise<FooResponse>
bar (webSocket: WebSocket, request: BarRequest): Promise<BarResponse>
baz (webSocket: WebSocket, request: BazRequest): Stream<BazResponse>
This module exports namespaces like ServerNameX
with our actual generated functions.