-
-
Notifications
You must be signed in to change notification settings - Fork 0
Trpc Support
- Getting Started
- How To Convert Action/REST API to TRPC Automatically?
- How To Start The TRPC Server?
- How To Get a Type Safe TRPC?
Starting from version 4.0.0 and above, Blaze
allow any actions to be converted into a TRPC procedure automatically. To start using trpc
in your Blaze
server, you need to install @trpc/server
(npm i @trpc/server
) to allow the conversions to works.
To convert your services actions into a TRPC procedure, you can start by adding the trpc
properties into your actions and defined it as a mutation
or a query
.
import { z, BlazeCreator } from '@busy-hour/blaze';
const createUserValidator = BlazeCreator.action.validator({
body: z.object({
// your validation in here
}),
});
const createUser = BlazeCreator.action({
rest: 'POST /',
validator: createUserValidator,
trpc: 'mutation', // Defining these actions as a mutation
async handler(ctx) {
// Do something about user request
}
});
export default createUser;
To start the trpc server with Blaze
you start it by calling the trpc
functions from the Blaze
instance and pass all the necessary arguments.
const app = new Blaze();
// By awaiting all the service to be loaded, we can make sure all
// of our service actions that we need to convert to TRPC procedure are ready
await app.load({
path: servicePath,
autoStart: true,
middlewares: [['ALL', cors()]],
});
app.trpc('/trpc/*', {
middlewares: [cors()], // Pass the cors middleware in here to prevent the CORS error
});
For more details how to set it up, please refer it on our examples.
To allow a full Type Safe support on the TRPC actions/procedures, please install @busy-hour/blaze-types
with at least version 0.2.0 (npm install @busy-hour/blaze-types@latest
) and please refer on how to use it on our examples.