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.
3
3
4
4
### Installation
5
5
6
6
``` shell
7
7
npm i --save-dev @manifoldco/swagger-to-ts
8
8
```
9
9
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
+ ```
11
24
12
25
#### From Swagger JSON
13
26
@@ -16,10 +29,14 @@ const { readFileSync, writeFileSync } = require('fs');
16
29
const swaggerToTS = require (' swagger-to-ts' );
17
30
18
31
const file = ' ./spec/swagger.json' ;
19
- const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ));
32
+ const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ), ' MySpec ' );
20
33
writeFileSync (' ./types/swagger.ts' ), typeData);
21
34
```
22
35
36
+ ``` js
37
+ import MySpec from ' ./types/swagger.ts' ;
38
+ ```
39
+
23
40
#### From Swagger YAML
24
41
25
42
Swagger files must be passed to ` swaggerToTS() ` in a JSON format, so you’ll
@@ -31,10 +48,14 @@ const { readFileSync, writeFileSync } = require('fs');
31
48
const yaml = require (' js-yaml' );
32
49
33
50
const 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 ' );
35
52
writeFileSync (' ./types/swagger.ts' ), typeData);
36
53
```
37
54
55
+ ``` js
56
+ import MySpec from ' ./types/swagger.ts' ;
57
+ ```
58
+
38
59
#### Generating multiple files
39
60
40
61
The [ glob] [ glob ] package is helpful in converting entire folders. The
@@ -50,8 +71,12 @@ const source1 = glob.sync('./swaggerspec/v1/**/*.yaml');
50
71
const source2 = glob .sync (' ./swaggerspec/v2/**/*.yaml' );
51
72
52
73
[... 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
+ );
55
80
writeFileSync (path .resolve (__dirname , ' types' , filename), typeData);
56
81
});
57
82
```
@@ -70,7 +95,7 @@ const file = './spec/swagger.json';
70
95
console .log (' 🏗 Generating types…' );
71
96
const timeStart = process .hrtime ();
72
97
73
- const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ));
98
+ const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ), ' MySpec ' );
74
99
writeFileSync (' ./types/swagger.ts' ), typeData);
75
100
76
101
const timeEnd = process .hrtime (timeStart);
@@ -94,9 +119,9 @@ It’s recommended to name the file `*.ts` and `import` the definitions. `*.d.ts
94
119
can’t be imported; they’re meant to be shipped alongside modules.
95
120
96
121
``` js
97
- import { User } from ' ../types/swagger' ;
122
+ import Swagger from ' ../types/swagger' ;
98
123
99
- const logIn = (user : User ) => {
124
+ const logIn = (user : Swagger . User ) => {
100
125
// …
101
126
` ` `
102
127
0 commit comments