Getting Started - busy-hour-studio/blaze GitHub Wiki

🔥 Blaze

An event driven framework for 🔥 Hono.js

Table of Contents

How To Add Blaze To My Project?

To gets started with Blaze, you need to add @busy-hour/blaze to your project dependencies. To add it to your project, run the following command:

  • NPM
npm i @busy-hour/blaze
  • Yarn
yarn add @busy-hour/blaze
  • PNPM
pnpm i @busy-hour/blaze
  • Bun
bun add @busy-hour/blaze

How To Create My Own Service?

In Blaze, there are two listeners can be created in your service. The first one is actions and the second one is events. The actions listener can be used to contains all the action that can happen when a user do a REST API call or when there is a communication between services. The events listeners can be used to contains an event that can be trigger when there is an action that happens in the actions listener or when there is another event that are running. Event are meant to be a "fire and forget" type of event and do not return any value.

To create your own service, you can start by importing the Service interface or using the BlazeCreator.service from the @busy-hour/blaze package.

import { BlazeCreator } from "@busy-hour/blaze";

const service = BlazeCreator.service({
  actions: {
    // ...
  },
  events: {
    // ...
  },
});

export default service;

Please keep in mind that inside a service, you also can add an onCreated, onStarted and onStopped listener which will be run when the service is created, started or stopped.

In-case of you want to use custom name, prefixes, or versioning for your service, you can add the name, prefix, and version property to your service definition.

import { BlazeCreator } from "@busy-hour/blaze";

const service = BlazeCreator.service({
  version: 2, // will add v2 at the start of the name of the service, `v2.` for BlazeContext call and `v2/` for REST API
  prefix: 'api', // will add api at the start of the name of the service, `api.` for BlazeContext call and `api/` for REST API
  name: 'users', // will add users at the end of the name of the service
  actions: {
    // ...
  },
  events: {
    // ...
  },
});

export default service;

From the example above, you just create service for users service with version 2 and prefix api. When you make an action call, the name of the action will be v2.api.users.some-action. When the user make a REST API call, the URL will be /v2/api/users/some-action.

How To Create My Own Action?

To create your own action, you can start by adding a function to the actions property of your service definition or you can create an action outside of the service definition.

import { BlazeCreator } from "@busy-hour/blaze";

const service = BlazeCreator.service({
  actions: {
    // Create user Action
    create: {
      REST: 'POST /',
      async handler(ctx) {
        // Do something about user request
      }
    }
  },
  events: {
    // ...
  },
});

export default service;
// user.create.ts

import { BlazeCreator } from "@busy-hour/blaze";

const createUser = BlazeCreator.action({
  rest: 'POST /',
  async handler(ctx) {
    // Do something about user request
  }
});

export default createUser;

// user.ts
import { BlazeCreator } from "@busy-hour/blaze";
import createUser from './user.create'

const service = BlazeCreator.service({
  actions: {
    // Create user Action
    create: createUser
  },
  events: {
    // ...
  },
});

export default service;

Read more:

How To Create My Own Event?

To create your own event, you can start by adding a function to the events property of your service definition or you can create an event outside of the service definition. The implementation of the event will be the same as the action function except the type that you need to import is Event instead of Action.

// user.log.event.ts

import { BlazeCreator } from "@busy-hour/blaze";

const logUserCreation = BlazeCreator.event({
  async handler(ctx) {
    // Do something about user request
  }
});

// user.ts
import { BlazeCreator } from "@busy-hour/blaze";
import logUserCreation from './user.log.event'

const service = BlazeCreator.service({
  actions: {
    // ...
  },
  events: {
    logUserCreation
  },
});

export default service;

Read more:

How To Start My Backend Server?

To start your own backend server, you can start it by importing Blaze from @busy-hour/blaze.

import { Blaze } from '@busy-hour/blaze'

After you import Blaze, you can continue by creating the Blaze instance.

const app = new Blaze()

After Blaze instance are created, now you can load your services by using load function and pass the path of the your services in the path property.

app.load({
  path: path.resolve('./services')
})

You can also pass autoStart: true when loading your services so it will trigger the onStart service event automatically.

app.load({
  autoStart: true,
  path: path.resolve('./services')
})

To start the backend server, you must install @hono/node-server first if you are using Blaze on Node.js then call the serve function from the Blaze instance.

// If you using Node

// The second param only available on node
app.serve(3000, (info) => {
  console.log(info)
)

// Or if you use Bun

export default app.serve(3000)

In the end, your code to load and start the server should be like this:

import path from 'node:path'
import { Blaze } from '@busy-hour/blaze'

const app = new Blaze({})

app.load({
  autoStart: true,
  path: path.resolve('./services')
})

app.serve(3000, (info) => {
  console.log(info)
)

If you want to load your services immediately on Blaze instance creation, you can pass autoStart and path property to the constructor.

import path from 'node:path'
import { Blaze } from '@busy-hour/blaze'

const app = new Blaze({
  autoStart: true,
  path: path.resolve('./services')
})

app.serve(3000, (info) => {
  console.log(info)
)

How To Disable CORS?

To disable CORS in your projects, you can start by importing cors from @busy-hour/blaze/cors and add it to your app middlewares properties or your services/actions middlewares properties.

import { Blaze } from '@busy-hour/blaze'
import { cors } from '@busy-hour/cors'

const app = new Blaze({
  middlewares: [['ALL', cors()]],
  path: ...,
})

Read more:

⚠️ **GitHub.com Fallback** ⚠️