diff --git a/gatsby-node.js b/gatsby-node.js index e4b2edea..526b79b5 100755 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -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() }) diff --git a/package.json b/package.json index 7c410eb9..e74920fc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/templates/ApiSwagger.jsx b/src/templates/ApiSwagger.jsx index bb601671..0a483fba 100644 --- a/src/templates/ApiSwagger.jsx +++ b/src/templates/ApiSwagger.jsx @@ -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 { />
- + - {Object.keys(pageContext.api.paths).map( - path => ( -
-

{path}

- {path.get &&

GET

} -
- ) - )} + {version} + + {Object.keys(api.paths).map(key => ( + <> +

{key}

+ + ))}
@@ -76,7 +79,7 @@ export default class ApiSwaggerTemplate extends Component { } } -export const pageQuery = graphql` +export const apiSwaggerQuery = graphql` query { allSectionsYaml { edges {