Skip to content

Commit bd31940

Browse files
committed
To build the API pages, run yarn run build-apidocs from the project directory.
No longer uses Hugo /data. Removes dependency on the Hugo-data-to-pages script. For each product endpoint, generate a page with the endpoint spec inside the frontmatter. Writes endpoint specs into each page's frontmatter. Assign a `type` property for API reference (OpenAPI) paths. For example, if the page structure is `content/influxdb/v2/api/v2/[OpenAPI path]`, then add `type: api_path` to the frontmatter and, if necessary, specify a layout: `layout: api_path`. Renders the spec param JSON using Rapidoc. TODO: - Cleanup - Create additional templates that process page data for nav, filtering, links, code samples, etc. - Fix circular reference issues in specs that prevent generating a dereferenced JSON bundle (which could also help us our own custom UI...should we choose).
1 parent f368438 commit bd31940

File tree

11 files changed

+189
-343
lines changed

11 files changed

+189
-343
lines changed

api-docs/getswagger.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ function postProcess() {
116116
# npm_config_yes=true npx overrides the prompt
117117
# and (vs. npx --yes) is compatible with npm@6 and npm@7.
118118
specPath="$1"
119+
# Replace the .yml extension in specPath with .json.
120+
specJsonPath="${specPath%.yml}.json"
119121
configPath="$2"
120122
api="$3"
121123

@@ -136,8 +138,14 @@ function postProcess() {
136138
API_DOCS_ROOT_PATH=$API_DOCS_ROOT \
137139
npm_config_yes=true \
138140
npx $openapiCLI bundle $specPath \
139-
-o $specPath \
140-
--config=$configPath
141+
--config=$configPath \
142+
--ext yml \
143+
--output $specPath #\
144+
# TODO: Uncommend this after fixing the circular reference ($ref) issues in the specs.
145+
# && npx $openapiCLI bundle $specPath \
146+
# --dereferenced \
147+
# --ext json \
148+
# --output $specJsonPath
141149
}
142150

143151
function updateOSS {

api-docs/scripts/create-pages.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* This script generates markdown files for each endpoint in the
3+
* configured OpenAPI specs.
4+
*/
5+
const { execSync } = require('child_process');
6+
const path = require('path');
7+
const fs = require('fs');
8+
const yaml = require('js-yaml');
9+
10+
// Calculate the relative paths
11+
const DOCS_ROOT = process.env.DOCS_ROOT || '.';
12+
13+
// Function to execute shell commands
14+
const execCommand = (command) => {
15+
try {
16+
console.log(`Executing: ${command}`);
17+
execSync(command, { stdio: 'inherit' });
18+
} catch (error) {
19+
console.error(`Error executing command: ${command}`);
20+
process.exit(1);
21+
}
22+
};
23+
24+
const openapiUtils = {
25+
isPlaceholderFragment: function(str) {
26+
const placeholderRegex = new RegExp('^\{.*\}$');
27+
return placeholderRegex.test(str);
28+
}
29+
}
30+
31+
function getPathGroups(openapi) {
32+
const pathGroups = {};
33+
Object.keys(openapi.paths).sort()
34+
.forEach((p) => {
35+
const delimiter = '/';
36+
let key = p.split(delimiter);
37+
let isItemPath = openapiUtils.isPlaceholderFragment(key[key.length - 1]);
38+
if(isItemPath) {
39+
key = key.slice(0, -1);
40+
}
41+
key = (key.slice(0, 4))
42+
isItemPath = openapiUtils.isPlaceholderFragment(key[key.length - 1]);
43+
if(isItemPath) {
44+
key = key.slice(0, -1);
45+
}
46+
const groupKey = key.join('/');
47+
pathGroups[groupKey] = pathGroups[groupKey] || {};
48+
pathGroups[groupKey][p] = openapi.paths[p];
49+
});
50+
return pathGroups;
51+
}
52+
53+
function main() {
54+
/**
55+
* Configure the product specs to generate markdown files for.
56+
*/
57+
const config = {
58+
dataDir: path.join(DOCS_ROOT, '/data/api/influxdb'),
59+
apis: [
60+
{
61+
name: 'cloud-v2',
62+
menu: 'influxdb_cloud',
63+
// Source OpenAPI spec file
64+
spec_file: path.join(DOCS_ROOT, '/api-docs/cloud/v2/ref.yml'),
65+
// Target content directory for generated endpoint spec pages
66+
// endpoints_dir: path.join(DOCS_ROOT, '/content/influxdb/cloud/api/v2/yaml'),
67+
// Target content directory for generated .md pages
68+
pages_dir: path.join(DOCS_ROOT, '/content/influxdb/cloud/api/v2'),
69+
},
70+
{
71+
name: 'oss-v2',
72+
menu: 'influxdb_v2',
73+
spec_file: path.join(DOCS_ROOT, '/api-docs/v2/ref.yml'),
74+
// Target content directory for generated endpoint spec pages
75+
// endpoints_dir: path.join(DOCS_ROOT, '/content/influxdb/v2/api/v2/yaml'),
76+
// Target content directory for generated .md pages
77+
pages_dir: path.join(DOCS_ROOT, '/content/influxdb/v2/api/v2'),
78+
}
79+
]
80+
}
81+
82+
config.apis.forEach(api => {
83+
// Execute the getswagger.sh script to fetch and bundle the configured spec.
84+
execCommand(`${path.join(DOCS_ROOT, '/api-docs/getswagger.sh')} ${api.name} -B`);
85+
86+
// Remove old Hugo pages
87+
fs.rmSync(api.pages_dir, {recursive: true, force: true});
88+
89+
spec = yaml.load(fs.readFileSync(api.spec_file));
90+
const pathGroups = getPathGroups(spec);
91+
92+
if (!fs.existsSync(api.pages_dir)) {
93+
fs.mkdirSync(api.pages_dir, { recursive: true });
94+
};
95+
96+
Object.keys(pathGroups).forEach( pathGroup => {
97+
// Deep copy the spec object
98+
let pathSpec = JSON.parse(JSON.stringify(spec));
99+
pathSpec.paths = pathGroups[pathGroup];
100+
pathSpec['x-pathGroupTitle'] = `${pathGroup}\n${spec.info.title}`;
101+
pathSpec['x-pathGroup'] = pathGroup;
102+
103+
const pageParams = {
104+
type: 'api',
105+
title: pathSpec['x-pathGroupTitle'],
106+
description: pathSpec.info.description,
107+
api: {
108+
part_of: api.spec_file,
109+
spec: JSON.stringify(pathSpec),
110+
},
111+
}
112+
pageParams.menu = {};
113+
pageParams.menu[api.menu] = {
114+
parent: 'INFLUXDB HTTP API',
115+
weight: 1,
116+
name: pathGroup
117+
};
118+
119+
let frontMatter = JSON.stringify(pageParams);
120+
121+
const pageName = `${pathGroup.replaceAll('/', '-').replace(/^-/, '')}`;
122+
const pagePath = path.join(api.pages_dir, `${pageName}.md`);
123+
fs.writeFileSync(pagePath, frontMatter);
124+
console.log(`Created: ${pagePath}`);
125+
});
126+
});
127+
}
128+
129+
main();

api-docs/scripts/generate-openapi-articles.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

api-docs/scripts/openapi-docs/cli.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)