1- Node script that can convert Swagger files to TypeScript interfaces. This uses
2- [ prettier ] [ prettier ] to format the interfaces to be somewhat human-readable .
1+ Convert Swagger files to TypeScript interfaces using Node.js . This uses
2+ [ Prettier ] [ prettier ] to prettify the interfaces.
33
44### Installation
55
66``` shell
77npm i --save-dev @manifoldco/swagger-to-ts
88```
99
10- ### Generating
10+ ### Usage
11+
12+ The function takes 2 parameters: a ** filepath** to a JSON file, and a
13+ ** namespace** for the API. Namespace is required because it’s very likely
14+ entities within the Swagger spec will collide with some other type in your
15+ system, but you still want to access something predictably-named.
16+
17+ ``` js
18+ const swaggerToTS = require (' swagger-to-ts' );
19+
20+ const filepath = ' ./path/to/my/file.json' ;
21+ const namespace = ' MySpec' ;
22+ const typeData = swaggerToJS (filepath, namespace);
23+ ```
1124
1225#### From Swagger JSON
1326
@@ -16,10 +29,14 @@ const { readFileSync, writeFileSync } = require('fs');
1629const swaggerToTS = require (' swagger-to-ts' );
1730
1831const file = ' ./spec/swagger.json' ;
19- const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ));
32+ const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ), ' MySpec ' );
2033writeFileSync (' ./types/swagger.ts' ), typeData);
2134```
2235
36+ ``` js
37+ import MySpec from ' ./types/swagger.ts' ;
38+ ```
39+
2340#### From Swagger YAML
2441
2542Swagger files must be passed to ` swaggerToTS() ` in a JSON format, so you’ll
@@ -31,10 +48,14 @@ const { readFileSync, writeFileSync } = require('fs');
3148const yaml = require (' js-yaml' );
3249
3350const file = ' ./spec/swagger.json' ;
34- const typeData = swaggerToTS (yaml .safeLoad (fs .readFileSync (file, ' UTF-8' )));
51+ const typeData = swaggerToTS (yaml .safeLoad (fs .readFileSync (file, ' UTF-8' )), ' MySpec ' );
3552writeFileSync (' ./types/swagger.ts' ), typeData);
3653```
3754
55+ ``` js
56+ import MySpec from ' ./types/swagger.ts' ;
57+ ```
58+
3859#### Generating multiple files
3960
4061The [ glob] [ glob ] package is helpful in converting entire folders. The
@@ -50,8 +71,12 @@ const source1 = glob.sync('./swaggerspec/v1/**/*.yaml');
5071const source2 = glob .sync (' ./swaggerspec/v2/**/*.yaml' );
5172
5273[... source1, ... source2].forEach (file => {
53- const typeData = swaggerToTS (yaml .safeLoad (readFileSync (file, ' UTF-8' )));
54- const filename = path .basename (file).replace (/ \. ya? ml$ / i , ' .ts' );
74+ const basename = path .basename (file);
75+ const filename = basename .replace (/ \. ya? ml$ / i , ' .ts' );
76+ const typeData = swaggerToTS (
77+ yaml .safeLoad (readFileSync (file, ' UTF-8' )),
78+ basename
79+ );
5580 writeFileSync (path .resolve (__dirname , ' types' , filename), typeData);
5681});
5782```
@@ -70,7 +95,7 @@ const file = './spec/swagger.json';
7095console .log (' 🏗 Generating types…' );
7196const timeStart = process .hrtime ();
7297
73- const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ));
98+ const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ), ' MySpec ' );
7499writeFileSync (' ./types/swagger.ts' ), typeData);
75100
76101const timeEnd = process .hrtime (timeStart);
@@ -94,9 +119,9 @@ It’s recommended to name the file `*.ts` and `import` the definitions. `*.d.ts
94119can’t be imported; they’re meant to be shipped alongside modules.
95120
96121``` js
97- import { User } from ' ../types/swagger' ;
122+ import Swagger from ' ../types/swagger' ;
98123
99- const logIn = (user : User ) => {
124+ const logIn = (user : Swagger . User ) => {
100125 // …
101126` ` `
102127
0 commit comments