1
0
mirror of synced 2025-12-30 12:02:01 -05:00
Files
docs/lib/rest/index.js
github-openapi-bot 757066487d Update OpenAPI Descriptions (#30924)
Co-authored-by: github-openapi-bot <github-openapi-bot@users.noreply.github.com>
Co-authored-by: Rachael Sewell <rachmari@github.com>
Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: Grace Park <gracepark@Graces-MacBook-Pro-2.local>
Co-authored-by: Hector Alfaro <hectorsector@github.com>
Co-authored-by: Ashwin Jeyaseelan <8gitbrix@github.com>
Co-authored-by: hubwriter <hubwriter@github.com>
Co-authored-by: Sarita Iyer <66540150+saritai@users.noreply.github.com>
2022-09-19 10:28:23 -07:00

118 lines
3.7 KiB
JavaScript

import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
import { readCompressedJsonFileFallback } from '../read-json-file.js'
import { getAutomatedPageMiniTocItems } from '../get-mini-toc-items.js'
import { allVersions } from '../all-versions.js'
import languages from '../languages.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const schemasPath = path.join(__dirname, 'static/decorated')
const ENABLED_APPS_FILENAME = path.join(__dirname, 'static/apps/enabled-for-apps.json')
const contentPath = path.join(process.cwd(), 'content/rest')
/*
Loads the schemas from the static/decorated folder into a single
object organized by version.
Example:
{
[version]: {
category: {
subcategory: [operations],
}
}
}
*/
const restOperationData = new Map()
Object.keys(languages).forEach((language) => {
restOperationData.set(language, new Map())
Object.keys(allVersions).forEach((version) => {
// setting to undefined will allow us to perform checks
// more easily later on
restOperationData.get(language).set(version, new Map())
})
})
export const categoriesWithoutSubcategories = fs
.readdirSync(contentPath)
.filter((file) => {
return file.endsWith('.md') && !file.includes('index.md') && !file.includes('README.md')
})
.map((filteredFile) => filteredFile.replace('.md', ''))
const restOperations = new Map()
export default async function getRest(version, category, subCategory) {
const openApiVersion = getOpenApiVersion(version)
if (!restOperations.has(openApiVersion)) {
const filename = `${openApiVersion}.json`
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
restOperations.set(
openApiVersion,
readCompressedJsonFileFallback(path.join(schemasPath, filename))
)
}
if (subCategory) {
return restOperations.get(openApiVersion)[category][subCategory]
} else if (category) {
return restOperations.get(openApiVersion)[category]
} else {
return restOperations.get(openApiVersion)
}
}
function getDocsVersion(openApiVersion) {
const version = Object.values(allVersions).find(
(version) => version.openApiVersionName === openApiVersion
).version
return version
}
function getOpenApiVersion(version) {
if (!(version in allVersions)) {
throw new Error(`Unrecognized version '${version}'. Not found in ${Object.keys(allVersions)}`)
}
return allVersions[version].openApiVersionName
}
// Generates the miniToc for a rest reference page.
export async function getRestMiniTocItems(
category,
subCategory,
restOperations,
language,
version,
context
) {
if (!restOperationData.get(language).get(version).has(category)) {
restOperationData.get(language).get(version).set(category, new Map())
}
if (!restOperationData.get(language).get(version).get(category).get(subCategory)) {
const languageTree = restOperationData.get(language)
const titles = restOperations.map((operation) => operation.title)
const restOperationsMiniTocItems = await getAutomatedPageMiniTocItems(titles, context, 3)
languageTree.get(version).get(category).set(subCategory, {
restOperationsMiniTocItems,
})
restOperationData.set(restOperationData, languageTree)
}
return restOperationData.get(language).get(version).get(category).get(subCategory)
}
export async function getEnabledForApps() {
// The `readCompressedJsonFileFallback()` function
// will check for both a .br and .json extension.
const appsData = readCompressedJsonFileFallback(ENABLED_APPS_FILENAME)
for (const version in appsData) {
const docsVersion = getDocsVersion(version)
appsData[docsVersion] = appsData[version]
delete appsData[version]
}
return appsData
}