TypeScript
Start with the gRPC guide to create a Platform API token and download platform.proto. Run the commands below from the directory containing that file.
Browsers cannot open native gRPC HTTP/2 connections. The Platform API also accepts gRPC-Web on its HTTP/1.1 endpoint. Install protoc, the protoc-gen-js and protoc-gen-grpc-web plugins, and the runtime packages:
npm install grpc-web google-protobuf
npm install --save-dev @types/google-protobuf
mkdir -p src/generated
protoc -I. platform.proto \
--js_out=import_style=commonjs,binary:src/generated \
--grpc-web_out=import_style=typescript,mode=grpcweb:src/generated
Download the compiler plugins from the official gRPC-Web releases and setup guide; they must be on your PATH. The command generates platform_pb.js, its TypeScript declarations, and PlatformServiceClientPb.ts.
import {PlatformServicePromiseClient} from './generated/PlatformServiceClientPb';
import {GetCatalogRequest, ListCatalogsRequest} from './generated/platform_pb';
// Supply a token at runtime. Do not bundle a long-lived API token in browser code.
export async function listCatalogDetails(token: string) {
const client = new PlatformServicePromiseClient('https://reconstruct.dev');
const metadata = {authorization: `Bearer ${token}`};
const listRequest = new ListCatalogsRequest();
listRequest.setPageSize(10);
const page = await client.listCatalogs(listRequest, metadata);
for (const catalog of page.getCatalogsList()) {
const detailRequest = new GetCatalogRequest();
detailRequest.setCatalogId(catalog.getCatalogId());
const detail = await client.getCatalog(detailRequest, metadata);
console.log(detail.getCatalogId(), detail.getDescription());
}
}
For a browser app, the public API must allow that app's origin through CORS. Keep persistent API tokens on a trusted server; a browser bundle cannot protect an embedded secret.