Skip to content

Commit

Permalink
feat(apidoc-viewer): add tag_mappings to ease menu organisation indep…
Browse files Browse the repository at this point in the history
…endently of microservice tags
  • Loading branch information
jeromefellus-sekoia committed Feb 15, 2025
1 parent ea6d56a commit 0477b62
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
8 changes: 6 additions & 2 deletions docs/developer/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@ template: overrides/openapi.html
"https://app.sekoia.io/api/v1/edl-gateway/openapi.json",
"https://app.sekoia.io/api/v2/inthreat/swagger.json?context=public",
],
tag_mappings:{
"mfa": "MFA",
"me": "User profile",
},
menu:[
{
name: "User",
tags: [
"User Authentication",
"me",
"mfa", // TODO: split 'mfa' into administration and self
"User profile",
"MFA",
"permissions",
],
},
Expand Down
12 changes: 6 additions & 6 deletions docs/javascript/openapi-viewer.min.js

Large diffs are not rendered by default.

23 changes: 18 additions & 5 deletions lib/openapi-viewer/src/OpenAPIViewer.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Teleport, capitalize, createApp, reactive } from "vue";
import { Teleport, createApp, reactive } from "vue";
import { fetch_and_merge_openapi_schemas } from "./openapi/openapi"
import { Examples } from "./Examples";
import { tagEncode, Chevron, debounce, scrollToAnchor } from "./utils"
import { tagEncode, Chevron, debounce, scrollToAnchor, capitalize } from "./utils"
import { Markdown } from "./Markdown"
import { Parameters } from "./Parameters"
import { Response } from "./Response"
Expand Down Expand Up @@ -38,7 +38,7 @@ export const data = reactive({
export const OpenAPIViewer = {

/** Gather all openapi.json/swagger.json manifests from {urls} and render as OpenAPIViewer Vue component */
async init({ title, urls, menu, regions }) {
async init({ title, urls, menu, regions, tag_mappings = {} }) {
const hash = location.hash
data.title = title

Expand Down Expand Up @@ -81,6 +81,20 @@ export const OpenAPIViewer = {
})
})

// Translate tags via tag_mappings defined in developer/api.md
// e.g. { "User": "Account" } will replace all "User" tags with "Account"
// e.g. { ["User", "Account"]: ["bla", "blu"] } will replace replace the ["User", "Account"] tags pair with ["bla", "blu"]
// (only if both tags are present in the endpoint)
data.endpoints.forEach(endpoint => {
Object.entries(tag_mappings || {}).forEach(([tag, mapping]) => {
mapping = Array.isArray(mapping) ? mapping : [mapping]
const tags = new Set(Array.isArray(tag) ? tag : [tag])
if (endpoint.tags.every(t => tags.has(t))) {
endpoint.tags = Array.from(new Set([...endpoint.tags.filter(t => !tags.has(t)), ...mapping]))
}
})
})

// Organize endpoints according to the menu tree
// and collect non-menu tags as filtering candidates
data.tags = new Set()
Expand All @@ -94,8 +108,6 @@ export const OpenAPIViewer = {
}
}

console.log(data.tree)

for (const endpoint of data.endpoints) {
endpoint.menu0 = data.tree.find(({ name }) => endpoint.tags.find(t => name.toLowerCase() === t.toLowerCase()))
endpoint.menu1 = endpoint.menu0?.children.find(({ name }) => endpoint.tags.find(t => name.toLowerCase() === t.toLowerCase()))
Expand Down Expand Up @@ -276,6 +288,7 @@ export const OpenAPIViewer = {
{data.tree?.map(({ name, children }) => <li class="md-nav__item md-nav__item--nested" class={{
active: data.cur0 === tagEncode(name),
}}>
{console.log(name)}
<a href={`#tag/${tagEncode(name)}`}>{name}</a>
<ul class='md-nav__list'>
{children?.map(({ name, endpoints }) => <li class='md-nav__item md-nav__item--nested accordion' class={{
Expand Down
4 changes: 4 additions & 0 deletions lib/openapi-viewer/src/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ export function scrollToAnchor(hash) {
top: document.getElementById(hash.substring(1)).getBoundingClientRect().top + window.scrollY,
behavior: 'smooth'
});
}

export function capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

0 comments on commit 0477b62

Please sign in to comment.