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 template and for an app with multiple segments, the structure of the generated client may look like this:
- index.ts (imports
./schema.ts
and./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.ts
and./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.ts
and./openapi.ts
) - schema.ts (imports
.vovk-schema/customer.json
) - openapi.json (customer segment OpenAPI schema)
- openapi.ts (imports
./openapi.json
) - index.ts (imports
./schema.ts
and./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 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 the same to the settings for the composed client.
/** @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`
},
};