@@ -32,47 +32,24 @@ export class PortalService {
3232 private readonly fileService = new FileService ( ) ;
3333
3434
35- async generatePortal ( buildPath : FilePath , configDir : DirectoryPath , authKey : string | null ) : Promise < NodeJS . ReadableStream > {
35+ async generatePortal ( buildPath : FilePath , configDir : DirectoryPath , authKey : string | null ) : Promise < Result < NodeJS . ReadableStream , string | NodeJS . ReadableStream > > {
3636 const buildFileStream = await this . fileService . getStream ( buildPath ) ;
3737 const file = new FileWrapper ( buildFileStream ) ;
38- const client = await this . getApiClient ( configDir , authKey ) ;
39- const docsPortalManagementController = new DocsPortalManagementController ( client ) ;
40- const response = await docsPortalManagementController . generateOnPremPortalViaBuildInput ( this . CONTENT_TYPE , file ) ;
41- return response . result as NodeJS . ReadableStream ;
42- }
4338
44- private async getApiClient ( configDir : DirectoryPath , authKey : string | null ) : Promise < Client > {
4539 const authInfo : AuthInfo | null = await getAuthInfo ( configDir . toString ( ) ) ;
46- // TODO: Add auth key
47- const authorizationHeader = this . createAuthorizationHeader ( authInfo , authKey ) ;
48- return this . createApiClient ( authorizationHeader ) ;
49- }
50-
51- public async generateOnPremPortal (
52- params : GeneratePortalParams ,
53- configDir : string
54- ) : Promise < Result < NodeJS . ReadableStream , string > > {
55- if ( ! ( await fsExtra . pathExists ( params . sourceBuildInputZipFilePath ) ) ) {
56- return Result . failure ( "Build file doesn't exist" ) ;
40+ if ( authInfo === null && ! authKey ) {
41+ return Result . failure ( "You are not logged in, please login using `auth:login` first." ) ;
5742 }
5843
59- const authInfo : AuthInfo | null = await getAuthInfo ( configDir ) ;
60- if ( authInfo === null && ! params . overrideAuthKey ) {
61- return Result . failure ( "You are not logged in, please login using `apimatic auth:login` first." ) ;
62- }
63-
64- const authorizationHeader = this . createAuthorizationHeader ( authInfo , params . overrideAuthKey ) ;
44+ const authorizationHeader = this . createAuthorizationHeader ( authInfo , authKey ) ;
6545 const client = this . createApiClient ( authorizationHeader ) ;
6646 const docsPortalManagementController = new DocsPortalManagementController ( client ) ;
6747
6848 try {
69- const stream = await this . generatePortalFromSyncEndpoint (
70- docsPortalManagementController ,
71- params . sourceBuildInputZipFilePath
72- ) ;
73- return Result . success ( stream ) ;
49+ const response = await docsPortalManagementController . generateOnPremPortalViaBuildInput ( this . CONTENT_TYPE , file ) ;
50+ return Result . success ( response . result as NodeJS . ReadableStream ) ;
7451 } catch ( error ) {
75- return Result . failure ( await this . handlePortalGenerationErrors ( error , params ) ) ;
52+ return Result . failure ( await this . handlePortalGenerationErrors ( error ) ) ;
7653 }
7754 }
7855
@@ -120,13 +97,13 @@ export class PortalService {
12097 } ;
12198
12299 private getUserAgent ( ) : string {
123- const osInfo = `${ os . platform ( ) } ${ os . release ( ) } ` ;
124- const engine = "Node.js" ;
125- const engineVersion = process . version ;
126-
127- return `APIMATIC CLI - [OS: ${ osInfo } , Engine: ${ engine } /${ engineVersion } ]` ;
128- }
129-
100+ const osInfo = `${ os . platform ( ) } ${ os . release ( ) } ` ;
101+ const engine = "Node.js" ;
102+ const engineVersion = process . version ;
103+
104+ return `APIMATIC CLI - [OS: ${ osInfo } , Engine: ${ engine } /${ engineVersion } ]` ;
105+ }
106+
130107 private createApiClient = ( authorizationHeader : string ) : Client => {
131108 return new Client ( {
132109 customHeaderAuthenticationCredentials : {
@@ -137,18 +114,7 @@ export class PortalService {
137114 } ) ;
138115 } ;
139116
140- private generatePortalFromSyncEndpoint = async (
141- docsPortalManagementController : DocsPortalManagementController ,
142- zippedBuildFilePath : string
143- ) : Promise < NodeJS . ReadableStream > => {
144- const file = new FileWrapper ( fs . createReadStream ( zippedBuildFilePath ) ) ;
145- const response : ApiResponse < NodeJS . ReadableStream | Blob > =
146- await docsPortalManagementController . generateOnPremPortalViaBuildInput ( this . CONTENT_TYPE , file ) ;
147-
148- return response . result as NodeJS . ReadableStream ;
149- } ;
150-
151- private handlePortalGenerationErrors = async ( error : unknown , params : GeneratePortalParams ) : Promise < string > => {
117+ private handlePortalGenerationErrors = async ( error : unknown ) : Promise < string | NodeJS . ReadableStream > => {
152118 if ( error instanceof UnauthorizedResponseError ) {
153119 //401
154120 const body = await this . parseErrorResponse ( error ) ;
@@ -160,7 +126,7 @@ export class PortalService {
160126 return getMessageInRedColor ( body . title + "\n- " + message ) ;
161127 } else if ( error instanceof ApiError && error . statusCode === 422 ) {
162128 //422
163- return await this . saveAndExtractErrorZipFile ( error , params ) ;
129+ return error . body as NodeJS . ReadableStream ;
164130 } else if ( error instanceof InternalServerErrorResponseError ) {
165131 //500
166132 const body = await this . parseErrorResponse ( error ) ;
@@ -197,7 +163,7 @@ export class PortalService {
197163 resolve (
198164 getMessageInRedColor (
199165 "An error occurred during portal generation due to an issue with the input. An error report has been written at the destination path: " +
200- path . join ( params . generatedPortalArtifactsFolderPath , "apimatic-debug" )
166+ path . join ( params . generatedPortalArtifactsFolderPath , "apimatic-debug" )
201167 )
202168 ) ;
203169 } )
0 commit comments