fork of hey-api/openapi-ts because I need some additional things
1import type { PostProcessor } from '@hey-api/shared';
2import { resolveSource, valueToObject } from '@hey-api/shared';
3import type { MaybeArray } from '@hey-api/types';
4
5import { postProcessors } from './postprocess';
6import type { Output, UserOutput } from './types';
7
8export function getOutput(userConfig: { output: MaybeArray<string | UserOutput> }): Output {
9 if (userConfig.output instanceof Array) {
10 throw new Error(
11 'Unexpected array of outputs in user configuration. This should have been expanded already.',
12 );
13 }
14
15 const userOutput =
16 typeof userConfig.output === 'string' ? { path: userConfig.output } : (userConfig.output ?? {});
17
18 const output = valueToObject({
19 defaultValue: {
20 clean: true,
21 entryFile: true,
22 fileName: {
23 case: 'preserve',
24 name: '{{name}}',
25 suffix: '_gen',
26 },
27 header: '# This file is auto-generated by @hey-api/openapi-python',
28 path: '',
29 postProcess: [],
30 preferExportAll: false,
31 },
32 mappers: {
33 object: (fields, defaultValue) => ({
34 ...fields,
35 fileName: valueToObject({
36 defaultValue: {
37 ...(defaultValue.fileName as Extract<
38 typeof defaultValue.fileName,
39 Record<string, unknown>
40 >),
41 },
42 mappers: {
43 function: (name) => ({ name }),
44 string: (name) => ({ name }),
45 },
46 value: fields.fileName,
47 }),
48 }),
49 },
50 value: userOutput,
51 }) as Output;
52 if (output.importFileExtension && !output.importFileExtension.startsWith('.')) {
53 output.importFileExtension = `.${output.importFileExtension}`;
54 }
55 output.postProcess = normalizePostProcess(userOutput.postProcess);
56 output.source = resolveSource(output);
57 return output;
58}
59
60function normalizePostProcess(input: UserOutput['postProcess']): ReadonlyArray<PostProcessor> {
61 if (!input) return [];
62
63 return input.map((item) => {
64 if (typeof item === 'string') {
65 const preset = postProcessors[item];
66 if (!preset) {
67 throw new Error(`Unknown post-processor preset: "${item}"`);
68 }
69 return preset;
70 }
71 return {
72 name: item.name ?? item.command,
73 ...item,
74 };
75 });
76}