Skip to content

Trpc Support

Muhammad Firdaus Sati edited this page Sep 22, 2024 · 3 revisions

🔥 Blaze

An event driven framework for 🔥 Hono.js

Table of Contents

Getting Started

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.

How To Convert Action/REST API to TRPC Automatically?

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;

How To Start The TRPC Server?

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.

How To Get a Type Safe TRPC?

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.

Clone this wiki locally