RPC/API Module
RPC module is a code-generated TypeScript object that mirrors back-end controllers and their methods, mapping them to front-end callable functions. While controller methods (or โproceduresโ) accept NextRequest object, the generated RPC module methods accept body, query, and params options, inferred from the back-end method signatures.
In other words, a controller with methids getPet and updatePet will generate an RPC module with methods getPet and updatePet, which can be called directly from the front-end code.
src/modules/pet/PetController.ts
// VovkRequest is an enhanced NextRequest with input type definition
import { get, put, type VovkRequest } from 'vovk';
export default class PetController {
@get('{id}')
static getPet(req: VovkRequest, { id }: { id: string }) {
// ...
}
@put('{id}')
static async updatePet(req: VovkRequest<{ petName: string }>, { id }: { id: string }) {
const { petName } = await req.json();
// ...
}
}The above controller will generate the following RPC module:
src/page.tsx
import { PetRPC } from 'vovk-client';
const pet = await PetRPC.getPet({ params: { id: '123' } });
const updatedPet = await PetRPC.updatePet({
params: { id: '123' },
body: { petName: 'Fluffy' },
});It provides type-safe, contract-less RPC over RESTful API with TypeScript type inference and mapping between the back-end and front-end.
TODO: XxxRPC.withDefaults
Last updated on