Skip to content

Commit

Permalink
Create PoC for generating model docs from open API JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Dec 12, 2023
1 parent 44716d5 commit 59abf8a
Show file tree
Hide file tree
Showing 3 changed files with 7,929 additions and 0 deletions.
106 changes: 106 additions & 0 deletions docusaurus/video/docusaurus/docs/api/_common_/OpenApiModel.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react';
import apiJson from '../video-openapi.json';

const OpenApiModel = ({ modelName, recursive = true }) => {

const models = React.useMemo(() => {
const modelsResult = [{name: modelName, properties: []}];

for (let i = 0; i < modelsResult.length; i++) {
const model = modelsResult[i];

const schemaJson = apiJson.components.schemas[model.name];
const propertiesJson = schemaJson.properties;

model.properties = Object.keys(propertiesJson).map(key => {
const property = propertiesJson[key];

// Parse type info
let type;
let typeHref;
let displayType;
let isArray = property.type === 'array';
if (property.$ref || isArray && property.items?.$ref) {
const ref = isArray ? property.items?.$ref : property.$ref;
// Example $ref: #/components/schemas/EdgeResponse
type = ref?.split('/')?.pop() || '';
typeHref = `#${type}`
if (recursive && apiJson.components.schemas[type] && !modelsResult.find(r => r.name === type)) {
modelsResult.push({name: type, properties: []});
}
} else {
type = (isArray ? property.items?.type : property.type) || ''
}
displayType = isArray ? `${type}[]` : type;
if (property.enum) {
displayType += ` (${property.enum.join(', ')})`
}

// Parse title + description
let description = property.title;
if (property.description) {
description += ` - ${property.description}`;
}

// Parse constraints
const constraints = [];
if (schemaJson.required?.includes(key)) {
constraints.push('Required');
}
if (property.minimum !== undefined) {
constraints.push(`Minimum: ${property.minimum}`)
}
if (property.maximum !== undefined) {
constraints.push(`Maximum: ${property.maximum}`)
}

return {
name: key,
type,
displayType,
typeHref,
constraints,
description
}
});
}

return modelsResult;
}, [modelName]);

return (
<div>
{models.map((model) => (
<React.Fragment>
<h2 id={model.name}>{model.name}</h2>
<table>
<thead>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Constraints</th>
</thead>
{model.properties.map(p => {
return (
<React.Fragment>
<tr>
<td>
<code>{p.name}</code>
</td>
<td>
{p.typeHref ? <a href={p.typeHref}><code>{p.displayType}</code></a> : <code>{p.displayType}</code>}
</td>
<td>{p.description || '-'}</td>
<td>{p.constraints.join(', ') || '-'}</td>
</tr>
</React.Fragment>
);
})}
</table>
</React.Fragment>
))}
</div>
);
};

export default OpenApiModel;
5 changes: 5 additions & 0 deletions docusaurus/video/docusaurus/docs/api/basics/calls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import CallFilters from '../../../shared/video/_call-filters.mdx';
import CallMemberFilters from '../../../shared/video/_call-member-filters.mdx';
import CallSort from '../../../shared/video/_call-sort-fields.mdx';
import CallMemberSort from '../../../shared/video/_call-member-sort-fields.mdx';
import OpenApiModel from '../_common_/OpenApiModel';

## Creating calls

Expand Down Expand Up @@ -68,6 +69,10 @@ await client.call('default', 'test-outgoing-call').getOrCreate({
</TabItem>
</Tabs>

### Model

<OpenApiModel modelName={'GetOrCreateCallRequest'}></OpenApiModel>

## Updating calls

<UpdateCall />
Expand Down
Loading

0 comments on commit 59abf8a

Please sign in to comment.