From c1c6549dd27f1b1ebba72ac3bc90837da81ac63e Mon Sep 17 00:00:00 2001 From: Sean Yesmunt Date: Mon, 28 Oct 2019 00:36:15 -0400 Subject: [PATCH] allow site to be ran without a github token --- app/views/api.js | 101 ++++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 41 deletions(-) diff --git a/app/views/api.js b/app/views/api.js index dc748f5..de75432 100644 --- a/app/views/api.js +++ b/app/views/api.js @@ -1,7 +1,5 @@ "use strict"; - - // I M P O R T S 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 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({ auth: `token ${process.env.GITHUB_OAUTH_TOKEN}` }); - - // E X P O R T -export default async(state) => { +export default async state => { const { tag } = state; const { wildcard } = state.params; - const repository = wildcard === "sdk" ? - "lbry-sdk" : - "lbrycrd"; + const repository = wildcard === "sdk" ? "lbry-sdk" : "lbrycrd"; state.lbry = { title: tag ? tag + " API Documentation" : "API Documentation", @@ -123,8 +114,6 @@ export default async(state) => { } }; - - // H E L P E R S function createApiContent(apiDetails) { @@ -133,20 +122,33 @@ function createApiContent(apiDetails) { apiDetails.forEach(apiDetail => { let apiDetailsReturns = ""; - if (apiDetail.returns) - apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns)); + if (apiDetail.returns) apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns)); apiContent.push(`

${apiDetail.name}

${apiDetail.description}

- ${apiDetail.arguments.length ? `

Arguments

` : ""} - ${apiDetail.returns ? `

Returns

${dedent(apiDetailsReturns)}
` : ""} + ${ + apiDetail.arguments.length ? + `

Arguments

` : + "" +} + ${ + apiDetail.returns ? + `

Returns

${dedent(apiDetailsReturns)}
` : + "" +}
- ${apiDetail.examples && apiDetail.examples.length ? renderExamples(apiDetail.examples).join("") : `
// example(s) for ${apiDetail.name} to come later
`} + ${ + apiDetail.examples && apiDetail.examples.length ? + renderExamples(apiDetail.examples).join("") : + `
// example(s) for ${apiDetail.name} to come later
` +}
`); }); @@ -232,7 +234,12 @@ function createSdkSidebar(apiDetails) { apiSidebar.push(` `); }); @@ -266,7 +273,8 @@ async function getTags(repositoryName) { tag.name !== "v0.38.0rc3" && tag.name !== "v0.38.0rc2" && tag.name !== "v0.38.0rc1" - ) tags.push(tag.name); + ) + tags.push(tag.name); }); break; @@ -278,7 +286,8 @@ async function getTags(repositoryName) { tag.name !== "v0.3.15" && tag.name !== "v0.3-osx" && tag.name !== "v0.2-alpha" - ) tags.push(tag.name); + ) + tags.push(tag.name); }); break; @@ -293,11 +302,11 @@ async function parseApiFile({ repo, tag }) { let apiFileLink = `${rawGitHubBase}${repo}/${tag}`; switch(true) { - case (repo === "lbrycrd"): + case repo === "lbrycrd": apiFileLink = `${apiFileLink}${filePathBlockchain}`; break; - case (repo === "lbry-sdk"): + case repo === "lbry-sdk": apiFileLink = `${apiFileLink}${filePathSdk}`; break; @@ -317,8 +326,7 @@ async function parseApiFile({ repo, tag }) { function renderArguments(args) { const argumentContent = []; - if (!args || args.length === 0) - return argumentContent; + if (!args || args.length === 0) return argumentContent; args.forEach(arg => { argumentContent.push(` @@ -328,7 +336,11 @@ function renderArguments(args) { ${arg.is_required === true ? "" : "optional"}${arg.type} -
${typeof arg.description === "string" ? arg.description.replace(//g, ">") : ""}
+
${ + typeof arg.description === "string" ? + arg.description.replace(//g, ">") : + "" +}
`); }); @@ -352,11 +364,15 @@ function renderExamples(args) { ${arg.lbrynet ? `
${arg.lbrynet}
` : ""} ${arg.python ? `
${arg.python}
` : ""} - ${arg.output ? ` + ${ + arg.output ? + `

Output


${arg.output}

- ` : ""} + ` : + "" +} `); }); @@ -366,17 +382,14 @@ function renderExamples(args) { function renderReturns(args) { let returnContent = []; - if (!args || args.length === 0) - return returnContent; + if (!args || args.length === 0) return returnContent; returnContent = dedent(JSON.parse(JSON.stringify(args))); return returnContent; } function renderVersionSelector(pageSlug, versions, desiredTag) { - const options = [ - "" - ]; + const options = [""]; let optionIndex = 0; @@ -384,13 +397,13 @@ function renderVersionSelector(pageSlug, versions, desiredTag) { optionIndex++; let selectedOption = false; - if (desiredTag && desiredTag === version) - selectedOption = true; - else if (optionIndex === 1) - selectedOption = true; + if (desiredTag && desiredTag === version) selectedOption = true; + else if (optionIndex === 1) selectedOption = true; options.push( - `` + `` ); }); @@ -402,9 +415,15 @@ function renderCodeLanguageToggles(pageSlug) { return [ "", - !onSdkPage ? "" : "", + !onSdkPage ? + "" : + "", "", - onSdkPage ? "" : "", - onSdkPage ? "" : "" + onSdkPage ? + "" : + "", + onSdkPage ? + "" : + "" ]; } -- 2.45.2