Skip to content

OpenAPI Documentation

Muhammad Firdaus Sati edited this page May 9, 2024 · 3 revisions

🔥 Blaze

An event driven framework for 🔥 Hono.js

Table of Contents

Getting Started

Blaze by design support auto validation on every action call/REST API call. From version 3.0.0, you need to install @asteasolutions/zod-to-openapi (npm i @asteasolutions/zod-to-openapi) to make the auto doc OpenAPI Spec works.

How To Validate Action/REST API Call Automatically?

To create validator for your Action/REST API Call, you can create by using BlazeCreator.action.validator/BlazeCreator.event.validator functions or use the satisfies ActionValidator keyword.

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,
  async handler(ctx) {
    // Do something about user request
  }
});

export default createUser;

How To Create OpenAPI Spec Of The REST API?

To create an OpenAPI Spec for your REST API, you can create the openapi spec information by using BlazeCreator.action.openapi function or satisfies ActionOpenAPI keyword.

import { z, BlazeCreator } from '@busy-hour/blaze';

const userOpenApi = BlazeCreator.action.openapi({
  body: {
    type: 'application/json',
    description: 'Create a new user',
    required: true,
  },
  responses: {
    200: {
      description: 'User created',
    },
    400: {
      description: 'Bad Request',
    },
  },
});

const createUserValidator = BlazeCreator.action.validator({
  body: z.object({
     // your validation in here
  }),
});

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

export default createUser;