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

openapi-to-ts

v1.1.0

Published

Generate TypeScript types from OpenAPI 3.0 specs.

Downloads

913

Readme

npm downloads npm npm

Example

Features

  • Generates TypeScript types from OpenAPI 3.0 specification files using Node.js.
  • Retrieve specification files from local or remote sources, including private GitHub repos.
  • Supports YAML and JSON specification files.
  • Generates interfaces and types.
  • Tiny size with only a few dependencies.

Install

npm install openapi-to-ts --save-dev

or

yarn add openapi-to-ts --dev

CLI Usage

To generate TypeScript types from the local file system, run the following command in your console:

npx openapi-to-ts -i ./examples/example.yaml -o ./examples/example.ts

To generate TypeScript types from a remote source, run the following command in your console:

npx openapi-to-ts -i https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml -o ./examples/example.ts

CLI Options

| Option | Alias | Required | Default | Description | | --------------------- | ----- | -------- | ------- | ------------------------------------------------------------------------------------ | | --input [input] | -i | Yes | | Specifies the OpenAPI 3.0 specification file that should be converted. | | --output [output] | -o | Yes | | Specifies the output file where the TypeScript types should be written to. | | --githubToken [token] | | No | | Attach a GitHub personal access token to the request if fetching the input remotely. | | --prefixWithI | | No | false | Appends the letter I as a prefix to all interface names. |

Input & Output

The input option allows you to define the location of the OpenAPI 3.0 specification file that you would like to convert. The location could be on your local file system or a remote source such as a GitHub repository.

The output option allows you to define the path to which you would like to write the generated types. The output path must be on your local file system.

To convert a specification file from your local file system you can run:

npx openapi-to-ts -i <PATH_TO_SPEC_FILE> -o <PATH_TO_OUTPUT>

This command will convert the specification file located at <PATH_TO_SPEC_FILE> to TypeScript types and write them to <PATH_TO_OUTPUT>.

To convert a specification file from a remote source, such as a GitHub repository you can run:

npx openapi-to-ts -i <URL_TO_SPEC_FILE> -o <PATH_TO_OUTPUT>

This command will fetch the specification file from the link specified above, convert it to TypeScript types and write the final output to the specified location.

GitHub Token for Private Repositories

If your OpenAPI 3.0 specification file is located in a private GitHub repository, you can use the --githubToken <token> option. This will attach your personal access token to the request that fetches the remotely stored specification file. For more information on how to create a personal access token in GitHub, please click here.

To use this option, please run:

npx openapi-to-ts -i <URL_TO_SPEC_FILE> -o <PATH_TO_OUTPUT> --githubToken <YOUR_PERSONAL_ACCESS_TOKEN>

Interface Prefixes

If your schema is called Cat, by default openapi-to-ts will generate the interface with the name Cat. However, you can optionally supply the --prefixWithI option to append the letter I to the interface's name. If you do so, the schema by the name of Cat will be converted to an interface called ICat.

To use this option, please run:

npx openapi-to-ts -i <PATH_TO_SPEC_FILE> -o <PATH_TO_OUTPUT> --prefixWithI

Planned Improvements

  • Add an option to run prettier on the generated TS file.
  • Implement additionalProperties.
  • Allow usage from Node.js and not just the CLI.
  • Cover the codebase with tests.

Bugs

Found a bug? Please report it here, and I would be happy to take a look. Contributions are more than welcome too!