-
-
Notifications
You must be signed in to change notification settings - Fork 568
Description
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 useopenapi-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
- Any options passed into your redocly.yaml config are respected
-
✨ 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 astransform()
andpostTransform()
. To migrate, you’ll have to use thetypescript
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
, andfetch
(Node.js-only) options were all removed from the CLI and Node.js API- To migrate, you’ll need to create a redocly.yaml config that specifies your auth options in the http setting
- 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
- The
-
⚠️ 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 moreexternal
export in schemas anymore. Everything gets flattened into thecomponents
object instead (if referencing a schema object from a remote partial, note it may have had a minor name change to avoid conflict). -
⚠️ BreakingdefaultNonNullable
option now defaults totrue
. You’ll now need to manually setfalse
to return to old behavior. -
⚠️ Breaking: Remove globbing schemas in favor ofredocly.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.