Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(docs): API sandbox #517

Merged
merged 19 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const SwaggerUI = require('swagger-ui')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean that Swagger runs on every page? If do, is there a way to only load it on the pages we'll need it for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I think @superbiche might know?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I missed that. I'm going to do a test and propose a change to just load swagger on that one page.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I just pushed a commit that:

  • Uses onCreateWebpackConfig to say "Gatsby, when you're building the site with SSR, and therefore there is no window object, just chill and don't bother about Swagger."
  • Loads swagger UI in the SwaggerSandbox component.


exports.onClientEntry = () => {
window.addEventListener('load', () => {
document.body.className = document.body.className.replace(/\bno-js\b/, '')
})
}

window.SwaggerUI = SwaggerUI
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
})
27 changes: 27 additions & 0 deletions src/components/common/swagger-sandbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react'
import 'swagger-ui/dist/swagger-ui.css'
import './swagger-sandbox.scss'

class SwaggerSandbox extends React.Component {
componentDidMount() {
window.SwaggerUI({
domNode: this.swaggerWrapper,
url: '/api-docs/COVID-tracking-endpoints-1.0-docs.json',
defaultModelExpandDepth: 10,
docExpansion: 'list',
})
}

render() {
return (
<div
ref={ref => {
this.swaggerWrapper = ref
}}
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