"use strict"; // I M P O R T S import asyncHtml from "choo-async/html"; import dedent from "dedent"; // U T I L S import headerBlockchain from "../components/api/header-blockchain"; import headerSdk from "../components/api/header-sdk"; import redirects from "../data/redirects.json"; const fetch = require("make-fetch-happen").defaults({ cacheManager: "./cache" }); // E X P O R T export default async(state) => { // How to set custom metadata for this page // state.lbry = { // description: "This is the API page for LBRY.tech", // "og:image": "/assets/media/images/carlsagan2.jpg", // "og:image:height": 300, // "og:image:width": 400 // }; try { const apiResponse = await parseApiFile(state.params.wildcard); return asyncHtml`
${createApiHeader(state.params.wildcard)}
${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)}
// 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 parseApiFile(urlSlug) { let apiFileLink; // checks below are related to rate limits, both URLs should return the same content switch(true) { case (urlSlug === "blockchain"): apiFileLink = process.env.NODE_ENV === "development" ? "https://rawgit.com/lbryio/lbrycrd/add_api_docs_scripts/contrib/devtools/generated/api_v1.json" : "https://cdn.rawgit.com/lbryio/lbrycrd/add_api_docs_scripts/contrib/devtools/generated/api_v1.json"; break; case (urlSlug === "sdk"): apiFileLink = process.env.NODE_ENV === "development" ? "https://rawgit.com/lbryio/lbry/master/docs/api.json" : "https://cdn.rawgit.com/lbryio/lbry/master/docs/api.json"; break; default: break; } if (!apiFileLink) return Promise.reject(new Error("Failed to fetch API docs")); return fetch(apiFileLink).then(() => fetch(apiFileLink, { cache: "no-cache" // forces a conditional request })) .then(res => res.json().then(body => body)); // res.status 304 = cache validated } function renderArguments(args) { const 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; }