|
| 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. |
| 3 | + |
| 4 | +### Installation |
| 5 | + |
| 6 | +```shell |
| 7 | +npm i --save-dev @manifoldco/swagger-to-ts |
| 8 | +``` |
| 9 | + |
| 10 | +### Generating |
| 11 | + |
| 12 | +#### From Swagger JSON |
| 13 | + |
| 14 | +```js |
| 15 | +const { readFileSync, writeFileSync } = require('fs'); |
| 16 | +const swaggerToTS = require('swagger-to-ts'); |
| 17 | + |
| 18 | +const file = './spec/swagger.json'; |
| 19 | +const typeData = swaggerToTS(readFileSync(file, 'UTF-8')); |
| 20 | +writeFileSync('./types/swagger.ts'), typeData); |
| 21 | +``` |
| 22 | + |
| 23 | +#### From Swagger YAML |
| 24 | + |
| 25 | +Swagger files must be passed to `swaggerToTS()` in a JSON format, so you’ll |
| 26 | +have to configure that part yourself using [js-yaml][js-yaml] or something |
| 27 | +similar. |
| 28 | + |
| 29 | +```js |
| 30 | +const { readFileSync, writeFileSync } = require('fs'); |
| 31 | +const yaml = require('js-yaml'); |
| 32 | + |
| 33 | +const file = './spec/swagger.json'; |
| 34 | +const typeData = swaggerToTS(yaml.safeLoad(fs.readFileSync(file, 'UTF-8'))); |
| 35 | +writeFileSync('./types/swagger.ts'), typeData); |
| 36 | +``` |
| 37 | + |
| 38 | +#### Generating multiple files |
| 39 | + |
| 40 | +The [glob][glob] package is helpful in converting entire folders. The |
| 41 | +renaming & mapping is up to you! |
| 42 | + |
| 43 | +```js |
| 44 | +const { readFileSync, writeFileSync } = require('fs'); |
| 45 | +const path = require('path'); |
| 46 | +const glob = require('glob'); |
| 47 | +const swaggerToTS = require('swagger-to-ts'); |
| 48 | + |
| 49 | +const source1 = glob.sync('./swaggerspec/v1/**/*.yaml'); |
| 50 | +const source2 = glob.sync('./swaggerspec/v2/**/*.yaml'); |
| 51 | + |
| 52 | +[...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'); |
| 55 | + writeFileSync(path.resolve(__dirname, 'types', filename), typeData); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +#### Fancy Console Messages |
| 60 | + |
| 61 | +Using the [chalk][chalk] package you can spice up the console message a little: |
| 62 | + |
| 63 | +```js |
| 64 | +const { readFileSync, writeFileSync } = require('fs'); |
| 65 | +const chalk = require('chalk'); |
| 66 | +const swaggerToTS = require('swagger-to-ts'); |
| 67 | + |
| 68 | +const file = './spec/swagger.json'; |
| 69 | + |
| 70 | +console.log('🏗 Generating types…'); |
| 71 | +const timeStart = process.hrtime(); |
| 72 | + |
| 73 | +const typeData = swaggerToTS(readFileSync(file, 'UTF-8')); |
| 74 | +writeFileSync('./types/swagger.ts'), typeData); |
| 75 | + |
| 76 | +const timeEnd = process.hrtime(timeStart); |
| 77 | +console.log( |
| 78 | + chalk.green( |
| 79 | + `Done generating types in ${chalk.bold(timeEnd[0] + Math.round(timeEnd[1] / 1000000))}ms` |
| 80 | + ) |
| 81 | +); |
| 82 | +``` |
| 83 | + |
| 84 | +This will output something like the following: |
| 85 | + |
| 86 | +```shell |
| 87 | +🏗 Generating types… |
| 88 | +Done generating types in 212ms |
| 89 | +``` |
| 90 | + |
| 91 | +### Using in TypeScript project |
| 92 | + |
| 93 | +It’s recommended to name the file `*.ts` and `import` the definitions. `*.d.ts` |
| 94 | +can’t be imported; they’re meant to be shipped alongside modules. |
| 95 | + |
| 96 | +```js |
| 97 | +import { User } from '../types/swagger'; |
| 98 | + |
| 99 | +const logIn = (user: User) => { |
| 100 | + // … |
| 101 | +``` |
| 102 | +
|
| 103 | +TypeScript would much rather have you ship `*.d.ts` files as part of an |
| 104 | +existing package, but sometimes that’s not possible when it comes to Swagger |
| 105 | +specs. `import`-ing everything like this isn’t ideal, but it’s better than |
| 106 | +not using static typing at all! |
| 107 | +
|
| 108 | +### Node Scripts |
| 109 | +
|
| 110 | +Setting this up to auto-run in a Node script is ideal. For instance, if you |
| 111 | +ship Swagger specs as part of a node package, and you want to regenerate the |
| 112 | +types on `postinstall`, you can specify that in your `package.json`: |
| 113 | +
|
| 114 | +```js |
| 115 | + "scripts": { |
| 116 | + "generate:types": "node scripts/generateTypesFromSwagger", |
| 117 | + "postinstall": "npm run generate:types", |
| 118 | + }, |
| 119 | +``` |
| 120 | +
|
| 121 | +In this example, `scripts/generateTypesFromSwagger` is a JS file you’ve manually |
| 122 | +created from one of the examples above, customized to fit your needs. |
| 123 | +
|
| 124 | +[chalk]: https://www.npmjs.com/package/chalk |
| 125 | +[glob]: https://npmjs.com/glob |
| 126 | +[js-yaml]: https://npmjs.com/js-yaml |
| 127 | +[prettier]: https://npmjs.com/prettier |
0 commit comments