Skip to content

Commit 74ab428

Browse files
fix: show login error on console and improve API key validation
add API key validation in import/export configurations
1 parent c528e1f commit 74ab428

8 files changed

Lines changed: 31 additions & 8 deletions

File tree

packages/contentstack-export/src/utils/export-config-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
100100

101101
config.apiKey =
102102
exportCmdFlags['stack-uid'] || exportCmdFlags['stack-api-key'] || config.apiKey || (await askAPIKey());
103-
if (typeof config.apiKey !== 'string') {
104-
log.debug('Invalid API key received!', { apiKey: config.apiKey });
105-
throw new Error('Invalid API key received');
103+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
104+
log.debug('Invalid or empty API key received!', { apiKey: config.apiKey });
105+
throw new Error('Invalid or empty API key received. Please provide a valid stack API key.');
106106
}
107107
}
108108
}

packages/contentstack-export/src/utils/interactive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,11 @@ export const askAPIKey = async (): Promise<string> => {
6464
type: 'input',
6565
message: 'Enter the stack api key',
6666
name: 'apiKey',
67+
validate: (input: string) => {
68+
if (!input || !input.trim()) {
69+
return 'Stack API key cannot be empty. Please enter a valid stack API key.';
70+
}
71+
return true;
72+
},
6773
});
6874
};

packages/contentstack-export/test/unit/utils/export-config-handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ describe('Export Config Handler', () => {
371371
await setupConfig(flags);
372372
expect.fail('Should have thrown an error');
373373
} catch (error: any) {
374-
expect(error.message).to.include('Invalid API key received');
374+
expect(error.message).to.include('Invalid or empty API key received. Please provide a valid stack API key.');
375375
}
376376
});
377377
});

packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
handleAndLogError,
1414
configHandler,
1515
createLogContext,
16+
cliux,
17+
loadChalk
1618
} from '@contentstack/cli-utilities';
1719

1820
import { ImportConfig, Context } from '../../../types';
@@ -64,6 +66,7 @@ export default class ImportSetupCommand extends Command {
6466
static usage = 'cm:stacks:import-setup [-k <value>] [-d <value>] [-a <value>] [--modules <value,value>]';
6567

6668
async run(): Promise<void> {
69+
await loadChalk();
6770
try {
6871
const { flags } = await this.parse(ImportSetupCommand);
6972
let importSetupConfig = await setupImportConfig(flags);
@@ -106,6 +109,7 @@ export default class ImportSetupCommand extends Command {
106109
} catch (error) {
107110
CLIProgressManager.printGlobalSummary();
108111
handleAndLogError(error);
112+
cliux.error(`ERROR: ${error instanceof Error ? error.message : String(error)}`);
109113
}
110114
}
111115

packages/contentstack-import-setup/src/utils/import-config-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
5959
} else {
6060
config.apiKey =
6161
importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.target_stack || (await askAPIKey());
62-
if (typeof config.apiKey !== 'string') {
63-
throw new Error('Invalid API key received');
62+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
63+
throw new Error('Invalid or empty API key received. Please provide a valid stack API key.');
6464
}
6565
}
6666
}

packages/contentstack-import-setup/src/utils/interactive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ export const askAPIKey = async (): Promise<string> => {
3030
type: 'input',
3131
message: 'Enter the stack api key',
3232
name: 'apiKey',
33+
validate: (input: string) => {
34+
if (!input || !input.trim()) {
35+
return 'Stack API key cannot be empty. Please enter a valid stack API key.';
36+
}
37+
return true;
38+
},
3339
});
3440
};

packages/contentstack-import/src/utils/import-config-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
8686
}
8787
config.apiKey =
8888
importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.apiKey || (await askAPIKey());
89-
if (typeof config.apiKey !== 'string') {
90-
throw new Error('Invalid API key received');
89+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
90+
log.debug('Invalid or empty API key received!', { apiKey: config.apiKey });
91+
throw new Error('Invalid or empty API key received. Please provide a valid stack API key.');
9192
}
9293
}
9394
}

packages/contentstack-import/src/utils/interactive.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export const askAPIKey = async (): Promise<string> => {
1919
type: 'input',
2020
message: 'Enter the stack api key',
2121
name: 'apiKey',
22+
validate: (input: string) => {
23+
if (!input || !input.trim()) {
24+
return 'Stack API key cannot be empty. Please enter a valid stack API key.';
25+
}
26+
return true;
27+
},
2228
});
2329
};
2430

0 commit comments

Comments
 (0)