Skip to content

Commit c6a61fe

Browse files
committed
Fix array generation, add more robust tests
1 parent 485b60a commit c6a61fe

11 files changed

+408
-112
lines changed

Diff for: .eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
parser: '@typescript-eslint/parser',
23
extends: ['prettier/react', 'airbnb'],
34
plugins: ['prettier', 'import'],
45
env: {
@@ -14,5 +15,6 @@ module.exports = {
1415
'no-underscore-dangle': 0,
1516
'react/jsx-filename-extension': 0,
1617
'react/destructuring-assignment': 0,
18+
'space-before-function-paren': 0, // let Prettier handle it
1719
},
1820
};

Diff for: README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@ Convert Swagger files to TypeScript interfaces using Node.js.
66

77
💅 Prettifies output with [Prettier][prettier].
88

9+
| OpenAPI Feature | TypeScript equivalent |
10+
| :---------------- | :-------------------: |
11+
| `type: 'string'` | `string` |
12+
| `type: 'number'` | `number` |
13+
| `type: 'integer'` | `number` |
14+
| `allOf` | `TypeB extends TypeA` |
15+
| `oneOf` | `TypeA \| TypeB` |
16+
| `required` | (not optional) |
17+
18+
To compare actual generated output, see the [example](./example) folder.
19+
920
## Usage
1021

1122
### CLI
1223

1324
```bash
14-
npx @manifoldco/swagger-to-ts schema.yaml --output schema.ts --namespace OpenAPI
25+
npx @manifoldco/swagger-to-ts schema.yaml --namespace OpenAPI --output schema.ts
1526

1627
# 🚀 schema.yaml -> schema.ts [2ms]
1728
```
1829

1930
This will save a `schema.ts` file in the current folder under the TypeScript
20-
[namespace][namespace] `OpenAPI`. The CLI can accept YAML or JSON for the
21-
input file.
31+
[namespace][namespace] `OpenAPI` (namespaces are required because chances of
32+
collision among specs is highly likely). The CLI can accept YAML or JSON for
33+
the input file.
2234

2335
### Node
2436

File renamed without changes.

