Skip to content

Commit

Permalink
use next cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shahednasser committed Oct 22, 2024
1 parent 8e40c66 commit 1695aae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
16 changes: 4 additions & 12 deletions www/apps/api-reference/app/api/schema/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { NextResponse } from "next/server"
import { SchemaObject } from "../../../types/openapi"
import path from "path"
import { existsSync, promises as fs } from "fs"
import { parseDocument } from "yaml"
import dereference from "../../../utils/dereference"
import { existsSync } from "fs"
import getSchemaContent from "../../../utils/get-schema-content"

export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
Expand Down Expand Up @@ -59,14 +57,8 @@ export async function GET(request: Request) {
)
}

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],
})
const { dereferencedDocument, originalSchema: schema } =
await getSchemaContent(schemaPath, baseSchemasPath)

return NextResponse.json(
{
Expand Down
13 changes: 12 additions & 1 deletion www/apps/api-reference/utils/get-paths-of-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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
29 changes: 29 additions & 0 deletions www/apps/api-reference/utils/get-schema-content.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1695aae

Please sign in to comment.