docs: fixes and refactoring for API reference (#9708)

* docs: fixes and refactoring for API reference

* add route caching

* remove caching

* use next cache
This commit is contained in:
Shahed Nasser
2024-10-22 18:20:06 +03:00
committed by GitHub
parent 6b989353ac
commit b2122c4073
20 changed files with 377 additions and 277 deletions
@@ -53,6 +53,9 @@ export default async function dereference({
canParse: /.*/,
},
},
dereference: {
circular: "ignore",
},
})) as unknown as Document
return document
@@ -5,8 +5,9 @@ import type { Operation, Document, ParsedPathItemObject } from "@/types/openapi"
import readSpecDocument from "./read-spec-document"
import getSectionId from "./get-section-id"
import dereference from "./dereference"
import { unstable_cache } from "next/cache"
export default async function getPathsOfTag(
async function getPathsOfTag_(
tagName: string,
area: string
): Promise<Document> {
@@ -47,3 +48,13 @@ export default async function getPathsOfTag(
paths: documents,
})
}
const getPathsOfTag = unstable_cache(
async (tagName: string, area: string) => getPathsOfTag_(tagName, area),
["tag-paths"],
{
revalidate: 3600,
}
)
export default getPathsOfTag
@@ -0,0 +1,29 @@
import { promises as fs } from "fs"
import { parseDocument } from "yaml"
import { SchemaObject } from "../types/openapi"
import dereference from "./dereference"
import { unstable_cache } from "next/cache"
async function getSchemaContent_(schemaPath: string, baseSchemasPath: string) {
const schemaContent = await fs.readFile(schemaPath, "utf-8")
const schema = parseDocument(schemaContent).toJS() as SchemaObject
// resolve references in schema
const dereferencedDocument = await dereference({
basePath: baseSchemasPath,
schemas: [schema],
})
return {
dereferencedDocument,
originalSchema: schema,
}
}
const getSchemaContent = unstable_cache(
async (schemaPath: string, baseSchemasPath: string) =>
getSchemaContent_(schemaPath, baseSchemasPath),
["tag-schema"]
)
export default getSchemaContent