2018-11-28 12:19:11 +01:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
2018-11-07 16:13:20 +01:00
|
|
|
const path = require('path')
|
|
|
|
const { createFilePath } = require('gatsby-source-filesystem')
|
2018-11-28 12:19:11 +01:00
|
|
|
const Swagger = require('swagger-client')
|
2018-11-07 12:24:53 +01:00
|
|
|
|
2018-11-07 16:13:20 +01:00
|
|
|
exports.onCreateNode = ({ node, getNode, actions }) => {
|
|
|
|
const { createNodeField } = actions
|
|
|
|
|
2018-11-08 16:03:16 +01:00
|
|
|
if (node.internal.type === 'MarkdownRemark') {
|
2018-11-07 16:13:20 +01:00
|
|
|
const fileNode = getNode(node.parent)
|
|
|
|
const parsedFilePath = path.parse(fileNode.relativePath)
|
2018-11-14 15:17:19 +01:00
|
|
|
|
|
|
|
let slug = createFilePath({ node, getNode, basePath: 'content' })
|
|
|
|
let section = parsedFilePath.dir
|
|
|
|
|
|
|
|
if (node.frontmatter.slug) {
|
|
|
|
;({ slug } = node.frontmatter)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.frontmatter.section) {
|
|
|
|
;({ section } = node.frontmatter)
|
|
|
|
}
|
2018-11-07 16:13:20 +01:00
|
|
|
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'slug',
|
|
|
|
value: slug
|
|
|
|
})
|
|
|
|
|
|
|
|
createNodeField({
|
|
|
|
node,
|
|
|
|
name: 'section',
|
|
|
|
value: section
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-11-07 16:13:20 +01:00
|
|
|
exports.createPages = ({ graphql, actions }) => {
|
|
|
|
const { createPage } = actions
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve(
|
|
|
|
graphql(
|
|
|
|
`
|
2018-11-27 13:54:00 +01:00
|
|
|
query {
|
2018-11-09 22:10:35 +01:00
|
|
|
allMarkdownRemark(
|
|
|
|
filter: { fileAbsolutePath: { regex: "/content/" } }
|
|
|
|
) {
|
2018-11-07 16:13:20 +01:00
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
fields {
|
|
|
|
slug
|
|
|
|
section
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 13:00:32 +01:00
|
|
|
|
2018-11-16 14:52:42 +01:00
|
|
|
devOceanDocs: allMarkdownRemark(
|
2018-11-14 15:17:19 +01:00
|
|
|
filter: {
|
2018-11-15 14:46:20 +01:00
|
|
|
fileAbsolutePath: { regex: "/dev-ocean/doc/" }
|
2018-11-14 15:17:19 +01:00
|
|
|
}
|
|
|
|
) {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
fields {
|
|
|
|
slug
|
|
|
|
section
|
2018-11-14 13:00:32 +01:00
|
|
|
}
|
2018-11-14 17:02:31 +01:00
|
|
|
frontmatter {
|
|
|
|
slug
|
|
|
|
title
|
|
|
|
description
|
|
|
|
section
|
|
|
|
}
|
2018-11-14 13:00:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-07 16:13:20 +01:00
|
|
|
}
|
|
|
|
`
|
2018-11-28 12:19:11 +01:00
|
|
|
).then(async result => {
|
2018-11-07 16:13:20 +01:00
|
|
|
if (result.errors) {
|
|
|
|
console.log(result.errors)
|
|
|
|
reject(result.errors)
|
|
|
|
}
|
|
|
|
|
|
|
|
const docTemplate = path.resolve('./src/templates/Doc.jsx')
|
2018-11-14 13:00:32 +01:00
|
|
|
const posts = result.data.allMarkdownRemark.edges
|
2018-11-07 16:13:20 +01:00
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-07 16:13:20 +01:00
|
|
|
// Create Doc pages
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-07 16:13:20 +01:00
|
|
|
posts.forEach(post => {
|
|
|
|
createPage({
|
|
|
|
path: `${post.node.fields.slug}`,
|
|
|
|
component: docTemplate,
|
|
|
|
context: {
|
|
|
|
slug: post.node.fields.slug,
|
|
|
|
section: post.node.fields.section
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-16 14:52:42 +01:00
|
|
|
// Create pages from dev-ocean contents
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-16 14:52:42 +01:00
|
|
|
const postsDevOcean = result.data.devOceanDocs.edges
|
2018-11-14 13:00:32 +01:00
|
|
|
|
2018-11-16 14:52:42 +01:00
|
|
|
postsDevOcean
|
2018-11-14 17:02:31 +01:00
|
|
|
// only grab files with required frontmatter defined
|
|
|
|
.filter(
|
|
|
|
post =>
|
2018-11-16 14:52:42 +01:00
|
|
|
post.node.frontmatter &&
|
2018-11-14 17:02:31 +01:00
|
|
|
post.node.frontmatter.slug &&
|
|
|
|
post.node.frontmatter.title &&
|
|
|
|
post.node.frontmatter.description &&
|
|
|
|
post.node.frontmatter.section
|
|
|
|
)
|
|
|
|
.forEach(post => {
|
|
|
|
createPage({
|
|
|
|
path: `${post.node.fields.slug}`,
|
|
|
|
component: docTemplate,
|
|
|
|
context: {
|
|
|
|
slug: post.node.fields.slug,
|
|
|
|
section: post.node.fields.section
|
|
|
|
}
|
|
|
|
})
|
2018-11-14 15:17:19 +01:00
|
|
|
})
|
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-27 12:01:44 +01:00
|
|
|
// Create pages from swagger json files
|
2018-11-28 12:19:11 +01:00
|
|
|
//
|
2018-11-27 12:01:44 +01:00
|
|
|
const apiSwaggerTemplate = path.resolve(
|
|
|
|
'./src/templates/ApiSwagger.jsx'
|
|
|
|
)
|
|
|
|
|
2018-11-28 13:20:03 +01:00
|
|
|
const petStoreSlug = '/api/petstore/'
|
2018-11-27 12:01:44 +01:00
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
try {
|
|
|
|
const spec = await getSpec()
|
2018-11-27 13:54:00 +01:00
|
|
|
|
2018-11-28 12:19:11 +01:00
|
|
|
createPage({
|
2018-11-28 13:20:03 +01:00
|
|
|
path: petStoreSlug,
|
2018-11-28 12:19:11 +01:00
|
|
|
component: apiSwaggerTemplate,
|
|
|
|
context: {
|
2018-11-28 13:20:03 +01:00
|
|
|
slug: petStoreSlug,
|
2018-11-28 12:19:11 +01:00
|
|
|
api: spec
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error)
|
|
|
|
}
|
2018-11-27 13:54:00 +01:00
|
|
|
|
2018-11-28 13:20:03 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
2018-11-27 12:01:44 +01:00
|
|
|
|
2018-11-07 16:13:20 +01:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|