Skip to content
New Message applications, with @chojs/message →

Modular applications, on every runtime

A tiny decorator-based framework for HTTP, command-line and message applications. Standard decorators, no reflection, the same code on Deno, Node.js, Bun and the edge.

$ deno add jsr:@chojs/web
The CHO butterfly

One codebase runs on

  • Deno
  • Node.js
  • Bun
  • Workers
01 · One shape

Learn it once. Build three kinds of application.

Modules group providers and controllers, and decorators say how they fit together. An HTTP service, a command-line tool and a message consumer are the same three things, with a route, a command or a topic on the method.

  • HTTP with @chojs/web: routes, typed arguments, validation, server-sent events.
  • Command line with @chojs/command: declared flags, generated help, exit codes.
  • Messages with @chojs/message: one handler for a topic, acknowledged after it returns.
How CHO is organized →
ts
import {
  ChoWeb,
  type ChoWebContext,
  Controller,
  Get,
  Injectable,
  Module,
} from "@chojs/web";

@Injectable()
class UserStore {
  find(id: string) {
    return { id, name: "Ada" };
  }
}

@Controller({ route: "users", dependencies: [UserStore] })
class UsersController {
  constructor(readonly store: UserStore) {}

  @Get(":id") // GET /users/42
  get(ctx: ChoWebContext) {
    return this.store.find(ctx.params.id!); // sent as JSON
  }
}

@Module({ providers: [UserStore], controllers: [UsersController] })
class App {}

const app = await ChoWeb.create(App);
ts
import {
  Action,
  ChoCommand,
  type ChoCommandContext,
  Controller,
  Help,
  Module,
} from "@chojs/command";

@Controller()
class Greeter {
  @Help("Say hello")
  @Action("hello", { string: ["name"] })
  hello(ctx: ChoCommandContext) {
    console.log(`Hello, ${ctx.args.name ?? "world"}!`);
  }
}

@Module({ controllers: [Greeter] })
class App {}

const app = ChoCommand.create(App, { name: "greet" });
const code = await app.run(Deno.args); // the exit code
ts
import {
  ChoMessage,
  type ChoMessageContext,
  Controller,
  Module,
  Subscribe,
} from "@chojs/message";

@Controller({ topic: "orders" })
class OrdersController {
  @Subscribe("created") // the topic orders.created
  created(ctx: ChoMessageContext) {
    console.log(ctx.key, ctx.json());
    // returning without an error: the message is handled
  }
}

@Module({ controllers: [OrdersController] })
class App {}

const app = await ChoMessage.create(App);
await app.handle("orders.created", { key: "42", value: '{"id":"42"}' });
02 · Portable

Your code never meets the HTTP library.

A handler receives the standard Request and answers with a standard Response. What matches the requests underneath is a small adapter you can swap, and the runtime is a line you do not write.

  • A router is built in, on the standard URLPattern.
  • Hono or Fastify with one option, and no change to a controller.
  • serve(app.handler) picks the server of the runtime it runs on.
Runtimes and adapters →
YoursModules, controllers, providers(req: Request) => Response
The framework@chojs/web
An adapter, swapped with one option
Built-in routerHonoFastify
A runtime
DenoNode.jsBunWorkers
03 · Standard decorators

No reflection. No compiler plugin. Still type-checked.

CHO runs with TC39 decorators, and with the legacy ones of TypeScript and Bun. Dependencies are declared, not guessed from emitted types, and the compiler checks them against the constructor.

  • No reflect-metadata, no emitDecoratorMetadata.
  • The same application with both forms of decorators.
  • A wrong dependency is a compile error, not a surprise at startup.
Decorators →
ts
const PORT = token<number>("PORT");

@Injectable([Logger, PORT]) // in constructor order
class Server {
  constructor(logger: Logger, port: number) {}
}

@Injectable([PORT, Logger]) 
class Broken {
  constructor(logger: Logger, port: number) {}
}

Compile error: the constructor of Broken wants (Logger, number).

04 · Dependency injection

It fails before it starts, or it does not fail.

The whole module graph is validated and linked once, when the application is created. A missing provider, a circular import or a duplicate route never waits for the first request.

  • A provider can await a connection before its value is ready.
  • Everything that depends on it waits, with no onInit hook to write.
  • close() releases the instances in reverse order.
Dependency injection →
deno run main.ts
$ deno run --allow-net main.ts
Error: Cannot resolve Database (argument #0) of UserStore
in module UsersModule. Provide it there or import a module
that exports it.
# fixed, and run again
$ deno run --allow-net main.ts
Error: Duplicate route "GET /users/:id": declared by
UsersController.get and AdminController.find
# fixed
$ deno run --allow-net main.ts
listening on http://localhost:3000
createConfig→Database→UserStore→UsersController
closeUsersController→UserStore→Database→Config

Start with one package.

It brings everything an application needs. A first HTTP application takes one file, and runs the same on every runtime.

sh
deno add jsr:@chojs/web
sh
npx jsr add @chojs/web
sh
bunx jsr add @chojs/web

Released under the MIT License.