Skip to Content
Segmented Mode

Segmented Client Mode

The Composed Client Mode fits single-page apps. In a larger app, you may not want one client to expose the whole schema. The Segmented Client is a separate RPC client for each segment: smaller TypeScript modules that you import one by one, so a page doesn’t load the RPC modules and schemas of unrelated segments. For example, “customer” pages don’t import the “admin” RPC modules, and admin details stay out of customer code.

By default, the segmented client is generated in the ./src/client folder (or ./client when the app isn’t in src/app) from the ts template. For an app with several segments, it can look like this:

      • index.ts (imports ./schema.ts)
      • schema.ts (imports .vovk-schema/root.json)
      • openapi.json (root segment OpenAPI schema)
      • openapi.ts (imports ./openapi.json)
      • index.ts (imports ./schema.ts)
      • schema.ts (imports .vovk-schema/admin.json)
      • openapi.json (admin segment OpenAPI schema)
      • openapi.ts (imports ./openapi.json)
      • index.ts (imports ./schema.ts)
      • schema.ts (imports .vovk-schema/customer.json)
      • openapi.json (customer segment OpenAPI schema)
      • openapi.ts (imports ./openapi.json)
        • index.ts (imports ./schema.ts)
        • schema.ts (imports .vovk-schema/customer/static.json)
        • openapi.json (customer static sub-segment OpenAPI schema)
        • openapi.ts (imports ./openapi.json)

An RPC module imported from one of these .ts files brings only the schema and the RPC modules of its segment. For example, UserRPC from the customer segment brings .vovk-schema/customer.json, but not .vovk-schema/admin.json or the files of other segments.

import { UserRPC } from '@/client/customer'; // the import tree has customer.json only await UserRPC.getUser({ params: { id: '123' } });

The Rust and Python templates work with the segmented client too, though it’s less common. Each segment folder then holds a whole package, with its Cargo.toml or pyproject.toml. The package is named after the project’s package and the segment: for a project named app, the foo segment’s package is app_foo and the root segment’s is app_root; py_name and rs_name get the same suffix. A package.name, py_name or rs_name set for the segment in outputConfig.segments is used as it is. Both templates write a README.md to each segment folder, so segmented Python and Rust clients need separate output folders: set outDir in the segmentedClient options of a template definition. vovk generate refuses two templates that write the same file.

To turn on the segmented client, set segmentedClient.enabled to true in vovk.config.mjs. To turn off the composed client, set composedClient.enabled to false.

vovk.config.mjs
/** @type {import('vovk').VovkConfig} */ const config = { segmentedClient: { enabled: true, }, composedClient: { enabled: false, }, }; export default config;

The options are the same as the options of the composed client:

vovk.config.mjs
/** @type {import('vovk').VovkConfig} */ const config = { segmentedClient: { enabled: false, // default fromTemplates: ['ts'], // default outDir: './src/client', // default includeSegments: ['foo'], // or excludeSegments: ['bar'], not both }, }; export default config;
Last updated on