Generating TypeScript gRPC bindings for Cratis Chronicle
This document explains the TypeScript gRPC package generation and publishing setup for Chronicle.
Overview
Section titled “Overview”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:
- Generate .proto files from C# gRPC service definitions (see Protobuf Extraction)
- Generate TypeScript code from proto files and publish the npm package
Components
Section titled “Components”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 nametsconfig.json- TypeScript compiler configurationrollup.config.mjs- Rollup bundler configuration for ESM and CJS buildsindex.ts- Main entry point that re-exports the generated barrel (./generated/index)generated/- Directory containing TypeScript files generated from proto definitionsREADME.md- Package documentation
How it works:
- Uses
ts-prototo generate TypeScript files from proto definitions - Generates proto
int64/uint64fields as TypeScriptbigint(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
2. GitHub Workflows
Section titled “2. GitHub Workflows”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:
- Setup .NET 10 and Node.js 24.x
- Install protoc (Protocol Buffer Compiler)
- Build the Contracts assembly
- Run ProtoGenerator to generate .proto files
- Install TypeScript dependencies
- Generate TypeScript files from proto definitions using ts-proto
- 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 ofcratis/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:
- Setup Node.js 24.x and upgrade npm for trusted publishing
- Install protoc
- Use the proto files artifact, or generate them when none is given
- Install TypeScript dependencies
- Generate TypeScript files using ts-proto
- Build the TypeScript package and check packed consumers
- Set the package version
- Publish to NPM with
npm publish --provenance --access public --tag latest
Publishing uses npm trusted publishing (id-token: write), not an NPM_TOKEN secret.
Prerequisites
Section titled “Prerequisites”Proto files must be generated first. See Protobuf Extraction for details on generating proto files from C# contracts.
Building TypeScript Package Locally
Section titled “Building TypeScript Package Locally”cd Source/Clients/TypeScriptyarn installyarn buildUsing the Published Package
Section titled “Using the Published Package”Install the package:
npm install @cratis/chronicle.contracts# oryarn add @cratis/chronicle.contractsUse 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(', ')}`);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);Service Definitions
Section titled “Service Definitions”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}`);Typed Clients
Section titled “Typed Clients”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);}Publishing a New Version
Section titled “Publishing a New Version”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:
- Use the proto files from the release run, or generate them from C# contracts
- Generate TypeScript code from proto files
- Build the TypeScript package
- Publish to NPM with the release version
Generated Files
Section titled “Generated Files”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
Maintenance
Section titled “Maintenance”When adding new gRPC services to the C# Contracts:
- Ensure services are decorated with
[Service]attribute - Ensure methods are decorated with
[Operation]attribute - Regenerate proto files (see Protobuf Extraction)
- Run the TypeScript generation to create new TypeScript clients
- The TypeScript package will automatically include the new services with full type safety
- A new version of the npm package is published by the publish workflow described above
Package Structure
Section titled “Package Structure”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.tsanddist/cjs/index.d.ts) - TypeScript .d.ts files for IntelliSense - Source maps - For debugging support
- README.md - Package documentation
Dependencies
Section titled “Dependencies”The TypeScript package has the following runtime dependencies:
@bufbuild/protobuf@grpc/grpc-jsnice-grpc
See Source/Clients/TypeScript/package.json for the versions.
Development dependencies include:
ts-proto- TypeScript code generation from proto filestypescript- TypeScript compilerrollup- Module bundler for builds- Various Rollup plugins for TypeScript and module resolution