Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | const fs = require('fs') const path = require('path') // Read the spec at cold-start time — it is bundled with the Lambda code asset let specContent const getSpec = () => { if (!specContent) { // When bundled from project root: // __dirname = /var/task/infrastructure/lambda/docs // api-definition.yaml is at /var/task/api-definition.yaml const raw = fs.readFileSync(path.join(__dirname, '../../../api-definition.yaml'), 'utf-8') // Override the servers list to only show the current environment so that // Swagger UI does not propose a choice between prod and dev. // We replace the block directly in the raw YAML — no parser needed. const apiBaseUrl = process.env.API_BASE_URL if (apiBaseUrl) { specContent = raw.replace( /^servers:[\s\S]*?(?=^\w)/m, `servers:\n - url: ${apiBaseUrl}\n description: Current environment\n` ) } else { specContent = raw } } return specContent } exports.handler = async () => { try { return { statusCode: 200, headers: { 'Content-Type': 'application/yaml', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'public, max-age=300' }, body: getSpec() } } catch (error) { console.error('Error serving OpenAPI spec:', error) return { statusCode: 500, headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, body: JSON.stringify({ error: 'Failed to load API spec' }) } } } |