Last active
July 2, 2018 02:31
-
-
Save hideokamoto/3bf8c6368322bc4eb66b426356b8dd6a to your computer and use it in GitHub Desktop.
Swaggerの定義情報を読むスクリプト。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
React Router Path: /v1/domains/{:domain} | |
API Path: PUT /v1/domains/{domain} | |
Parameters | |
name: domain | |
in: path | |
required: true | |
type: string | |
React Router Path: /v1/domains/{:domain} | |
API Path: POST /v1/domains/{domain} | |
Parameters | |
name: domain | |
in: path | |
required: true | |
type: string | |
name: request | |
in: body | |
required: false | |
schema: | |
owner: string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const SwaggerParser = require('swagger-parser') | |
const filePath = __dirname + "/swagger.yaml" | |
const genReactRouterPath = (basePath, path) => { | |
if (!/\{/.test(path)) return `${basePath}${path}` | |
const newpath = path.replace(/\{/, '{:') | |
return `${basePath}${newpath}` | |
} | |
SwaggerParser.dereference(filePath) | |
.then((data) => { | |
console.log(`Swagger: ${data.swagger}`) | |
console.log(`Info:`) | |
Object.keys(data.info).forEach(key => console.log(`${data.info[key]}\n`)) | |
const { basePath, paths } = data | |
Object.keys(paths).forEach(key => { | |
const routePath = genReactRouterPath(basePath, key) | |
const routes = paths[key] | |
console.log(`React Router Path: ${routePath}`) | |
Object.keys(routes).forEach(method => { | |
console.log(`API Path: ${method.toUpperCase()}${basePath}${key}`) | |
const item = routes[method] | |
const { parameters } = item | |
if (!parameters || parameters.length === 0) return; | |
console.log('Parameters') | |
parameters.forEach(param => { | |
Object.keys(param).forEach(j => { | |
const data = param[j] | |
if (j === 'name') { | |
console.log(` ${j}: ${data}`) | |
} else if (j === 'schema') { | |
console.log(' schema:') | |
const props = data.properties | |
Object.keys(props).forEach(k => { | |
const schema = props[k] | |
console.log(` ${k}: ${schema.type}`) | |
}) | |
} else { | |
console.log(` ${j}: ${data}`) | |
} | |
}) | |
}) | |
console.log('\n') | |
}) | |
console.log('\n') | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment