Skip to Content
Segmented Client

Segmented RPC Client

Composed RPC Client is suitable for single-page applications but in larger applications, exposing schema for all segments in a single RPC client isn’t always desirable. In this case, with a simple configuration change, you can instruct Vovk.ts to generate separate RPC clients for each segment. This approach is called β€œSegmented RPC Client” and it allows to split the client into smaller TypeScript files that can be imported separately, hiding RPC modules and corresponding schema on specific pages. For example, pages responsible for β€œcustomer” functionality aren’t going to import β€œadmin” RPC modules, therefore β€œadmin” implementation details are hidden from the customer pages.

By default, the segmented client is generated into the ./src/client folder (or ./client folder if you don’t use src folder) from ts and schemaTs templates and for an app with multiple segments, the structure of the generated client may look like this:

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

When you import an RPC module from one of the .ts files, the import tree will contain only the schema for that segment and the RPC modules that are used in that segment. For example, if you import UserRPC from the customer segment, the import tree will contain only the schema for the customer segment located at .vovk-schema/customer.json and the RPC modules that are used in that segment. .vovk-schema/admin.json and other JSON files for other segments won’t be included in the import tree.

src/client/customer/index.ts
import { UserRPC } from '@/client/customer'; // import three is going to contain customer.json await UserRPC.getUser({ id: '123' });

The segmented client is also aplicable for Rust and Python templates, but it has an unknown use.

The settings are similar to the settings for the composed client, the only difference is readme and package options are turned into records readmes and packages that allow to specify different options for each segment.

The segmented client can be configured with the following options:

vovk.config.mjs
/** @type {import('vovk').VovkConfig} */ const config = { segmentedClient: { enabled: true, // default fromTemplates: ['ts'], // default outDir: './src/client', // default includeSegments: ['foo'], // exclusive with `excludeSegments` excludeSegments: ['bar'], // exclusive with `includeSegments` packages: { foo: { version: '1.0.0', description: 'My library', }, }, readmes: { foo: { banner: 'My library banner', }, }, }, };

packages

Extends package properties for each segment separately. This allows you to specify different package properties for each segment.

readmes

Extends readme properties for each segment separately. This allows you to specify different readme properties for each segment.

See other options in the Composed Client section.

Last updated on