Skip to content

Add wrapper option #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ To compare actual generated output, see the [example](./example) folder.
### CLI

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

# 🚀 schema.yaml -> schema.ts [2ms]
```
Expand All @@ -35,13 +35,42 @@ This will save a `schema.ts` file in the current folder under the TypeScript
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:

```ts
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 `{}`):

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

As mentioned before, this uses [Prettier][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:

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

# 🚀 schema.yaml -> schema.ts [2ms]
```
Expand All @@ -67,13 +96,12 @@ also use the Node API (below).

#### CLI Options

| Option | Alias | Default | Description |
| :-------------------- | :---- | :--------: | :--------------------------------------------------------------------------------------------------- |
| `--output [location]` | `-o` | (stdout) | Where should the output file be saved? |
| `--namespace [name]` | `-n` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
| `--swagger [version]` | `-s` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `--camelcase` | `-c` | `false` | Convert `snake_case` properties to `camelCase`? |
| `--export` | `-e` | `false` | Exports the namespace |
| 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

Expand All @@ -86,7 +114,7 @@ const { readFileSync } = require('fs');
const swaggerToTS = require('@manifoldco/swagger-to-ts');

const input = JSON.parse(readFileSync('spec.json', 'utf8')); // Input be any JS object (OpenAPI format)
const output = swaggerToTS(input, { namespace: 'MySpec' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
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
Expand All @@ -103,12 +131,11 @@ in handy.

#### Node Options

| Name | Type | Default | Description |
| :---------- | :-------: | :--------: | :--------------------------------------------------------------------------------------------------- |
| `namespace` | `string` | `OpenAPI2` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
| `swagger` | `number` | `2` | Which Swagger version to use. Currently only supports `2`. |
| `camelcase` | `boolean` | `false` | Convert `snake_case` properties to `camelCase` |
| `export` | `boolean` | `false` | Exports the namespace |
| 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
Expand Down
23 changes: 18 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ Usage
$ swagger-to-ts [input] [options]

Options
--namespace, -n specify namespace to prefix types
--help display this
--wrapper, -w specify wrapper (default: "declare namespace OpenAPI2")
--output, -o specify output file
--camelcase, -c convert snake_case properties to camelCase (default: off)
--swagger, -s specify Swagger version (default: 2)
--export, -e exports the namespace (default: false)
`,
{
flags: {
Expand All @@ -28,9 +27,10 @@ Options
default: false,
alias: 'c',
},
namespace: {
wrapper: {
type: 'string',
alias: 'n',
default: 'declare namespace OpenAPI2',
alias: 'w',
},
output: {
type: 'string',
Expand All @@ -40,9 +40,12 @@ Options
type: 'number',
alias: 's',
},
namespace: {
type: 'string',
alias: 'n',
},
export: {
type: 'boolean',
default: false,
alias: 'e',
},
},
Expand All @@ -51,6 +54,16 @@ Options

let spec = cli.input[0];

if (typeof cli.flags.namespace === 'string' && cli.flags.namespace.length > 0) {
console.error(chalk.red('--namespace option is deprecated. Please use --wrapper instead.'));
return;
}

if (cli.flags.export === true) {
console.error(chalk.red('--export option is deprecated. Please use --wrapper instead.'));
return;
}

// If input is a file, load it
const pathname = resolve(process.cwd(), spec);
if (existsSync(pathname)) {
Expand Down
2 changes: 1 addition & 1 deletion example/output.ts → example/output.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace OpenAPI2 {
declare namespace OpenAPI2 {
export interface ValueProp {
// Heading of a value proposition.
header: string;
Expand Down
Loading