Skip to content

v7 Preview 🎉  #1368

@drwpow

Description

@drwpow

v7 Preview

openapi-typescript@7 is now in preview! 🎉 The major additions to this library are:

  • ✨ Built-in validation using the Redoc CLI — no more needing an external tool
  • ✨ Respecting redocly.yaml for API config (define your schemas there in one place, and use openapi-typescript to generate TypeScript without having to manage a separate config)
  • ✨ Addition of the --enum flag for generating true TypeScript enums instead of unions (default behavior still unions)
  • 📘 A mini-rewrite to use the TypeScript AST instead of string mashing

Full Changelog

Features

  • Feature: automatically validate schemas with Redocly CLI (docs). No more need for external tools to report errors! 🎉

    • By default, it will only throw on actual schema errors (uses Redocly’s default settings)
    • For stricter linting or custom rules, you can create a redocly.yaml config
  • Feature: bundle schemas with Redocly CLI

  • Feature: add enum option to export top-level enums from schemas ([Feature request] Generate Enums AND Union types #941)

  • Feature: add formatOptions to allow formatting TS output

  • Feature: header responses add [key: string]: unknown index type to allow for additional untyped headers

  • Feature: allow configuration of schemas via apis key in redocly.config.yaml. See docs for more info.

Breaking Changes

  • ⚠️ Breaking: The Node.js API now returns the TypeScript AST for the main method as well as transform() and postTransform(). To migrate, you’ll have to use the typescript compiler API:

    + import ts from "typescript";
    
    + const DATE = ts.factory.createIdentifier("Date");
    + const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());
    
      const ast = await openapiTS(mySchema, {
        transform(schemaObject, metadata) {
        if (schemaObject.format === "date-time") {
    -       return schemaObject.nullable ? "Date | null" : "Date";
    +       return schemaObject.nullable
    +         ? ts.factory.createUnionTypeNode([DATE, NULL])
    +         : DATE;
          }
        },
      };

    Though it’s more verbose, it’s also more powerful, as now you have access to additional properties of the generated code you didn’t before (such as injecting comments).

    For example syntax, search this codebae to see how the TypeScript AST is used.

    Also see AST Explorer’s typescript parser to inspect how TypeScript is interpreted as an AST.

  • ⚠️ Breaking: Changing of several CLI flags and Node.js API options

    • The --auth, --httpHeaders, --httpMethod, and fetch (Node.js-only) options were all removed from the CLI and Node.js API
    • You can also set your fetch client in redocly.yaml as well.
    • --immutable-types has been renamed to --immutable
    • --support-array-length has been renamed to --array-length
  • ⚠️ Breaking: Most optional objects are now always present in types, just typed as :never. This includes keys of the Components Object as well as HTTP methods.

  • ⚠️ Breaking: No more external export in schemas anymore. Everything gets flattened into the components object instead (if referencing a schema object from a remote partial, note it may have had a minor name change to avoid conflict).

  • ⚠️ Breaking defaultNonNullable option now defaults to true. You’ll now need to manually set false to return to old behavior.

  • ⚠️ Breaking: Remove globbing schemas in favor of redocly.yaml config. Specify multiple schemas with outputs in there instead. See Multiple schemas for more info.

And dozens of small performance improvements and quality fixes.

Give it a try today with the @next flag:

npm i openapi-typescript@next

Testers wanted 🙏

Will pin this issue for a while while people kick the tires and report bugs while a release candidate is developed. For many people, it should be a drop-in replacement; for others, it will only require a few minor changes.

If people could report any errors/issues they face while trying this, that’d be much appreciated. Any issues posted here or as separate issues will help patch holes in the docs site.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions