Segmented RPC Client
The Composed RPC Client works well for single-page applications, but in larger apps exposing the entire schema through a single client may be undesirable. With a small configuration change, you can instruct Vovk.ts to generate separate RPC clients for each segment. This approach—called the Segmented RPC Client—splits the client into smaller per-segment TypeScript modules that can be imported independently, keeping RPC modules and their schemas hidden from unrelated pages. For example, “customer” pages won’t import “admin” RPC modules, keeping admin details out of customer code.
By default, the segmented client is generated in the ./src/client folder (or ./client if you don’t use a src folder) from the ts template. For an app with multiple segments, the generated structure may look like this:
- index.ts (imports
./schema.tsand./openapi.ts) - schema.ts (imports
.vovk-schema/root.json) - openapi.json (root segment OpenAPI schema)
- openapi.ts (imports
./openapi.json)
- index.ts (imports
- index.ts (imports
./schema.tsand./openapi.ts) - schema.ts (imports
.vovk-schema/admin.json) - openapi.json (admin segment OpenAPI schema)
- openapi.ts (imports
./openapi.json)
- index.ts (imports
- index.ts (imports
./schema.tsand./openapi.ts) - schema.ts (imports
.vovk-schema/customer.json) - openapi.json (customer segment OpenAPI schema)
- openapi.ts (imports
./openapi.json) - index.ts (imports
./schema.tsand./openapi.ts) - schema.ts (imports
.vovk-schema/customer/static.json) - openapi.json (customer static sub-segment OpenAPI schema)
- openapi.ts (imports
./openapi.json)
- index.ts (imports
- index.ts (imports
When you import an RPC module from one of the .ts files, the import tree includes only the schema and RPC modules for that segment. For example, importing UserRPC from the customer segment pulls in .vovk-schema/customer.json only; .vovk-schema/admin.json and other segment files are not included.
import { UserRPC } from '@/client/customer'; // import tree will contain customer.json
await UserRPC.getUser({ id: '123' });The segmented client also applies to Rust and Python templates, though use cases are less common.
To enable the segmented client, set segmentedClient.enabled to true in vovk.config.mjs, and optionally disable the composed client by setting composedClient.enabled to false.
/** @type {import('vovk').VovkConfig} */
const config = {
segmentedClient: {
enabled: true,
},
composedClient: {
enabled: false,
},
};
export default config;In this case, the vovk-client package is no longer needed.
The settings are the same as the settings for the composed client.
/** @type {import('vovk').VovkConfig} */
const config = {
segmentedClient: {
enabled: false, // default
fromTemplates: ['ts'], // default
outDir: './src/client', // default
includeSegments: ['foo'], // exclusive with `excludeSegments`
excludeSegments: ['bar'], // exclusive with `includeSegments`
},
};