npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pradiptakp-org/openapi-typescript

v5.1.0

Published

Generate TypeScript types from Swagger OpenAPI specs

Downloads

1

Readme

version(scoped) npm downloads (weekly) codecov

All Contributors

📘️ openapi-typescript

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

Features

  • OpenAPI 3.0
  • Swagger 2.0
  • ✅ Supports YAML and JSON schema formats
  • ✅ Supports loading via remote URL (simple authentication supported with the --auth flag)
  • ✅ Supports remote references: $ref: "external.yaml#components/schemas/User"
  • ✅ Formats using Prettier
  • ✅ TypeScript 4.0 features

Examples

Usage

🖥️ CLI

🗄️ Reading specs from file system

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

# 🔭 Loading spec from schema.yaml…
# 🚀 schema.yaml -> schema.ts [250ms]

npx openapi-typescript "specs/**/*.yaml" --output schemas/

# 🔭 Loading spec from specs/one.yaml…
# 🔭 Loading spec from specs/two.yaml…
# 🔭 Loading spec from specs/three.yaml…
# 🚀 specs/one.yaml -> schemas/specs/one.ts [250ms]
# 🚀 specs/two.yaml -> schemas/specs/two.ts [250ms]
# 🚀 specs/three.yaml -> schemas/specs/three.ts [250ms]

Note: if generating a single schema, --output must be a file (preferably *.ts). If using globs, --output must be a directory.

Thanks to @sharmarajdaksh for the glob feature!

☁️ 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]

Note: globbing doesn’t work for remote schemas because there is no reliable way to determine a list of files to select from a remote file system.

Thanks to @psmyrdek for the remote spec feature!

Using in TypeScript

Import any top-level item from the generated spec to use it. It works best if you also alias types to save on typing:

import { components } from "./generated-schema.ts";

type APIResponse = components["schemas"]["APIResponse"];

Because OpenAPI schemas may have invalid TypeScript characters as names, the square brackets are a safe way to access every property.

Also note that there’s a special operations interface that you can import OperationObjects by their operationId:

import { operations } from "./generated-schema.ts";

type getUsersById = operations["getUsersById"];

Even though operations isn’t present in your original schema, it’s a simple convenience and won’t disrupt any of your other types.

Thanks to @gr2m for the operations feature!

openapi-typescript-fetch

The generated spec can also be used with openapi-typescript-fetch which implements a typed fetch client for openapi-typescript.

import { paths } from "./petstore";

import { Fetcher } from "openapi-typescript-fetch";

// declare fetcher for paths
const fetcher = Fetcher.for<paths>()

// global configuration
fetcher.configure({
  baseUrl: "https://petstore.swagger.io/v2",
  init: {
    headers: {
      ...
    },
  },
  use: [...] // middlewares
})

// create fetch operations
const findPetsByStatus = fetcher.path("/pet/findByStatus").method("get").create()
const addPet = fetcher.path("/pet").method("post").create()

// fetch
try {
  const { status, data: pets } = await findPetsByStatus({
    status: ["available", "pending"],
  })
  await addPet({ ... })
} catch(e) {
  // check which operation threw the exception
  if (e instanceof addPet.Error) {
    // get discriminated union { status, data }
    const error = e.getActualType()
    if (error.status === 400) {
      error.data.validationErrors // 400 response data
    } else if (error.status === 500) {
      error.data.errorMessage // 500 response data
    } else {
      ...
    }
  }
}

Outputting to stdout

Simply omit the --output flag to return to stdout:

npx openapi-typescript schema.yaml

CLI Options

| Option | Alias | Default | Description | | :----------------------------- | :---- | :------: | :-------------------------------------------------------------------------------------------------------------------------------------- | | --output [location] | -o | (stdout) | Where should the output file be saved? | | --auth [token] | | | (optional) Provide an auth token to be passed along in the request (only if accessing a private schema) | | --immutable-types | -it | false | (optional) Generates immutable types (readonly properties and readonly array) | | --additional-properties | -ap | false | (optional) Allow arbitrary properties for all schema objects without additionalProperties: false | | --default-non-nullable | | false | (optional) Treat schema objects with default values as non-nullable | | --prettier-config [location] | -c | | (optional) Path to your custom Prettier configuration for output | | --raw-schema | | false | Generate TS types from partial schema (e.g. having components.schema at the top level) | | --httpMethod | -m | GET | (optional) Provide the HTTP Verb/Method for fetching a schema from a remote URL | | --headersObject | -h | | (optional) Provide a JSON object as string of HTTP headers for remote schema request. This will take priority over --header | | --header | -x | | (optional) Provide an array of or singular headers as an alternative to a JSON object. Each header must follow the key: value pattern |

🐢 Node

npm i --save-dev openapi-typescript
import fs from "fs";
import openapiTS from "openapi-typescript";

// example 1: load [object] as schema (JSON only)
const schema = await fs.promises.readFile("spec.json", "utf8") // must be OpenAPI JSON
const output = await openapiTS(JSON.parse(schema));

// example 2: load [string] as local file (YAML or JSON; released in v4.0)
const localPath = new URL("./spec.yaml", import.meta.url); // may be YAML or JSON format
const output = await openapiTS(localPath);

// example 3: load [string] as remote URL (YAML or JSON; released in v4.0)
const output = await openapiTS("https://myurl.com/v1/openapi.yaml");

The Node API may be useful if dealing with dynamically-created schemas, or you’re using within context of a larger application. Pass in either a JSON-friendly object to load a schema from memory, or a string to load a schema from a local file or remote URL (it will load the file quickly using built-in Node methods). Note that a YAML string isn’t supported in the Node.js API; either use the CLI or convert to JSON using js-yaml first.

⚠️ As of v4.0, openapiTS() is an async function.

Custom Formatter

If using the Node.js API, you can optionally pass a formatter to openapi-typescript. This is useful if you want to override the default types and substitute your own.

For example, say your schema has the following property:

properties:
  updated_at:
    type: string
    format: date-time

By default, this will generate a type updated_at?: string;. But we can override this by passing a formatter to the Node API, like so:

const types = openapiTS(mySchema, {
  formatter(node: SchemaObject) {
    if (node.format === "date-time") {
      return "Date"; // return the TypeScript “Date” type, as a string
    }
  // for all other schema objects, let openapi-typescript decide (return undefined)
});

This will generate updated_at?: Date instead. Note that you will still have to do the parsing of your data yourself. But this will save you from having to also update all your types.

Note: you don’t have to use .format—this is just an example! You can use any property on a schema object to overwrite its generated type if desired.

🏅 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!