11import type { OpenAPI } from 'openapi-types' ;
2- import type { CommandIdForTopic } from '../index.js' ;
2+ import type { CommandIdForTopic , OpenAPICommands } from '../index.js' ;
33
44import chalk from 'chalk' ;
55import OASNormalize from 'oas-normalize' ;
66import { getAPIDefinitionType } from 'oas-normalize/lib/utils' ;
77import ora from 'ora' ;
88
99import isCI from './isCI.js' ;
10- import { debug , info , oraOptions } from './logger.js' ;
10+ import { oraOptions } from './logger.js' ;
1111import promptTerminal from './promptWrapper.js' ;
1212import readdirRecursive from './readdirRecursive.js' ;
1313
@@ -49,26 +49,9 @@ function capitalizeSpecType<T extends 'openapi' | 'postman' | 'swagger' | 'unkno
4949/**
5050 * Normalizes, validates, and (optionally) bundles an OpenAPI definition.
5151 */
52- export default async function prepareOas (
53- /**
54- * Path to a spec file. If this is missing, the current directory is searched for
55- * certain file names.
56- */
57- path : string | undefined ,
58- /**
59- * The command context in which this is being run within (uploading a spec,
60- * validation, or reducing one).
61- */
62- command : `openapi ${OpenAPIAction } `,
63- opts : {
64- /**
65- * An optional title to replace the value in the `info.title` field.
66- * @see {@link https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#info-object }
67- */
68- title ?: string ;
69- } = { } ,
70- ) {
71- let specPath = path ;
52+ export default async function prepareOas ( this : OpenAPICommands ) {
53+ let specPath = this . args . spec ;
54+ const command = this . id satisfies `openapi ${OpenAPIAction } `;
7255
7356 if ( ! specPath ) {
7457 /**
@@ -97,31 +80,31 @@ export default async function prepareOas(
9780 file . toLowerCase ( ) . endsWith ( '.yml' ) ,
9881 ) ;
9982
100- debug ( `number of JSON or YAML files found: ${ jsonAndYamlFiles . length } ` ) ;
83+ this . debug ( `number of JSON or YAML files found: ${ jsonAndYamlFiles . length } ` ) ;
10184
10285 const possibleSpecFiles : FoundSpecFile [ ] = (
10386 await Promise . all (
10487 jsonAndYamlFiles . map ( file => {
105- debug ( `attempting to oas-normalize ${ file } ` ) ;
88+ this . debug ( `attempting to oas-normalize ${ file } ` ) ;
10689 const oas = new OASNormalize ( file , { enablePaths : true } ) ;
10790 return oas
10891 . version ( )
10992 . then ( ( { specification, version } ) => {
110- debug ( `specification type for ${ file } : ${ specification } ` ) ;
111- debug ( `version for ${ file } : ${ version } ` ) ;
93+ this . debug ( `specification type for ${ file } : ${ specification } ` ) ;
94+ this . debug ( `version for ${ file } : ${ version } ` ) ;
11295 return [ 'openapi' , 'swagger' , 'postman' ] . includes ( specification )
11396 ? { filePath : file , specType : capitalizeSpecType ( specification ) as SpecType , version }
11497 : null ;
11598 } )
11699 . catch ( e => {
117- debug ( `error extracting API definition specification version for ${ file } : ${ e . message } ` ) ;
100+ this . debug ( `error extracting API definition specification version for ${ file } : ${ e . message } ` ) ;
118101 return null ;
119102 } ) ;
120103 } ) ,
121104 )
122105 ) . filter ( truthy ) ;
123106
124- debug ( `number of possible OpenAPI/Swagger files found: ${ possibleSpecFiles . length } ` ) ;
107+ this . debug ( `number of possible OpenAPI/Swagger files found: ${ possibleSpecFiles . length } ` ) ;
125108
126109 if ( ! possibleSpecFiles . length ) {
127110 fileFindingSpinner . fail ( ) ;
@@ -134,7 +117,7 @@ export default async function prepareOas(
134117
135118 if ( possibleSpecFiles . length === 1 ) {
136119 fileFindingSpinner . stop ( ) ;
137- info ( chalk . yellow ( `We found ${ specPath } and are attempting to ${ action } it.` ) ) ;
120+ this . info ( chalk . yellow ( `We found ${ specPath } and are attempting to ${ action } it.` ) ) ;
138121 } else if ( possibleSpecFiles . length > 1 ) {
139122 if ( isCI ( ) ) {
140123 fileFindingSpinner . fail ( ) ;
@@ -160,9 +143,9 @@ export default async function prepareOas(
160143
161144 const spinner = ora ( { text : `Validating the API definition located at ${ specPath } ...` , ...oraOptions ( ) } ) . start ( ) ;
162145
163- debug ( `about to normalize spec located at ${ specPath } ` ) ;
146+ this . debug ( `about to normalize spec located at ${ specPath } ` ) ;
164147 const oas = new OASNormalize ( specPath , { colorizeErrors : true , enablePaths : true } ) ;
165- debug ( 'spec normalized' ) ;
148+ this . debug ( 'spec normalized' ) ;
166149
167150 // We're retrieving the original specification type here instead of after validation because if
168151 // they give us a Postman collection we should tell them that we handled a Postman collection, not
@@ -182,42 +165,42 @@ export default async function prepareOas(
182165 } )
183166 . catch ( ( err : Error ) => {
184167 spinner . fail ( ) ;
185- debug ( `raw oas load error object: ${ JSON . stringify ( err ) } ` ) ;
168+ this . debug ( `raw oas load error object: ${ JSON . stringify ( err ) } ` ) ;
186169 throw err ;
187170 } ) ;
188171
189172 let api : OpenAPI . Document ;
190173 await oas . validate ( ) . catch ( ( err : Error ) => {
191174 spinner . fail ( ) ;
192- debug ( `raw validation error object: ${ JSON . stringify ( err ) } ` ) ;
175+ this . debug ( `raw validation error object: ${ JSON . stringify ( err ) } ` ) ;
193176 throw err ;
194177 } ) ;
195178
196179 // If we were supplied a Postman collection this will **always** convert it to OpenAPI 3.0.
197- debug ( 'converting the spec to OpenAPI 3.0 (if necessary)' ) ;
180+ this . debug ( 'converting the spec to OpenAPI 3.0 (if necessary)' ) ;
198181 api = await oas . convert ( ) . catch ( ( err : Error ) => {
199182 spinner . fail ( ) ;
200- debug ( `raw openapi conversion error object: ${ JSON . stringify ( err ) } ` ) ;
183+ this . debug ( `raw openapi conversion error object: ${ JSON . stringify ( err ) } ` ) ;
201184 throw err ;
202185 } ) ;
203186
204187 spinner . stop ( ) ;
205188
206- debug ( '👇👇👇👇👇 spec validated! logging spec below 👇👇👇👇👇' ) ;
207- debug ( api ) ;
208- debug ( '👆👆👆👆👆 finished logging spec 👆👆👆👆👆' ) ;
209- debug ( `spec type: ${ specType } ` ) ;
189+ this . debug ( '👇👇👇👇👇 spec validated! logging spec below 👇👇👇👇👇' ) ;
190+ this . debug ( api ) ;
191+ this . debug ( '👆👆👆👆👆 finished logging spec 👆👆👆👆👆' ) ;
192+ this . debug ( `spec type: ${ specType } ` ) ;
210193
211- if ( opts . title ) {
212- debug ( `renaming title field to ${ opts . title } ` ) ;
213- api . info . title = opts . title ;
194+ if ( this . flags . title ) {
195+ this . debug ( `renaming title field to ${ this . flags . title } ` ) ;
196+ api . info . title = this . flags . title ;
214197 }
215198
216199 const specFileType = oas . type ;
217200
218201 // No need to optional chain here since `info.version` is required to pass validation
219202 const specVersion : string = api . info . version ;
220- debug ( `version in spec: ${ specVersion } ` ) ;
203+ this . debug ( `version in spec: ${ specVersion } ` ) ;
221204
222205 const commandsThatBundle : ( typeof command ) [ ] = [
223206 'openapi inspect' ,
@@ -229,7 +212,7 @@ export default async function prepareOas(
229212 if ( commandsThatBundle . includes ( command ) ) {
230213 api = await oas . bundle ( ) ;
231214
232- debug ( 'spec bundled' ) ;
215+ this . debug ( 'spec bundled' ) ;
233216 }
234217
235218 return {
0 commit comments