diff --git a/package.json b/package.json index 6836bbcb..47731b6a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@gofynd/fdk-cli", - "version": "8.0.2", + "version": "8.0.3", "main": "index.js", "license": "MIT", "bin": { diff --git a/src/lib/Auth.ts b/src/lib/Auth.ts index 358bb099..acf1349b 100644 --- a/src/lib/Auth.ts +++ b/src/lib/Auth.ts @@ -47,6 +47,11 @@ export const getApp = async () => { if (Auth.wantToChangeOrganization){ ConfigStore.delete(CONFIG_KEYS.AUTH_TOKEN); clearExtensionContext(); + const currentEnv = ConfigStore.get( + CONFIG_KEYS.CURRENT_ENV_VALUE, + ); + ConfigStore.clear(); + ConfigStore.set(CONFIG_KEYS.CURRENT_ENV_VALUE, currentEnv); } const expiryTimestamp = Math.floor(Date.now() / 1000) + req.body.auth_token.expires_in; @@ -248,6 +253,7 @@ export default class Auth { ConfigStore.clear(); ConfigStore.set(CONFIG_KEYS.CURRENT_ENV_VALUE, currentEnv); ConfigStore.set(CONFIG_KEYS.EXTRAS, extras); + ConfigStore.set('extensionSections', {}); } public static getUserInfo() { diff --git a/src/lib/ExtensionSection.ts b/src/lib/ExtensionSection.ts index 86838847..d98ce219 100644 --- a/src/lib/ExtensionSection.ts +++ b/src/lib/ExtensionSection.ts @@ -137,7 +137,7 @@ export default class ExtensionSection { if (!extensions?.length) { throw new Error( - 'No installed extensions found!', + 'No extension found in the organization!', ); } const selectedExtensionName = await promptUser({ @@ -151,13 +151,15 @@ export default class ExtensionSection { )?._id; } catch (error) { Logger.error( - 'Could not fetch the list of extensions', + error.message ); - extensionId = await promptUser({ - type: 'text', - name: 'extensionId', - message: 'Please Enter your extensionId: ', - }); + if(error.code !== 403 && error.code !== 401){ + extensionId = await promptUser({ + type: 'text', + name: 'extensionId', + message: 'Please Enter your extensionId: ', + }); + } } finally { Configstore.set( 'extensionSections.extensionId', @@ -202,8 +204,10 @@ export default class ExtensionSection { themeId: undefined, companyType: 'live', }; + let isError = false; + let errorObj: any; try { - const configObj = + const configObj: any = await Theme.selectCompanyAndStore(); const { data: appConfig } = await configurationService.getApplicationDetails( @@ -223,15 +227,23 @@ export default class ExtensionSection { companyType: configObj['accountType'], }; } catch (error) { - Logger.error('Could not fetch the applied!'); - for (let lkey in themeDetails) { - themeDetails[lkey] = await promptUser({ - type: 'text', - name: lkey, - message: `Please enter ${lkey}: `, - }); + Logger.error('Could not fetch the applied themes!'); + if(error.code !== 403 && error.code !== 401){ + for (let lkey in themeDetails) { + themeDetails[lkey] = await promptUser({ + type: 'text', + name: lkey, + message: `Please enter ${lkey}: `, + }); + } + } else { + isError = true; + errorObj = error; } } finally { + if(isError){ + throw errorObj; + } Configstore.set( 'extensionSections.appliedTheme', themeDetails, @@ -242,7 +254,9 @@ export default class ExtensionSection { default: return null; } - } catch (error) { } + } catch (error) { + throw new CommandError(error.message); + } } if (!Configstore.all.extensionSections) { @@ -280,6 +294,9 @@ export default class ExtensionSection { const userInput = {}; for (let val in missingKeys) { const result = await getOption(missingKeys[val]); + if(!result){ + break; + } userInput[missingKeys[val]] = result; } @@ -287,7 +304,7 @@ export default class ExtensionSection { return finalContext; } catch (error) { - console.log(error); + throw new CommandError(error.message); } } @@ -373,6 +390,10 @@ export default class ExtensionSection { } else { throw new CommandError('Unsupported framework!'); } + } else if(bindingInterface) { + throw new CommandError( + 'Unsupported Interface! Only Web Themes are supported', + ); } @@ -457,18 +478,19 @@ export default class ExtensionSection { context, sectionData.status, ); + Logger.info('Code published ...'); + } else { throw new CommandError( 'Unsupported Framework! Only react and vue2 are supported', ); } - } else { + } else if(bindingInterface) { throw new CommandError( 'Unsupported Interface! Only Web Themes are supported', ); } - Logger.info('Code published ...'); } static async buildExtensionCodeVue({ bundleName }) { @@ -678,18 +700,18 @@ export default class ExtensionSection { context, sectionData.status, ); + Logger.info('Draft successful!'); } else { throw new CommandError( 'Unsupported Framework! Only react and vue2 are supported', ); } - } else { + } else if(bindingInterface) { throw new CommandError( 'Unsupported Interface! Only Web Themes are supported', ); } - Logger.info('Draft successful!'); } static async watchExtensionCodeBuild( @@ -831,7 +853,7 @@ export default class ExtensionSection { data, ); } catch (error) { - console.log(error); + throw new CommandError(error.message); } } public static async previewExtension(options: any) { @@ -996,7 +1018,7 @@ export default class ExtensionSection { 'Unsupported Framework! Only react and vue2 are supported', ); } - } else { + } else if(bindingInterface) { throw new CommandError( 'Unsupported Interface! Only Web Themes are supported', ); diff --git a/src/lib/Theme.ts b/src/lib/Theme.ts index 7c74013e..6a1c9726 100644 --- a/src/lib/Theme.ts +++ b/src/lib/Theme.ts @@ -371,7 +371,7 @@ export default class Theme { throw new CommandError(`Folder ${options.name} already exists`); } - const configObj = await Theme.selectCompanyAndStore(); + const configObj: any = await Theme.selectCompanyAndStore(); const { data: appConfig } = await ConfigurationService.getApplicationDetails(configObj); @@ -520,7 +520,7 @@ export default class Theme { options, targetDirectory, appConfig, - themeType, + themeType ); shouldDelete = true; process.chdir(path.join('.', options.name)); @@ -632,7 +632,6 @@ export default class Theme { }); Logger.info(b5.toString()); } catch (error) { - Logger.error(error); if (shouldDelete) await Theme.cleanUp(targetDirectory); throw new CommandError(error.message, error.code); } @@ -643,7 +642,7 @@ export default class Theme { let targetDirectory = ''; let dir_name = 'default'; try { - let configObj = await Theme.selectCompanyAndStore(); + let configObj: any = await Theme.selectCompanyAndStore(); configObj = await Theme.selectTheme(configObj); const { data: appConfig } = await ConfigurationService.getApplicationDetails(configObj); @@ -962,7 +961,6 @@ export default class Theme { }); Logger.debug(b5.toString()); } catch (error) { - Logger.error(error); throw new CommandError(error.message, error.code); } finally { rimraf.sync(path.join(process.cwd(), Theme.SRC_FOLDER)); @@ -3312,7 +3310,7 @@ private static async getAvailableReactSectionsForSync(sections, sectionChunkingE options, targetDirectory, appConfig, - themeType, + themeType ) => { const defaultTheme = await ThemeService.getDefaultTheme({ company_id: appConfig.company_id, diff --git a/src/lib/ThemeContext.ts b/src/lib/ThemeContext.ts index 44779f5a..fb1a015e 100644 --- a/src/lib/ThemeContext.ts +++ b/src/lib/ThemeContext.ts @@ -33,7 +33,7 @@ export default class ThemeContext { throw new CommandError( 'Context with the same name already exists', ); - let configObj = await Theme.selectCompanyAndStore(); + let configObj: any = await Theme.selectCompanyAndStore(); configObj = await Theme.selectTheme(configObj); const { data: appConfig } = await ConfigurationService.getApplicationDetails(configObj); diff --git a/src/lib/api/helper/interceptors.ts b/src/lib/api/helper/interceptors.ts index d248159c..55fa769d 100644 --- a/src/lib/api/helper/interceptors.ts +++ b/src/lib/api/helper/interceptors.ts @@ -123,8 +123,21 @@ function getErrorMessage(error) { export function responseErrorInterceptor() { return (error) => { // Request made and server responded - Debug(error); + Debug(`Error recieved from backend: ${JSON.stringify(error)}`); + + let errorCode = ""; + let errorMessage = error?.response?.data?.message; + if(error?.response?.data?.code){ + errorCode = error.response.data.code.replace(/_([a-z])/g, (match, letter)=> letter.toUpperCase()); + } if ( + error.response && + (error.response.status === 401 || error.response.status === 403) && + errorMessage && !errorMessage.includes('login') + ) { + throw new CommandError(errorMessage, error.response.status); + } + else if ( error.response && (error.response.status === 401 || error.response.status === 403) ) { diff --git a/src/lib/api/services/url.ts b/src/lib/api/services/url.ts index f70e72b3..6d0f3cc7 100644 --- a/src/lib/api/services/url.ts +++ b/src/lib/api/services/url.ts @@ -102,7 +102,7 @@ export const URLS = { ); }, - GET_APPLIED_THEME: (company_id: number, application_id: string) => { + GET_APPLIED_THEME: (company_id: number, application_id: string) => { return urlJoin( THEME_URL(), `organization/${getOrganizationId()}/company/${company_id}/application/${application_id}/applied-theme`,