Skip to Content
hono-bento

Add a feature

Create a new feature folder and register it with one line in specs.ts.

This guide adds a projects feature with list and create routes.

Or generate it

hono-bento add feature writes the steps below for you, wherever your hono-bento.config.ts puts things:

bunx hono-bento add feature projects --routes list,create

It writes the schemas, the folder (spec.ts, one handler per route, store.ts, a test file with an it.todo per route) and the line in specs.ts. list answers with an empty list from store.list. The other handlers return 501 until you write them. Then fill in the TODOs and grant the scopes it prints. The CLI has its options.

Create the schema module

packages/schemas/src/projects.ts
import { z } from 'hono-bento/zod';

export const projectSchema = z
  .object({
    object: z.literal('project'),
    id: z.string(),
    name: z.string(),
  })
  .openapi('Project');

export const projectListSchema = z
  .object({ object: z.literal('list'), data: z.array(projectSchema) })
  .openapi('ProjectList');

export const projectCreateSchema = z.object({ name: z.string().min(1) });

export type Project = z.infer<typeof projectSchema>;
export type ProjectCreate = z.infer<typeof projectCreateSchema>;

The package's wildcard export makes it importable as @todos/schemas/projects right away.

Create the feature folder

spec.ts
list.ts
create.ts
store.ts
projects.test.ts

spec.ts declares each route's contract, then joins each to its handler:

features/projects/spec.ts
import { projectCreateSchema, projectListSchema, projectSchema } from '@todos/schemas/projects';
import { contract, defineSpec, route } from 'hono-bento';

import { createProject } from './create';
import { listProjects } from './list';

export const contracts = {
  list: contract({
    operation: 'list',
    method: 'get',
    path: '/',
    summary: 'List projects',
    auth: { scopes: ['projects.read'] },
    responses: { 200: { description: 'A list of projects.', schema: projectListSchema } },
  }),
  create: contract({
    operation: 'create',
    method: 'post',
    path: '/',
    summary: 'Create a project',
    auth: { scopes: ['projects.write'] },
    validation: { body: projectCreateSchema },
    responses: { 201: { description: 'The created project.', schema: projectSchema } },
  }),
};

export const spec = defineSpec({
  basePath: '/v1/projects',
  name: 'Projects',
  collection: 'projects',
  model: 'project',
  routes: [route({ ...contracts.list, handler: listProjects }), route({ ...contracts.create, handler: createProject })],
});

Each handler takes its types from its contract:

features/projects/create.ts
import { defineHandler } from 'hono-bento';

import type { Session } from '../../auth';
import type { contracts } from './spec';
import { store } from './store';

export const createProject = defineHandler<Session, typeof contracts.create>(async ({ deps, body, respond }) =>
  respond(201, await store.create(deps.db, body))
);

Give the spec a basePath and a collection that no other feature uses. mountSpecs throws at startup if two routes collide.

Register it

This is the only change outside the folder:

apps/api/src/specs.ts
import { spec as me } from './features/me/spec';
import { spec as projects } from './features/projects/spec'; 
import { spec as todos } from './features/todos/spec';

export const specs = [todos, me]; 
export const specs = [todos, me, projects]; 

Grant the scopes

Add projects.read and projects.write to the roles that should have them in auth.ts.

Test and regenerate

bun run openapi
bun run test   # includes structure.test.ts, which runs hono-bento check on the new folder

On this page