Skip to content

Commit

Permalink
Merge pull request #517 from friej715/jane/api-sandbox
Browse files Browse the repository at this point in the history
feat(docs): API sandbox
  • Loading branch information
kevee authored Apr 30, 2020
2 parents d0042d2 + 608408f commit 589b278
Show file tree
Hide file tree
Showing 8 changed files with 1,086 additions and 1 deletion.
16 changes: 15 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
exports.onCreateWebpackConfig = ({ stage, actions, loaders, getConfig }) => {
const { setWebpackConfig } = actions
if (stage === 'build-javascript') {
const config = getConfig()
const miniCssExtractPlugin = config.plugins.find(
Expand All @@ -89,4 +90,17 @@ exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
}
actions.replaceWebpackConfig(config)
}

if (stage === 'build-html') {
setWebpackConfig({
module: {
rules: [
{
test: /swagger-ui/,
use: loaders.null(),
},
],
},
})
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"react-helmet": "^5.2.1",
"react-typography": "^0.16.19",
"slugify": "^1.4.0",
"swagger-ui": "^3.25.0",
"typography": "^0.16.19",
"understory": "^1.11.0"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Components : Common: Swagger sandbox renders correctly 1`] = `
<div
id="swaggerWrapper"
/>
`;
19 changes: 19 additions & 0 deletions src/__tests__/components/common/swagger-sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react'
import renderer from 'react-test-renderer'
import SwaggerUI from 'swagger-ui'
import SwaggerSandbox from '../../../components/common/swagger-sandbox'

beforeEach(() => {
window.SwaggerUI = SwaggerUI
})

describe('Components : Common: Swagger sandbox', () => {
it('renders correctly', () => {
const tree = renderer.create(<SwaggerSandbox />).toJSON()
expect(tree).toMatchSnapshot()
})
})

afterEach(() => {
delete window.SwaggerUI
})
24 changes: 24 additions & 0 deletions src/components/common/swagger-sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React, { useEffect, useRef } from 'react'
import 'swagger-ui/dist/swagger-ui.css'
import './swagger-sandbox.scss'
import SwaggerUI from 'swagger-ui'

const SwaggerSandbox = () => {
const swaggerRef = useRef(null)

useEffect(() => {
if (typeof window === 'undefined') {
return
}
SwaggerUI({
domNode: swaggerRef.current,
url: '/api-docs/COVID-tracking-endpoints-1.0-docs.json',
defaultModelExpandDepth: 10,
docExpansion: 'list',
})
}, [])

return <div ref={swaggerRef} id="swaggerWrapper" />
}

export default SwaggerSandbox
114 changes: 114 additions & 0 deletions src/components/common/swagger-sandbox.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@import '../../scss/colors.module.scss';
@import '../../scss/breakpoints.module.scss';

#swaggerWrapper {
.swagger-ui {
pre.microlight {
line-height: 1rem;
}

.wrapper {
padding: 0px;
}

.download-contents {
font-size: 0.7rem;
line-height: 1.2rem;
}

.model {
border:0px;

td, tr {
border-top: none;
border-bottom: none;
padding-top:20px;
padding-bottom:20px;
}

tbody tr {
border-top:1px solid rgb(200, 200, 200);
}
}

table, th, td {
border-color: rgb(175, 175, 175);
border-left: none;
border-right: none;
}

td {
font-size: 0.8rem;
}

tr {
line-height: 1.5rem;
}

ul {
margin: auto;
}

.url {
display: none;
}

.scheme-container {
box-shadow: none;
padding-top: 0px;
margin-top: 0px;
}

select.content-type {
border-color: $color-plum-600;
}

.opblock-title span:after,
.opblock-summary-method {
background-color: $color-plum-600;
}

.btn.execute {
background-color: $color-plum-600;
border-color: $color-plum-600;
}

.btn.execute:active,
.btn.execute:hover {
background-color: $color-plum-700;
}

.response-control-media-type__accept-message,
.model .prop-type {
color: $color-plum-600;
}

.opblock-summary,
.opblock-get {
background-color: $color-plum-200;
border-color: $color-plum-300;
border-radius: 2px;
}

@media (max-width: $viewport-ms) {
.responses-table,
.model {
table-layout: fixed;
}
.col_header.response-col_description {
width: 55%;
}

.model td {
display: block;
padding: 0px;
max-width: 100%;
}

.model td:nth-child(2) {
margin-bottom: 30px;
border-bottom: 0px;
}
}
}
}
36 changes: 36 additions & 0 deletions src/pages/data/api-sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react'
import { graphql } from 'gatsby'
import SwaggerSandbox from '../../components/common/swagger-sandbox'
import Layout from '../../components/layout'

export default ({ data }) => (
<Layout
title="API Sandbox"
navigation={data.allContentfulNavigationGroup.edges[0].node.pages}
>
Below, you can explore each of our endpoints, try out API requests, and
learn about our schema.
<SwaggerSandbox />
</Layout>
)

export const query = graphql`
query {
allContentfulNavigationGroup(filter: { slug: { eq: "data" } }) {
edges {
node {
pages {
... on ContentfulPage {
title
link: slug
}
... on ContentfulNavigationLink {
title
link: url
}
}
}
}
}
}
`
Loading

0 comments on commit 589b278

Please sign in to comment.