11import { createSwaggerSpec } from "next-swagger-doc" ;
22
3+ interface OperationObject {
4+ summary ?: string ;
5+ description ?: string ;
6+ parameters ?: Array < {
7+ name : string ;
8+ in : "query" | "header" | "path" | "cookie" ;
9+ description ?: string ;
10+ required ?: boolean ;
11+ schema ?: {
12+ type : string ;
13+ format ?: string ;
14+ } ;
15+ } > ;
16+ responses : {
17+ [ statusCode : string ] : {
18+ description : string ;
19+ content ?: {
20+ [ mediaType : string ] : {
21+ schema : {
22+ type : string ;
23+ properties ?: Record < string , unknown > ;
24+ } ;
25+ } ;
26+ } ;
27+ } ;
28+ } ;
29+ }
30+
31+ interface PathObject {
32+ get ?: OperationObject ;
33+ post ?: OperationObject ;
34+ put ?: OperationObject ;
35+ delete ?: OperationObject ;
36+ patch ?: OperationObject ;
37+ options ?: OperationObject ;
38+ head ?: OperationObject ;
39+ }
40+
41+ interface OpenAPISpec {
42+ openapi : string ;
43+ info : {
44+ title : string ;
45+ version : string ;
46+ description : string ;
47+ } ;
48+ servers : Array < {
49+ url : string ;
50+ description : string ;
51+ } > ;
52+ paths : Record < string , PathObject > ;
53+ components ?: {
54+ schemas ?: Record < string , unknown > ;
55+ responses ?: Record < string , unknown > ;
56+ parameters ?: Record < string , unknown > ;
57+ securitySchemes ?: Record < string , unknown > ;
58+ } ;
59+ }
60+
61+ // Cache the generated spec to avoid re-computation on every request
62+ const cachedSpec = {
63+ current : null as OpenAPISpec | null
64+ } ;
65+
366export const getApiDocs = ( ) => {
4- const spec = createSwaggerSpec ( {
5- apiFolder : "app/api" , // Path to API folder
67+ // Return cached spec if available
68+ if ( cachedSpec . current ) {
69+ return cachedSpec . current ;
70+ }
71+
72+ // Generate spec with optimized settings
73+ const generatedSpec = createSwaggerSpec ( {
74+ apiFolder : "app/api" ,
675 definition : {
776 openapi : "3.0.0" ,
877 info : {
@@ -12,11 +81,20 @@ export const getApiDocs = () => {
1281 } ,
1382 servers : [
1483 {
15- url : "http://localhost:3000" ,
16- description : "Development server "
84+ url : process . env . NEXT_PUBLIC_API_URL || "http://localhost:3000" ,
85+ description : "API Server "
1786 }
1887 ]
88+ } ,
89+ // Add cache settings
90+ cache : {
91+ maxAge : 60 * 60 * 1000 , // 1 hour
92+ cacheControl : "public, max-age=3600"
1993 }
20- } ) ;
21- return spec ;
94+ } ) as OpenAPISpec ;
95+
96+ // Cache the generated spec
97+ cachedSpec . current = generatedSpec ;
98+
99+ return cachedSpec . current ;
22100} ;
0 commit comments