# swagger-to-io-ts

> Generate io-ts types from Swagger OpenAPI specs

Latest version **0.1.1** (published 2019-10-30) · ISC license · 0 weekly downloads

## Install

```sh
npm install swagger-to-io-ts
pnpm add swagger-to-io-ts
yarn add swagger-to-io-ts
bun add swagger-to-io-ts
```

Provides the command `swagger-to-io-ts`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2019-10-30 |
| First published | 2019-08-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 130.2 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| Maintainers | mkredz |

## Links

- npm: https://www.npmjs.com/package/swagger-to-io-ts
- npm.io page: https://npm.io/package/swagger-to-io-ts

## Dependencies (7)

- [got](https://npm.io/package/got.md) 9.6.0
- [meow](https://npm.io/package/meow.md) 5.0.0
- [chalk](https://npm.io/package/chalk.md) 2.4.2
- [js-yaml](https://npm.io/package/js-yaml.md) 3.13.1
- [fs-extra](https://npm.io/package/fs-extra.md) 8.1.0
- [prettier](https://npm.io/package/prettier.md) 1.18.2
- [@types/prettier](https://npm.io/package/@types/prettier.md) 1.18.3

## Recent versions

- 0.1.1 (latest) — 2019-10-30
- 0.1.0 — 2019-08-21
- 0.0.1 — 2019-08-21

## README

# 📘️ swagger-to-io-ts

Convert Swagger files to [io-ts](https://github.com/gcanti/io-ts) types using Node.js. Based on [swagger-to-ts](https://github.com/manifoldco/swagger-to-ts).

💅 Prettifies output with [Prettier][prettier].

To compare actual generated output, see the [example](./example) folder.

## Usage

### CLI

```bash
npx swagger-to-io-ts schema.yaml --output schema.ts

# 🚀 schema.yaml -> schema.ts [2ms]
```

This will save a `schema.ts` file in the current folder. The CLI can accept YAML or JSON for the input file.

#### 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:

```json
"scripts": {
  "generate:specs": "npm run generate:specs:one && npm run generate:specs:two",
  "generate:specs:one": "npx swagger-to-io-ts one.yaml -o one.d.ts",
  "generate:specs:two": "npx swagger-to-io-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`?            |

### Node

```bash
npm i --save-dev swagger-to-io-ts
```

```js
const { readFileSync } = require('fs');
const swaggerToIoTS = require('swagger-to-io-ts');

const input = JSON.parse(readFileSync('spec.json', 'utf8')); // Input can be any JS object (OpenAPI format)
const output = swaggerToIoTS(input); // 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][js-yaml]. If you’re batching large folders of
specs, [glob][glob] may also come in handy.

#### Node Options

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

[glob]: https://www.npmjs.com/package/glob
[js-yaml]: https://www.npmjs.com/package/js-yaml
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html
[prettier]: https://npmjs.com/prettier

---
_Source: https://npm.io/package/swagger-to-io-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
