UNPKG

@curveball/browser

Version:

Automatic API browser generator. A middleware that turns your JSON responses into HTML if accessed by a browser.

55 lines 1.93 kB
import * as React from 'react'; import * as url from 'url'; import { getNavLinks } from '../util.js'; const types = { 'csv': { label: 'CSV', cssClass: 'type-csv', }, 'atom+xml': { label: 'ATOM', cssClass: 'type-feed', }, 'rss+xml': { label: 'RSS', cssClass: 'type-feed', }, }; export function Alternate(props) { const links = getNavLinks(props.resourceState.links.getAll(), props.options, 'alternate'); if (!links.length) { return null; } const alternateElems = links.map(link => { let cssClass; let label; if (link.type) { const subtype = link.type.split('/')[1]; let typeInfo = types[subtype]; if (typeInfo === undefined) { typeInfo = { cssClass: 'type-' + subtype, label: subtype.toUpperCase(), }; } cssClass = typeInfo.cssClass; label = types[subtype] !== undefined ? types[subtype].label : subtype.toUpperCase(); } else { label = link.title ? link.title : link.rel; } let href = link.href; // If the url is relative, we're adding our secret argument to make the Accept header work. if ((href === '/' || href.match(/^\/[^/]/) !== null) && link.type) { const urlObj = url.parse(href, true); urlObj.query['_browser-accept'] = link.type; urlObj.search = null; href = url.format(urlObj); } return React.createElement("a", { href: href, rel: link.rel, key: label, title: label, className: cssClass }, label); }); return React.createElement("div", { className: "alternate" }, React.createElement("h3", null, "Other formats"), React.createElement("ul", null, alternateElems)); } //# sourceMappingURL=alternate.js.map