Skip to content

Commit

Permalink
fix(openapi): type fixes in prepareOas (#1141)
Browse files Browse the repository at this point in the history
## 🧰 Changes

when reviewing #1126, i noticed a
few small bugs in our `prepareOas` helper function. this PR makes a few
typesafety improvements so we shouldn't run into this issue again(?)

~~one outstanding question that i'd like to figure out prior to merge,
see #1142 (**edit**: going to
continue bundling and handle this issue in a follow-up PR)

## 🧬 QA & Testing

do tests + types pass?
  • Loading branch information
kanadgupta authored Jan 6, 2025
1 parent 9805505 commit fc55d28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/commands/openapi/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
async run() {
const { spec } = this.args;

const { preparedSpec, specFileType, specPath, specVersion } = await prepareOas(spec, 'openapi');
const { preparedSpec, specFileType, specPath, specVersion } = await prepareOas(spec, 'openapi upload');

const version = this.flags.useSpecVersion ? specVersion : this.flags.version;

Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ export const COMMANDS = {

whoami: WhoAmICommand,
};

/**
* A type-safe way to get the command IDs in the CLI for a specific topic.
*
* @example type OpenAPIAction = CommandIdForTopic<'openapi'>;
*/
export type CommandIdForTopic<
T extends 'openapi',
U extends keyof typeof COMMANDS = keyof typeof COMMANDS,
> = U extends `${T}:${infer Suffix}` ? `${Suffix}` : never;
18 changes: 8 additions & 10 deletions src/lib/prepareOas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CommandIdForTopic } from '../index.js';
import type { OpenAPI } from 'openapi-types';

import chalk from 'chalk';
Expand Down Expand Up @@ -36,6 +37,8 @@ function truthy<T>(value: T): value is Truthy<T> {
return !!value;
}

type OpenAPIAction = CommandIdForTopic<'openapi'>;

const capitalizeSpecType = (type: string) =>
type === 'openapi' ? 'OpenAPI' : type.charAt(0).toUpperCase() + type.slice(1);

Expand All @@ -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: {
/**
* An optional title to replace the value in the `info.title` field.
Expand Down Expand Up @@ -78,14 +81,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 =>
Expand Down Expand Up @@ -216,7 +212,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');
Expand Down

0 comments on commit fc55d28

Please sign in to comment.