|
| 1 | +import path from 'node:path'; |
| 2 | + |
| 3 | +import { generateLegacyOutput, generateOutput } from './generate/output'; |
| 4 | +import { getSpec } from './getSpec'; |
| 5 | +import type { IR } from './ir/types'; |
| 6 | +import { parseLegacy, parseOpenApiSpec } from './openApi'; |
| 7 | +import { processOutput } from './processOutput'; |
| 8 | +import type { Client } from './types/client'; |
| 9 | +import type { Config } from './types/config'; |
| 10 | +import type { WatchValues } from './types/types'; |
| 11 | +import { isLegacyClient, legacyNameFromConfig } from './utils/config'; |
| 12 | +import type { Templates } from './utils/handlebars'; |
| 13 | +import { Performance } from './utils/performance'; |
| 14 | +import { postProcessClient } from './utils/postprocess'; |
| 15 | + |
| 16 | +export const createClient = async ({ |
| 17 | + config, |
| 18 | + templates, |
| 19 | + watch: _watch, |
| 20 | +}: { |
| 21 | + config: Config; |
| 22 | + templates: Templates; |
| 23 | + watch?: WatchValues; |
| 24 | +}) => { |
| 25 | + const inputPath = config.input.path; |
| 26 | + const timeout = config.watch.timeout; |
| 27 | + |
| 28 | + const watch: WatchValues = _watch || { headers: new Headers() }; |
| 29 | + |
| 30 | + Performance.start('spec'); |
| 31 | + const { data, error, response } = await getSpec({ |
| 32 | + inputPath, |
| 33 | + timeout, |
| 34 | + watch, |
| 35 | + }); |
| 36 | + Performance.end('spec'); |
| 37 | + |
| 38 | + // throw on first run if there's an error to preserve user experience |
| 39 | + // if in watch mode, subsequent errors won't throw to gracefully handle |
| 40 | + // cases where server might be reloading |
| 41 | + if (error && !_watch) { |
| 42 | + throw new Error( |
| 43 | + `Request failed with status ${response.status}: ${response.statusText}`, |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + let client: Client | undefined; |
| 48 | + let context: IR.Context | undefined; |
| 49 | + |
| 50 | + if (data) { |
| 51 | + if (_watch) { |
| 52 | + console.clear(); |
| 53 | + console.log(`⏳ Input changed, generating from ${inputPath}`); |
| 54 | + } else { |
| 55 | + console.log(`⏳ Generating from ${inputPath}`); |
| 56 | + } |
| 57 | + |
| 58 | + Performance.start('parser'); |
| 59 | + if ( |
| 60 | + config.experimentalParser && |
| 61 | + !isLegacyClient(config) && |
| 62 | + !legacyNameFromConfig(config) |
| 63 | + ) { |
| 64 | + context = parseOpenApiSpec({ config, spec: data }); |
| 65 | + } |
| 66 | + |
| 67 | + // fallback to legacy parser |
| 68 | + if (!context) { |
| 69 | + const parsed = parseLegacy({ openApi: data }); |
| 70 | + client = postProcessClient(parsed, config); |
| 71 | + } |
| 72 | + Performance.end('parser'); |
| 73 | + |
| 74 | + Performance.start('generator'); |
| 75 | + if (context) { |
| 76 | + await generateOutput({ context }); |
| 77 | + } else if (client) { |
| 78 | + await generateLegacyOutput({ client, openApi: data, templates }); |
| 79 | + } |
| 80 | + Performance.end('generator'); |
| 81 | + |
| 82 | + Performance.start('postprocess'); |
| 83 | + if (!config.dryRun) { |
| 84 | + processOutput({ config }); |
| 85 | + |
| 86 | + const outputPath = process.env.INIT_CWD |
| 87 | + ? `./${path.relative(process.env.INIT_CWD, config.output.path)}` |
| 88 | + : config.output.path; |
| 89 | + console.log(`🚀 Done! Your output is in ${outputPath}`); |
| 90 | + } |
| 91 | + Performance.end('postprocess'); |
| 92 | + } |
| 93 | + |
| 94 | + if (config.watch.enabled && typeof inputPath === 'string') { |
| 95 | + setTimeout(() => { |
| 96 | + createClient({ config, templates, watch }); |
| 97 | + }, config.watch.interval); |
| 98 | + } |
| 99 | + |
| 100 | + return context || client; |
| 101 | +}; |
0 commit comments