7.6.1 • Published 5 months ago

openapi-typescript v7.6.1

Weekly downloads
10,530
License
MIT
Repository
github
Last release
5 months ago

openapi-typescript turns OpenAPI 3.0 & 3.1 schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.

The code is MIT-licensed and free for use.

Tip: New to OpenAPI? Speakeasy’s Intro to OpenAPI is an accessible guide to newcomers that explains the “why” and “how” of OpenAPI.

Features

  • ✅ Supports OpenAPI 3.0 and 3.1 (including advanced features like discriminators)
  • ✅ Generate runtime-free types that outperform old school codegen
  • ✅ Load schemas from YAML or JSON, locally or remotely
  • ✅ Generate types for even huge schemas within milliseconds

Note: OpenAPI 2.x is supported with versions 5.x and previous

Examples

👀 See examples

Setup

This library requires the latest version of Node.js installed (20.x or higher recommended). With that present, run the following in your project:

npm i -D openapi-typescript typescript

And in your tsconfig.json, to load the types properly:

{
  "compilerOptions": {
+    "module": "ESNext", // or "NodeNext"
+    "moduleResolution": "Bundler" // or "NodeNext"
  }
}

Highly recommended

Also adding the following can boost type safety:

{
  "compilerOptions": {
+    "noUncheckedIndexedAccess": true
  }
}

Basic usage

First, generate a local type file by running npx openapi-typescript, first specifying your input schema (JSON or YAML), and where you’d like the --output (-o) to be saved:

# Local schema
npx openapi-typescript ./path/to/my/schema.yaml -o ./path/to/my/schema.d.ts
# 🚀 ./path/to/my/schema.yaml -> ./path/to/my/schema.d.ts [7ms]

# Remote schema
npx openapi-typescript https://myapi.dev/api/v1/openapi.yaml -o ./path/to/my/schema.d.ts
# 🚀 https://myapi.dev/api/v1/openapi.yaml -> ./path/to/my/schema.d.ts [250ms]

Then in your TypeScript project, import types where needed:

import type { paths, components } from "./my-openapi-3-schema"; // generated by openapi-typescript

// Schema Obj
type MyType = components["schemas"]["MyType"];

// Path params
type EndpointParams = paths["/my/endpoint"]["parameters"];

// Response obj
type SuccessResponse =
  paths["/my/endpoint"]["get"]["responses"][200]["content"]["application/json"]["schema"];
type ErrorResponse =
  paths["/my/endpoint"]["get"]["responses"][500]["content"]["application/json"]["schema"];

From here, you can use these types for any of the following (but not limited to):

  • Using an OpenAPI-supported fetch client (like openapi-fetch)
  • Asserting types for other API requestBodies and responses
  • Building core business logic based on API types
  • Validating mock test data is up-to-date with the current schema
  • Packaging API types into any npm packages you publish (such as client SDKs, etc.)

📓 Docs

View Docs

@nudodesigns/noxy-epos-types@nudodesigns/noxy-loyalty-types@nudodesigns/typesswagger-to-service-js@infinitebrahmanuniverse/nolb-openaopenapi-tsclient-schema-codegen@everything-registry/sub-chunk-2382swagger-to-apifilesvviinn-widgetsswagger_wrapper_generatorswaggerapi-typescript-apisxfe-create-apivite-plugin-interface-portalvite-plugin-open-client@doyokit/generator@doyokit/utils@bpinternal/opapiplaywright.testing.template@ourongxing/nitro@ourongxing/nitropackjv-coretehmusimhujantwo-stroke@algea_care/xpertyme-api@backan/builder@altearius/openapi-type-gen@byyuurin/nitro-openapi@0x-jerry/openapi-ts@_dotlas/api@aabelmann/ui-layer@devix-tecnologia/directus2tsyasdkzimic@hebilicious/nitro@hebilicious/unstable-nitro@flagz/api-types@fotonjs/api@hempflower/nuxt-open-fetch@elliots/openapi-ts-backend@galaxyproject/galaxy-client@frontstackdev/cli@gigwage/client@giosg/wfb-types@grafana/openapi-to-k6@insertish/oapi@kurojs/schema@logion/rest-api-core@logstrap/core@iqrf/iqrf-gateway-webapp-client@liangskyli/openapi-gen-ts@lunarhq/rosetta-ts-client@luxuryescapes/router@nlo/nuxt-common@ensi-platform/typescript-openapi-generator@encoura/apollo-rest-utils@mojaloop/api-snippets@jambff/oac@immediate_media/openapi-client@kiyoshiro/openapi-typescript-any-client@ogcio/building-blocks-sdk@quinck/fastify-openapi-typescript-generator@openapi-qraft/openapi-typescript-plugin@openapi-qraft/plugin@openapi-contrib/openapi-schema-to-json-schema@openapi-ts/backend@openapi-ts/typegen@openapi-typescript-infra/openapi-typescript-enum@nosana/cli@nudodesigns/noxy-admin-types@occtoo/destination-client@nuxtus/generator@opensdks/cli@paaragon/generator-microservice@sparklytech/sparkly-cli@sparklytech/sparkly@rest-api-codegen/vue-query@rigo-m/nitropackminusquisopenapi-generate-configopenapi-sdk-builderopenapi-requestopenapi-ts-backendopenapi-ts-typegenopenapi-typescript-heft-pluginopenapi-v3-request-generatoropenapi-ts-defineopenapi-fetch-typescriptoxpressopenapi-to-serviceokareo-ts-sdknuxtpaginationsnuxt-yapinuxt-openapi-typescriptnuxt-open-fetchnuxt-open-fetch-xpatepangdeuiscavenger-hunt-serversedanbosokschema-table-componentschema-to-ts
7.4.4

7 months ago

7.4.3

8 months ago

7.5.2

6 months ago

7.5.1

6 months ago

7.5.0

6 months ago

7.6.1

5 months ago

7.6.0

5 months ago

7.4.2

8 months ago

7.3.1

10 months ago

7.4.1

10 months ago

7.4.0

10 months ago

7.3.3

10 months ago

7.3.2

10 months ago

7.3.0

11 months ago

7.0.0-rc.1

1 year ago

7.0.0

1 year ago

7.0.4

12 months ago

7.0.3

12 months ago

7.0.2

12 months ago

7.0.1

1 year ago

7.1.2

11 months ago

7.1.1

11 months ago

7.1.0

12 months ago

7.2.0

11 months ago

7.0.0-rc.0

1 year ago

6.7.6

1 year ago

7.0.0-next.11

1 year ago

7.0.0-next.9

1 year ago

7.0.0-next.10

1 year ago

7.0.0-next.8

1 year ago

6.7.5

1 year ago

5.4.2

1 year ago

7.0.0-next.7

1 year ago

7.0.0-next.6

1 year ago

6.7.4

1 year ago

6.7.3

2 years ago

7.0.0-next.5

2 years ago

7.0.0-next.3

2 years ago

7.0.0-next.4

2 years ago

7.0.0-next.1

2 years ago

7.0.0-next.2

2 years ago

7.0.0-next.0

2 years ago

6.7.0

2 years ago

6.7.2

2 years ago

6.7.1

2 years ago

6.6.2-beta.0

2 years ago

6.6.1

2 years ago

6.6.0

2 years ago

6.6.2

2 years ago

6.5.5

2 years ago

6.5.0

2 years ago

6.2.5

2 years ago

6.2.7

2 years ago

6.2.6

2 years ago

6.2.9

2 years ago

6.2.8

2 years ago

6.3.4

2 years ago

6.3.3

2 years ago

6.3.6

2 years ago

6.3.5

2 years ago

6.3.8

2 years ago

6.3.7

2 years ago

6.3.9

2 years ago

6.3.0

2 years ago

6.3.2

2 years ago

6.3.1

2 years ago

6.4.3

2 years ago

6.4.2

2 years ago

6.4.5

2 years ago

6.4.4

2 years ago

6.4.1

2 years ago

6.4.0

2 years ago

6.5.2

2 years ago

6.5.1

2 years ago

6.5.4

2 years ago

6.5.3

2 years ago

6.2.4

2 years ago

6.2.3

2 years ago

6.2.2

2 years ago

6.1.1

2 years ago

6.2.1

2 years ago

6.2.0

2 years ago

6.1.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

6.0.0-pre.0

3 years ago

5.4.1

3 years ago

5.4.0

3 years ago

5.3.0

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

5.2.0

3 years ago

5.0.1

3 years ago

5.0.0

4 years ago

4.5.0

4 years ago

4.4.0

4 years ago

4.2.0

4 years ago

4.1.0

4 years ago

4.3.0

4 years ago

4.1.1

4 years ago

4.0.2

4 years ago

3.4.0-pre.1

4 years ago

3.4.0-pre.2

4 years ago

3.4.0-pre.0

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.4.1

4 years ago

4.0.0-pre.0

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.3.0

4 years ago

3.2.4

4 years ago

3.3.0-next.0

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.1.0-rc.0

4 years ago

3.1.0-alpha.0

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-alpha.3

4 years ago

2.5.0

4 years ago

3.0.0-alpha.2

4 years ago

3.0.0-alpha.1

4 years ago

3.0.0-alpha.0

4 years ago

3.0.0-rc.0

5 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago