Skip to content

Commit

Permalink
🎉 First pass at internal route hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Dec 22, 2020
1 parent e6599ba commit f358335
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/OpenAPIBlock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,50 @@
/** @jsx jsx */
import { css, jsx } from '@emotion/react'
import { useState } from 'react'
import { ProgressCircle } from '@adobe/react-spectrum'
import { Flex, ProgressCircle, Switch } from '@adobe/react-spectrum'
import { RedocStandalone } from 'redoc'

const hideInternalRoutes = (spec) => {
const specCopy = JSON.parse(JSON.stringify(spec))
if (typeof specCopy === 'object' && specCopy !== null && specCopy.spec) {
const paths = specCopy.spec.paths
for (const path in paths) {
for (const route in paths[path]) {
if (paths[path][route]['x-internal']) {
delete paths[path][route]
}
}
}
}
return specCopy
}

export const OpenAPIBlock = ({ specUrl, spec }) => {
const [showProgress, setShowProgress] = useState(true)
const input = specUrl ? { specUrl } : { spec }
const [hideInternal, setHideInternal] = useState(false)
const [input, setInput] = useState(specUrl ? { specUrl } : { spec })
const [internal] = useState(specUrl ? { specUrl } : { spec })
const [external] = useState(hideInternalRoutes(internal))

const toggleInternal = (hide) => {
hide ? setInput(external) : setInput(internal)
setHideInternal(hide)
}

return (
<div
css={css`
height: calc(100% - 64px);
`}
>
<Flex direction='column' gap='size-100' alignItems='flex-end'>
<div>
<Switch isSelected={hideInternal} onChange={toggleInternal}>
Hide Internal Routes
</Switch>
</div>
</Flex>

<div
css={css`
position: fixed;
Expand Down
171 changes: 171 additions & 0 deletions src/OpenAPIBlock/stories/OpenAPIBlock.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,174 @@ export const OpenAPIStory = () => (
)}
/>
)

export const OpenAPIPrivateStory = () => (
<OpenAPIBlock
spec={{
openapi: '3.0.0',
info: {
version: '1.0.0',
title: 'Swagger Petstore',
license: {
name: 'MIT'
}
},
servers: [
{
url: 'http://petstore.swagger.io/v1'
}
],
paths: {
'/pets': {
get: {
summary: 'List all pets',
operationId: 'listPets',
tags: ['pets'],
parameters: [
{
name: 'limit',
in: 'query',
description: 'How many items to return at one time (max 100)',
required: false,
schema: {
type: 'integer',
format: 'int32'
}
}
],
responses: {
200: {
description: 'A paged array of pets',
headers: {
'x-next': {
description: 'A link to the next page of responses',
schema: {
type: 'string'
}
}
},
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Pets'
}
}
}
},
default: {
description: 'unexpected error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
}
}
}
}
}
},
post: {
summary: 'Create a pet',
operationId: 'createPets',
tags: ['pets'],
responses: {
201: {
description: 'Null response'
},
default: {
description: 'unexpected error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
}
}
}
}
}
}
},
'/pets/{petId}': {
get: {
'x-internal': true,
summary: 'Info for a specific pet',
operationId: 'showPetById',
tags: ['pets'],
parameters: [
{
name: 'petId',
in: 'path',
required: true,
description: 'The id of the pet to retrieve',
schema: {
type: 'string'
}
}
],
responses: {
200: {
description: 'Expected response to a valid request',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Pet'
}
}
}
},
default: {
description: 'unexpected error',
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/Error'
}
}
}
}
}
}
}
},
components: {
schemas: {
Pet: {
type: 'object',
required: ['id', 'name'],
properties: {
id: {
type: 'integer',
format: 'int64'
},
name: {
type: 'string'
},
tag: {
type: 'string'
}
}
},
Pets: {
type: 'array',
items: {
$ref: '#/components/schemas/Pet'
}
},
Error: {
type: 'object',
required: ['code', 'message'],
properties: {
code: {
type: 'integer',
format: 'int32'
},
message: {
type: 'string'
}
}
}
}
}
}}
/>
)

0 comments on commit f358335

Please sign in to comment.