|
| 1 | +import type { FastifyInstance } from 'fastify' |
| 2 | +import { PostgresMeta } from '../../../lib/index.js' |
| 3 | +import { DEFAULT_POOL_CONFIG } from '../../constants.js' |
| 4 | +import { extractRequestForLogging } from '../../utils.js' |
| 5 | +import { apply as applySwiftTemplate, AccessControl } from '../../templates/swift.js' |
| 6 | +import { getGeneratorMetadata } from '../../../lib/generators.js' |
| 7 | + |
| 8 | +export default async (fastify: FastifyInstance) => { |
| 9 | + fastify.get<{ |
| 10 | + Headers: { pg: string } |
| 11 | + Querystring: { |
| 12 | + excluded_schemas?: string |
| 13 | + included_schemas?: string |
| 14 | + access_control?: AccessControl |
| 15 | + } |
| 16 | + }>('/', async (request, reply) => { |
| 17 | + const connectionString = request.headers.pg |
| 18 | + const excludedSchemas = |
| 19 | + request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? [] |
| 20 | + const includedSchemas = |
| 21 | + request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? [] |
| 22 | + const accessControl = request.query.access_control ?? 'internal' |
| 23 | + |
| 24 | + const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString }) |
| 25 | + const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, { |
| 26 | + includedSchemas, |
| 27 | + excludedSchemas, |
| 28 | + }) |
| 29 | + if (generatorMetaError) { |
| 30 | + request.log.error({ error: generatorMetaError, request: extractRequestForLogging(request) }) |
| 31 | + reply.code(500) |
| 32 | + return { error: generatorMetaError.message } |
| 33 | + } |
| 34 | + |
| 35 | + return applySwiftTemplate({ |
| 36 | + ...generatorMeta, |
| 37 | + accessControl, |
| 38 | + }) |
| 39 | + }) |
| 40 | +} |
0 commit comments