OpenAPI

TableCraft can automatically generate OpenAPI 3.0 specifications from your table configurations.

How It Works

1

Generate Spec from Config

import { generateOpenApiSpec } from '@tablecraft/engine';
import { orders } from './tablecraft.config';

const spec = generateOpenApiSpec(orders.toConfig());

console.log(JSON.stringify(spec, null, 2));
2

Serve as JSON Endpoint

Create an endpoint that serves the spec.

Next.js Example:

// app/api/docs/[table]/route.ts
import { generateOpenApiSpec } from '@tablecraft/engine';
import * as configs from '@/tablecraft.config';

export async function GET(
  request: Request,
  { params }: { params: { table: string } }
) {
  const config = configs[params.table];
  
  if (!config) {
    return Response.json({ error: 'Table not found' }, { status: 404 });
  }

  const spec = generateOpenApiSpec(config.toConfig());
  return Response.json(spec);
}

Access: GET /api/docs/orders

3

Combine Multiple Tables

Generate a unified spec for all your tables.

function generateFullSpec() {
  const baseSpec = {
    openapi: '3.0.3',
    info: { title: 'My API', version: '1.0.0' },
    paths: {},
  };

  // Merge all table specs
  for (const [name, config] of Object.entries(configs)) {
    const tableSpec = generateOpenApiSpec(config.toConfig());
    Object.assign(baseSpec.paths, tableSpec.paths);
  }

  return baseSpec;
}

Generated Spec Structure

Example Input

const orders = defineTable(ordersTable)
  .search('status', 'customerName')
  .options('status', [
    { value: 'pending', label: 'Pending' },
    { value: 'completed', label: 'Completed' },
  ])
  .aggregate('totalRevenue', 'sum', 'total')
  .exportable('csv', 'json')
  .access({ roles: ['admin', 'sales'] });

Generated Output (Snippet)

Use Cases

Serve the spec and mount Swagger UI:

What Gets Included

✅ Automatically Generated

  • Paths: One per table (/api/{tableName})

  • Query Parameters: Pagination, Sorting, Filtering, Search, Export

  • Response Schema: data[], meta, aggregations

  • Security: Bearer auth (if access is configured)

❌ Not Included

  • POST/PUT/DELETE: Only GET endpoints (TableCraft is read-only)

  • Nested includes: Doesn't document nested relations

  • Raw SQL columns: Included but type may be generic

Last updated

Was this helpful?