The Typescript API client supports operation in a web browser - e.g. in a web app/SPA - or in Node on a server.
npm install @connectrpc/connect @connectrpc/connect-web
npm config set @awts:registry https://buf.build/gen/npm/v1
npm install @buf/alphaweighted_awts.connectrpc_es
Create a new file called awts.ts
in your project, and put the below into it:
Replace http://localhost:35000
with the URL to wherever your AWTS environment is installed - this can be set from an environment variable if you prefer.
import { MuxService } from "@buf/alphaweighted_awts.connectrpc_es/awts/mux/v1/mux_connect";
import { createPromiseClient } from "@connectrpc/connect";
import { createGrpcWebTransport } from "@connectrpc/connect-web";
const transport = createGrpcWebTransport({
baseUrl: String('http://localhost:36000'),
});
export const muxClient = createPromiseClient(MuxService, transport)
Then, from your code, call APIs easily with a Promise-based syntax like this:
import { muxClient } from "@/backend/awts";
muxClient.status({}).then((resp:StatusResponse) => {
console.log(resp)
}).catch((reason:any) => {
console.error(reason)
})
Each of the API services can be accessed in the same way, using the respective service objects, e.g. DataService
, NewsService
, RunnerService
, etc.
For environments using Mux you can reuse the same transport
to connect to Mux, as it serves all service APIs and proxies requests through to the relevant backend module.
For example:
import { DataService } from "@buf/alphaweighted_awts.connectrpc_es/awts/data/v1/data_connect";
export const dataClient = createPromiseClient(DataService, transport)
dataClient.value({ref: 'POLYGON:SPX'}).then((resp:StatusResponse) => {
console.log('current price of SPX:', resp.last)
}).catch((reason:any) => {
console.error(reason)
})
Buf's material on how they generate Typescript from the .proto
specs is here:
https://buf.build/docs/bsr/generated-sdks/npm