@curveball/browser
Version:
Automatic API browser generator. A middleware that turns your JSON responses into HTML if accessed by a browser.
81 lines • 3.42 kB
JavaScript
import * as React from 'react';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import * as path from 'node:path';
function loadLinkData(fileName) {
return JSON.parse(readFileSync(path.join(fileURLToPath(new URL(import.meta.url + '/..')), '../../data/', fileName + '.json'), 'utf-8'));
}
const linkDescriptions = {
...loadLinkData('iana-links'),
...loadLinkData('editor-links'),
...loadLinkData('level3-rest-links')
};
export function LinksTable(props) {
const linkRows = [];
// Grouping links by rel.
const groups = {};
for (const link of props.resourceState.links.getAll()) {
if (!props.options.allLinks &&
(props.options.hiddenRels.includes(link.rel) || link.rel in props.options.navigationLinks || link.rendered)) {
continue;
}
if (groups[link.rel]) {
groups[link.rel].push(link);
}
else {
groups[link.rel] = [link];
}
}
for (const group of Object.values(groups)) {
const linkCount = group.length;
let index = 0;
for (const link of group) {
const linkBadges = [];
if (link.hints?.allow) {
for (const method of link.hints.allow) {
linkBadges.push(React.createElement("span", { key: method, className: `link-badge method-${method.toLowerCase()}` }, method.toUpperCase()));
}
}
if (link.hints?.status) {
switch (link.hints.status) {
case 'deprecated':
linkBadges.push(React.createElement("span", { key: "deprecated", className: "link-badge status-deprecated" }, "Deprecated"));
break;
case 'gone':
linkBadges.push(React.createElement("span", { key: "gone", className: "link-badge status-gone" }, "Gone"));
break;
}
}
linkRows.push(React.createElement("tr", { key: 'link-' + index },
index === 0 ? React.createElement("td", { rowSpan: linkCount },
React.createElement(LinkRel, { link: link })) : null,
link.templated ? React.createElement("td", null, link.href) : React.createElement("td", null,
React.createElement("a", { href: link.href }, link.href)),
React.createElement("td", null,
link.title,
linkBadges)));
index++;
}
}
if (!linkRows.length)
return null;
return React.createElement(React.Fragment, null,
React.createElement("h2", null, "Links"),
React.createElement("table", { className: "links" },
React.createElement("tr", null,
React.createElement("th", null, "Relationship"),
React.createElement("th", null, "Url"),
React.createElement("th", null, "Title")),
linkRows));
}
function LinkRel(props) {
const rel = props.link.rel;
if (linkDescriptions[rel] !== undefined) {
const link = linkDescriptions[rel];
return React.createElement("a", { className: "definition", title: link.description, href: link.href }, rel);
}
else {
return React.createElement(React.Fragment, null, rel);
}
}
//# sourceMappingURL=links-table.js.map