use swagger-js to fetch and parse a spec from url

This commit is contained in:
Matthias Kretschmann 2018-11-28 12:19:11 +01:00
parent efbac73ee9
commit ae7e16a932
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 85 additions and 36 deletions

View File

@ -1,5 +1,8 @@
/* eslint-disable no-console */
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const Swagger = require('swagger-client')
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
@ -33,6 +36,26 @@ exports.onCreateNode = ({ node, getNode, actions }) => {
}
}
// https://github.com/swagger-api/swagger-js
const getSpec = async () => {
const spec = await Swagger(
'http://petstore.swagger.io/v2/swagger.json'
).then(client => {
return client.spec // The resolved spec
// client.originalSpec // In case you need it
// client.errors // Any resolver errors
// Tags interface
// client.apis.pet.addPet({id: 1, name: "bobby"}).then(...)
// TryItOut Executor, with the `spec` already provided
// client.execute({operationId: 'addPet', parameters: {id: 1, name: "bobby") }).then(...)
})
return spec
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
@ -76,9 +99,8 @@ exports.createPages = ({ graphql, actions }) => {
}
}
`
).then(result => {
).then(async result => {
if (result.errors) {
/* eslint-disable-next-line no-console */
console.log(result.errors)
reject(result.errors)
}
@ -86,7 +108,9 @@ exports.createPages = ({ graphql, actions }) => {
const docTemplate = path.resolve('./src/templates/Doc.jsx')
const posts = result.data.allMarkdownRemark.edges
//
// Create Doc pages
//
posts.forEach(post => {
createPage({
path: `${post.node.fields.slug}`,
@ -98,7 +122,9 @@ exports.createPages = ({ graphql, actions }) => {
})
})
//
// Create pages from dev-ocean contents
//
const postsDevOcean = result.data.devOceanDocs.edges
postsDevOcean
@ -122,34 +148,53 @@ exports.createPages = ({ graphql, actions }) => {
})
})
//
// Create pages from swagger json files
//
const apiSwaggerTemplate = path.resolve(
'./src/templates/ApiSwagger.jsx'
)
const aquariusSpecs = require('./data/aquarius.json')
const aquariusSlug = '/api/aquarius/'
createPage({
path: aquariusSlug,
component: apiSwaggerTemplate,
context: {
slug: aquariusSlug,
api: aquariusSpecs
}
})
const brizoSpecs = require('./data/brizo.json')
const brizoSlug = '/api/brizo/'
createPage({
path: brizoSlug,
component: apiSwaggerTemplate,
context: {
slug: brizoSlug,
api: brizoSpecs
}
})
try {
const spec = await getSpec()
createPage({
path: brizoSlug,
component: apiSwaggerTemplate,
context: {
slug: brizoSlug,
api: spec
}
})
} catch (error) {
console.log(error)
}
// const aquariusSpecs = require('./data/aquarius.json')
// const aquariusSlug = '/api/aquarius/'
// createPage({
// path: aquariusSlug,
// component: apiSwaggerTemplate,
// context: {
// slug: aquariusSlug,
// api: aquariusSpecs
// }
// })
// const brizoSpecs = require('./data/brizo.json')
// const brizoSlug = '/api/brizo/'
// createPage({
// path: brizoSlug,
// component: apiSwaggerTemplate,
// context: {
// slug: brizoSlug,
// api: brizoSpecs
// }
// })
resolve()
})

View File

@ -59,7 +59,8 @@
"react-helmet": "^5.2.0",
"rehype-react": "^3.1.0",
"remark": "^10.0.1",
"remark-react": "^5.0.0"
"remark-react": "^5.0.0",
"swagger-client": "^3.8.22"
},
"devDependencies": {
"@svgr/webpack": "^4.1.0",

View File

@ -9,7 +9,7 @@ import Sidebar from '../components/Sidebar'
import DocHeader from '../components/DocHeader'
import SEO from '../components/Seo'
import stylesDoc from './Doc.module.scss'
import styles from './ApiSwagger.module.scss'
// import styles from './ApiSwagger.module.scss'
export default class ApiSwaggerTemplate extends Component {
static propTypes = {
@ -21,7 +21,8 @@ export default class ApiSwaggerTemplate extends Component {
render() {
const { location, data, pageContext } = this.props
const sections = data.allSectionsYaml.edges
const { title, description } = pageContext.api.info
const { api } = pageContext
const { title, description, version } = api.info
// output section title as defined in sections.yml
const sectionTitle = sections.map(({ node }) => {
@ -57,16 +58,18 @@ export default class ApiSwaggerTemplate extends Component {
/>
</aside>
<article className={stylesDoc.main}>
<DocHeader title={title} />
<DocHeader
title={title}
description={description}
/>
{Object.keys(pageContext.api.paths).map(
path => (
<div key={path} className={styles.path}>
<h2>{path}</h2>
{path.get && <h3>GET</h3>}
</div>
)
)}
{version}
{Object.keys(api.paths).map(key => (
<>
<h2 key={key}>{key}</h2>
</>
))}
</article>
</main>
</Content>
@ -76,7 +79,7 @@ export default class ApiSwaggerTemplate extends Component {
}
}
export const pageQuery = graphql`
export const apiSwaggerQuery = graphql`
query {
allSectionsYaml {
edges {