Skip to content

Commit

Permalink
fix: Add changes in API Reference (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-md authored Apr 30, 2024
1 parent eb199f3 commit ca1098f
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 102 deletions.
6 changes: 5 additions & 1 deletion .github/scripts/generateApiReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ const generateSampleApiResponse = async (
}

const slugify = text => text?.replace?.(/ /g, '-').replace(/\//g, '-')
const resolveRef = ref => jsonFile.definitions[ref.split('/').pop()]
const resolveRef = ref => {
const refName = ref.split('/').pop()
return { refName, ...jsonFile.definitions[refName] }
}

const resolveRefs = obj => {
if (typeof obj === 'object') {
for (const key in obj) {
Expand Down
56 changes: 36 additions & 20 deletions components/ApiReference/Network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ const NetworkSwitcher: React.FC = () => {
const [network, setNetwork] = useContext(NetworkContext)
return (
<>
<Grid container mt={2} mb={[8, 0]} alignItems='center'>
<Grid
container
mt={2}
mb={[8, 0]}
alignItems='center'
flexDirection={['column', 'row']}
>
<Select
value={network}
onChange={e => {
Expand All @@ -73,26 +79,36 @@ const NetworkSwitcher: React.FC = () => {
</MenuItem>
))}
</Select>
<Link href={network} target='_blank' rel='noopener noreferrer'>
<Typography variant='caption' mx={1} my={2}>
<strong style={{ color: 'white' }}>Base URL:</strong> {network}{' '}
</Typography>
</Link>
<CopyToClipboard getValue={() => `${network}`} />
<Link
href={`${network}?format=openapi`}
target='_blank'
rel='noopener noreferrer'
>
<Button
variant='contained'
color='primary'
sx={{ ml: 3 }}
endIcon={<GetAppIcon />}
<Grid mr={[1, 3]} my={1} item wrap='nowrap'>
<Link href={network} target='_blank' rel='noopener noreferrer'>
<Typography
variant='caption'
mx={1}
my={2}
textOverflow='ellipsis'
noWrap
>
<strong style={{ color: 'white' }}>Base URL:</strong> {network}{' '}
</Typography>
</Link>
<CopyToClipboard getValue={() => `${network}`} />
</Grid>
<Grid sx={{ width: ['100%', 'auto'] }} item justifyContent='flex-end'>
<Link
href={`${network}?format=openapi`}
target='_blank'
rel='noopener noreferrer'
>
Download Specs
</Button>
</Link>
<Button
variant='contained'
color='primary'
sx={{ width: ['100%', 'auto'] }}
endIcon={<GetAppIcon />}
>
Download Specs
</Button>
</Link>
</Grid>
</Grid>

<Grid container mt={2} alignItems='center'></Grid>
Expand Down
155 changes: 122 additions & 33 deletions components/ApiReference/Property.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,133 @@
import { useState } from 'react'
import Grid from '@mui/material/Grid'
import Typography from '@mui/material/Typography'
import { theme } from '../../styles/theme'
import AccordionDetails from '@mui/material/AccordionDetails'
import Accordion from '@mui/material/Accordion'
import { AccordionSummary } from '@mui/material'
import Link from '@mui/material/Link'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Property: React.FC<{ property: any, required?: boolean }> = ({
property,
required
}) => (
<Grid
container
pl={2}
my={1}
sx={{ color: ({ palette }) => palette.grey[400] }}
>
<Grid item xs={6}>
<code style={{ fontSize: '14px' }}>{property.name}</code>
{required === true && (
<>
{' - '}
<code style={{ color: theme.palette.error.main, fontSize: '14px' }}>
required
</code>
</>
}) => {
const [isExpanded, setIsExpanded] = useState(false)
const reference =
property.value?.type === 'object'
? property.value?.properties
: property.value?.type === 'array'
? property.value?.items?.properties
: null
const refName = property?.value?.refName ?? property.value?.items?.refName

return (
<Accordion
expanded={isExpanded}
disableGutters
onChange={() => {
if (
property.value?.type === 'object' ||
property.value?.type === 'array'
) {
setIsExpanded(!isExpanded)
}
}}
sx={{
border: 'none',
background: 'transparent',
color: theme.palette.grey[400],
boxShadow: 'none',
'&.Mui-expanded': {
border: 'none'
},
'&:before': {
display: 'none'
},
'& .MuiAccordionSummary-content': {
margin: 0
}
}}
>
<AccordionSummary sx={{ pl: 0 }}>
<Grid
container
pl={2}
my={1}
sx={{ color: ({ palette }) => palette.grey[400] }}
>
<Grid
container
item
xs={6}
justifyContent='flex-start'
alignItems='center'
fontSize='12px'
>
<code style={{ fontSize: '14px', marginRight: '5px' }}>
{property.name}
</code>
{required === true && (
<>
{' - '}
<code
style={{
color: theme.palette.error.main,
fontSize: '10px',
display: 'inline-block',
marginLeft: '5px'
}}
>
required
</code>
</>
)}
</Grid>
<Grid item xs={6} flexDirection='column'>
<code
style={{
fontSize: '14px'
}}
>
<Link>{refName}</Link>
{property.value?.type === 'array'
? '[]'
: property.value?.type === 'object'
? ''
: property.value?.type}
</code>
<Typography
variant='body1'
color='grey.300'
sx={{
'@media (min-width:600px)': {
fontSize: '14px'
}
}}
>
{property.value?.description}
</Typography>
</Grid>
</Grid>
</AccordionSummary>
{reference != null && (
<AccordionDetails>
{Object.entries(reference as Record<string, string>).map(
([name, value]) => (
<Property
key={name}
property={{ name, value }}
required={
property.value.required?.includes(name) ??
property.value.items.required?.includes(name)
}
/>
)
)}
</AccordionDetails>
)}
</Grid>
<Grid item xs={6} flexDirection='column'>
<code style={{ fontSize: '14px' }}>{property.value?.type}</code>
<Typography
variant='body1'
color='grey.300'
sx={{
'@media (min-width:600px)': {
fontSize: '14px'
}
}}
>
{property.value?.description}
</Typography>
</Grid>
</Grid>
)
</Accordion>
)
}

export default Property
37 changes: 21 additions & 16 deletions components/ApiReference/Response.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ const Response: React.FC<{ response: any, index: number }> = ({
type === 'object'
? response.schema?.properties
: response.schema?.items
).map(([key, value]) => ({ name: key, value }))
).map(([key, value]) => ({
name: key,
value,
required: response.schema?.required?.includes(key)
}))

return (
<>
Expand Down Expand Up @@ -102,26 +106,27 @@ const Response: React.FC<{ response: any, index: number }> = ({
>
{properties
?.filter(
property =>
!(
type === 'array' &&
(property.name === 'required' || property.name === 'type')
)
property => !(type === 'array' && property.name !== 'properties')
)
?.map?.((property, index) => {
return type === 'object' ? (
<Property key={index} property={property} />
?.map?.((property, index) =>
type === 'object' ? (
<Property
key={index}
property={property}
required={property.required}
/>
) : (
Object.entries(property.value as Record<string, unknown>)
.map(([key, value]) => ({
name: key,
value
}))
.map((property, index) => (
<Property key={index} property={property} />
.map(([name, value]) => ({ name, value }))
.map((_property, index) => (
<Property
key={index}
property={_property}
required={property.required}
/>
))
)
})}{' '}
)}{' '}
</AccordionDetails>
</Accordion>
<Hr />
Expand Down
Loading

0 comments on commit ca1098f

Please sign in to comment.