generate-docs.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { generateFiles } from "fumadocs-openapi";
  2. import { openapi, products } from "../src/lib/openapi";
  3. const outDir = "./content/docs/openapi/";
  4. export async function generateDocs() {
  5. await generateFiles({
  6. input: openapi,
  7. output: outDir,
  8. per: "operation",
  9. name: (output, document) => {
  10. // Extract product name from the OpenAPI document info
  11. const productName = getProductName(document.info.title);
  12. if (output.type === "operation") {
  13. const operation =
  14. document.paths?.[output.item.path]?.[output.item.method];
  15. const operationId =
  16. operation?.operationId ??
  17. output.item.path.replaceAll(/[^a-zA-Z0-9]/g, "_");
  18. return `${productName}/${operationId}`;
  19. }
  20. return `${productName}/webhooks/${output.item.name}`;
  21. },
  22. index: {
  23. url: {
  24. baseUrl: "/openapi/",
  25. contentDir: "./content/docs/openapi",
  26. },
  27. items: Object.keys(products).map((productName) => ({
  28. path: `${productName}/index.mdx`,
  29. })),
  30. },
  31. });
  32. }
  33. function getProductName(title: string) {
  34. // Match the title to a product name
  35. const titleLower = title.toLowerCase();
  36. for (const [name] of Object.entries(products)) {
  37. if (titleLower.includes(name)) {
  38. return name;
  39. }
  40. }
  41. return "unknown";
  42. }
  43. await generateDocs();