lbry.tech/app/views/api.js

143 lines
4.3 KiB
JavaScript
Raw Normal View History

2018-08-29 01:57:18 +02:00
"use strict";
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";
2018-10-01 04:06:35 +02:00
import redirectOr404 from "../modules/redirectOr404";
2018-10-01 06:40:24 +02:00
import headerBlockchain from "../components/api/header-blockchain";
import headerSdk from "../components/api/header-sdk";
2018-08-29 01:57:18 +02:00
const fetch = require("make-fetch-happen").defaults({ cacheManager: "./cache" });
2018-09-28 23:34:42 +02:00
module.exports = exports = state => parseApiFile(state.params.wildcard).then(response => {
/*
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
};
*/
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">&times;</div>
<ul class="api__toc__search__results"></ul>
</div>
2018-10-01 01:43:35 +02:00
<ul class="api__toc__items" id="toc" role="navigation">${createApiSidebar(response)}</ul>
2018-09-28 23:34:42 +02:00
</aside>
2018-10-01 06:40:24 +02:00
<section class="api__content">
${createApiHeader(state.params.wildcard)}
<div class="api__documentation" id="toc-content">
${createApiContent(response)}
</div>
</section>
2018-09-28 23:34:42 +02:00
</div>
<script src="/assets/scripts/plugins/jets.js"></script>
2018-10-01 01:43:35 +02:00
<script src="/assets/scripts/api.js"></script>
2018-09-28 23:34:42 +02:00
`;
2018-10-01 04:06:35 +02:00
}).catch(() => {
redirectOr404(state.href);
2018-09-28 23:34:42 +02:00
});
2018-08-29 01:57:18 +02:00
2018-09-30 19:34:29 +02:00
2018-08-29 01:57:18 +02:00
// H E L P E R S
2018-10-01 06:40:24 +02:00
function createApiHeader(slug) {
switch (slug) {
case "sdk":
return headerSdk();
case "blockchain":
return headerBlockchain();
}
}
2018-08-29 01:57:18 +02:00
function createApiContent(apiDetails) {
const apiContent = [];
for (const apiDetail of apiDetails) {
let apiDetailsReturns = "";
if (apiDetail.returns) apiDetailsReturns = JSON.parse(JSON.stringify(apiDetail.returns));
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;
}
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;
}
function parseApiFile(urlSlug) {
let apiFileLink;
2018-09-30 19:34:29 +02:00
//checks below are related to rate limits, both URLs should return the same content
if (urlSlug === "sdk") apiFileLink = process.env.NODE_ENV === "development" ?
2018-08-29 01:57:18 +02:00
"https://rawgit.com/lbryio/lbry/master/docs/api.json" :
2018-09-30 19:34:29 +02:00
"https://cdn.rawgit.com/lbryio/lbry/master/docs/api.json"
2018-08-29 01:57:18 +02:00
;
if (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"
;
2018-09-30 19:34:29 +02:00
if (!apiFileLink) return Promise.reject(new Error("Failed to fetch API docs")); // TODO: Error handling
2018-08-29 01:57:18 +02:00
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(`
<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, "&lt;").replace(/>/g, "&gt;") : ""}</div>
</li>
`);
}
return argumentContent;
}