Browse Source
Nostr-Signature: 9a1ba983e0b0db8cff3675a078a376df5c9ad351c3988ea893f3e8084a65a1e6 573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc 724a326cbd6a33f1ff6a2c37b242c7571e35149281609e9eb1c6a197422a13834d9ac2f5d0719026bc66126bd0022df49adf50aa08af93dd95076f407b0f0456main
6 changed files with 363 additions and 22 deletions
@ -1,21 +1,76 @@
@@ -1,21 +1,76 @@
|
||||
import { json } from '@sveltejs/kit'; |
||||
import { json, error } from '@sveltejs/kit'; |
||||
import type { RequestHandler } from './$types'; |
||||
import { readFileSync } from 'fs'; |
||||
import { join } from 'path'; |
||||
import { fileURLToPath } from 'url'; |
||||
|
||||
// Read the file dynamically to avoid Vite caching issues
|
||||
const __dirname = fileURLToPath(new URL('.', import.meta.url)); |
||||
const specPath = join(__dirname, 'openapi.json'); |
||||
// Cache the spec in production
|
||||
let cachedSpec: any = null; |
||||
|
||||
export const GET: RequestHandler = async () => { |
||||
// Read file fresh on each request to avoid cache issues during development
|
||||
const openApiSpec = JSON.parse(readFileSync(specPath, 'utf-8')); |
||||
try { |
||||
// Use cached spec if available (in production)
|
||||
if (cachedSpec && typeof process !== 'undefined' && process.env.NODE_ENV === 'production') { |
||||
return json(cachedSpec, { |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
'Cache-Control': 'public, max-age=3600' |
||||
} |
||||
}); |
||||
} |
||||
|
||||
// Try to read the file using different path resolution methods
|
||||
let openApiSpec: any = null; |
||||
let lastError: Error | null = null; |
||||
|
||||
// Method 1: Try using fileURLToPath (works in most cases)
|
||||
try { |
||||
const __dirname = fileURLToPath(new URL('.', import.meta.url)); |
||||
const specPath = join(__dirname, 'openapi.json'); |
||||
openApiSpec = JSON.parse(readFileSync(specPath, 'utf-8')); |
||||
} catch (err) { |
||||
lastError = err instanceof Error ? err : new Error(String(err)); |
||||
|
||||
return json(openApiSpec, { |
||||
headers: { |
||||
'Content-Type': 'application/json', |
||||
'Cache-Control': 'public, max-age=3600' |
||||
// Method 2: Try using process.cwd() (fallback for build environments)
|
||||
try { |
||||
if (typeof process !== 'undefined' && process.cwd) { |
||||
const specPath = join(process.cwd(), 'src/routes/api/openapi.json/openapi.json'); |
||||
openApiSpec = JSON.parse(readFileSync(specPath, 'utf-8')); |
||||
} |
||||
} catch (err2) { |
||||
lastError = err2 instanceof Error ? err2 : new Error(String(err2)); |
||||
|
||||
// Method 3: Try relative to import.meta.url parent
|
||||
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) { |
||||
const errorMessage = err instanceof Error ? err.message : String(err); |
||||
console.error('Error reading openapi.json:', errorMessage); |
||||
return error(500, `Failed to load OpenAPI specification: ${errorMessage}`); |
||||
} |
||||
}; |
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue