7.6.1 • Published 9 months ago

openapi-typescript v7.6.1

Weekly downloads
10,530
License
MIT
Repository
github
Last release
9 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@bpinternal/opapiinterface-portal-pluginmangudinlagirajinlotus-typescriptkubernatekoboldcpp@whitedigital-eu/wd-frontend-shared@windmill-labs/openapi-codegen-cli@workleap/create-schemas@xiankq/openapi-typescript-expand@uniform-foundation/openapi-generator-client@uniform-foundation/openapi-generator-server@this-is-not-a-drill/react-core@tomw2w/my-nuxt-layer@triargos/dashboard-node@vrplatform/api@zalastax/nolb-openaanakketigaanaklanangteaanakwadontea@zygarde-projects/api-gen@zimic/httpabaca-cliapi-farmerapiverseapi-typingapicastapikit-utilsapikit-builder@grafana/openapi-to-k6@iqrf/iqrf-gateway-webapp-client@0x-jerry/openapi-ts@aabelmann/ui-layer@_dotlas/api@axxy/nitropack@easecrypt/node-sdk@altearius/openapi-type-gen@algea_care/xpertyme-apiplaywright.testing.template@ourongxing/nitro@ourongxing/nitropackjv-coretehmusimhujantwo-stroke@backan/builder@byyuurin/nitro-openapiyasdkzimic@kurojs/schema@logion/rest-api-core@logstrap/core@liangskyli/openapi-gen-ts@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-tsnitro-gonitro-nightlynitropack-edgenitropackpileuleuyanteacloudflare-realtime-api
7.4.4

11 months ago

7.4.3

11 months ago

7.5.2

10 months ago

7.5.1

10 months ago

7.5.0

10 months ago

7.6.1

9 months ago

7.6.0

9 months ago

7.4.2

12 months ago

7.3.1

1 year ago

7.4.1

1 year ago

7.4.0

1 year ago

7.3.3

1 year ago

7.3.2

1 year ago

7.3.0

1 year ago

7.0.0-rc.1

1 year ago

7.0.0

1 year ago

7.0.4

1 year ago

7.0.3

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

7.1.2

1 year ago

7.1.1

1 year ago

7.1.0

1 year ago

7.2.0

1 year 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

2 years ago

6.7.5

2 years ago

5.4.2

2 years ago

7.0.0-next.7

2 years ago

7.0.0-next.6

2 years ago

6.7.4

2 years 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

3 years ago

6.2.1

3 years ago

6.2.0

3 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

4 years ago

5.1.0

4 years ago

5.2.0

4 years ago

5.0.1

4 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

5 years ago

3.2.3

5 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.1.0-rc.0

5 years ago

3.1.0-alpha.0

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

3.0.0-alpha.3

5 years ago

2.5.0

5 years ago

3.0.0-alpha.2

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

5 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