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
- 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 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:
/** @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.