@@ -8,10 +8,8 @@ import {
88 FileWrapper ,
99 TransformationController ,
1010 Transformation ,
11- UnauthorizedResponseError ,
1211 ApiError ,
13- InternalServerErrorResponseError ,
14- ProblemDetailsError
12+ Environment
1513} from "@apimatic/sdk" ;
1614
1715import { AuthInfo , getAuthInfo } from "../../client-utils/auth-manager.js" ;
@@ -98,42 +96,48 @@ export class TransformationService {
9896 return `X-Auth-Key ${ key ?? "" } ` ;
9997 }
10098
101- private createApiClient ( authorizationHeader : string ) : Client {
99+ private createApiClient = ( authorizationHeader : string ) : Client => {
100+ if ( envInfo . getBaseUrl ( ) ) {
101+ return this . createTestingApiClient ( authorizationHeader ) ;
102+ }
103+ return this . createProductionApiClient ( authorizationHeader ) ;
104+ } ;
105+
106+ readonly createProductionApiClient = ( authorizationHeader : string ) : Client => {
102107 return new Client ( {
103108 customHeaderAuthenticationCredentials : {
104109 Authorization : authorizationHeader
105110 } ,
106111 userAgent : envInfo . getUserAgent ( ) ,
107- timeout : this . TIMEOUT
112+ timeout : this . TIMEOUT ,
113+ environment : Environment . Production
108114 } ) ;
109- }
115+ } ;
116+
117+ readonly createTestingApiClient = ( authorizationHeader : string ) : Client => {
118+ return new Client ( {
119+ customHeaderAuthenticationCredentials : {
120+ Authorization : authorizationHeader
121+ } ,
122+ userAgent : envInfo . getUserAgent ( ) ,
123+ timeout : this . TIMEOUT ,
124+ environment : Environment . Testing ,
125+ customUrl : envInfo . getBaseUrl ( )
126+ } ) ;
127+ } ;
110128
111129 private readonly handleTransformationErrors = async ( error : unknown ) : Promise < string > => {
112- if ( error instanceof UnauthorizedResponseError ) {
113- //401
114- const unAuthError = error as UnauthorizedResponseError ;
115- return getMessageInRedColor ( unAuthError . result ?. message ?? "Authorization has been denied for this request." ) ;
116- } else if ( error instanceof ProblemDetailsError ) {
117- //400 & 403
118- const probDetailsError = error as ProblemDetailsError ;
119- const message = ( probDetailsError . result ! . errors as Record < string , string [ ] > ) ?. [ "" ] ?. [ 0 ] ;
120- return getMessageInRedColor ( probDetailsError . result ! . title + "\n- " + message ) ;
121- } else if ( error instanceof ApiError && error . statusCode === 422 ) {
122- //422
123- return "Validation error occurred during transformation. Please check your API specification." ;
124- } else if ( error instanceof InternalServerErrorResponseError ) {
125- //500
126- const internalServerError = error as InternalServerErrorResponseError ;
127- const message = internalServerError . result ?. message ;
128- return getMessageInRedColor (
129- `${
130- message ?? "An unkown error occurred."
131- } Please try again later or reach out to our team at support@apimatic.io for help if your problem persists.`
132- ) ;
130+ if ( error instanceof ApiError ) {
131+ const apiError = error as ApiError ;
132+ if ( apiError . statusCode === 400 ) {
133+ return "Your API Definition is invalid. Please use the APIMatic VS Code Extension to fix the errors and try again." ;
134+ } else if ( apiError . statusCode === 401 ) {
135+ const message = JSON . parse ( apiError . body as string ) . message ;
136+ return `${ message } Please run the 'auth:login' command and try again or reach out to our team at support@apimatic.io.` ;
137+ }
138+ return `Error ${ apiError . statusCode } : An error occurred during the transformation. Please try again or contact support@apimatic.io for assistance.` ;
133139 } else {
134- return getMessageInRedColor (
135- "An unexpected error occurred while transforming the spec file, please try again later. If the problem persists, please reach out to our team at support@apimatic.io"
136- ) ;
140+ return "An unexpected error occurred while validating your API Definition. Please try again later. If the problem persists, please reach out to our team at support@apimatic.io" ;
137141 }
138142 } ;
139143}
0 commit comments