Skip to content

Commit 1695aae

Browse files
committed
use next cache
1 parent 8e40c66 commit 1695aae

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

www/apps/api-reference/app/api/schema/route.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { NextResponse } from "next/server"
2-
import { SchemaObject } from "../../../types/openapi"
32
import path from "path"
4-
import { existsSync, promises as fs } from "fs"
5-
import { parseDocument } from "yaml"
6-
import dereference from "../../../utils/dereference"
3+
import { existsSync } from "fs"
4+
import getSchemaContent from "../../../utils/get-schema-content"
75

86
export async function GET(request: Request) {
97
const { searchParams } = new URL(request.url)
@@ -59,14 +57,8 @@ export async function GET(request: Request) {
5957
)
6058
}
6159

62-
const schemaContent = await fs.readFile(schemaPath, "utf-8")
63-
const schema = parseDocument(schemaContent).toJS() as SchemaObject
64-
65-
// resolve references in schema
66-
const dereferencedDocument = await dereference({
67-
basePath: baseSchemasPath,
68-
schemas: [schema],
69-
})
60+
const { dereferencedDocument, originalSchema: schema } =
61+
await getSchemaContent(schemaPath, baseSchemasPath)
7062

7163
return NextResponse.json(
7264
{

www/apps/api-reference/utils/get-paths-of-tag.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type { Operation, Document, ParsedPathItemObject } from "@/types/openapi"
55
import readSpecDocument from "./read-spec-document"
66
import getSectionId from "./get-section-id"
77
import dereference from "./dereference"
8+
import { unstable_cache } from "next/cache"
89

9-
export default async function getPathsOfTag(
10+
async function getPathsOfTag_(
1011
tagName: string,
1112
area: string
1213
): Promise<Document> {
@@ -47,3 +48,13 @@ export default async function getPathsOfTag(
4748
paths: documents,
4849
})
4950
}
51+
52+
const getPathsOfTag = unstable_cache(
53+
async (tagName: string, area: string) => getPathsOfTag_(tagName, area),
54+
["tag-paths"],
55+
{
56+
revalidate: 3600,
57+
}
58+
)
59+
60+
export default getPathsOfTag
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { promises as fs } from "fs"
2+
import { parseDocument } from "yaml"
3+
import { SchemaObject } from "../types/openapi"
4+
import dereference from "./dereference"
5+
import { unstable_cache } from "next/cache"
6+
7+
async function getSchemaContent_(schemaPath: string, baseSchemasPath: string) {
8+
const schemaContent = await fs.readFile(schemaPath, "utf-8")
9+
const schema = parseDocument(schemaContent).toJS() as SchemaObject
10+
11+
// resolve references in schema
12+
const dereferencedDocument = await dereference({
13+
basePath: baseSchemasPath,
14+
schemas: [schema],
15+
})
16+
17+
return {
18+
dereferencedDocument,
19+
originalSchema: schema,
20+
}
21+
}
22+
23+
const getSchemaContent = unstable_cache(
24+
async (schemaPath: string, baseSchemasPath: string) =>
25+
getSchemaContent_(schemaPath, baseSchemasPath),
26+
["tag-schema"]
27+
)
28+
29+
export default getSchemaContent

0 commit comments

Comments
 (0)