Diff for: types/catalog/v1.ts renamed to example/output.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace OpenAPI2 {
4343
features?: ProductIntegrationFeatures;
4444
}
4545
export enum UpdateProductBodyIntegrationVersion {
46-
V1 = 'v1',
46+
V1 = "v1"
4747
}
4848
export interface UpdateProduct {
4949
id: string;
@@ -84,10 +84,10 @@ namespace OpenAPI2 {
8484
body: RegionBody;
8585
}
8686
export enum RegionVersion {
87-
Version1 = 1,
87+
Version1 = 1
8888
}
8989
export enum RegionType {
90-
Region = 'region',
90+
Region = "region"
9191
}
9292
export interface ProviderBody {
9393
teamId: string;
@@ -104,10 +104,10 @@ namespace OpenAPI2 {
104104
body: ProviderBody;
105105
}
106106
export enum ProviderType {
107-
Provider = 'provider',
107+
Provider = "provider"
108108
}
109109
export enum ProviderVersion {
110-
Version1 = 1,
110+
Version1 = 1
111111
}
112112
export interface ProductListing {
113113
// When true, everyone can see the product when requested. When false it will
@@ -155,8 +155,8 @@ namespace OpenAPI2 {
155155
region?: ProductIntegrationFeaturesRegion;
156156
}
157157
export enum ProductIntegrationFeaturesRegion {
158-
UserSpecified = 'user-specified',
159-
Unspecified = 'unspecified',
158+
UserSpecified = "user-specified",
159+
Unspecified = "unspecified"
160160
}
161161
export interface ProductBody {
162162
providerId: string;
@@ -189,19 +189,19 @@ namespace OpenAPI2 {
189189
features: ProductIntegrationFeatures;
190190
}
191191
export enum ProductBodyIntegrationVersion {
192-
V1 = 'v1',
192+
V1 = "v1"
193193
}
194194
export interface ProductBodyBilling {
195195
type: ProductBodyBillingType;
196196
currency: ProductBodyBillingCurrency;
197197
}
198198
export enum ProductBodyBillingCurrency {
199-
Usd = 'usd',
199+
Usd = "usd"
200200
}
201201
export enum ProductBodyBillingType {
202-
MonthlyProrated = 'monthly-prorated',
203-
MonthlyAnniversary = 'monthly-anniversary',
204-
AnnualAnniversary = 'annual-anniversary',
202+
MonthlyProrated = "monthly-prorated",
203+
MonthlyAnniversary = "monthly-anniversary",
204+
AnnualAnniversary = "annual-anniversary"
205205
}
206206
export interface ProductBodyTerms {
207207
url?: string;
@@ -214,10 +214,10 @@ namespace OpenAPI2 {
214214
body: ProductBody;
215215
}
216216
export enum ProductType {
217-
Product = 'product',
217+
Product = "product"
218218
}
219219
export enum ProductVersion {
220-
Version1 = 1,
220+
Version1 = 1
221221
}
222222
export interface PlanBody {
223223
providerId: string;
@@ -244,10 +244,10 @@ namespace OpenAPI2 {
244244
body: PlanBody;
245245
}
246246
export enum PlanType {
247-
Plan = 'plan',
247+
Plan = "plan"
248248
}
249249
export enum PlanVersion {
250-
Version1 = 1,
250+
Version1 = 1
251251
}
252252
export interface FeatureValueDetails {
253253
label: string;
@@ -299,9 +299,9 @@ namespace OpenAPI2 {
299299
values?: FeatureValueDetails[];
300300
}
301301
export enum FeatureTypeType {
302-
Boolean = 'boolean',
303-
String = 'string',
304-
Number = 'number',
302+
Boolean = "boolean",
303+
String = "string",
304+
Number = "number"
305305
}
306306
export interface FeatureNumericRange {
307307
// Defines the end of the range ( inclusive ), from the previous, or 0;
@@ -338,10 +338,10 @@ namespace OpenAPI2 {
338338
provider: Provider;
339339
}
340340
export enum ExpandedProductType {
341-
Product = 'product',
341+
Product = "product"
342342
}
343343
export enum ExpandedProductVersion {
344-
Version1 = 1,
344+
Version1 = 1
345345
}
346346
export interface ExpandedPlanBody extends PlanBody {
347347
// An array of feature definitions for the plan, as defined on the Product.
@@ -360,10 +360,10 @@ namespace OpenAPI2 {
360360
body: ExpandedPlanBody;
361361
}
362362
export enum ExpandedPlanType {
363-
Plan = 'plan',
363+
Plan = "plan"
364364
}
365365
export enum ExpandedPlanVersion {
366-
Version1 = 1,
366+
Version1 = 1
367367
}
368368
export interface ExpandedFeature extends FeatureType {
369369
// The string value set for the feature on the plan, this should only be used if the value property is null.

Diff for: package-lock.json

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

Diff for: package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@manifoldco/swagger-to-ts",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Generate TypeScript types from Swagger OpenAPI specs",
55
"main": "dist/cjs",
66
"engines": {
@@ -34,10 +34,11 @@
3434
"build": "node scripts/build",
3535
"pregenerate": "npm run build",
3636
"generate": "node scripts/generate",
37-
"lint:js": "eslint --ignore-path .gitignore --ext .js src",
37+
"lint:js": "eslint --ignore-path .gitignore --ext .js,.ts src",
3838
"prepublish": "npm run build",
3939
"pretest": "npm run build",
40-
"test": "jest"
40+
"test": "jest",
41+
"test:coverage": "jest --coverage"
4142
},
4243
"dependencies": {
4344
"chalk": "^2.4.2",
@@ -51,6 +52,7 @@
5152
"@types/jest": "^23.3.13",
5253
"@types/js-yaml": "^3.12.0",
5354
"@types/prettier": "^1.15.2",
55+
"@typescript-eslint/parser": "^1.1.1",
5456
"eslint": "^5.12.1",
5557
"eslint-config-airbnb": "^17.1.0",
5658
"eslint-config-prettier": "^2.10.0",

Diff for: scripts/generate.js

+5-25
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
1-
const { resolve, sep } = require('path');
1+
const { resolve } = require('path');
22
const { exec } = require('child_process');
3-
const glob = require('glob');
43

54
const swaggerToTS = resolve(__dirname, '..', 'bin', 'cli.js');
65

76
// Settings
8-
const INPUT_DIR = 'spec';
9-
const OUTPUT_DIR = 'types';
7+
const INPUT = resolve(__dirname, '..', 'example', 'input.yaml');
8+
const OUTPUT = resolve(__dirname, '..', 'example', 'output.ts');
109

11-
// Methods
12-
function rename(filename) {
13-
return filename
14-
.replace(`${sep}${INPUT_DIR}${sep}`, `${sep}${OUTPUT_DIR}${sep}`)
15-
.replace(/\.ya?ml$/i, '.ts');
16-
}
17-
18-
// Initialize
19-
glob('./spec/**/*.yaml', { root: resolve(__dirname, '..') }, (error, matches) => {
20-
if (error) {
21-
console.error('No files found');
22-
return;
23-
}
24-
25-
if (typeof matches === 'string') {
26-
exec(`node ${swaggerToTS} ${matches} -o ${rename(matches)}`);
27-
return;
28-
}
29-
30-
matches.forEach(file => exec(`node ${swaggerToTS} ${file} -o ${rename(file)}`));
31-
});
10+
// Generate
11+
exec(`node ${swaggerToTS} ${INPUT} -o ${OUTPUT}`);

Diff for: src/index.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import swagger2, { Swagger2 } from './swagger-2';
22

3-
export default (spec: Swagger2, options?: { namespace?: string; version?: number }) => {
4-
const version = (options && options.version) || 2;
5-
const namespace = (options && options.namespace) || `OpenAPI${version}`;
3+
export default function(
4+
spec: Swagger2,
5+
options?: { output?: string; namespace?: string; swagger?: number }
6+
) {
7+
const swagger = (options && options.swagger) || 2;
8+
const namespace = (options && options.namespace) || `OpenAPI${swagger}`;
69

7-
if (version === 1 || version === 3) {
8-
console.error('That version is not supported');
9-
return;
10+
if (swagger !== 2) {
11+
throw new Error(`Swagger version ${swagger} is not supported`);
1012
}
1113

1214
return swagger2(spec, namespace);
13-
};
15+
}

0 commit comments

Comments
 (0)