Skip to content

CLI and code generation ​

bash
npx @flattype/cli --help        # or: npm install -D @flattype/cli, then use `flattype …`
bash
# Convert, in pipes or files
curl -s https://api.example.com/users | npx @flattype/cli encode
npx @flattype/cli decode users.flat -o users.json

# Compare sizes
npx @flattype/cli stats users.json
#                 bytes      gzip
# JSON           169545     12890
# Flat            93606     12568
# saved             45%        2%

# Generate typed models from an OpenAPI spec (3.0, 3.1 or Swagger 2; JSON or YAML)…
npx @flattype/cli gen --openapi openapi.yaml -o src/models.ts

# …or from a real response
curl -s https://api.example.com/users > users.json && npx @flattype/cli gen --sample users.json --name UsersPage

For an OpenAPI schema like

yaml
Customer:
  type: object
  required: [id, full_name, created_at]
  properties:
    id: { type: integer }
    full_name: { type: string }
    created_at: { type: string, format: date-time }
    address: { $ref: "#/components/schemas/Address" }

flattype gen --openapi writes:

ts
export const Customer = defineModel({
  id: "integer",
  fullName: "string",
  createdAt: "date",
  address: { type: Address, optional: true },
}, { keys: "snake" });
export type Customer = Infer<typeof Customer>;

It handles $ref between schemas, allOf, required (other fields become optional), nullable and type: [..., "null"], date and date-time formats, enums and inline objects. A recursive reference becomes "any", and the CLI warns you. Exit code 1 means bad input, 2 means a usage error. From code, use fromOpenAPI(spec) together with toSource(models).

Released under the MIT License.