Docs
Developer Documentation

Handlers

Keep an extension's business logic in one place, shared by routes and CLI commands.

Handlers hold your business logic and sit between routes/CLI and models. They keep validation and orchestration in one place so a route and a CLI command can share it. Handlers live in src/handlers/ and load from every *.js file in that folder.

src/handlers/Products.js
import { Handlers } from "#kora/extensions";

export class Products extends Handlers {
    constructor(kora) {
        super(kora, { name: "Products" });
    }

    model() {
        return this.kora.extensions.models.get(this.extension, "Products");
    }

    create(name, options = {}) {
        if (!name) this.fail(400, "A product name is required.");
        return this.model().create({ name, ...options });
    }

    listAll() {
        return this.model().list();
    }
}

this.fail(status, message) throws an error carrying an HTTP status code. Thrown from within a route it becomes that status and message in the response, so you can reject bad input in one line.