UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

120 lines (119 loc) 3.07 kB
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { extractRequestBody, createParameterMap } from "../../../helpers/openapi.js"; function createSearchIndex(document) { const index = []; function processEntries(entriesToProcess) { entriesToProcess.forEach((entry) => { addEntryToIndex(entry, index, document); if ("children" in entry && entry.children) { processEntries(entry.children); } }); } processEntries(document?.["x-scalar-navigation"]?.children ?? []); return index; } function addEntryToIndex(entry, index, document) { if (entry.type === "operation") { const operation = getResolvedRef(document?.paths?.[entry.path]?.[entry.method]) ?? {}; const requestBodyOrParameterMap = extractRequestBody(operation) || createParameterMap(operation); const body = typeof requestBodyOrParameterMap !== "boolean" ? requestBodyOrParameterMap : null; index.push({ type: "operation", title: entry.title, id: entry.id, description: operation.description || "", method: entry.method, path: entry.path, body: body || "", operationId: operation.operationId, entry }); return; } if (entry.type === "webhook") { const webhook = getResolvedRef(document?.webhooks?.[entry.name]?.[entry.method]) ?? {}; index.push({ id: entry.id, type: "webhook", title: entry.title, description: "Webhook", method: entry.method, body: webhook.description || "", operationId: webhook.operationId, entry }); return; } if (entry.type === "model") { const schema = getResolvedRef(document?.components?.schemas?.[entry.name]); const description = schema?.description ?? ""; index.push({ type: "model", title: entry.title, description: "Model", id: entry.id, body: description, entry }); return; } if (entry.type === "models") { index.push({ id: entry.id, type: "heading", title: "Models", description: "Heading", body: "", entry }); return; } if (entry.type === "tag" && entry.isWebhooks === true) { index.push({ id: entry.id, type: "heading", title: "Webhooks", description: "Heading", body: "", entry }); return; } if (entry.type === "tag" && entry.isGroup === false) { index.push({ id: entry.id, title: entry.title, description: entry.description || "", type: "tag", body: "", entry }); return; } if (entry.type === "tag" && entry.isGroup === true) { index.push({ id: entry.id, title: entry.title, description: "Tag Group", type: "tag", body: "", entry }); return; } if (entry.type === "text") { index.push({ id: entry.id, type: "heading", title: entry.title ?? "", description: "Heading", body: "", entry }); return; } } export { createSearchIndex };