Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(openapi): type fixes in prepareOas #1141

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {
/**
* Optionally convert the supplied or discovered API definition to the latest OpenAPI release.
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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'];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧐 do we want to bundle by default for openapi upload? see #1142

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we have to because if we don't then they might upload an invalid spec that has ref pointers to a local file


if (commandsThatBundle.includes(command)) {
api = await oas.bundle();

debug('spec bundled');
Expand Down
Loading