import type { PostProcessor } from '@hey-api/shared'; import { resolveSource, valueToObject } from '@hey-api/shared'; import type { MaybeArray } from '@hey-api/types'; import { postProcessors } from './postprocess'; import type { Output, UserOutput } from './types'; export function getOutput(userConfig: { output: MaybeArray }): Output { if (userConfig.output instanceof Array) { throw new Error( 'Unexpected array of outputs in user configuration. This should have been expanded already.', ); } const userOutput = typeof userConfig.output === 'string' ? { path: userConfig.output } : (userConfig.output ?? {}); const output = valueToObject({ defaultValue: { clean: true, entryFile: true, fileName: { case: 'preserve', name: '{{name}}', suffix: '_gen', }, header: '# This file is auto-generated by @hey-api/openapi-python', path: '', postProcess: [], preferExportAll: false, }, mappers: { object: (fields, defaultValue) => ({ ...fields, fileName: valueToObject({ defaultValue: { ...(defaultValue.fileName as Extract< typeof defaultValue.fileName, Record >), }, mappers: { function: (name) => ({ name }), string: (name) => ({ name }), }, value: fields.fileName, }), }), }, value: userOutput, }) as Output; if (output.importFileExtension && !output.importFileExtension.startsWith('.')) { output.importFileExtension = `.${output.importFileExtension}`; } output.postProcess = normalizePostProcess(userOutput.postProcess); output.source = resolveSource(output); return output; } function normalizePostProcess(input: UserOutput['postProcess']): ReadonlyArray { if (!input) return []; return input.map((item) => { if (typeof item === 'string') { const preset = postProcessors[item]; if (!preset) { throw new Error(`Unknown post-processor preset: "${item}"`); } return preset; } return { name: item.name ?? item.command, ...item, }; }); }