6.7.5 • Published 3 days ago

openapi-typescript v6.7.5

Weekly downloads
10,530
License
MIT
Repository
github
Last release
3 days ago

version(scoped) codecov

All Contributors

📘️ openapi-typescript

🚀 Convert OpenAPI 3.0 and 2.0 (Swagger) schemas to TypeScript interfaces using Node.js.

💅 The output is prettified with Prettier (and can be customized!).

👉 Works for both local and remote resources (filesystem and HTTP).

View examples:

Usage

CLI

🗄️ Reading specs from file system

npx openapi-typescript schema.yaml --output schema.ts

# 🤞 Loading spec from tests/v2/specs/stripe.yaml…
# 🚀 schema.yaml -> schema.ts [250ms]

☁️ Reading specs from remote resource

npx openapi-typescript https://petstore.swagger.io/v2/swagger.json --output petstore.ts

# 🤞 Loading spec from https://petstore.swagger.io/v2/swagger.json…
# 🚀 https://petstore.swagger.io/v2/swagger.json -> petstore.ts [650ms]

Thanks to @psmyrdek for this feature!

Outputting to stdout

npx openapi-typescript schema.yaml

Generating multiple schemas

In your package.json, for each schema you’d like to transform add one generate:specs:[name] npm-script. Then combine them all into one generate:specs script, like so:

"scripts": {
  "generate:specs": "npm run generate:specs:one && npm run generate:specs:two && npm run generate:specs:three",
  "generate:specs:one": "npx openapi-typescript one.yaml -o one.ts",
  "generate:specs:two": "npx openapi-typescript two.yaml -o two.ts",
  "generate:specs:three": "npx openapi-typescript three.yaml -o three.ts"
}

If you use npm-run-all, you can shorten this:

