@@ -7,7 +7,18 @@ Node script that can convert Swagger files to TypeScript interfaces. This uses
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** (` ./path/to/my/spec.json ` ),
13
+ and a ** namespace** ('MySpec'). Namespace is required because it’s very
14
+ likely entities within the Swagger spec will collide with some other type in
15
+ your system, but you still want to access something predictably-named.
16
+
17
+ ``` js
18
+ const swaggerToTS = require (' swagger-to-ts' );
19
+
20
+ const typeData = swaggerToJS (' ./path/to/my/spec.json' , ' MySpec' );
21
+ ```
11
22
12
23
#### From Swagger JSON
13
24
@@ -16,10 +27,14 @@ const { readFileSync, writeFileSync } = require('fs');
16
27
const swaggerToTS = require (' swagger-to-ts' );
17
28
18
29
const file = ' ./spec/swagger.json' ;
19
- const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ));
30
+ const typeData = swaggerToTS (readFileSync (file, ' UTF-8' ), ' MySpec ' );
20
31
writeFileSync (' ./types/swagger.ts' ), typeData);
21
32
```
22
33
34
+ ``` js
35
+ import MySpec from ' ./types/swagger.ts' ;
36
+ ```
37
+
23
38
#### From Swagger YAML
24
39
25
40
Swagger files must be passed to ` swaggerToTS() ` in a JSON format, so you’ll
@@ -31,10 +46,14 @@ const { readFileSync, writeFileSync } = require('fs');
31
46
const yaml = require (' js-yaml' );
32
47
33
48
const file = ' ./spec/swagger.json' ;
34
- const typeData = swaggerToTS (yaml .safeLoad (fs .readFileSync (file, ' UTF-8' )));
49
+ const typeData = swaggerToTS (yaml .safeLoad (fs .readFileSync (file, ' UTF-8' )), ' MySpec ' );
35
50
writeFileSync (' ./types/swagger.ts' ), typeData);
36
51
```
37
52
53
+ ``` js
54
+ import MySpec from ' ./types/swagger.ts' ;
55
+ ```
56
+
38
57
#### Generating multiple files
39
58
40
59
The [ glob] [ glob ] package is helpful in converting entire folders. The
@@ -50,8 +69,12 @@ const source1 = glob.sync('./swaggerspec/v1/**/*.yaml');
50
69
const source2 = glob .sync (' ./swaggerspec/v2/**/*.yaml' );
51
70
52
71
[... 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' );
72
+ const basename = path .basename (file);
73
+ const filename = basename .replace (/ \. ya? ml$ / i , ' .ts' );
74
+ const typeData = swaggerToTS (
75
+ yaml .safeLoad (readFileSync (file, ' UTF-8' )),
76
+ basename
77
+ );
55
78
writeFileSync (path .resolve (__dirname , ' types' , filename), typeData);
56
79
});
57
80
```
@@ -94,9 +117,9 @@ It’s recommended to name the file `*.ts` and `import` the definitions. `*.d.ts
94
117
can’t be imported; they’re meant to be shipped alongside modules.
95
118
96
119
``` js
97
- import { User } from ' ../types/swagger' ;
120
+ import Swagger from ' ../types/swagger' ;
98
121
99
- const logIn = (user : User ) => {
122
+ const logIn = (user : Swagger . User ) => {
100
123
// …
101
124
` ` `
102
125
0 commit comments