2018-08-29 01:57:18 +02:00
|
|
|
"use strict";
|
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
|
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
// I M P O R T S
|
2018-10-01 22:47:10 +02:00
|
|
|
|
2018-08-29 18:58:55 +02:00
|
|
|
import asyncHtml from "choo-async/html";
|
2018-08-29 01:57:18 +02:00
|
|
|
import dedent from "dedent";
|
2019-01-08 18:25:34 +01:00
|
|
|
import got from "got";
|
2018-10-01 22:47:10 +02:00
|
|
|
|
2018-10-10 19:56:35 +02:00
|
|
|
// U T I L S
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2019-01-30 00:29:14 +01:00
|
|
|
import headerBlockchain from "@component/api/header-blockchain";
|
|
|
|
import headerSdk from "@component/api/header-sdk";
|
|
|
|
import redirects from "@data/redirects.json";
|
2018-11-30 21:46:22 +01:00
|
|
|
|
2018-12-31 15:30:32 +01:00
|
|
|
const blockchainApi = "https://cdn.jsdelivr.net/gh/lbryio/lbrycrd@master/contrib/devtools/generated/api_v1.json";
|
2019-01-08 18:25:34 +01:00
|
|
|
const cache = new Map();
|
2018-12-12 19:32:05 +01:00
|
|
|
const sdkApi = "https://cdn.jsdelivr.net/gh/lbryio/lbry@master/docs/api.json";
|
2018-10-01 22:47:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// E X P O R T
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
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`
|
|
|
|
<div class="__slate">
|
|
|
|
<aside class="api__toc">
|
|
|
|
<div class="api__toc__search">
|
|
|
|
<input class="api__toc__search__field" id="input-search" placeholder="Search" type="search"/>
|
|
|
|
<div class="api__toc__search__clear" id="clear-search" title="Clear search query">×</div>
|
|
|
|
<ul class="api__toc__search__results"></ul>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<ul class="api__toc__items" id="toc" role="navigation">${createApiSidebar(apiResponse)}</ul>
|
|
|
|
</aside>
|
|
|
|
<section class="api__content">
|
|
|
|
${createApiHeader(state.params.wildcard)}
|
|
|
|
<div class="api__documentation" id="toc-content">
|
|
|
|
${createApiContent(apiResponse)}
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script src="/assets/scripts/plugins/jets.js"></script>
|
|
|
|
<script src="/assets/scripts/api.js"></script>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:25:34 +01:00
|
|
|
catch(error) {
|
2018-10-06 22:53:01 +02:00
|
|
|
const redirectUrl = redirects[state.href];
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-10-06 22:53:01 +02:00
|
|
|
return asyncHtml`
|
2018-10-06 22:59:46 +02:00
|
|
|
<article class="page" itemtype="http://schema.org/BlogPosting">
|
|
|
|
<header class="page__header">
|
|
|
|
<div class="page__header-wrap">
|
|
|
|
<div class="inner-wrap">
|
|
|
|
<h1 class="page__header__title" itemprop="name headline">404</h1>
|
|
|
|
</div>
|
2018-10-01 22:47:10 +02:00
|
|
|
</div>
|
2018-10-06 22:59:46 +02:00
|
|
|
</header>
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-10-06 22:59:46 +02:00
|
|
|
<section class="page__content page__markup" itemprop="articleBody">
|
|
|
|
<div class="inner-wrap">
|
|
|
|
<p>Redirecting you to <strong>${redirectUrl}</strong></p>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.href = "${redirectUrl}";
|
|
|
|
}, 2000);
|
|
|
|
</script>
|
|
|
|
`;
|
2018-11-30 21:46:22 +01:00
|
|
|
}
|
|
|
|
};
|
2018-09-30 19:34:29 +02:00
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
|
|
|
|
// H E L P E R S
|
2018-10-01 06:40:24 +02:00
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
function createApiContent(apiDetails) {
|
|
|
|
const apiContent = [];
|
|
|
|
|
|
|
|
for (const apiDetail of apiDetails) {
|
|
|
|
let apiDetailsReturns = "";
|
2018-10-06 22:53:01 +02:00
|
|
|
|
2018-11-30 21:46:22 +01:00
|
|
|
if (apiDetail.returns)
|
|
|
|
apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns));
|
2018-08-29 01:57:18 +02:00
|
|
|
|
|
|
|
apiContent.push(`
|
|
|
|
<div class="api__content__body">
|
|
|
|
<h2 id="${apiDetail.name}">${apiDetail.name}</h2>
|
|
|
|
<p>${apiDetail.description}</p>
|
|
|
|
|
|
|
|
${apiDetail.arguments.length ? `<h3>Arguments</h3><ul class="api__content__body__arguments">${renderArguments(apiDetail.arguments).join("")}</ul>` : ""}
|
|
|
|
|
|
|
|
<h3>Returns</h3>
|
|
|
|
<pre><code>${dedent(apiDetailsReturns)}</code></pre>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="api__content__example">
|
|
|
|
<pre><code>// example(s) for ${apiDetail.name} to come later</code></pre>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiContent;
|
|
|
|
}
|
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
function createApiHeader(slug) {
|
|
|
|
switch(slug) {
|
|
|
|
case "blockchain":
|
|
|
|
return headerBlockchain();
|
|
|
|
|
|
|
|
case "sdk":
|
|
|
|
return headerSdk();
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 01:57:18 +02:00
|
|
|
function createApiSidebar(apiDetails) {
|
|
|
|
const apiSidebar = [];
|
|
|
|
|
|
|
|
for (const apiDetail of apiDetails) {
|
|
|
|
apiSidebar.push(`
|
|
|
|
<li class="api__toc__item">
|
|
|
|
<a href="#${apiDetail.name}" title="Go to ${apiDetail.name} section">
|
|
|
|
${apiDetail.name}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiSidebar;
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:25:34 +01:00
|
|
|
async function parseApiFile(urlSlug) {
|
2018-08-29 01:57:18 +02:00
|
|
|
let apiFileLink;
|
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
switch(true) {
|
|
|
|
case (urlSlug === "blockchain"):
|
2018-12-12 19:32:05 +01:00
|
|
|
apiFileLink = blockchainApi;
|
2018-10-01 22:47:10 +02:00
|
|
|
break;
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2018-10-01 22:47:10 +02:00
|
|
|
case (urlSlug === "sdk"):
|
2018-12-12 19:32:05 +01:00
|
|
|
apiFileLink = sdkApi;
|
2018-10-01 22:47:10 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-08-29 01:57:18 +02:00
|
|
|
|
2019-01-08 18:25:34 +01:00
|
|
|
if (!apiFileLink)
|
|
|
|
return Promise.reject(new Error("Failed to fetch API docs"));
|
|
|
|
|
|
|
|
const response = await got(apiFileLink, { cache: cache, json: true });
|
|
|
|
|
|
|
|
try {
|
|
|
|
return response.body;
|
|
|
|
} catch(error) {
|
|
|
|
return "Issue loading API documentation";
|
|
|
|
}
|
2018-08-29 01:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function renderArguments(args) {
|
|
|
|
const argumentContent = [];
|
|
|
|
|
|
|
|
for (const arg of args) {
|
|
|
|
argumentContent.push(`
|
|
|
|
<li class="api__content__body__argument">
|
|
|
|
<div class="left">
|
|
|
|
<strong>${arg.name}</strong><br/>
|
|
|
|
${arg.is_required === true ? "" : "<span>optional</span>" }<span>${arg.type}</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="right">${typeof arg.description === "string" ? arg.description.replace(/</g, "<").replace(/>/g, ">") : ""}</div>
|
|
|
|
</li>
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return argumentContent;
|
|
|
|
}
|