nori

Package Version Hex Docs

OpenAPI 3.0 / 3.1 code generation for Gleam. nori parses a YAML or JSON spec into a typed Document and validates it. Anything it can't handle comes back as a typed issue. Generators (the built-in ones and your own) work from a stable CodegenIR.

Built-in generators:

YAML parsing uses taffy.

Install

gleam add nori

Quick start

gleam run -m nori/cli -- init        # scaffold config + starter spec
# edit openapi.yaml
gleam run -m nori/cli -- generate    # write generated files

CLI

gleam run -m nori/cli -- init                            # scaffold
gleam run -m nori/cli -- generate                        # generate from config
gleam run -m nori/cli -- generate --spec=./api.yaml      # override spec
gleam run -m nori/cli -- generate --allow-unsupported    # skip capability gate
gleam run -m nori/cli -- generate --dry-run              # list files, write nothing
gleam run -m nori/cli -- generate --no-format            # skip `gleam format` on the output
gleam run -m nori/cli -- generate --quiet                # errors only (--verbose for per-file)
cat api.yaml | gleam run -m nori/cli -- generate --spec=-  # read spec from stdin
gleam run -m nori/cli -- bundle spec.yaml                # bundle multi-file spec
gleam run -m nori/cli -- validate spec.yaml              # structural + capability check
gleam run -m nori/cli -- validate spec.yaml --format=json  # machine-readable diagnostics

Every command exits non-zero on error, so they work in CI. generate runs the gleam format found on PATH over the Gleam files it writes, so the output passes gleam format --check with the same compiler your CI uses; without gleam on PATH it prints a note and leaves them unformatted. --spec=- (and validate -) read the spec from stdin.

generate aborts by default when the spec uses features nori can't generate correctly (discriminator without $ref members, callbacks, multipart/form-data, deepObject params, etc.). Pass --allow-unsupported to generate anyway, with degraded output.

Config

Only the Gleam target runs by default. A TypeScript target runs when the config names it: writing a block for it turns it on, and enabled: false turns it back off.

# nori.config.yaml
spec: ./openapi.yaml

output:
  gleam:
    dir: ./src/generated
    generated_suffix: false       # types.gleam (not types.generated.gleam)

  typescript:                     # listed, so it runs
    dir: ./src/api
    generated_suffix: true        # types.generated.ts
    use_interfaces: true
    use_exports: true

  react_query:
    dir: ./src/api

  swr:
    enabled: false                # explicitly off (it is off by default anyway)

Splitting the Gleam output across projects

The generated Gleam modules do not all belong to the same place: types is shared, routes, middleware and mcp are the backend's, client is the frontend's. dirs overrides the directory per file, and types_module is the module path the others import types by when it lands in another project.

output:
  gleam:
    dir: ./shared/src/generated          # default for anything not listed
    dirs:
      routes: ./backend/src/generated
      middleware: ./backend/src/generated
      client: ./frontend/src/generated
    types_module: shared/generated       # what routes/client import types by

Opt-in type shapes

Two options change the shape of the generated types, so they are off by default:

output:
  gleam:
    inline_enums: true    # an inline `enum` on a property gets a named type
    single_option: true   # optional and nullable is Option(T), not Option(Option(T))

See nori.config.example.yaml for every option.

MCP tools

Mark an operation with x-mcp to expose it as an MCP tool as well:

/threads/{threadId}/messages:
  post:
    operationId: sendMessage
    x-mcp:
      tool: send_message       # defaults to the snake_case operationId
      scopes: [messages:write] # what the caller's token needs
      confirm: true            # has side effects: run it in two steps

mcp.gleam then has tools() (name, title, description, inputSchema, outputSchema, scopes, confirm), tool_to_json for a tools/list entry, and decode_call(name, arguments), which returns a ToolCall such as SendMessage(thread_id: "t1", body: types.SendMessageRequest(..)). Arguments are path and query parameters by their spec names plus the request body under body (a parameter whose name is already taken, such as a query path next to a {path} segment, gets its location appended: path_query); query parameters decode into the same routes.*Query record the HTTP reader returns, with the same constraint checks. Serving /mcp, checking scopes and honouring confirm stay in your application. Only a response that is a record gets an outputSchema, since MCP's structured content is an object; header and cookie parameters are not part of a tool.

What it generates

Gleam (server-side):

Query parameters are typed in both directions. For GET /issues?status=&limit=:

case routes.list_issues_query(wisp.get_query(req)) {
  Ok(q) -> // q.status is Option(IssueStatus), q.limit is Option(Int)
  Error(routes.MissingQueryParam(name)) -> // a client omitted something
  Error(routes.InvalidQueryParam(name, expected)) -> // ...or sent nonsense
}

A repeated key (?tag=a&tag=b) reads as a List, following OpenAPI's default style: form, explode: true. A comma-joined value is never split: ?q=a,b is one value containing a comma.

TypeScript (client-side):

Library API

You can skip the CLI and call nori directly, to parse and inspect a spec or to run your own generator on CodegenIR:

import gleam/int
import gleam/io
import gleam/list
import nori
import nori/capability

pub fn main() {
  let assert Ok(doc) = nori.parse_file("./openapi.yaml")

  case nori.check_capabilities(doc) {
    Ok(_) -> Nil
    Error(issues) ->
      list.each(issues, fn(i) { io.println(capability.issue_to_string(i)) })
  }

  let codegen_ir = nori.build_ir(doc)
  io.println("Endpoints: " <> int.to_string(list.length(codegen_ir.endpoints)))
}

Usage with Wisp

import gleam/json
import wisp.{type Request, type Response}
import generated/routes
import generated/types

pub fn handle_request(req: Request) -> Response {
  let segments = wisp.path_segments(req)
  case routes.match_route(req.method, segments) {
    routes.ListTodos -> {
      let items = get_todos_from_db()
      let body = json.array(items, types.encode_todo)
      json_response(body, 200)
    }
    routes.GetTodo(_id) -> todo
    routes.NotFound -> wisp.not_found()
  }
}

Complete example: examples/wisp_app/.

Custom templates

TypeScript generation runs through handles templates. nori init drops editable .hbs files in templates/:

templates/typescript_types.hbs
templates/typescript_client.hbs
templates/typescript_react_query.hbs
templates/typescript_swr.hbs

Edit them and re-run generate. If the files are missing, nori uses its built-in copies.

Extending nori

nori/codegen/ir.CodegenIR is the public contract. To add a new target (a language, framework or tool), write a separate package that reads it:

import nori/codegen/ir

pub fn generate(ir: ir.CodegenIR) -> String {
  // walk ir.types, ir.endpoints, ir.security_schemes, …
  // produce your own code.
}

Planned add-on packages: nori_oauth (OAuth2 / OIDC), nori_multipart (multipart bodies).

Limitations

The capability check catches these, and generation stops unless you pass --allow-unsupported:

Validation keywords on a property or a query parameter are checked by the generated Gleam record decoders and query readers: minimum, maximum, exclusiveMinimum, exclusiveMaximum, minLength, maxLength (in code points), minItems, maxItems, and the same on an array's inline items. Not checked: pattern, multipleOf, uniqueItems, constraints on a $ref'd schema, and path parameters.

Tracked roadmap:

Examples

examples/regenerate.sh rewrites all three with the current generator; CI fails when the committed output is stale.

Development

gleam test    # run the tests
gleam check   # type check
gleam format src test

License

Apache-2.0. See LICENSE.