"use strict"; // I M P O R T S import asyncHtml from "choo-async/html"; import dedent from "dedent"; import got from "got"; // 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 blockchainApi = "https://cdn.jsdelivr.net/gh/lbryio/lbrycrd@master/contrib/devtools/generated/api_v1.json"; const cache = new Map(); const sdkApi = "https://cdn.jsdelivr.net/gh/lbryio/lbry@master/docs/api.json"; // E X P O R T export default async(state) => { // below is evil, I just inherited it -- Jeremy const apilabel = state.params.wildcard === "sdk" ? "SDK" : state.params.wildcard.charAt(0).toLocaleUpperCase() + state.params.wildcard.substring(1); state.lbry = { title: apilabel + " API Documentation", description: "See API documentation, signatures, and sample calls for the LBRY " + apilabel + " APIs." }; try { const apiResponse = await parseApiFile(state.params.wildcard); return asyncHtml`
${createApiHeader(state.params.wildcard)} ${apilabel === "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 = []; for (const apiDetail of apiDetails) { let apiDetailsReturns = ""; if (apiDetail.returns) apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns)); apiContent.push(`

${apiDetail.name}

${apiDetail.description}

${apiDetail.arguments.length ? `

Arguments

` : ""}

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) { switch(slug) { case "blockchain": return headerBlockchain(); case "sdk": return headerSdk(); default: break; } } function createApiSidebar(apiDetails) { const apiSidebar = []; for (const apiDetail of apiDetails) { apiSidebar.push(`
  • ${apiDetail.name}
  • `); } return apiSidebar; } function createSdkContent(apiDetails) { const apiContent = []; const sectionTitles = Object.keys(apiDetails); for (const title of sectionTitles) { 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 = []; for (const title of sectionTitles) { const commands = apiDetails[title].commands; apiSidebar.push(` `); } return apiSidebar; } async function parseApiFile(urlSlug) { let apiFileLink; switch(true) { case (urlSlug === "blockchain"): apiFileLink = blockchainApi; break; case (urlSlug === "sdk"): apiFileLink = sdkApi; break; default: break; } if (!apiFileLink) 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; for (const arg of args) { 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; } for (const arg of args) { exampleContent.push(`

    ${arg.title}


    ${arg.curl}
    ${arg.lbrynet}
    ${arg.python}

    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; }