Skip to content

Generating TypeScript gRPC bindings for Cratis Chronicle

This document explains the TypeScript gRPC package generation and publishing setup for Chronicle.

The Chronicle TypeScript gRPC package (@cratis/chronicle.contracts) provides strongly-typed TypeScript bindings for Chronicle’s gRPC services. The package generation process consists of two main steps:

  1. Generate .proto files from C# gRPC service definitions (see Protobuf Extraction)
  2. Generate TypeScript code from proto files and publish the npm package

1. TypeScript Package (Source/Clients/TypeScript)

Section titled “1. TypeScript Package (Source/Clients/TypeScript)”

An npm package that provides strongly-typed TypeScript bindings for the Chronicle gRPC services.

Structure:

  • package.json - Package configuration with @cratis/chronicle.contracts as the package name
  • tsconfig.json - TypeScript compiler configuration
  • rollup.config.mjs - Rollup bundler configuration for ESM and CJS builds
  • index.ts - Main entry point that re-exports the generated barrel (./generated/index)
  • generated/ - Directory containing TypeScript files generated from proto definitions
  • README.md - Package documentation

How it works:

  • Uses ts-proto to generate TypeScript files from proto definitions
  • Generates proto int64/uint64 fields as TypeScript bigint (forceLong=bigint)
  • Generates strongly-typed clients with full IDE support and IntelliSense
  • Exports the generated messages and service definitions through the barrel in index.ts
  • Builds both ESM and CJS versions using Rollup

Build Workflow (.github/workflows/typescript-build.yml)

Section titled “Build Workflow (.github/workflows/typescript-build.yml)”

Validates the TypeScript package.

Triggers:

  • Manual dispatch (workflow_dispatch)
  • Non-draft pull requests (opened, synchronized, reopened or marked ready for review) that change the TypeScript client, Contracts, Protobuf, or ProtoGenerator paths. It does not run on push to main.

Steps:

  1. Setup .NET 10 and Node.js 24.x
  2. Install protoc (Protocol Buffer Compiler)
  3. Build the Contracts assembly
  4. Run ProtoGenerator to generate .proto files
  5. Install TypeScript dependencies
  6. Generate TypeScript files from proto definitions using ts-proto
  7. Build TypeScript package

Publish Workflow (.github/workflows/publish.yml)

Section titled “Publish Workflow (.github/workflows/publish.yml)”

Publishes the TypeScript package to NPM alongside the .NET packages.

Triggers:

  • Manual dispatch (workflow_dispatch) with a version
  • A merged pull request into main that changes the paths the workflow watches (such as Source/**), subject to the release selection of cratis/release-action; it publishes only when that selects a release

Delegation: after the release and compatibility jobs, publish.yml calls the reusable workflow .github/workflows/publish-typescript-client.yml with the release version and the generated proto files artifact.

Steps in the reusable workflow:

  1. Setup Node.js 24.x and upgrade npm for trusted publishing
  2. Install protoc
  3. Use the proto files artifact, or generate them when none is given
  4. Install TypeScript dependencies
  5. Generate TypeScript files using ts-proto
  6. Build the TypeScript package and check packed consumers
  7. Set the package version
  8. Publish to NPM with npm publish --provenance --access public --tag latest

Publishing uses npm trusted publishing (id-token: write), not an NPM_TOKEN secret.

Proto files must be generated first. See Protobuf Extraction for details on generating proto files from C# contracts.

Terminal window
cd Source/Clients/TypeScript
yarn install
yarn build

Install the package:

Terminal window
npm install @cratis/chronicle.contracts
# or
yarn add @cratis/chronicle.contracts

Use the generated service definitions from your gRPC transport code:

import { EventStoresDefinition } from '@cratis/chronicle.contracts';
const eventStoresService = EventStoresDefinition.fullName;
const eventStoresMethods = Object.keys(EventStoresDefinition.methods);
console.log(`Generated service ${eventStoresService} exposes: ${eventStoresMethods.join(', ')}`);

View TypeScript snippet source on GitHub

The package also includes generated request and response message helpers:

import { EnsureEventStoreRequest, EnsureNamespaceRequest, AllNamespacesRequest } from '@cratis/chronicle.contracts';
const eventStore = EnsureEventStoreRequest.create({ Name: 'shopping' });
const namespace = EnsureNamespaceRequest.create({ EventStore: eventStore.Name, Namespace: 'tenant-one' });
const namespaces = AllNamespacesRequest.create({ EventStore: eventStore.Name });
console.log(eventStore, namespace, namespaces);

View TypeScript snippet source on GitHub

Service definitions expose the protobuf service name and generated method definitions:

import { NamespacesDefinition } from '@cratis/chronicle.contracts';
const allNamespacesMethod = NamespacesDefinition.methods.allNamespaces;
console.log(`${NamespacesDefinition.fullName}.${allNamespacesMethod.name}`);

View TypeScript snippet source on GitHub

When a gRPC transport creates clients from the generated definitions, use the generated client interfaces for type safety:

import { EventStoresClient, NamespacesClient } from '@cratis/chronicle.contracts';
async function readAvailableNamespaces(eventStores: EventStoresClient, namespaces: NamespacesClient): Promise<string[]> {
await eventStores.ensureEventStore({ Name: 'shopping' });
const result = await namespaces.allNamespaces({ EventStore: 'shopping' });
return result.Data.map(namespace => namespace.Name);
}

View TypeScript snippet source on GitHub

The TypeScript package is published by publish-typescript-client.yml, which supports direct manual dispatch and is also called by publish.yml after manual dispatch or an eligible merged pull request into main, subject to release selection. It does not use a release-created trigger. The workflow will:

  1. Use the proto files from the release run, or generate them from C# contracts
  2. Generate TypeScript code from proto files
  3. Build the TypeScript package
  4. Publish to NPM with the release version

Proto files: Source/Kernel/Protobuf/*.proto

See Protobuf Extraction for the complete list of generated proto files.

TypeScript files: Source/Clients/TypeScript/generated/*.ts

The TypeScript generation creates:

  • Service client interfaces and implementations
  • Request and response message types
  • Enum definitions
  • Type definitions for all Chronicle concepts

When adding new gRPC services to the C# Contracts:

  1. Ensure services are decorated with [Service] attribute
  2. Ensure methods are decorated with [Operation] attribute
  3. Regenerate proto files (see Protobuf Extraction)
  4. Run the TypeScript generation to create new TypeScript clients
  5. The TypeScript package will automatically include the new services with full type safety
  6. A new version of the npm package is published by the publish workflow described above

The published npm package includes:

  • ESM build (dist/esm/) - ES modules for modern bundlers
  • CJS build (dist/cjs/) - CommonJS for Node.js compatibility
  • Type definitions (dist/esm/index.d.ts and dist/cjs/index.d.ts) - TypeScript .d.ts files for IntelliSense
  • Source maps - For debugging support
  • README.md - Package documentation

The TypeScript package has the following runtime dependencies:

  • @bufbuild/protobuf
  • @grpc/grpc-js
  • nice-grpc

See Source/Clients/TypeScript/package.json for the versions.

Development dependencies include:

  • ts-proto - TypeScript code generation from proto files
  • typescript - TypeScript compiler
  • rollup - Module bundler for builds
  • Various Rollup plugins for TypeScript and module resolution