Skip to content

Commit c8a9b83

Browse files
committed
Add namespace support
1 parent 110d5d1 commit c8a9b83

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

Diff for: README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ Node script that can convert Swagger files to TypeScript interfaces. This uses
77
npm i --save-dev @manifoldco/swagger-to-ts
88
```
99

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+
```
1122

1223
#### From Swagger JSON
1324

@@ -16,10 +27,14 @@ const { readFileSync, writeFileSync } = require('fs');
1627
const swaggerToTS = require('swagger-to-ts');
1728

1829
const file = './spec/swagger.json';
19-
const typeData = swaggerToTS(readFileSync(file, 'UTF-8'));
30+
const typeData = swaggerToTS(readFileSync(file, 'UTF-8'), 'MySpec');
2031
writeFileSync('./types/swagger.ts'), typeData);
2132
```
2233

34+
```js
35+
import MySpec from './types/swagger.ts';
36+
```
37+
2338
#### From Swagger YAML
2439

2540
Swagger files must be passed to `swaggerToTS()` in a JSON format, so you’ll
@@ -31,10 +46,14 @@ const { readFileSync, writeFileSync } = require('fs');
3146
const yaml = require('js-yaml');
3247

3348
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');
3550
writeFileSync('./types/swagger.ts'), typeData);
3651
```
3752

53+
```js
54+
import MySpec from './types/swagger.ts';
55+
```
56+
3857
#### Generating multiple files
3958

4059
The [glob][glob] package is helpful in converting entire folders. The
@@ -50,8 +69,12 @@ const source1 = glob.sync('./swaggerspec/v1/**/*.yaml');
5069
const source2 = glob.sync('./swaggerspec/v2/**/*.yaml');
5170

5271
[...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+
);
5578
writeFileSync(path.resolve(__dirname, 'types', filename), typeData);
5679
});
5780
```
@@ -94,9 +117,9 @@ It’s recommended to name the file `*.ts` and `import` the definitions. `*.d.ts
94117
can’t be imported; they’re meant to be shipped alongside modules.
95118

96119
```js
97-
import { User } from '../types/swagger';
120+
import Swagger from '../types/swagger';
98121

99-
const logIn = (user: User) => {
122+
const logIn = (user: Swagger.User) => {
100123
// …
101124
```
102125

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@manifoldco/swagger-to-ts",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "Convert Swagger files to TypeScript",
55
"main": "dist/swaggerToTS.js",
66
"scripts": {

Diff for: src/swaggerToTS.js

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
const { format } = require('prettier'); // eslint-disable-line import/no-extraneous-dependencies
22

3-
const DEFAULT_OPTIONS = {
4-
enum: false,
5-
};
6-
73
// Primitives only!
84
const TYPES = {
95
string: 'string',
@@ -18,12 +14,12 @@ const camelCase = name =>
1814
letter.toUpperCase().replace(/[^0-9a-z]/gi, '')
1915
);
2016

21-
const buildTypes = (spec, options) => {
17+
const buildTypes = (spec, namespace) => {
2218
const { definitions } = spec;
2319

2420
const queue = [];
2521
const enumQueue = [];
26-
const output = [];
22+
const output = [`namespace ${namespace} {`];
2723

2824
const getRef = lookup => {
2925
const ref = lookup.replace('#/definitions/', '');
@@ -47,7 +43,7 @@ const buildTypes = (spec, options) => {
4743
};
4844

4945
const buildNextEnum = ([ID, options]) => {
50-
output.push(`enum ${ID} {`);
46+
output.push(`export enum ${ID} {`);
5147
options.forEach(option => {
5248
if (typeof option === 'number') {
5349
const lastWord = ID.search(/[A-Z](?=[^A-Z]*$)/);
@@ -70,7 +66,7 @@ const buildTypes = (spec, options) => {
7066
}
7167

7268
// Open interface
73-
output.push(`interface ${camelCase(ID)} {`);
69+
output.push(`export interface ${camelCase(ID)} {`);
7470

7571
// Populate interface
7672
Object.entries(properties).forEach(([key, value]) => {
@@ -117,13 +113,12 @@ const buildTypes = (spec, options) => {
117113
buildNextInterface();
118114
}
119115

116+
output.push('}'); // Close namespace
120117
return output.join('\n');
121118
};
122119

123-
module.exports = (input, userOptions = {}) => {
124-
const options = { ...DEFAULT_OPTIONS, ...userOptions };
125-
126-
return format(buildTypes(input, options), {
120+
module.exports = (filename, namespace) => {
121+
return format(buildTypes(filename, namespace), {
127122
parser: 'typescript',
128123
printWidth: 100,
129124
singleQuote: true,

0 commit comments

Comments
 (0)