allow site to be ran without a github token #306

Closed
neb-b wants to merge 1 commit from fix-build into master
Showing only changes of commit c1c6549dd2 - Show all commits

View file

@ -1,7 +1,5 @@
"use strict"; "use strict";
// I M P O R T S // I M P O R T S
import asyncHtml from "choo-async/html"; import asyncHtml from "choo-async/html";
@ -20,24 +18,17 @@ const filePathBlockchain = "/contrib/devtools/generated/api_v1.json";
const filePathSdk = "/lbry/docs/api.json"; const filePathSdk = "/lbry/docs/api.json";
const rawGitHubBase = "https://raw.githubusercontent.com/lbryio/"; const rawGitHubBase = "https://raw.githubusercontent.com/lbryio/";
if (!process.env.GITHUB_OAUTH_TOKEN) // No point in rendering this page
throw new Error("Missing GitHub token");
const octokit = new Octokit({ const octokit = new Octokit({
auth: `token ${process.env.GITHUB_OAUTH_TOKEN}` auth: `token ${process.env.GITHUB_OAUTH_TOKEN}`
}); });
// E X P O R T // E X P O R T
export default async(state) => { export default async state => {
const { tag } = state; const { tag } = state;
const { wildcard } = state.params; const { wildcard } = state.params;
const repository = wildcard === "sdk" ? const repository = wildcard === "sdk" ? "lbry-sdk" : "lbrycrd";
"lbry-sdk" :
"lbrycrd";
state.lbry = { state.lbry = {
title: tag ? tag + " API Documentation" : "API Documentation", title: tag ? tag + " API Documentation" : "API Documentation",
@ -123,8 +114,6 @@ export default async(state) => {
} }
}; };
// H E L P E R S // H E L P E R S
function createApiContent(apiDetails) { function createApiContent(apiDetails) {
@ -133,20 +122,33 @@ function createApiContent(apiDetails) {
apiDetails.forEach(apiDetail => { apiDetails.forEach(apiDetail => {
let apiDetailsReturns = ""; let apiDetailsReturns = "";
if (apiDetail.returns) if (apiDetail.returns) apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns));
apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns));
apiContent.push(` apiContent.push(`
<div class="api-content__body"> <div class="api-content__body">
<h2 id="${apiDetail.name}">${apiDetail.name}</h2> <h2 id="${apiDetail.name}">${apiDetail.name}</h2>
<p>${apiDetail.description}</p> <p>${apiDetail.description}</p>
${apiDetail.arguments.length ? `<h3>Arguments</h3><ul class="api-content__body-arguments">${renderArguments(apiDetail.arguments).join("")}</ul>` : ""} ${
${apiDetail.returns ? `<h3>Returns</h3><pre><code>${dedent(apiDetailsReturns)}</code></pre>` : ""} apiDetail.arguments.length ?
`<h3>Arguments</h3><ul class="api-content__body-arguments">${renderArguments(
apiDetail.arguments
).join("")}</ul>` :
""
}
${
apiDetail.returns ?
`<h3>Returns</h3><pre><code>${dedent(apiDetailsReturns)}</code></pre>` :
""
}
</div> </div>
<div class="api-content__example"> <div class="api-content__example">
${apiDetail.examples && apiDetail.examples.length ? renderExamples(apiDetail.examples).join("") : `<pre><code>// example(s) for ${apiDetail.name} to come later</code></pre>`} ${
apiDetail.examples && apiDetail.examples.length ?
renderExamples(apiDetail.examples).join("") :
`<pre><code>// example(s) for ${apiDetail.name} to come later</code></pre>`
}
</div> </div>
`); `);
}); });
@ -232,7 +234,12 @@ function createSdkSidebar(apiDetails) {
apiSidebar.push(` apiSidebar.push(`
<ul class="api-toc__section"> <ul class="api-toc__section">
<li class="api-toc__title">${title}</li> <li class="api-toc__title">${title}</li>
${(commands.map(command => `<li class="api-toc__command"><a href="#${command.name}" title="Go to ${command.name} section">${command.name}</a></li>`)).join("")} ${commands
.map(
command =>
`<li class="api-toc__command"><a href="#${command.name}" title="Go to ${command.name} section">${command.name}</a></li>`
)
.join("")}
</ul> </ul>
`); `);
}); });
@ -266,7 +273,8 @@ async function getTags(repositoryName) {
tag.name !== "v0.38.0rc3" && tag.name !== "v0.38.0rc3" &&
tag.name !== "v0.38.0rc2" && tag.name !== "v0.38.0rc2" &&
tag.name !== "v0.38.0rc1" tag.name !== "v0.38.0rc1"
) tags.push(tag.name); )
tags.push(tag.name);
}); });
break; break;
@ -278,7 +286,8 @@ async function getTags(repositoryName) {
tag.name !== "v0.3.15" && tag.name !== "v0.3.15" &&
tag.name !== "v0.3-osx" && tag.name !== "v0.3-osx" &&
tag.name !== "v0.2-alpha" tag.name !== "v0.2-alpha"
) tags.push(tag.name); )
tags.push(tag.name);
}); });
break; break;
@ -293,11 +302,11 @@ async function parseApiFile({ repo, tag }) {
let apiFileLink = `${rawGitHubBase}${repo}/${tag}`; let apiFileLink = `${rawGitHubBase}${repo}/${tag}`;
switch(true) { switch(true) {
case (repo === "lbrycrd"): case repo === "lbrycrd":
apiFileLink = `${apiFileLink}${filePathBlockchain}`; apiFileLink = `${apiFileLink}${filePathBlockchain}`;
break; break;
case (repo === "lbry-sdk"): case repo === "lbry-sdk":
apiFileLink = `${apiFileLink}${filePathSdk}`; apiFileLink = `${apiFileLink}${filePathSdk}`;
break; break;
@ -317,8 +326,7 @@ async function parseApiFile({ repo, tag }) {
function renderArguments(args) { function renderArguments(args) {
const argumentContent = []; const argumentContent = [];
if (!args || args.length === 0) if (!args || args.length === 0) return argumentContent;
return argumentContent;
args.forEach(arg => { args.forEach(arg => {
argumentContent.push(` argumentContent.push(`
@ -328,7 +336,11 @@ function renderArguments(args) {
${arg.is_required === true ? "" : "<span>optional</span>"}<span>${arg.type}</span> ${arg.is_required === true ? "" : "<span>optional</span>"}<span>${arg.type}</span>
</div> </div>
<div class="right">${typeof arg.description === "string" ? arg.description.replace(/</g, "&lt;").replace(/>/g, "&gt;") : ""}</div> <div class="right">${
typeof arg.description === "string" ?
arg.description.replace(/</g, "&lt;").replace(/>/g, "&gt;") :
""
}</div>
</li> </li>
`); `);
}); });
@ -352,11 +364,15 @@ function renderExamples(args) {
${arg.lbrynet ? `<pre data-api-example-type="lbrynet"><code>${arg.lbrynet}</code></pre>` : ""} ${arg.lbrynet ? `<pre data-api-example-type="lbrynet"><code>${arg.lbrynet}</code></pre>` : ""}
${arg.python ? `<pre data-api-example-type="python"><code>${arg.python}</code></pre>` : ""} ${arg.python ? `<pre data-api-example-type="python"><code>${arg.python}</code></pre>` : ""}
${arg.output ? ` ${
arg.output ?
`
<h3>Output</h3><br/> <h3>Output</h3><br/>
<pre><code>${arg.output}</code></pre> <pre><code>${arg.output}</code></pre>
<hr/> <hr/>
` : ""} ` :
""
}
`); `);
}); });
@ -366,17 +382,14 @@ function renderExamples(args) {
function renderReturns(args) { function renderReturns(args) {
let returnContent = []; let returnContent = [];
if (!args || args.length === 0) if (!args || args.length === 0) return returnContent;
return returnContent;
returnContent = dedent(JSON.parse(JSON.stringify(args))); returnContent = dedent(JSON.parse(JSON.stringify(args)));
return returnContent; return returnContent;
} }
function renderVersionSelector(pageSlug, versions, desiredTag) { function renderVersionSelector(pageSlug, versions, desiredTag) {
const options = [ const options = ["<option disabled>Select a version</option>"];
"<option disabled>Select a version</option>"
];
let optionIndex = 0; let optionIndex = 0;
@ -384,13 +397,13 @@ function renderVersionSelector(pageSlug, versions, desiredTag) {
optionIndex++; optionIndex++;
let selectedOption = false; let selectedOption = false;
if (desiredTag && desiredTag === version) if (desiredTag && desiredTag === version) selectedOption = true;
selectedOption = true; else if (optionIndex === 1) selectedOption = true;
else if (optionIndex === 1)
selectedOption = true;
options.push( options.push(
`<option value="${pageSlug}-${version}"${selectedOption ? " selected" : ""}>${version}</option>` `<option value="${pageSlug}-${version}"${
selectedOption ? " selected" : ""
}>${version}</option>`
); );
}); });
@ -402,9 +415,15 @@ function renderCodeLanguageToggles(pageSlug) {
return [ return [
"<button class='api-content__item menu' id='toggle-menu'>menu</button>", "<button class='api-content__item menu' id='toggle-menu'>menu</button>",
!onSdkPage ? "<button class='api-content__item' id='toggle-cli' type='button'>cli</button>" : "", !onSdkPage ?
"<button class='api-content__item' id='toggle-cli' type='button'>cli</button>" :
"",
"<button class='api-content__item' id='toggle-curl' type='button'>curl</button>", "<button class='api-content__item' id='toggle-curl' type='button'>curl</button>",
onSdkPage ? "<button class='api-content__item' id='toggle-lbrynet' type='button'>lbrynet</button>" : "", onSdkPage ?
onSdkPage ? "<button class='api-content__item' id='toggle-python' type='button'>python</button>" : "" "<button class='api-content__item' id='toggle-lbrynet' type='button'>lbrynet</button>" :
"",
onSdkPage ?
"<button class='api-content__item' id='toggle-python' type='button'>python</button>" :
""
]; ];
} }