Git Product home page Git Product logo

swagger-to-ts's Introduction

version (scoped) codecov

πŸ“˜οΈ swagger-to-ts

Convert Swagger files to TypeScript interfaces using Node.js.

πŸ’… Prettifies output with Prettier.

OpenAPI Feature TypeScript equivalent
type: 'string' string
type: 'number' number
type: 'integer' number
allOf TypeB extends TypeA
oneOf TypeA | TypeB
required (not optional)
enum 'a' | 'b'

To compare actual generated output, see the example folder.

Usage

CLI

npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace OpenAPI" --output schema.d.ts

# πŸš€ schema.yaml -> schema.d.ts [2ms]

This will save a schema.ts file in the current folder under the TypeScript namespace OpenAPI (namespaces are required because chances of collision among specs is highly likely). The CLI can accept YAML or JSON for the input file.

Wrapper option

There are many different ways to expose types in TypeScript. To name a few:

namespace MyNamespace {}
export namespace MyNamespace {}
declare namespace MyNamespace {}
declare module MyModule {}

The --wrapper flag lets you specify any of the above with a string (omit the {}):

npx @manifoldco/swagger-to-ts schema.yaml --wrapper "namespace API"
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "export namespace API"
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare namespace API"
npx @manifoldco/swagger-to-ts schema.yaml --wrapper "declare module '@api'"

By default, wrapper is declare namespace OpenAPI2. You can skip exposing types via a wrapper by adding the --nowrapper flag:

npx @manifoldco/swagger-to-ts schema.yaml --nowrapper

As mentioned before, this uses Prettier to clean up output, so extra spaces are generally OK here. Prettier also will err on cleanup if you specify invalid TypeScript, letting you know on generation if anything went wrong.

Note: previous versions of the CLI tool used --namespace and --export. These have both been deprecated in favor of --wrapper.

CamelCasing properties

Within interfaces, you may want to convert snake_case properties to camelCase by adding the --camelcase flag:

npx @manifoldco/swagger-to-ts schema.yaml --camelcase --wrapper "declare namespace OpenAPI" --output schema.d.ts

# πŸš€ schema.yaml -> schema.d.ts [2ms]

Generating multiple schemas

Say you have multiple schemas you need to parse. I’ve found the simplest way to do that is to use npm scripts. In your package.json, you can do something like the following:

"scripts": {
  "generate:specs": "npm run generate:specs:one && npm run generate:specs:two",
  "generate:specs:one": "npx @manifoldco/swagger-to-ts one.yaml -o one.d.ts",
  "generate:specs:two": "npx @manifoldco/swagger-to-ts two.yaml -o two.d.ts"
}

Rinse and repeat for more specs.

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

CLI Options

Option Alias Default Description
--wrapper -w declare namespace OpenAPI2 How should this export the types?
--output [location] -o (stdout) Where should the output file be saved?
--swagger [version] -s 2 Which Swagger version to use. Currently only supports 2.
--camelcase -c false Convert snake_case properties to camelCase?
--nowrapper -nw false Disables rendering a wrapper

Node

npm i --save-dev @manifoldco/swagger-to-ts
const { readFileSync } = require('fs');
const swaggerToTS = require('@manifoldco/swagger-to-ts');

const input = JSON.parse(readFileSync('spec.json', 'utf8')); // Input can be any JS object (OpenAPI format)
const output = swaggerToTS(input, { wrapper: 'declare namespace MyAPI' }); // 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.

Node Options

Name Type Default Description
wrapper string | false declare namespace OpenAPI2 How should this export the types? Pass false to disable rendering a wrapper
swagger number 2 Which Swagger version to use. Currently only supports 2.
camelcase boolean false Convert snake_case properties to camelCase

swagger-to-ts's People

Contributors

sgarza-ksm avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.