Skip to content

OpenAPI Generation

🎯 Generate OpenAPI 3.0 specifications from your Nevr entities automatically.

Why OpenAPI?

OpenAPI (formerly Swagger) provides:

  • API Documentation - Interactive documentation for your API
  • Client Generation - Generate SDKs for any language
  • Testing Tools - Use tools like Postman, Insomnia
  • Validation - Validate requests/responses against spec

Quick Start

typescript
import { generateOpenAPI } from "nevr/generator"
import { user, post, order } from "./entities"

const spec = generateOpenAPI([user, post, order], {
  info: {
    title: "My API",
    version: "1.0.0",
    description: "API for my application",
  },
  servers: [
    { url: "http://localhost:3000/api", description: "Development" },
    { url: "https://api.myapp.com", description: "Production" },
  ],
  outPath: "./docs/openapi.json",  // Optional: Write to file
})

Entity Actions in OpenAPI

Entity actions are automatically documented:

typescript
const order = entity("order", {
  total: int,
  status: string.default("pending"),
})
  .actions({
    checkout: action("checkout")
      .post()
      .input({
        paymentMethod: string,
        shippingAddress: string,
      })
      .meta({
        summary: "Process order checkout",
        description: "Validates payment and initiates shipping",
        tags: ["Orders", "Checkout"],
      }),
  })
  .build()

This generates:

yaml
/orders/{id}/checkout:
  post:
    summary: Process order checkout
    description: Validates payment and initiates shipping
    tags:
      - Orders
      - Checkout
    requestBody:
      content:
        application/json:
          schema:
            type: object
            properties:
              paymentMethod:
                type: string
              shippingAddress:
                type: string
            required:
              - paymentMethod
              - shippingAddress

Multi-Entity Operations

Actions with .creates(), .updates(), .deletes() document their operations:

typescript
const user = entity("user", { email: string, name: string })
  .actions({
    signUp: action("sign-up")
      .post()
      .input({
        email: string.email(),
        password: string.min(8),
        name: string.optional(),
      })
      .creates("user")
      .creates("session", ctx => ({ userId: ctx.results.user.id }))
      .returns("user", { omit: ["password"] })
      .meta({
        summary: "Create user account",
        tags: ["Authentication"],
      }),
  })
  .build()

Generated OpenAPI includes operations info:

yaml
/users/sign-up:
  post:
    summary: Create user account
    description: |
      Execute sign-up action
      
      **Operations:**
      • CREATE user (auto-mapped from input)
      • CREATE session (custom data)
      
      **Returns: user (excluding: password)**
    responses:
      "200":
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/User"

Rich Metadata

Use .meta() on actions to enhance OpenAPI:

typescript
action("verify")
  .post()
  .onResource()
  .meta({
    summary: "Verify user email",
    description: "Confirms email using verification token",
    tags: ["Authentication", "Email"],
  })

Plugin Routes in OpenAPI

Plugin routes are also included:

typescript
const spec = generateOpenAPI(entities, {
  info: { title: "API", version: "1.0.0" },
  plugins: [authPlugin],  // Include plugin routes
  basePath: "/api",
})

Configuration Options

OptionTypeDescription
infoOpenAPIInfoAPI title, version, description
serversOpenAPIServer[]Server URLs
basePathstringBase path for all routes (default: /api)
pluginsPlugin[]Plugins whose routes to include
outPathstringWrite spec to file
securitybooleanInclude security schemes (default: true)

CLI Generation

Generate from command line:

bash
# Generate JSON (default)
npx nevr openapi

# Generate YAML
npx nevr openapi --format yaml

# Custom output path
npx nevr openapi -o ./docs/api-spec.json

# With custom title and version
npx nevr openapi --title "My API" --version "2.0.0"

# Full example
npx nevr openapi \
  --format yaml \
  -o ./docs/openapi.yaml \
  --title "E-Commerce API" \
  --base-path "/v1"

CLI Options

OptionDefaultDescription
-o, --output <path>openapi.jsonOutput file path
--format <fmt>jsonFormat: json or yaml
--title <title>Nevr APIAPI title
--version <ver>1.0.0API version
--base-path <path>/apiBase path for routes

See CLI Reference for more details.


Best Practices

  1. Add .meta() to all actions - Makes docs useful
  2. Use descriptive tags - Groups endpoints logically
  3. Include examples - Use .example() on fields
  4. Document errors - List possible error codes

Released under the MIT License.