Skip to content

Commit 4a92b34

Browse files
committed
Generate enums
1 parent 9902c22 commit 4a92b34

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.1.0",
3+
"version": "0.2.0",
44
"description": "Convert Swagger files to TypeScript",
55
"main": "dist/swaggerToTS.js",
66
"scripts": {

src/swaggerToTS.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const TYPES = {
1414
const capitalize = str => `${str[0].toUpperCase()}${str.slice(1)}`;
1515

1616
const camelCase = name =>
17-
name.replace(/(-|_|\.|\s)+[a-z]/g, letter => letter.toUpperCase().replace(/[^0-9a-z]/gi, ''));
17+
name.replace(/(-|_|\.|\s)+[a-z]/g, letter =>
18+
letter.toUpperCase().replace(/[^0-9a-z]/gi, '')
19+
);
1820

1921
const buildTypes = (spec, options) => {
2022
const { definitions } = spec;
@@ -47,8 +49,9 @@ const buildTypes = (spec, options) => {
4749
const buildNextEnum = ([ID, options]) => {
4850
output.push(`enum ${ID} {`);
4951
options.forEach(option => {
50-
const name = typeof option === 'string' ? capitalize(camelCase(option)) : option;
51-
output.push(`${name}: ${option};`);
52+
const name =
53+
typeof option === 'string' ? capitalize(camelCase(option)) : option;
54+
output.push(`${name} = ${JSON.stringify(option)},`);
5255
});
5356
output.push('}');
5457
};
@@ -70,13 +73,20 @@ const buildTypes = (spec, options) => {
7073
const name = `${camelCase(key)}${optional ? '?' : ''}`;
7174
const type = getType(value);
7275

76+
if (typeof value.description === 'string') {
77+
// Print out descriptions as comments, but only if there’s something there (.*)
78+
output.push(
79+
`// ${value.description.replace(/\n$/, '').replace(/\n/g, '\n// ')}`
80+
);
81+
}
82+
7383
// If this is a nested object, let’s add it to the stack for later
7484
if (type === 'object') {
7585
const newID = camelCase(`${ID}_${key}`);
7686
queue.push([newID, value]);
7787
output.push(`${name}: ${newID};`);
7888
return;
79-
} else if (options.enum === true && Array.isArray(value.enum)) {
89+
} else if (Array.isArray(value.enum)) {
8090
const newID = camelCase(`${ID}_${key}`);
8191
enumQueue.push([newID, value.enum]);
8292
output.push(`${name}: ${newID};`);
@@ -102,8 +112,6 @@ const buildTypes = (spec, options) => {
102112
buildNextInterface();
103113
}
104114

105-
// console.log(output.join('\n'));
106-
107115
return output.join('\n');
108116
};
109117

0 commit comments

Comments
 (0)