@@ -19,6 +19,7 @@ import {
1919  BuildConfigBase , 
2020  BuildConfigFile , 
2121  BuildConfigRef , 
22+   BuilderType , 
2223  ExportDocument , 
2324  FileId , 
2425  isPublishBuildConfig , 
@@ -58,7 +59,6 @@ import {
5859  DEFAULT_BATCH_SIZE , 
5960  DEFAULT_VALIDATION_RULES_SEVERITY_CONFIG , 
6061  EXPORT_BUILD_TYPES , 
61-   ExportBuildType , 
6262  MESSAGE_SEVERITY , 
6363  SUPPORTED_FILE_FORMATS , 
6464  VERSION_STATUS , 
@@ -283,6 +283,22 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
283283    return  this . buildResult 
284284  } 
285285
286+   private  findApiBuilderByApiType ( apiType : BuilderType ) : ApiBuilder  { 
287+     const  apiBuilder  =  this . apiBuilders . find ( apiBuilder  =>  apiBuilder . apiType  ===  apiType ) 
288+     if  ( ! apiBuilder )  { 
289+       throw  new  Error ( `Cannot find apiBuilder for apiType ${ apiType }  ) 
290+     } 
291+     return  apiBuilder 
292+   } 
293+ 
294+   private  findApiBuilderBySpecType ( type : string ) : ApiBuilder  { 
295+     const  apiBuilder  =  this . apiBuilders . find ( apiBuilder  =>  apiBuilder . types . includes ( type ) ) 
296+     if  ( ! apiBuilder )  { 
297+       throw  new  Error ( `Cannot find apiBuilder for specification type ${ type }  ) 
298+     } 
299+     return  apiBuilder 
300+   } 
301+ 
286302  async  parsedFileResolver ( fileId : string ) : Promise < SourceFile  |  null >  { 
287303    if  ( this . parsedFiles . has ( fileId ) )  { 
288304      return  this . parsedFiles . get ( fileId )  ??  null 
@@ -325,11 +341,7 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
325341      if  ( ! document )  { 
326342        throw  new  Error ( `Raw document ${ slug }  ) 
327343      } 
328-       const  apiBuilder  =  this . apiBuilders . find ( apiBuilder  =>  apiBuilder . types . includes ( document ?. type ) ) 
329-       if  ( ! apiBuilder )  { 
330-         // todo 
331-         throw  new  Error ( `Cannot find apiBuilder for type ${ document ?. type }  ) 
332-       } 
344+       const  apiBuilder  =  this . findApiBuilderBySpecType ( document . type ) 
333345      return  new  File ( [ apiBuilder . dumpDocument ( document ) ] ,  document . filename ) 
334346    } 
335347
@@ -475,12 +487,14 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
475487    apiType ?: OperationsApiType , 
476488  ) : Promise < ResolvedVersionDocuments  |  null >  { 
477489    packageId  =  packageId  ??  this . config . packageId 
478-     // this is the case when a version has been built just now, and there's nothing to fetch yet, so 
479-     // the only way to get the docs is to get them from buildResult, but the referenced packages map will be empty (packages: {}) 
480490    if  ( this . canBeResolvedLocally ( version ,  packageId ) )  { 
481-       const  apiBuilder  =  this . apiBuilders . find ( apiBuilder  =>  apiBuilder . apiType  ===  apiType ) 
482-       const  currentApiTypeDocuments  =  this . documentList . filter ( ( {  type } )  =>  apiBuilder ?. types . includes ( type ) ) 
483-       return  {  documents : currentApiTypeDocuments ,  packages : { }  } 
491+       // this is the case when a version has been built just now, and there's nothing to fetch yet, so 
492+       // the only way to get the docs is to get them from buildResult, but the referenced packages map will be empty (packages: {}) 
493+       if  ( apiType )  { 
494+         const  apiBuilder  =  this . findApiBuilderByApiType ( apiType ) 
495+         return  {  documents : this . documentList . filter ( ( {  type } )  =>  apiBuilder . types . includes ( type ) ) ,  packages : { }  } 
496+       } 
497+       return  {  documents : this . documentList ,  packages : { }  } 
484498    } 
485499
486500    const  {  versionDocumentsResolver }  =  this . params . resolvers 
@@ -538,7 +552,7 @@ export class PackageVersionBuilder implements IPackageVersionBuilder {
538552  private  canBeResolvedLocally ( version : string ,  packageId : string  |  undefined ) : boolean  { 
539553    return  this . config . buildType  !==  BUILD_TYPE . CHANGELOG  && 
540554      this . config . buildType  !==  BUILD_TYPE . PREFIX_GROUPS_CHANGELOG  && 
541-       ! EXPORT_BUILD_TYPES . includes ( this . config . buildType   as   ExportBuildType )  && 
555+       EXPORT_BUILD_TYPES . every ( type   =>   type   !==   this . config . buildType )  && 
542556      version  ===  this . config . version  && 
543557      packageId  ===  this . config . packageId 
544558  } 
0 commit comments