@ -1,16 +1,16 @@
import { json , error } from '@sveltejs/kit' ;
import { json } from '@sveltejs/kit' ;
import type { RequestHandler } from './$types' ;
import type { RequestHandler } from './$types' ;
import { readFileSync } from 'fs' ;
import { readFileSync , existsSync } from 'fs' ;
import { join } from 'path' ;
import { join } from 'path' ;
import { fileURLToPath } from 'url' ;
import { fileURLToPath } from 'url' ;
// Cache the spec in production
// Cache the spec
let cachedSpec : any = null ;
let cachedSpec : any = null ;
export const GET : RequestHandler = async ( ) = > {
export const GET : RequestHandler = async ( ) = > {
try {
try {
// Use cached spec if available (in production)
// Return cached spec if available
if ( cachedSpec && typeof process !== 'undefined' && process . env . NODE_ENV === 'production' ) {
if ( cachedSpec ) {
return json ( cachedSpec , {
return json ( cachedSpec , {
headers : {
headers : {
'Content-Type' : 'application/json' ,
'Content-Type' : 'application/json' ,
@ -19,58 +19,79 @@ export const GET: RequestHandler = async () => {
} ) ;
} ) ;
}
}
// Try to read the file using different path resolution methods
// Try to import directly first (works in most build scenarios)
let openApiSpec : any = null ;
try {
// @ts-ignore - Dynamic import for JSON
const imported = await import ( './openapi.json?raw' ) ;
const fileContent = typeof imported . default === 'string' ? imported.default : String ( imported . default || imported ) ;
cachedSpec = JSON . parse ( fileContent ) ;
return json ( cachedSpec , {
headers : {
'Content-Type' : 'application/json' ,
'Cache-Control' : 'public, max-age=3600'
}
} ) ;
} catch ( importErr ) {
// Fallback to file system read
console . log ( 'Direct import failed, trying file system read:' , importErr ) ;
}
// Otherwise, try reading from file system
let specPath : string ;
let lastError : Error | null = null ;
let lastError : Error | null = null ;
// Method 1: Try using fileURLToPath (works in most cases)
// Try method 1: Use import.meta.url (works in most cases)
try {
try {
const __dirname = fileURLToPath ( new URL ( '.' , import . meta . url ) ) ;
const __filename = fileURLToPath ( import . meta . url ) ;
const specPath = join ( __dirname , 'openapi.json' ) ;
const __dirname = join ( __filename , '..' ) ;
openApiSpec = JSON . parse ( readFileSync ( specPath , 'utf-8' ) ) ;
specPath = join ( __dirname , 'openapi.json' ) ;
console . log ( 'Trying path:' , specPath ) ;
if ( existsSync ( specPath ) ) {
const fileContent = readFileSync ( specPath , 'utf-8' ) ;
cachedSpec = JSON . parse ( fileContent ) ;
return json ( cachedSpec , {
headers : {
'Content-Type' : 'application/json' ,
'Cache-Control' : 'public, max-age=3600'
}
} ) ;
}
throw new Error ( ` File not found at ${ specPath } ` ) ;
} catch ( err ) {
} catch ( err ) {
lastError = err instanceof Error ? err : new Error ( String ( err ) ) ;
lastError = err instanceof Error ? err : new Error ( String ( err ) ) ;
console . log ( 'Method 1 failed:' , lastError . message ) ;
// Method 2: Try using process.cwd() (fallback for build environments)
// Try method 2: Use process.cwd() (fallback for production build s)
try {
try {
if ( typeof process !== 'undefined' && process . cwd ) {
if ( typeof process !== 'undefined' && process . cwd ) {
const specPath = join ( process . cwd ( ) , 'src/routes/api/openapi.json/openapi.json' ) ;
specPath = join ( process . cwd ( ) , 'src/routes/api/openapi.json/openapi.json' ) ;
openApiSpec = JSON . parse ( readFileSync ( specPath , 'utf-8' ) ) ;
console . log ( 'Trying path:' , specPath ) ;
if ( existsSync ( specPath ) ) {
const fileContent = readFileSync ( specPath , 'utf-8' ) ;
cachedSpec = JSON . parse ( fileContent ) ;
return json ( cachedSpec , {
headers : {
'Content-Type' : 'application/json' ,
'Cache-Control' : 'public, max-age=3600'
}
} ) ;
}
throw new Error ( ` File not found at ${ specPath } ` ) ;
} else {
throw lastError ;
}
}
} catch ( err2 ) {
} catch ( err2 ) {
lastError = err2 instanceof Error ? err2 : new Error ( String ( err2 ) ) ;
lastError = err2 instanceof Error ? err2 : new Error ( String ( err2 ) ) ;
console . log ( 'Method 2 failed:' , lastError . message ) ;
// Method 3: Try relative to import.meta.url parent
throw new Error ( ` Failed to locate openapi.json. Tried multiple paths. Last error: ${ lastError . message } ` ) ;
try {
const __filename = fileURLToPath ( import . meta . url ) ;
const __dirname = join ( __filename , '..' ) ;
const specPath = join ( __dirname , 'openapi.json' ) ;
openApiSpec = JSON . parse ( readFileSync ( specPath , 'utf-8' ) ) ;
} catch ( err3 ) {
lastError = err3 instanceof Error ? err3 : new Error ( String ( err3 ) ) ;
throw new Error ( ` Failed to locate openapi.json file. Tried multiple paths. Last error: ${ lastError . message } ` ) ;
}
}
}
}
}
if ( ! openApiSpec ) {
throw new Error ( 'Failed to load OpenAPI specification' ) ;
}
// Cache for production
if ( typeof process !== 'undefined' && process . env . NODE_ENV === 'production' ) {
cachedSpec = openApiSpec ;
}
return json ( openApiSpec , {
headers : {
'Content-Type' : 'application/json' ,
'Cache-Control' : 'public, max-age=3600'
}
} ) ;
} catch ( err ) {
} catch ( err ) {
const errorMessage = err instanceof Error ? err.message : String ( err ) ;
const errorMessage = err instanceof Error ? err.message : String ( err ) ;
console . error ( 'Error reading openapi.json:' , errorMessage ) ;
console . error ( 'Error reading openapi.json:' , errorMessage ) ;
return error ( 500 , ` Failed to load OpenAPI specification: ${ errorMessage } ` ) ;
return json (
{ error : 'Failed to load OpenAPI specification' , details : errorMessage } ,
{ status : 500 }
) ;
}
}
} ;
} ;