npm NuGet v1.0.0 MIT
Protobuf to Fetch Client Generator
@metaengine/protobuf-fetchConvert Protobuf 3 to framework-agnostic TypeScript services and message types over the gRPC/Connect protocol on native Fetch. Zero dependencies, tree-shakeable, runs on Node, browsers, Vite, SvelteKit, and Next.js.
Install
Pick your registry
npmPrimary@metaengine/protobuf-fetch
$npm install @metaengine/protobuf-fetch
v1.0.0
NuGetMetaEngine.TypeScript.Protobuf.Fetch
$dotnet add package MetaEngine.TypeScript.Protobuf.Fetch
v1.0.0
Usage
Drive it from the CLI or programmatically
npm distributes the generator as a command-line tool. NuGet provides the C# fluent API for .NET projects.
npm · npx · CI-friendly
After install, point the CLI at your schema. It writes the generated tree to your chosen output directory.
Basic syntax
npx @metaengine/protobuf-fetch <input> <output> [options]Generates framework-agnostic TypeScript services and message types from Protobuf 3 .proto files, using gRPC/Connect over native Fetch.
Quick examples
Generate from a .proto file
npx @metaengine/protobuf-fetch service.proto ./src/apiWith Vite, result pattern and middleware
npx @metaengine/protobuf-fetch service.proto ./src/api \
--import-meta-env \
--base-url-env VITE_API_URL \
--result-pattern \
--middleware \
--documentationProduction setup with auth, timeout and headers
npx @metaengine/protobuf-fetch service.proto ./src/api \
--bearer-auth API_TOKEN \
--custom-header X-Tenant-ID=TENANT_ID \
--timeout 30 \
--error-handlingCLI options
Option
Description
--base-url-env <name>
Environment variable name for base URL [default: API_BASE_URL]
--service-suffix <suffix>
Service naming suffix [default: Service]
--options-threshold <n>
Parameter count to use options object [default: 4]
--documentation
Generate JSDoc comments
--import-meta-env
Use import.meta.env for env access (Vite, SvelteKit, Next.js 15+)
--result-pattern
Return ApiResult<T> instead of T for structured error handling
--middleware
Emit onRequest / onResponse / onError hooks in the generated client
--error-handling
Enable gRPC-native error handling keyed on gRPC status codes
--bearer-auth <env-var-name>
Bearer token from env var (adds Authorization header to all requests)
--basic-auth <userEnv:passEnv>
Basic auth from env vars. Format: <username-env>:<password-env> (e.g. API_USER:API_PASS)
--timeout <seconds>
Request timeout in seconds for all operations
--custom-header <header=envVarName>
Static header from env var. Repeatable (e.g. X-Tenant-ID=TENANT_ID)
--date-transformation
Convert google.protobuf.Timestamp fields in responses to JavaScript Date objects
--types-barrel
Emit an index.ts barrel per folder plus a root index.ts re-exporting everything
--type-mapping <mapping>
Override TS type for a protobuf type. Repeatable. Targets: string · number · Date · bigint · boolean (e.g. google.protobuf.Timestamp=string)
--clean
Clean output directory (remove files not in generation)
--verbose
Enable verbose logging
--help, -h
Show help message
Options reference
Every knob, documented
Every option is available on the C# fluent API as a method, and most are also exposed as CLI flags. Cross-cutting auth, headers, retries and timeouts apply across frameworks.
Fetch Options
5WithBaseUrlEnvVar(string)Environment variable name for base URL (default: "API_BASE_URL"). Enables getDefaultClient() lazy singleton.WithImportMetaEnv()Use import.meta.env instead of process.env (Vite, SvelteKit, Next.js 15+)WithResultPattern()Return ApiResult<T> for structured error handling without exceptionsWithMiddleware()Emit onRequest / onResponse / onError hooks in the generated clientWithResponseDateTransformation()Convert google.protobuf.Timestamp fields in responses to Date objects
Auth · Headers · Resilience
9WithErrorHandling()Enable gRPC-native error handling keyed on gRPC status codes (NOT_FOUND / PERMISSION_DENIED → null · INVALID_ARGUMENT / FAILED_PRECONDITION → error body · UNAUTHENTICATED / INTERNAL / UNAVAILABLE → throw)WithErrorHandling(grpc => grpc...)Tune the gRPC taxonomy: ReturnNullForCodes(...) · ReturnErrorForCodes(...) · ThrowForCodes(...) · MapGrpcCode(code, behavior). Overrides merge on top of the defaultsWithBearerAuth()Bearer token from env var (default API_TOKEN) — adds Authorization headerWithBearerAuth(string)Bearer token from a specific env var nameWithBearerAuth(string, string)Bearer token with custom header nameWithBasicAuth(string, string)HTTP Basic auth from username + password env varsWithCustomHeader(string, string)Static header from env var. Repeatable (e.g. X-Tenant-ID ← TENANT_ID)WithTimeout(double)Request timeout in seconds, emitted as a grpc-timeout header on every callWithTimeout(double?, double?, double?, double?)Granular timeout: connect · read · write · pool
Protobuf Options
5WithDocumentation()Generate JSDoc comments from .proto field and service descriptionsWithTypeFilter(Func)Filter extracted proto types before registration — only types returning true are generatedWithTypeMapping(string, Type)Override the TS mapping for a protobuf well-known type by fully-qualified name (e.g. google.protobuf.Timestamp → string)WithMethodNames(Func)Custom method naming ruleWithOptionsObjectThreshold(int)Parameter count for options object (default: 4)
TypeScript Output
1WithTypesBarrel()Emit an index.ts barrel per folder plus a root index.ts re-exporting everything
Naming Transformations
3Types(Func)Transform type namesPaths(Func)Transform output pathsFileNames(Func)Transform file names
File Management
5CleanDestination()Clean output directory before generationAlwaysOverwrite()Always overwrite existing filesOnlyWhenModelChanged()Update only when model changesOnlyWhenNew()Write only new files, preserve existingCleanDirectories(...)Clean specific subdirectories
Diagnostics
2Verbose()Enable verbose loggingWithLogger(Action<string>)Route generation log output to a custom sink
Features
Why this package is different
Tree-shakeable
One file per tag (or per operation) means bundlers drop unused clients at build time. No runtime dispatch table.
Strict, no any
Output compiles under strict with no any. Unions, nullability and enums are modelled precisely so tsc catches drift.
Zero runtime
The default emit is types plus a thin client — no decorators, no reflection, no dependency surface beyond your HTTP lib.
Deterministic output
Same spec + same options produce byte-identical files. Safe to commit, safe to diff in review, safe to cache in CI.
Flexible naming
Case conventions are configurable per role — types, properties, operations, enums. Idiomatic in the target by default.
Semver-honest
Spec diff drives the version bump. Additive changes = minor, removed operations = major. Never a surprise in your lockfile.