@@ -25,7 +25,7 @@ To compare actual generated output, see the [example](./example) folder.
2525### CLI
2626
2727``` bash
28- npx @manifoldco/swagger-to-ts schema.yaml --namespace OpenAPI --output schema.ts
28+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare namespace OpenAPI" --output schema.d .ts
2929
3030# 🚀 schema.yaml -> schema.ts [2ms]
3131```
@@ -35,13 +35,42 @@ This will save a `schema.ts` file in the current folder under the TypeScript
3535collision among specs is highly likely). The CLI can accept YAML or JSON for
3636the input file.
3737
38+ #### Wrapper option
39+
40+ There are many different ways to expose types in TypeScript. To name a few:
41+
42+ ``` ts
43+ namespace MyNamespace {}
44+ export namespace MyNamespace {}
45+ declare namespace MyNamespace {}
46+ declare module MyModule {}
47+ ```
48+
49+ The ` --wrapper ` flag lets you specify any of the above with a string (omit
50+ the ` {} ` ):
51+
52+ ``` bash
53+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " namespace API"
54+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare namespace API"
55+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare namespace API"
56+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare module '@api'"
57+ ```
58+
59+ As mentioned before, this uses [ Prettier] [ prettier ] to clean up output, so
60+ extra spaces are generally OK here. Prettier also will err on cleanup if you
61+ specify invalid TypeScript, letting you know on generation if anything went
62+ wrong.
63+
64+ _ Note: previous versions of the CLI tool used ` --namespace ` and ` --export ` .
65+ These have both been deprecated in favor of ` --wrapper ` ._
66+
3867#### CamelCasing properties
3968
4069Within interfaces, you may want to convert ` snake_case ` properties to
4170` camelCase ` by adding the ` --camelcase ` flag:
4271
4372``` bash
44- npx @manifoldco/swagger-to-ts schema.yaml --camelcase -- namespace OpenAPI --output schema.ts
73+ npx @manifoldco/swagger-to-ts schema.yaml --wrapper " declare namespace OpenAPI" --output schema.d .ts
4574
4675# 🚀 schema.yaml -> schema.ts [2ms]
4776```
@@ -67,13 +96,12 @@ also use the Node API (below).
6796
6897#### CLI Options
6998
70- | Option | Alias | Default | Description |
71- | :-------------------- | :---- | :--------: | :--------------------------------------------------------------------------------------------------- |
72- | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
73- | ` --namespace [name] ` | ` -n ` | ` OpenAPI2 ` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
74- | ` --swagger [version] ` | ` -s ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
75- | ` --camelcase ` | ` -c ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` ? |
76- | ` --export ` | ` -e ` | ` false ` | Exports the namespace |
99+ | Option | Alias | Default | Description |
100+ | :-------------------- | :---- | :--------------------------: | :--------------------------------------------------------- |
101+ | ` --wrapper ` | ` -w ` | ` declare namespace OpenAPI2 ` | How should this export the types? |
102+ | ` --output [location] ` | ` -o ` | (stdout) | Where should the output file be saved? |
103+ | ` --swagger [version] ` | ` -s ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
104+ | ` --camelcase ` | ` -c ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` ? |
77105
78106### Node
79107
@@ -86,7 +114,7 @@ const { readFileSync } = require('fs');
86114const swaggerToTS = require (' @manifoldco/swagger-to-ts' );
87115
88116const input = JSON .parse (readFileSync (' spec.json' , ' utf8' )); // Input be any JS object (OpenAPI format)
89- const output = swaggerToTS (input, { namespace : ' MySpec ' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
117+ const output = swaggerToTS (input, { wrapper : ' declare namespace MyAPI ' }); // Outputs TypeScript defs as a string (to be parsed, or written to a file)
90118```
91119
92120The Node API is a bit more flexible: it will only take a JS object as input
@@ -103,12 +131,11 @@ in handy.
103131
104132#### Node Options
105133
106- | Name | Type | Default | Description |
107- | :---------- | :-------: | :--------: | :--------------------------------------------------------------------------------------------------- |
108- | ` namespace ` | ` string ` | ` OpenAPI2 ` | How should the output be namespaced? (namespacing is enforced as there’s a high chance of collision) |
109- | ` swagger ` | ` number ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
110- | ` camelcase ` | ` boolean ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` |
111- | ` export ` | ` boolean ` | ` false ` | Exports the namespace |
134+ | Name | Type | Default | Description |
135+ | :---------- | :-------: | :--------------------------: | :--------------------------------------------------------- |
136+ | ` --wrapper ` | ` string ` | ` declare namespace OpenAPI2 ` | How should this export the types? |
137+ | ` swagger ` | ` number ` | ` 2 ` | Which Swagger version to use. Currently only supports ` 2 ` . |
138+ | ` camelcase ` | ` boolean ` | ` false ` | Convert ` snake_case ` properties to ` camelCase ` |
112139
113140[ glob ] : https://www.npmjs.com/package/glob
114141[ js-yaml ] : https://www.npmjs.com/package/js-yaml
0 commit comments