Skip to content

Commit

Permalink
fix: Fix network switcher (#656)
Browse files Browse the repository at this point in the history
* Fix network switcher

* Update generation script to the latest version of swagger

* Minor fixes

* Generate missing networks
  • Loading branch information
louis-md authored Dec 5, 2024
1 parent 8052548 commit 4bfacac
Show file tree
Hide file tree
Showing 11 changed files with 5,483 additions and 5,360 deletions.
18 changes: 11 additions & 7 deletions .github/scripts/generateApiReference.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const { capitalize } = require('lodash')
const YAML = require('yaml')

const jsonFile = require('../../components/ApiReference/mainnet-swagger.json')
const pathsMetadata = require('../../components/ApiReference/paths-metadata.json')
Expand Down Expand Up @@ -201,7 +202,7 @@ const generateSampleApiResponse = async (
const slugify = (text: string) => text?.replace?.(/ /g, '-').replace(/\//g, '-')
const resolveRef = (ref: string) => {
const refName = ref.split('/').pop()
return { refName, ...jsonFile.definitions[refName as string] }
return { refName, ...jsonFile.components.schemas[refName as string] }
}

const resolveRefs = (obj: any) => {
Expand Down Expand Up @@ -243,8 +244,9 @@ const addMethodContext = (json: any) => ({
})

const getApiJson = async (url: string) => {
const response = await fetch(url + '/?format=openapi')
const json = await response.json()
const response = await fetch(url + '/schema/')
const yaml = await response.text()
const json = YAML.parse(yaml)
const withContext = addMethodContext(json)
fs.writeFileSync(
'./components/ApiReference/mainnet-swagger.json',
Expand Down Expand Up @@ -298,7 +300,7 @@ const generateMethodContent = (path: string, method: string) => {
method === 'get'
? undefined
: _method.parameters
.filter((p: any) => p.in === 'body')
?.filter((p: any) => p.in === 'body')
.map((p: any) => p.schema?.properties)
.reduce((acc: any, obj: any) => {
for (const key in obj) {
Expand All @@ -317,11 +319,11 @@ const generateMethodContent = (path: string, method: string) => {
// It is planned to move this into a separate script.
// generateSampleApiResponse(path, pathWithParams + query, method, requestBody)

const codeBlockWithinDescription = _method.description.match(
const codeBlockWithinDescription = _method.description?.match(
/```[a-z]*\n[\s\S]*?\n```/
)?.[0]
const description = _method.description
.replace(codeBlockWithinDescription, '___insert code block___')
?.replace(codeBlockWithinDescription, '___insert code block___')
.replace(/{/g, '\\{')
.replace(/}/g, '\\}')
.replace(/(?<=\n)-/g, '\n\\-')
Expand Down Expand Up @@ -464,7 +466,9 @@ const main = async () => {
await getApiJson('https://safe-transaction-mainnet.safe.global')
txServiceNetworks.forEach(
async (network: { chainId: string; txServiceUrl: string }) => {
const networkName = network.txServiceUrl.split('-')[2].split('.')[0]
const networkName = network.txServiceUrl
.replace('https://safe-transaction-', '')
.split('.')[0]
fs.writeFileSync(
`./pages/core-api/transaction-service-reference/${networkName}.mdx`,
`
Expand Down
32 changes: 21 additions & 11 deletions components/ApiReference/Network.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,26 @@ const NetworkSwitcher: React.FC = () => {
}}
>
{transactionServiceUrls.map(url => (
<MenuItem key={url} value={url}>
<Link
href={
'/core-api/transaction-service-reference/' +
url.split('-')[2].split('.')[0]
}
>
{capitalize(url.split('-')[2].split('.')[0])}
</Link>
</MenuItem>
<Link
// @ts-expect-error - value is not a valid prop for Link
value={url}
key={url}
href={
'/core-api/transaction-service-reference/' +
url
.replace('https://safe-transaction-', '')
.replace('.safe.global', '')
}
>
<MenuItem>
{capitalize(
url
.replace('https://safe-transaction-', '')
.replace('.safe.global', '')
.replace('-', ' ')
)}
</MenuItem>
</Link>
))}
</Select>
<Grid mr={1} my={2} sx={{ width: ['100%', '100%', 'auto'] }} item>
Expand All @@ -112,7 +122,7 @@ const NetworkSwitcher: React.FC = () => {
</Grid>
<Grid sx={{ width: '100%' }} item mr={1}>
<Link
href={`${network}?format=openapi`}
href={network + '/schema'}
target='_blank'
rel='noopener noreferrer'
>
Expand Down
3 changes: 1 addition & 2 deletions components/ApiReference/Path.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const Path: React.FC<{ path: string; method: string }> = ({ path, method }) => {
overflow: 'auto'
}}
>
/api
{path.split('/').map((p, i) => {
const isParam = p.startsWith('{')
return (
Expand All @@ -54,7 +53,7 @@ const Path: React.FC<{ path: string; method: string }> = ({ path, method }) => {
mt: 0.6
}}
>
<CopyToClipboard getValue={() => `${network}/api${path}`} />
<CopyToClipboard getValue={() => `${network}${path}`} />
</Box>
</Grid>
)
Expand Down
Loading

0 comments on commit 4bfacac

Please sign in to comment.