chore(api): Use templates for creating pages, page functions return a string of YAML frontmatter and Markdown.

jts/api-uplift
Jason Stirnaman 2024-11-25 10:06:22 -06:00
parent f3ead26e1a
commit 200881baa0
1 changed files with 38 additions and 30 deletions

View File

@ -55,9 +55,8 @@ function getPathGroups(openapi) {
return pathGroups; return pathGroups;
} }
function createPageIdentifier(apiName, pathGroup) { function createPageIdentifier(uniqueName) {
pathGroup = pathGroup && `-${pathGroup}` || ''; return (`api-reference-${uniqueName}`).replace(/-/g, '_');
return (`api-reference-${apiName}${pathGroup}`).replace(/-/g, '_');
} }
function createIndexPage(spec, params) { function createIndexPage(spec, params) {
@ -103,12 +102,26 @@ function getTraitTags(pathSpec) {
return pathSpec['tags'].filter( k => k[`x-traitTag`]); return pathSpec['tags'].filter( k => k[`x-traitTag`]);
} }
// Create a page for each group of operations within a path ("path group")
// In OpenAPI, tags are used to group endpoints. OpenAPI doesn't allow
// a description field for a path, so we use the name and description of
// the first tag in the path.
// Returns a string containing the page content
// The page frontmatter contains an api.spec property to be rendered as the API reference doc for the path group.
function createPathGroupPage(pathGroup, pathSpec, params) { function createPathGroupPage(pathGroup, pathSpec, params) {
// In OpenAPI, tags are used to group endpoints. OpenAPI doesn't allow
// a description field for a path, so we'll use the name and description of const page = getPageTemplate('path');
// the first tag in the path. const menuKey = Object.keys(params.menu)[0];
const primaryTag = getPathTags(pathSpec).flat()[0]; const menu = {
[menuKey]: {...page.menu, ...params.menu[menuKey]}
};
page.menu = menu;
params.title = pathGroup; params.title = pathGroup;
params.menu[menuKey].parent = pathSpec.info.title;
params.menu[menuKey].weight = 1;
params.menu[menuKey].name = params.list_title;
const primaryTag = getPathTags(pathSpec).flat()[0];
if(primaryTag) { if(primaryTag) {
params.list_title = `${primaryTag['name']} ${pathGroup}`; params.list_title = `${primaryTag['name']} ${pathGroup}`;
params.description = (primaryTag && primaryTag['description']) || ''; params.description = (primaryTag && primaryTag['description']) || '';
@ -116,12 +129,15 @@ function createPathGroupPage(pathGroup, pathSpec, params) {
logger.log('warn', `Name: ${pathSpec.info.title} - No tags found for path group: ${pathGroup}`); logger.log('warn', `Name: ${pathSpec.info.title} - No tags found for path group: ${pathGroup}`);
} }
// Create a unique identifier for the menu item
params.menu[menuKey].identifier = createPageIdentifier(`${params.api_name}_${pathGroup}`);
params.api = { params.api = {
spec: JSON.stringify(pathSpec), spec: JSON.stringify(pathSpec),
path_group: pathGroup, path_group: pathGroup,
}; };
params.related = [];
params.related = [];
if(pathSpec['x-influxdata-related-endpoints']) { if(pathSpec['x-influxdata-related-endpoints']) {
params.related = [...pathSpec['x-influxdata-related-endpoints']]; params.related = [...pathSpec['x-influxdata-related-endpoints']];
} }
@ -130,34 +146,24 @@ function createPathGroupPage(pathGroup, pathSpec, params) {
...params.related, ...pathSpec['x-influxdata-related-content'] ...params.related, ...pathSpec['x-influxdata-related-content']
]; ];
} }
const menuKey = Object.keys(params.menu)[0];
params.menu[menuKey].parent = pathSpec.info.title;
params.menu[menuKey].weight = 1;
params.menu[menuKey].name = params.list_title;
// Create a unique identifier for the menu item
params.menu[menuKey].identifier = (`api-reference-${params.api_name}_${pathGroup}`).replace(/-/g, '_');
// Return params as a YAML frontmatter string // Return params as a YAML frontmatter string
return `---\n${yaml.dump(params)}\n---\n`; return `---\n${yaml.dump(params)}\n---\n`;
} }
export function createOverviewPage(spec, params) { export function createOverviewPage(spec, params) {
const pageParams = { const page = getPageTemplate('overview');
title: 'Overview',
description: `${spec.info.description}. The API provides predictable
endpoint paths and supports standard HTTP methods, status codes, and authorization schemes. Resource management endpoints support JSON in requests and responses.`,
menu: {},
};
const menuKey = Object.keys(params.menu)[0]; const menuKey = Object.keys(params.menu)[0];
pageParams.menu[menuKey].parent = params.api_name; const menu = {
params.menu[menuKey].weight = 10; [menuKey]: {...page.menu, ...params.menu[menuKey]}
params.menu[menuKey].name = 'Overview'; };
params.menu[menuKey].identifier = (`api-reference-${params.api_name}_overview`).replace(/-/g, '_'); page.menu = menu;
// Create a unique identifier for the menu item
params.menu[menuKey].identifier = createPageIdentifier(`${params.api_name}-overview`);
// const overviewSpec = JSON.parse(JSON.stringify(spec)); // const overviewSpec = JSON.parse(JSON.stringify(spec));
// overviewSpec.paths = null; // overviewSpec.paths = null;
// params.api = {spec: JSON.stringify(overviewSpec)}; // params.api = {spec: JSON.stringify(overviewSpec)};
let toc = '';
let body = ''; let body = '';
getTraitTags(spec).forEach( traitTag => { getTraitTags(spec).forEach( traitTag => {
@ -165,7 +171,9 @@ export function createOverviewPage(spec, params) {
body = body.concat(traitTag['description'], "\n"); body = body.concat(traitTag['description'], "\n");
}); });
return (JSON.stringify(params)).concat(body); // Return params as a YAML frontmatter string
return (`---\n${yaml.dump(params)}\n---\n\n`)
.concat(spec.info.description, '\\n', body)
} }
export function createAPIPages(params, specPath, docPath) { export function createAPIPages(params, specPath, docPath) {
@ -187,9 +195,9 @@ export function createAPIPages(params, specPath, docPath) {
writeFileSync(path.join(docPath, '_index.md'), createIndexPage(spec, JSON.parse(paramsClone))); writeFileSync(path.join(docPath, '_index.md'), createIndexPage(spec, JSON.parse(paramsClone)));
// // Create the overview page // // Create the overview page
// writeFileSync( writeFileSync(
// path.join(docPath, 'overview.md'), path.join(docPath, 'overview.md'),
// createOverviewPage(spec, JSON.parse(paramsClone))); createOverviewPage(spec, JSON.parse(paramsClone)));
// Create a page for each group of operations within a path ("path group") // Create a page for each group of operations within a path ("path group")
const pathGroups = getPathGroups(spec); const pathGroups = getPathGroups(spec);