"use strict"; // I M P O R T S import asyncHtml from "choo-async/html"; import dedent from "dedent"; import got from "got"; import Octokit from "@octokit/rest"; // U T I L S import headerBlockchain from "~component/api/header-blockchain"; import headerSdk from "~component/api/header-sdk"; import redirects from "~data/redirects.json"; const cache = new Map(); const filePathBlockchain = "/contrib/devtools/generated/api_v1.json"; const filePathSdk = "/docs/api.json"; const rawGitHubBase = "https://raw.githubusercontent.com/lbryio/"; const octokit = new Octokit({ auth: `token ${process.env.GITHUB_OAUTH_TOKEN}` }); // E X P O R T export default async(state) => { const { tag } = state; const { wildcard } = state.params; const repository = wildcard === "sdk" ? "lbry-sdk" : "lbrycrd"; state.lbry = { title: tag ? tag + " API Documentation" : "API Documentation", description: "See API documentation, signatures, and sample calls for the LBRY APIs." }; const tags = await getTags(repository); const currentTag = tag && tag.length ? tag : tags[0]; try { const apiResponse = await parseApiFile({ repo: repository, tag: currentTag }); return asyncHtml`
${createApiHeader(wildcard, currentTag)} ${wildcard === "sdk" ? createSdkContent(apiResponse) : createApiContent(apiResponse)}
`; } catch(error) { const redirectUrl = redirects[state.href]; return asyncHtml`

Redirecting you to ${redirectUrl}

`; } }; // H E L P E R S function createApiContent(apiDetails) { const apiContent = []; apiDetails.forEach(apiDetail => { let apiDetailsReturns = ""; 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.examples && apiDetail.examples.length ? renderExamples(apiDetail.examples).join("") : `
// example(s) for ${apiDetail.name} to come later
`}
`); }); return apiContent; } function createApiHeader(slug, apiVersion) { switch(slug) { case "blockchain": return headerBlockchain(apiVersion); case "sdk": return headerSdk(apiVersion); default: break; } } function createApiSidebar(apiDetails) { const apiSidebar = []; apiDetails.forEach(apiDetail => { apiSidebar.push(`
  • ${apiDetail.name}
  • `); }); return apiSidebar; } function createSdkContent(apiDetails) { const apiContent = []; const sectionTitles = Object.keys(apiDetails); sectionTitles.forEach(title => { const commands = apiDetails[title].commands; const description = apiDetails[title].doc; apiContent.push( commands.length ? commands.map(command => createSdkContentSections(title, description, command)).join("") : "" ); }); return apiContent; } function createSdkContentSections(sectionTitle, sectionDescription, sectionDetails) { return `

    ${sectionDetails.name}

    ${sectionDetails.description}

    Arguments

    Returns

    ${renderReturns(sectionDetails.returns)}
    ${renderExamples(sectionDetails.examples).join("")}
    `; } function createSdkSidebar(apiDetails) { const sectionTitles = Object.keys(apiDetails); const apiSidebar = []; sectionTitles.forEach(title => { const commands = apiDetails[title].commands; apiSidebar.push(` `); }); return apiSidebar; } async function getTags(repositoryName) { const { data } = await octokit.repos.listTags({ owner: "lbryio", repo: repositoryName }); const tags = ["master"]; // NOTE: // The versioning in our repos do not make sense so extra // exclusion code is needed to make this work. // // Documentation is only available after specific versions. switch(true) { case repositoryName === "lbry-sdk": data.forEach(tag => { if (tag.name >= "v0.52.0") tags.push(tag.name); }); break; case repositoryName === "lbrycrd": data.forEach(tag => { if ( tag.name >= "v0.17.1.0" && tag.name !== "v0.3.16" && tag.name !== "v0.3.15" && tag.name !== "v0.3-osx" && tag.name !== "v0.2-alpha" ) tags.push(tag.name); }); break; default: break; } return tags; } async function parseApiFile({ repo, tag }) { let apiFileLink = `${rawGitHubBase}${repo}/${tag}`; switch(true) { case (repo === "lbrycrd"): apiFileLink = `${apiFileLink}${filePathBlockchain}`; break; case (repo === "lbry-sdk"): apiFileLink = `${apiFileLink}${filePathSdk}`; break; default: return Promise.reject(new Error("Failed to fetch API docs")); } const response = await got(apiFileLink, { cache, json: true }); try { return response.body; } catch(error) { return "Issue loading API documentation"; } } function renderArguments(args) { const argumentContent = []; if (!args || args.length === 0) return argumentContent; args.forEach(arg => { argumentContent.push(`
  • ${arg.name}
    ${arg.is_required === true ? "" : "optional"}${arg.type}
    ${typeof arg.description === "string" ? arg.description.replace(//g, ">") : ""}
  • `); }); return argumentContent; } function renderExamples(args) { const exampleContent = []; if (!args || args.length === 0) { exampleContent.push("
    // example(s) to come later
    "); return exampleContent; } args.forEach(arg => { exampleContent.push(` ${arg.title ? `

    ${arg.title}


    ` : ""} ${arg.cli ? `
    ${arg.cli}
    ` : ""} ${arg.curl ? `
    ${arg.curl}
    ` : ""} ${arg.lbrynet ? `
    ${arg.lbrynet}
    ` : ""} ${arg.python ? `
    ${arg.python}
    ` : ""} ${arg.output ? `

    Output


    ${arg.output}

    ` : ""} `); }); return exampleContent; } function renderReturns(args) { let returnContent = []; if (!args || args.length === 0) return returnContent; returnContent = dedent(JSON.parse(JSON.stringify(args))); return returnContent; } function renderVersionSelector(pageSlug, versions, desiredTag) { const options = [ "" ]; let optionIndex = 0; versions.forEach(version => { optionIndex++; let selectedOption = false; if (desiredTag && desiredTag === version) selectedOption = true; else if (optionIndex === 1) selectedOption = true; options.push( `` ); }); return options; } function renderCodeLanguageToggles(pageSlug) { const onSdkPage = pageSlug === "sdk"; return [ "", !onSdkPage ? "" : "", "", onSdkPage ? "" : "", onSdkPage ? "" : "" ]; }