mirror of
https://github.com/LBRYFoundation/lbry-tech.git
synced 2025-03-04 06:57:53 +00:00
create api docs
This commit is contained in:
parent
a208780abf
commit
0dec5b59e8
6 changed files with 276 additions and 35 deletions
61
src/components/APIArguments.astro
Normal file
61
src/components/APIArguments.astro
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
---
|
||||||
|
const {args} = Astro.props;
|
||||||
|
---
|
||||||
|
|
||||||
|
{(args && args.length) ? (
|
||||||
|
<h3>Arguments</h3>
|
||||||
|
<table class="arguments">
|
||||||
|
<tbody>
|
||||||
|
{args.map(arg=>(
|
||||||
|
<tr class="argument">
|
||||||
|
<th>
|
||||||
|
<strong>{arg.name}</strong>
|
||||||
|
<br/>
|
||||||
|
<span>
|
||||||
|
{!arg.is_required && (
|
||||||
|
"optional "
|
||||||
|
)}
|
||||||
|
{arg.type}
|
||||||
|
</span>
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
<span>{arg.description}</span>
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
) : ''}
|
||||||
|
<style>
|
||||||
|
.arguments {
|
||||||
|
border: 1px solid var(--tertiary-background);
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .left, .arguments .right {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .argument {
|
||||||
|
background-color: var(--tertiary-background);
|
||||||
|
border-bottom: 1px solid var(--tertiary-background);
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .argument th {
|
||||||
|
text-align: left;
|
||||||
|
padding: 0.25rem 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .argument span {
|
||||||
|
text-align: left;
|
||||||
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .argument .left, .arguments .argument .right {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arguments .argument:nth-child(2n) {
|
||||||
|
background-color: var(--secondary-background);
|
||||||
|
}
|
||||||
|
</style>
|
71
src/components/APIExamples.astro
Normal file
71
src/components/APIExamples.astro
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
---
|
||||||
|
import { Code } from "astro/components";
|
||||||
|
|
||||||
|
const {examples, cmd} = Astro.props;
|
||||||
|
|
||||||
|
if (!examples) return;
|
||||||
|
|
||||||
|
// Treat title and output as metadata and remove from examples
|
||||||
|
const title = examples.title;
|
||||||
|
const output = examples.output;
|
||||||
|
delete examples.title;
|
||||||
|
delete examples.output;
|
||||||
|
|
||||||
|
function setExample(example, cmd){
|
||||||
|
console.log(example, cmd);
|
||||||
|
document.body.classList.toggle(example);
|
||||||
|
}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
<div class="examples">
|
||||||
|
{examples ? (
|
||||||
|
<div>
|
||||||
|
<h3>Examples</h3>
|
||||||
|
<h4>{title}</h4>
|
||||||
|
{Object.keys(examples).map(example=>(
|
||||||
|
<button data-example={example}>{example}</button>
|
||||||
|
))}
|
||||||
|
{Object.keys(examples).map(example=>(
|
||||||
|
<div class:list={["example", cmd, example]}>
|
||||||
|
<p>{example}</p>
|
||||||
|
<Code lang={"bash"} code={`${examples[example]}`} />
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<script define:vars={{examples, cmd}}>
|
||||||
|
|
||||||
|
document.querySelectorAll(`.${cmd} button`).forEach(btn=>{
|
||||||
|
btn.addEventListener('click', (e)=>{
|
||||||
|
Object.keys(examples).map(example=>{
|
||||||
|
document.body.classList.remove(example);
|
||||||
|
});
|
||||||
|
document.body.classList.add(btn.getAttribute('data-example'));
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
|
) : ''}
|
||||||
|
</div>
|
||||||
|
<style>
|
||||||
|
.example {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
body:not(.curl, .lbrynet, .python) .examples .example:first-of-type {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.curl .example.curl {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.lbrynet .example.lbrynet {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.python .example.python {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
</style>
|
|
@ -8,6 +8,7 @@ const links = [
|
||||||
{ name: "Overview", href: "/overview" },
|
{ name: "Overview", href: "/overview" },
|
||||||
{ name: "Playground", href: "/playground" },
|
{ name: "Playground", href: "/playground" },
|
||||||
{ name: "Resources", href: "/resources"},
|
{ name: "Resources", href: "/resources"},
|
||||||
|
{ name: "API", href: "/api"},
|
||||||
{ name: "Tutorials", href: "/tutorials"},
|
{ name: "Tutorials", href: "/tutorials"},
|
||||||
{ name: "Community", href: "/community"}
|
{ name: "Community", href: "/community"}
|
||||||
]
|
]
|
||||||
|
|
|
@ -88,4 +88,9 @@ export const REDIRECTS = {
|
||||||
"/specification": "/spec",
|
"/specification": "/spec",
|
||||||
"/tour": "/playground",
|
"/tour": "/playground",
|
||||||
"/whitepaper": "/spec"
|
"/whitepaper": "/spec"
|
||||||
|
}
|
||||||
|
|
||||||
|
export const API_REPOS = {
|
||||||
|
"lbry-sdk": "docs/api.json",
|
||||||
|
"lbrycrd": "contrib/devtools/generated/api_v1.json"
|
||||||
}
|
}
|
19
src/pages/api.astro
Normal file
19
src/pages/api.astro
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
---
|
||||||
|
import Layout from '../layouts/Layout.astro';
|
||||||
|
|
||||||
|
import * as config from "../config.js";
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
<Layout title="API">
|
||||||
|
<div class="wrapper">
|
||||||
|
<ul>
|
||||||
|
{Object.keys(config.API_REPOS).map(repo=>(
|
||||||
|
<li>
|
||||||
|
<a href={`/api/${repo}`}>{repo}</a>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</Layout>
|
|
@ -1,14 +1,15 @@
|
||||||
---
|
---
|
||||||
import Layout from '../../layouts/Layout.astro';
|
import Layout from '../../layouts/Layout.astro';
|
||||||
|
import TableOfContents from '../../components/TableOfContents.astro';
|
||||||
|
import APIArguments from '../../components/APIArguments.astro';
|
||||||
|
import APIExamples from '../../components/APIExamples.astro';
|
||||||
|
|
||||||
import * as config from "../../config.js";
|
import * as config from "../../config.js";
|
||||||
import { Code } from 'astro:components';
|
import { Code } from 'astro:components';
|
||||||
|
|
||||||
export function getStaticPaths() {
|
export function getStaticPaths() {
|
||||||
const repos = [
|
|
||||||
"lbry-sdk",
|
|
||||||
]
|
|
||||||
|
|
||||||
const paths = repos.map(repo=>{
|
const paths = Object.keys(config.API_REPOS).map(repo=>{
|
||||||
return {params: {repo}};
|
return {params: {repo}};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -20,44 +21,127 @@ const { repo } = Astro.params;
|
||||||
const tags = (await (await fetch(`https://data.jsdelivr.com/v1/packages/gh/${config.GITHUB_ORG}/${repo}`)).json()).versions;
|
const tags = (await (await fetch(`https://data.jsdelivr.com/v1/packages/gh/${config.GITHUB_ORG}/${repo}`)).json()).versions;
|
||||||
tags.unshift({version: "master"});
|
tags.unshift({version: "master"});
|
||||||
|
|
||||||
const docs = (await (await fetch(`https://cdn.jsdelivr.net/gh/${config.GITHUB_ORG}/${repo}/docs/api.json`)).json());
|
const tag = tags[0];
|
||||||
console.log(docs);
|
|
||||||
|
|
||||||
|
let docs = (await (await fetch(`https://cdn.jsdelivr.net/gh/${config.GITHUB_ORG}/${repo}@${tag.version}/${config.API_REPOS[repo]}`)).json());
|
||||||
|
|
||||||
|
|
||||||
|
// Convert lbrycrd docs to the same structure as lbry-sdk
|
||||||
|
if (repo === 'lbrycrd') {
|
||||||
|
const crd = {};
|
||||||
|
for (let i in docs) {
|
||||||
|
if (!crd[docs[i].namespace]) crd[docs[i].namespace] = {
|
||||||
|
doc: docs[i].namespace,
|
||||||
|
commands: []
|
||||||
|
};
|
||||||
|
crd[docs[i].namespace].commands.push(docs[i]);
|
||||||
|
console.log(docs[i]);
|
||||||
|
|
||||||
|
//docs.main[i].command = docs.main[i].name;
|
||||||
|
}
|
||||||
|
docs = crd;
|
||||||
|
}
|
||||||
|
|
||||||
|
const headings: Array<object> = [];
|
||||||
|
|
||||||
|
// Generate headings
|
||||||
|
Object.keys(docs).forEach(group=>{
|
||||||
|
headings.push({
|
||||||
|
slug: group,
|
||||||
|
text: group.charAt(0).toUpperCase() + group.substr(1).toLowerCase(),
|
||||||
|
depth: 2
|
||||||
|
});
|
||||||
|
|
||||||
|
docs[group].commands.forEach(cmd=>{
|
||||||
|
headings.push({
|
||||||
|
slug: cmd.name,
|
||||||
|
text: cmd.name,
|
||||||
|
depth: 3
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
||||||
<Layout title={repo}>
|
<Layout title={repo}>
|
||||||
<div>Looking at repo: {repo}!</div>
|
<div class="wrapper">
|
||||||
<div class="sidebar">
|
|
||||||
<ul>
|
<TableOfContents headings={headings} />
|
||||||
{Object.keys(docs).map(header=>(
|
<select>
|
||||||
<li>
|
{tags.map(tag=>(
|
||||||
<a href={`#${header}`}>{header}</a>
|
<option>{tag.version}</option>
|
||||||
{docs[header].commands.map(cmd=>(
|
|
||||||
<p><a href={`#${cmd.name}`}>{cmd.name}</a></p>
|
|
||||||
))}
|
|
||||||
</li>
|
|
||||||
))}
|
))}
|
||||||
</ul>
|
</select>
|
||||||
</div>
|
<div class="main">
|
||||||
<select>
|
<h1>{repo} {tag.version}</h1>
|
||||||
{tags.map(tag=>(
|
<p>Methods and signatures provided by {repo} are documented below. To build, download, or run the daemon, see the project
|
||||||
<option>{tag.version}</option>
|
<a href={`https://github.com/${config.GITHUB_ORG}/${repo}/blob/${tag.version}/README.md`}>README</a>.
|
||||||
))}
|
</p>
|
||||||
</select>
|
<div class="commands">
|
||||||
<div>
|
{Object.keys(docs).map(header=>(
|
||||||
{Object.keys(docs).map(header=>(
|
<section>
|
||||||
<section>
|
<h1 id={header}>{header}</h1>
|
||||||
<h2 id={header}>{header}</h2>
|
{docs[header].commands.map(cmd=>(
|
||||||
{docs[header].commands.map(cmd=>(
|
<div class:list={["command", cmd.name]}>
|
||||||
<h3 id={cmd.name}>{cmd.name}</h3>
|
<h2 id={cmd.name}>{cmd.name}</h2>
|
||||||
<p>{cmd.description}</p>
|
<p>{cmd.description}</p>
|
||||||
<Code lang={"json"} code={cmd.returns} />
|
<APIArguments args={cmd.arguments}/>
|
||||||
<code>
|
<APIExamples cmd={cmd.name} examples={cmd.examples[0]}/>
|
||||||
</code>
|
<h3>Returns</h3>
|
||||||
|
<Code lang={"json"} code={`${cmd.returns || "Not available."}`} class:list={["returns"]} />
|
||||||
|
</div>
|
||||||
|
<hr/>
|
||||||
|
))}
|
||||||
|
</section>
|
||||||
))}
|
))}
|
||||||
</section>
|
</div>
|
||||||
))}
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<style>
|
||||||
|
.wrapper {
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wrapper .main {
|
||||||
|
/* width: calc(100vw - var(--sidebar-width)); */
|
||||||
|
padding-inline-end: var(--sidebar-width);
|
||||||
|
transition: 0.3s;
|
||||||
|
overflow-x: hidden;
|
||||||
|
max-width: 1750px;
|
||||||
|
width: calc(100%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.commands {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 25px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.commands section {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 25px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.commands section .command {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 1em;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<style is:inline>
|
||||||
|
.commands > section pre {
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 9px;
|
||||||
|
max-width: 80%;
|
||||||
|
background-color: var(--secondary-background) !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.commands > section pre code span {
|
||||||
|
white-space: break-spaces;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
</Layout>
|
</Layout>
|
Loading…
Reference in a new issue