CLIENT Type extraction
vovk module provides a collection of types that allow to infer the types of the RPC methods.
import { UserRPC, StreamRPC } from 'vovk-client';
import type { VovkBody, VovkQuery, VovkParams, VovkReturnType, VovkYieldType } from 'vovk';
// infer body
type Body = VovkBody<typeof UserRPC.updateUser>;
// infer query
type Query = VovkQuery<typeof UserRPC.updateUser>;
// infer params
type Params = VovkParams<typeof UserRPC.updateUser>;
// infer return type
type Return = VovkReturnType<typeof UserRPC.updateUser>;
// infer yield type from stream methods
type Yield = VovkYieldType<typeof StreamRPC.streamTokens>;
For example, if you want to create a custom function that makes requests to the server, you can borrow types from the RPC client to define the arguments.
src/modules/user/UserState.ts
import { UserRPC } from 'vovk-client';
import type { VovkBody, VovkQuery, VovkParams } from 'vovk';
export function updateUser(
id: VovkParams<typeof UserRPC.updateUser>['id'],
body: VovkBody<typeof UserRPC.updateUser>,
query: VovkQuery<typeof UserRPC.updateUser>
) {
return UserController.updateUser({
params: { id },
body,
query,
});
}
The exported updateUser
can be called the following way:
import { updateUser } from '../modules/user/UserState';
// ...
await updateUser('69', { firstName: 'John', lastName: 'Doe' }, { notify: 'push' });
SERVER Type extraction
vovk module provides a collection of types that allow to infer the types of the controller methods.
src/modules/user/UserController.ts
import type { VovkRequest } from 'vovk';
import UserService from './UserService';
export default class UserController {
@get()
static async updateUser(req: VovkRequest<{ email: string }, { id: string }>, { param }: { param: string }) {
return UserService.updateUser(req.nextUrl.searchParams.get('id'), await req.json());
}
}
import UserController from './UserController';
import type {
VovkControllerBody,
VovkControllerQuery,
VovkControllerParams,
VovkControllerReturnType,
VovkControllerYieldType,
VovkControllerOutput,
VovkControllerIteration,
} from 'vovk';
// infer body
type Body = VovkControllerBody<typeof UserController.updateUser>;
// infer query
type Query = VovkControllerQuery<typeof UserController.updateUser>;
// infer params
type Params = VovkControllerParams<typeof UserController.updateUser>;
// infer return type
type Return = VovkControllerReturnType<typeof UserController.updateUser>;
// infer yield type from stream methods
type Yield = VovkControllerYieldType<typeof StreamController.streamTokens>;
These types are useful at services. If you want to create a custom function that makes requests to the server, you can borrow types from the client to build the arguments.
src/modules/user/UserService.ts
import type { VovkControllerBody, VovkControllerQuery } from 'vovk';
import type UserController from './UserController';
export default class UserService {
static async updateUser(
id: VovkControllerQuery<typeof UserController.updateUser>['id'],
body: VovkControllerBody<typeof UserController.updateUser>
) {
// ...
}
}
Last updated on