"scripts": {
  "generate:specs": "run-p generate:specs:*",

You can even specify unique options per-spec, if needed. To generate them all together, run:

npm run generate:specs

Rinse and repeat for more specs.

For anything more complicated, or for generating specs dynamically, you can also use the Node API.

CLI Options

OptionAliasDefaultDescription
--output [location]-o(stdout)Where should the output file be saved?
--prettier-config [location](optional) Path to your custom Prettier configuration for output

Node

npm i --save-dev openapi-typescript
const { readFileSync } = require("fs");
const swaggerToTS = require("openapi-typescript");

const input = JSON.parse(readFileSync("spec.json", "utf8")); // Input can be any JS object (OpenAPI format)
const output = swaggerToTS(input); // Outputs TypeScript defs as a string (to be parsed, or written to a file)

The Node API is a bit more flexible: it will only take a JS object as input (OpenAPI format), and return a string of TS definitions. This lets you pull from any source (a Swagger server, local files, etc.), and similarly lets you parse, post-process, and save the output anywhere.

If your specs are in YAML, you’ll have to convert them to JS objects using a library such as js-yaml. If you’re batching large folders of specs, glob may also come in handy.

PropertyMapper

In order to allow more control over how properties are parsed, and to specifically handle x-something-properties, the propertyMapper option may be specified as the optional 2nd parameter.

This is a function that, if specified, is called for each property and allows you to change how openapi-typescript handles parsing of Swagger files.

An example on how to use the x-nullable property to control if a property is optional:

const getNullable = (d: { [key: string]: any }): boolean => {
  const nullable = d["x-nullable"];
  if (typeof nullable === "boolean") {
    return nullable;
  }
  return true;
};

const output = swaggerToTS(swagger, {
  propertyMapper: (swaggerDefinition, property): Property => ({
    ...property,
    optional: getNullable(swaggerDefinition),
  }),
});

Thanks to @atlefren for this feature!

Migrating from v1 to v2

Migrating from v1 to v2

Project Goals

  1. Support converting any OpenAPI 3.0 or 2.0 (Swagger) schema to TypeScript types, no matter how complicated
  2. The generated TypeScript types must match your schema as closely as possible (i.e. don’t convert names to PascalCase or follow any TypeScript-isms; faithfully reproduce your schema as closely as possible, capitalization and all)
  3. This library is a TypeScript generator, not a schema validator.

Contributing

PRs are welcome! Please see our CONTRIBUTING.md guide. Opening an issue beforehand to discuss is encouraged but not required.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

@nudodesigns/noxy-epos-types@nudodesigns/noxy-loyalty-types@nudodesigns/typesswagger-to-service-js@infinitebrahmanuniverse/nolb-openaopenapi-tsclient-schema-codegen@everything-registry/sub-chunk-2382oxpressnitropack-nightlynitropacknitropack-edgeminusquisplaywright.testing.templateokareo-ts-sdknuxtpaginationsopenapi-fetch-typescriptopenapi-generate-configopenapi-requestopenapi-sdk-builderopenapi-v3-request-generatoropenapi-to-serviceopenapi-ts-typegenopenapi-ts-backendopenapi-typescript-heft-pluginnuxt-open-fetchnuxt-openapi-typescriptpileuleuyanteanuxt-api-partypatepangdeui@devix-tecnologia/directus2ts@doyokit/generator@doyokit/utils@galaxyproject/galaxy-client@zalastax/nolb-openaautools@bpinternal/opapi@byyuurin/nitro-openapivviinn-widgetsapi-typingapicastyasdkapikit-builderapikit-utilsapiverseanakketigaanaklanangteaanakwadontea@algea_care/xpertyme-apiclientele-cli@hempflower/nuxt-open-fetch@hebilicious/nitro@hebilicious/unstable-nitrolotus-typescriptmangudinlagirajinschema-table-componentsedanbosoktehmusimhujanswagger-to-apifilesswagger_wrapper_generatorswaggerapi-typescript-apivite-plugin-open-clienttwo-strokevite-plugin-interface-portal@zygarde-projects/api-genabaca@windmill-labs/openapi-codegen-cli@aabelmann/ui-layer@_dotlas/api@0x-jerry/openapi-tsdesign-system-trial-milyasbpa@logion/rest-api-core@giosg/wfb-types@gigwage/client@insertish/oapi@nudodesigns/noxy-admin-types@nuxtus/generator@openapi-contrib/openapi-schema-to-json-schema@openapi-ts/backend@openapi-ts/typegen@openapi-typescript-infra/openapi-typescript-enum@openapi-qraft/pluginfp-swagger-schema-ts@luxuryescapes/router@lunarhq/rosetta-ts-client@liangskyli/openapi-gen-ts@mojaloop/api-snippetsgendtsgeneratetypesfromloops@nlo/nuxt-commongerimismalamsenindirectus-typescript-gendirectus-typescript-generator@immediate_media/openapi-client@kiyoshiro/openapi-typescript-any-client@rigo-m/nitropack@opensdks/cli@rest-api-codegen/vue-queryinterface-portal-plugin@jambff/oac@quinck/fastify-openapi-typescript-generator
7.0.0-next.9

3 days ago

7.0.0-next.10

3 days ago

7.0.0-next.8

2 months ago

6.7.5

1 month ago

5.4.2

2 months ago

7.0.0-next.7

3 months ago

7.0.0-next.6

3 months ago

6.7.4

3 months ago

6.7.3

4 months ago

7.0.0-next.5

5 months ago

7.0.0-next.3

5 months ago

7.0.0-next.4

5 months ago

7.0.0-next.1

7 months ago

7.0.0-next.2

7 months ago

7.0.0-next.0

7 months ago

6.7.0

7 months ago

6.7.2

5 months ago

6.7.1

6 months ago

6.6.2-beta.0

7 months ago

6.6.1

8 months ago

6.6.0

8 months ago

6.6.2

7 months ago

6.5.5

8 months ago

6.5.0

9 months ago

6.2.5

11 months ago

6.2.7

11 months ago

6.2.6

11 months ago

6.2.9

10 months ago

6.2.8

11 months ago

6.3.4

10 months ago

6.3.3

10 months ago

6.3.6

9 months ago

6.3.5

9 months ago

6.3.8

9 months ago

6.3.7

9 months ago

6.3.9

9 months ago

6.3.0

10 months ago

6.3.2

10 months ago

6.3.1

10 months ago

6.4.3

9 months ago

6.4.2

9 months ago

6.4.5

9 months ago

6.4.4

9 months ago

6.4.1

9 months ago

6.4.0

9 months ago

6.5.2

8 months ago

6.5.1

8 months ago

6.5.4

8 months ago

6.5.3

8 months ago

6.2.4

1 year ago

6.2.3

1 year ago

6.2.2

1 year ago

6.1.1

1 year ago

6.2.1

1 year ago

6.2.0

1 year ago

6.1.0

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

6.0.3

1 year ago

6.0.2

1 year ago

6.0.0-pre.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.3.0

2 years ago

5.1.1

2 years ago

5.1.0

2 years ago

5.2.0

2 years ago

5.0.1

2 years ago

5.0.0

2 years ago

4.5.0

2 years ago

4.4.0

3 years ago

4.2.0

3 years ago

4.1.0

3 years ago

4.3.0

3 years ago

4.1.1

3 years ago

4.0.2

3 years ago

3.4.0-pre.1

3 years ago

3.4.0-pre.2

3 years ago

3.4.0-pre.0

3 years ago

3.4.0

3 years ago

3.3.1

3 years ago

3.4.1

3 years ago

4.0.0-pre.0

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.3.0

3 years ago

3.2.4

3 years ago

3.3.0-next.0

3 years ago

3.2.3

3 years ago

3.2.2

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.1.0-rc.0

3 years ago

3.1.0-alpha.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-alpha.3

3 years ago

2.5.0

3 years ago

3.0.0-alpha.2

3 years ago

3.0.0-alpha.1

3 years ago

3.0.0-alpha.0

3 years ago

3.0.0-rc.0

3 years ago

2.4.2

3 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago