diff --git a/src/commands/openapi/upload.ts b/src/commands/openapi/upload.ts index d594902cb..0bcc54ac4 100644 --- a/src/commands/openapi/upload.ts +++ b/src/commands/openapi/upload.ts @@ -101,7 +101,7 @@ export default class OpenAPIUploadCommand extends BaseCommand; + */ +export type CommandIdForTopic< + T extends 'openapi', + U extends keyof typeof COMMANDS = keyof typeof COMMANDS, +> = U extends `${T}:${infer Suffix}` ? `${Suffix}` : never; diff --git a/src/lib/prepareOas.ts b/src/lib/prepareOas.ts index 555bc0a88..845bfb260 100644 --- a/src/lib/prepareOas.ts +++ b/src/lib/prepareOas.ts @@ -1,3 +1,4 @@ +import type { CommandIdForTopic } from '../index.js'; import type { OpenAPI } from 'openapi-types'; import chalk from 'chalk'; @@ -36,6 +37,8 @@ function truthy(value: T): value is Truthy { return !!value; } +type OpenAPIAction = CommandIdForTopic<'openapi'>; + const capitalizeSpecType = (type: string) => type === 'openapi' ? 'OpenAPI' : type.charAt(0).toUpperCase() + type.slice(1); @@ -49,7 +52,7 @@ const capitalizeSpecType = (type: string) => */ export default async function prepareOas( path: string | undefined, - command: 'openapi convert' | 'openapi inspect' | 'openapi reduce' | 'openapi validate' | 'openapi', + command: `openapi ${OpenAPIAction}`, opts: { /** * Optionally convert the supplied or discovered API definition to the latest OpenAPI release. @@ -84,14 +87,7 @@ export default async function prepareOas( const fileFindingSpinner = ora({ text: 'Looking for API definitions...', ...oraOptions() }).start(); - let action: 'convert' | 'inspect' | 'reduce' | 'upload' | 'validate'; - switch (command) { - case 'openapi': - action = 'upload'; - break; - default: - action = command.split(' ')[1] as 'convert' | 'inspect' | 'reduce' | 'validate'; - } + const action: OpenAPIAction = command.replace('openapi ', '') as OpenAPIAction; const jsonAndYamlFiles = readdirRecursive('.', true).filter( file => @@ -213,7 +209,9 @@ export default async function prepareOas( const specVersion: string = api.info.version; debug(`version in spec: ${specVersion}`); - if (['openapi', 'openapi inspect', 'openapi reduce'].includes(command)) { + const commandsThatBundle: (typeof command)[] = ['openapi inspect', 'openapi reduce', 'openapi upload']; + + if (commandsThatBundle.includes(command)) { api = await oas.bundle(); debug('spec bundled');