11/* eslint-disable no-restricted-syntax */
22import { join , relative } from 'node:path' ;
33import { $RefParser } from '@apidevtools/json-schema-ref-parser' ;
4- import type { OpenAPIV3 , OpenAPIV3_1 } from 'openapi-types ' ;
4+ import type { oas30 , oas31 } from 'openapi3-ts ' ;
55import toposort from 'toposort' ;
66import {
77 InterfaceDeclaration ,
@@ -14,6 +14,7 @@ import {
1414 VariableDeclarationKind ,
1515 Writers ,
1616} from 'ts-morph' ;
17+ import type { Simplify } from 'type-fest' ;
1718import { registerTypesFromSchema , schemaToType } from './process-schema.js' ;
1819import {
1920 castToValidJsIdentifier ,
@@ -60,7 +61,7 @@ function createUnion(...types: (string | undefined)[]) {
6061
6162export async function processOpenApiDocument (
6263 outputDir : string ,
63- schema : OpenAPIV3_1 . Document ,
64+ schema : Simplify < oas31 . OpenAPIObject > ,
6465 tags ?: string [ ] | undefined ,
6566) {
6667 const project = new Project ( ) ;
@@ -191,22 +192,22 @@ export async function processOpenApiDocument(
191192 ) ;
192193 }
193194
194- for ( const [ path , pathItemObject ] of Object . entries ( schema . paths || { } ) ) {
195+ for ( const [ path , pathItemObject ] of Object . entries < oas31 . PathItemObject > (
196+ schema . paths || { } ,
197+ ) ) {
195198 if ( pathItemObject ) {
196- for ( const [ method , operationObject ] of Object . entries (
197- pathItemObject ,
198- ) . filter (
199- ( [ , o ] ) =>
200- ! tags ||
201- ( typeof o === 'object' && 'tags' in o
202- ? o . tags . some ( ( t ) => tags . includes ( t ) )
203- : false ) ,
204- ) ) {
199+ for ( const [ method , operationObject ] of Object . entries ( pathItemObject )
200+ // ensure op is an object
201+ . filter (
202+ ( e ) : e is [ string , oas31 . OperationObject ] => typeof e [ 1 ] === 'object' ,
203+ )
204+ // tags
205+ . filter ( ( [ , o ] ) => ! tags || o . tags ?. some ( ( t ) => tags ?. includes ( t ) ) ) ) {
205206 if (
206207 typeof operationObject === 'object' &&
207208 'operationId' in operationObject
208209 ) {
209- const pathParameters : OpenAPIV3 . ParameterObject [ ] = [ ] ;
210+ const pathParameters : oas30 . ParameterObject [ ] = [ ] ;
210211
211212 const commandName = pascalCase (
212213 operationObject . operationId . replace ( / c o m m a n d $ / i, '' ) ,
@@ -258,13 +259,13 @@ export async function processOpenApiDocument(
258259 ? operationObject . requestBody
259260 : undefined ;
260261
261- const queryParameters : OpenAPIV3 . ParameterObject [ ] = [ ] ;
262+ const queryParameters : oas30 . ParameterObject [ ] = [ ] ;
262263
263264 for ( const parameter of [
264265 ...( operationObject . parameters || [ ] ) ,
265266 ...( pathItemObject . parameters || [ ] ) ,
266267 ] ) {
267- const resolvedParameter : OpenAPIV3 . ParameterObject =
268+ const resolvedParameter : oas30 . ParameterObject =
268269 '$ref' in parameter ? refs . get ( parameter . $ref ) : parameter ;
269270
270271 if ( resolvedParameter . in === 'path' ) {
@@ -481,7 +482,7 @@ export async function processOpenApiDocument(
481482 }
482483
483484 for ( const [ statusCode , response ] of Object . entries (
484- operationObject . responses ,
485+ operationObject . responses || { } ,
485486 ) . filter ( ( [ s ] ) => s . startsWith ( '2' ) ) ) {
486487 // early out if response is 204
487488 if ( statusCode === '204' ) {
0 commit comments