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 template and for an app with multiple segments, the structure of the generated client looks like this:
- index.ts (imports
./schema.ts
) - schema.ts (imports
.vovk-schema/root.json)
- index.ts (imports
- index.ts (imports
./schema.ts
) - schema.ts (imports
.vovk-schema/admin.json)
- index.ts (imports
- 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
)
- index.ts (imports
- index.ts (imports
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.
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 at this case it has 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:
/** @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',
},
},
},
};
enabled
If set to true
, the segmented client will be generated.
fromTemplates
An array of templates to use for the segmented client generation. By default, it uses ["ts"]
templates that generate TS modules. The array can contain names of built-in templates as well as of custom templates.
outDir
The path where the segmented client will be generated. By default, it uses ./src/client
folder. The path is relative to CWD.
includeSegments
An array of segments to include in the segmented client. By default, it includes all segments. You can use this option to include only specific segments in the segmented client.
excludeSegments
An array of segments to exclude from the segmented client. By default, it excludes no segments. You can use this option to exclude specific segments from the segmented client. This property is mutually exclusive with includeSegments
.
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.