Skip to content

Commit daf1307

Browse files
committed
Refactor API key validation in import/export configuration handlers
1 parent b5a8676 commit daf1307

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,8 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
8181

8282
config.apiKey =
8383
exportCmdFlags['stack-uid'] || exportCmdFlags['stack-api-key'] || config.source_stack || (await askAPIKey());
84-
if (typeof config.apiKey !== 'string') {
85-
log.debug('Invalid API key received!', { apiKey: config.apiKey });
86-
throw new Error('Invalid API key received');
87-
}
88-
if (!config.apiKey || !config.apiKey.trim()) {
89-
log.debug('Empty API key received!', { apiKey: config.apiKey });
84+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
85+
log.debug('Invalid or empty API key received!', { apiKey: config.apiKey });
9086
throw new Error('Stack API key cannot be empty. Please provide a valid stack API key.');
9187
}
9288
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
6060
} else {
6161
config.apiKey =
6262
importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.target_stack || (await askAPIKey());
63-
if (typeof config.apiKey !== 'string') {
64-
throw new Error('Invalid API key received');
65-
}
66-
if (!config.apiKey || !config.apiKey.trim()) {
63+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
6764
throw new Error('Stack API key cannot be empty. Please provide a valid stack API key.');
6865
}
6966
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,8 @@ const setupConfig = async (importCmdFlags: any): Promise<ImportConfig> => {
8181
}
8282
config.apiKey =
8383
importCmdFlags['stack-uid'] || importCmdFlags['stack-api-key'] || config.target_stack || (await askAPIKey());
84-
if (typeof config.apiKey !== 'string') {
85-
throw new Error('Invalid API key received');
86-
}
87-
if (!config.apiKey || !config.apiKey.trim()) {
88-
log.debug('Empty API key received!', { apiKey: config.apiKey });
84+
if (typeof config.apiKey !== 'string' || !config.apiKey || !config.apiKey.trim()) {
85+
log.debug('Invalid or empty API key received!', { apiKey: config.apiKey });
8986
throw new Error('Stack API key cannot be empty. Please provide a valid stack API key.');
9087
}
9188
}

0 commit comments

Comments
 (0)