Skip to content

Commit

Permalink
implement dereferenced openapi schema
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwidlund committed May 16, 2024
1 parent 7ac0320 commit 5163714
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { CONFIG } from '@/utils/config'
import { ContentPasteOutlined } from '@mui/icons-material'
import openapiSchema from '@polar-sh/sdk/openapi'
import { OpenAPIV3_1 } from 'openapi-types'
import Button from 'polarkit/components/ui/atoms/button'
import {
Expand All @@ -13,23 +12,13 @@ import {
} from 'polarkit/components/ui/atoms/tabs'
import { useCallback, useMemo } from 'react'

const resolveSchema = (schemaName: string) => {
return (openapiSchema as unknown as OpenAPIV3_1.Document).components
?.schemas?.[schemaName]
}

export const requestBodyParameters = (
endpointMethod: OpenAPIV3_1.OperationObject,
) => {
if (endpointMethod.requestBody && !('content' in endpointMethod.requestBody))
return undefined

return resolveSchema(
// @ts-ignore
endpointMethod.requestBody?.content['application/json'].schema?.['$ref']
.split('/')
.pop(),
)
return endpointMethod.requestBody?.content['application/json'].schema
}

export const APIContainer = ({
Expand All @@ -49,8 +38,13 @@ export const APIContainer = ({
url: string,
endpoint: OpenAPIV3_1.OperationObject,
) => {
const requiredBodyParameters = requestBodyParameters(endpoint)?.properties
? Object.entries(requestBodyParameters(endpoint)?.properties ?? {})
const requiredBodyParameters = endpoint.requestBody?.content?.[
'application/json'
].schema.properties
? Object.entries(
endpoint.requestBody?.content?.['application/json'].schema
.properties ?? {},
)
.map(([key]) => ({
[key]: `<${key}>`,
}))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import openapiSchema from '@polar-sh/sdk/openapi'
import { OpenAPIV3_1 } from 'openapi-types'
import {
Tabs,
Expand All @@ -7,18 +6,6 @@ import {
TabsTrigger,
} from 'polarkit/components/ui/atoms/tabs'

export const resolveSchema = (schemaName: string) => {
return (openapiSchema as unknown as OpenAPIV3_1.Document).components
?.schemas?.[schemaName]
}

export const getResponseSchema = (response: OpenAPIV3_1.ResponseObject) => {
return resolveSchema(
// @ts-ignore
response.content?.['application/json'].schema?.['$ref'].split('/').pop(),
)
}

export const ResponseContainer = ({
responses,
}: {
Expand Down Expand Up @@ -49,8 +36,7 @@ export const ResponseContainer = ({
<TabsContent key={statusCode} value={statusCode} className="p-2 py-0">
<pre className="dark:text-polar-50 max-h-72 select-text overflow-auto p-4 font-mono text-xs leading-normal text-gray-900">
{JSON.stringify(
getResponseSchema(response as OpenAPIV3_1.ResponseObject)
?.properties,
response.content?.['application/json'].schema,
null,
2,
)}
Expand Down
17 changes: 3 additions & 14 deletions clients/apps/web/src/app/docs/api-reference/[...endpoint]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import openapiSchema from '@polar-sh/sdk/openapi'
import { OpenAPIV3_1 } from 'openapi-types'
import { PropsWithChildren, useCallback, useMemo } from 'react'
import { PropsWithChildren, useMemo } from 'react'
import { SchemaPathKey } from '../../APINavigation'
import { APIContainer } from './APIContainer'
import { ResponseContainer } from './ResponseContainer'
Expand All @@ -19,25 +19,14 @@ export default function Page({
method
] as OpenAPIV3_1.OperationObject

const resolveSchema = useCallback((schemaName: string) => {
return (openapiSchema as unknown as OpenAPIV3_1.Document).components
?.schemas?.[schemaName]
}, [])

const requestBodyParameters = useMemo(() => {
if (
endpointMethod.requestBody &&
!('content' in endpointMethod.requestBody)
)
return undefined

return resolveSchema(
// @ts-ignore
endpointMethod.requestBody?.content['application/json'].schema?.['$ref']
.split('/')
.pop(),
)
}, [endpointMethod, resolveSchema])
return endpointMethod.requestBody?.content['application/json'].schema
}, [endpointMethod])

if (!endpointMethod) return null

Expand Down
3 changes: 2 additions & 1 deletion clients/packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
"scripts": {
"download": "node scripts/generate-schema.js http://127.0.0.1:8000/openapi.json openapi/source.json openapi/updated.json",
"generate": "pnpm download && ./generate",
"build": "tsup src/index.ts --format cjs,esm --dts",
"build": "tsup src/index.ts --format cjs,esm --dts && cp -r openapi dist/openapi && node scripts/dereference-schema.js dist/openapi/source.json",
"prepublishOnly": "pnpm run build"
},
"devDependencies": {
"@stoplight/json-ref-resolver": "^3.1.6",
"tsconfig": "workspace:*",
"tsup": "^7.2.0",
"typescript": "5.3.3"
Expand Down
39 changes: 39 additions & 0 deletions clients/packages/sdk/scripts/dereference-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import refResolver from '@stoplight/json-ref-resolver'
import fs from 'fs'


const resolveRefs = async (schema) => {
const resolver = new refResolver.Resolver()
return await resolver.resolve(schema)
}

const getOpenAPISchema = async (schemaPath) => {
return new Promise((resolve, reject) => {
fs.readFile(schemaPath, 'utf8', (err, data) => {
if (err) {
reject(err)
}
resolve(JSON.parse(data))
})

})
}

const save = (filename, schema) => {
const asJson = JSON.stringify(schema, null, 4)
const written = fs.writeFileSync(filename, asJson)
return written
}

const main = async (schemaPath) => {
let schema = await getOpenAPISchema(schemaPath)
schema = (await resolveRefs(schema)).result
save(schemaPath, schema)
}

const argv = process.argv.slice(2)
if (argv.length !== 1) {
throw new Error('Args: <schemaPath>')
}
main(argv[0])

84 changes: 84 additions & 0 deletions clients/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5163714

Please sign in to comment.