[SDK] Make public-operations non throw (#18343)
Followup https://github.com/twentyhq/twenty/pull/18320
This commit is contained in:
@@ -12,32 +12,24 @@ export class AppUninstallCommand {
|
||||
appPath?: string;
|
||||
askForConfirmation: boolean;
|
||||
}): Promise<ApiResponse<any>> {
|
||||
try {
|
||||
console.log(chalk.blue('🚀 Uninstall Twenty Application'));
|
||||
console.log(chalk.gray(`📁 App Path: ${appPath}`));
|
||||
console.log('');
|
||||
console.log(chalk.blue('🚀 Uninstall Twenty Application'));
|
||||
console.log(chalk.gray(`📁 App Path: ${appPath}`));
|
||||
console.log('');
|
||||
|
||||
if (askForConfirmation && !(await this.confirmationPrompt())) {
|
||||
console.error(chalk.red('⛔️ Aborting uninstall'));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const result = await appUninstall({ appPath });
|
||||
|
||||
if (!result.success) {
|
||||
console.error(chalk.red('❌ Uninstall failed:'), result.error.message);
|
||||
return { success: false, error: result.error.message };
|
||||
}
|
||||
|
||||
console.log(chalk.green('✅ Application uninstalled successfully'));
|
||||
return { success: true, data: undefined };
|
||||
} catch (error) {
|
||||
console.error(
|
||||
chalk.red('Uninstall failed:'),
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
throw error;
|
||||
if (askForConfirmation && !(await this.confirmationPrompt())) {
|
||||
console.error(chalk.red('⛔️ Aborting uninstall'));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const result = await appUninstall({ appPath });
|
||||
|
||||
if (!result.success) {
|
||||
console.error(chalk.red('❌ Uninstall failed:'), result.error.message);
|
||||
return { success: false, error: result.error.message };
|
||||
}
|
||||
|
||||
console.log(chalk.green('✅ Application uninstalled successfully'));
|
||||
return { success: true, data: undefined };
|
||||
}
|
||||
|
||||
private async confirmationPrompt(): Promise<boolean> {
|
||||
|
||||
@@ -7,63 +7,55 @@ export class AuthLoginCommand {
|
||||
private configService = new ConfigService();
|
||||
|
||||
async execute(options: { apiKey?: string; apiUrl?: string }): Promise<void> {
|
||||
try {
|
||||
let { apiKey, apiUrl } = options;
|
||||
let { apiKey, apiUrl } = options;
|
||||
|
||||
const config = await this.configService.getConfig();
|
||||
const config = await this.configService.getConfig();
|
||||
|
||||
if (!apiUrl) {
|
||||
const urlAnswer = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'apiUrl',
|
||||
message: 'Twenty API URL:',
|
||||
default: config.apiUrl,
|
||||
validate: (input) => {
|
||||
try {
|
||||
new URL(input);
|
||||
return true;
|
||||
} catch {
|
||||
return 'Please enter a valid URL';
|
||||
}
|
||||
},
|
||||
if (!apiUrl) {
|
||||
const urlAnswer = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'apiUrl',
|
||||
message: 'Twenty API URL:',
|
||||
default: config.apiUrl,
|
||||
validate: (input) => {
|
||||
try {
|
||||
new URL(input);
|
||||
return true;
|
||||
} catch {
|
||||
return 'Please enter a valid URL';
|
||||
}
|
||||
},
|
||||
]);
|
||||
apiUrl = urlAnswer.apiUrl;
|
||||
}
|
||||
},
|
||||
]);
|
||||
apiUrl = urlAnswer.apiUrl;
|
||||
}
|
||||
|
||||
if (!apiKey) {
|
||||
const keyAnswer = await inquirer.prompt([
|
||||
{
|
||||
type: 'password',
|
||||
name: 'apiKey',
|
||||
message: 'API Key:',
|
||||
mask: '*',
|
||||
validate: (input) => input.length > 0 || 'API key is required',
|
||||
},
|
||||
]);
|
||||
apiKey = keyAnswer.apiKey;
|
||||
}
|
||||
if (!apiKey) {
|
||||
const keyAnswer = await inquirer.prompt([
|
||||
{
|
||||
type: 'password',
|
||||
name: 'apiKey',
|
||||
message: 'API Key:',
|
||||
mask: '*',
|
||||
validate: (input) => input.length > 0 || 'API key is required',
|
||||
},
|
||||
]);
|
||||
apiKey = keyAnswer.apiKey;
|
||||
}
|
||||
|
||||
const result = await authLogin({ apiKey: apiKey!, apiUrl: apiUrl! });
|
||||
const result = await authLogin({ apiKey: apiKey!, apiUrl: apiUrl! });
|
||||
|
||||
if (result.success) {
|
||||
const activeWorkspace = ConfigService.getActiveWorkspace();
|
||||
console.log(
|
||||
chalk.green(
|
||||
`✓ Successfully authenticated with Twenty (workspace: ${activeWorkspace})`,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
console.log(
|
||||
chalk.red('✗ Authentication failed. Please check your credentials.'),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
chalk.red('Login failed:'),
|
||||
error instanceof Error ? error.message : error,
|
||||
if (result.success) {
|
||||
const activeWorkspace = ConfigService.getActiveWorkspace();
|
||||
console.log(
|
||||
chalk.green(
|
||||
`✓ Successfully authenticated with Twenty (workspace: ${activeWorkspace})`,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
console.log(
|
||||
chalk.red('✗ Authentication failed. Please check your credentials.'),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -4,20 +4,16 @@ import chalk from 'chalk';
|
||||
|
||||
export class AuthLogoutCommand {
|
||||
async execute(): Promise<void> {
|
||||
try {
|
||||
await authLogout();
|
||||
const activeWorkspace = ConfigService.getActiveWorkspace();
|
||||
console.log(
|
||||
chalk.green(
|
||||
`✓ Successfully logged out (workspace: ${activeWorkspace})`,
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
chalk.red('Logout failed:'),
|
||||
error instanceof Error ? error.message : error,
|
||||
);
|
||||
const result = await authLogout();
|
||||
|
||||
if (!result.success) {
|
||||
console.error(chalk.red('Logout failed:'), result.error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const activeWorkspace = ConfigService.getActiveWorkspace();
|
||||
console.log(
|
||||
chalk.green(`✓ Successfully logged out (workspace: ${activeWorkspace})`),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,134 +21,126 @@ export class LogicFunctionExecuteCommand {
|
||||
functionName?: string;
|
||||
payload?: string;
|
||||
}): Promise<void> {
|
||||
let parsedPayload: Record<string, unknown>;
|
||||
try {
|
||||
let parsedPayload: Record<string, unknown>;
|
||||
try {
|
||||
parsedPayload = JSON.parse(payload);
|
||||
} catch {
|
||||
console.error(
|
||||
chalk.red('Invalid JSON payload. Please provide valid JSON.'),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const identifier = postInstall
|
||||
? 'post install'
|
||||
: (functionUniversalIdentifier ?? functionName);
|
||||
|
||||
console.log(chalk.blue(`🚀 Executing function "${identifier}"...`));
|
||||
console.log(chalk.gray(` Payload: ${JSON.stringify(parsedPayload)}`));
|
||||
console.log('');
|
||||
|
||||
const executeOptions = postInstall
|
||||
? { appPath, postInstall: true as const, payload: parsedPayload }
|
||||
: functionUniversalIdentifier
|
||||
? { appPath, functionUniversalIdentifier, payload: parsedPayload }
|
||||
: { appPath, functionName: functionName!, payload: parsedPayload };
|
||||
|
||||
const result = await functionExecute(executeOptions);
|
||||
|
||||
if (!result.success) {
|
||||
switch (result.error.code) {
|
||||
case APP_ERROR_CODES.MANIFEST_NOT_FOUND: {
|
||||
console.error(chalk.red('Failed to build manifest.'));
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.FETCH_FUNCTIONS_FAILED: {
|
||||
console.error(
|
||||
chalk.red('Failed to fetch functions:'),
|
||||
result.error.message,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.FUNCTION_NOT_FOUND: {
|
||||
console.error(chalk.red(result.error.message));
|
||||
console.log('');
|
||||
|
||||
const availableFunctions = (result.error.details
|
||||
?.availableFunctions ?? []) as Array<{
|
||||
name: string;
|
||||
universalIdentifier: string;
|
||||
}>;
|
||||
|
||||
if (availableFunctions.length > 0) {
|
||||
console.log(chalk.cyan('Available functions:'));
|
||||
availableFunctions.forEach((logicFunction) => {
|
||||
console.log(
|
||||
` - ${chalk.white(logicFunction.name)} (${logicFunction.universalIdentifier})`,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
console.log(
|
||||
chalk.yellow(
|
||||
'No functions found for this application. Have you synced your app with `yarn app:dev`?',
|
||||
),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.EXECUTION_FAILED: {
|
||||
console.error(chalk.red('Execution failed:'), result.error.message);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.error(chalk.red(result.error.message));
|
||||
}
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const executionResult = result.data;
|
||||
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
console.log(chalk.cyan('Execution Result'));
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
|
||||
const statusColor =
|
||||
executionResult.status === 'SUCCESS' ? chalk.green : chalk.red;
|
||||
console.log(
|
||||
`${chalk.bold('Status:')} ${statusColor(executionResult.status)}`,
|
||||
);
|
||||
|
||||
console.log(`${chalk.bold('Duration:')} ${executionResult.duration}ms`);
|
||||
|
||||
if (isDefined(executionResult.data)) {
|
||||
console.log('');
|
||||
console.log(chalk.bold('Data:'));
|
||||
console.log(chalk.white(JSON.stringify(executionResult.data, null, 2)));
|
||||
}
|
||||
|
||||
if (executionResult.error) {
|
||||
console.log('');
|
||||
console.log(chalk.bold.red('Error:'));
|
||||
console.log(chalk.red(` Type: ${executionResult.error.errorType}`));
|
||||
console.log(
|
||||
chalk.red(` Message: ${executionResult.error.errorMessage}`),
|
||||
);
|
||||
if (executionResult.error.stackTrace) {
|
||||
console.log('');
|
||||
console.log(chalk.gray('Stack trace:'));
|
||||
console.log(chalk.gray(executionResult.error.stackTrace));
|
||||
}
|
||||
}
|
||||
|
||||
if (executionResult.logs) {
|
||||
console.log('');
|
||||
console.log(chalk.bold('Logs:'));
|
||||
console.log(chalk.gray(executionResult.logs));
|
||||
}
|
||||
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
|
||||
if (executionResult.status !== 'SUCCESS') {
|
||||
process.exit(1);
|
||||
}
|
||||
} catch (error) {
|
||||
parsedPayload = JSON.parse(payload);
|
||||
} catch {
|
||||
console.error(
|
||||
chalk.red('Execution failed:'),
|
||||
error instanceof Error ? error.message : error,
|
||||
chalk.red('Invalid JSON payload. Please provide valid JSON.'),
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const identifier = postInstall
|
||||
? 'post install'
|
||||
: (functionUniversalIdentifier ?? functionName);
|
||||
|
||||
console.log(chalk.blue(`🚀 Executing function "${identifier}"...`));
|
||||
console.log(chalk.gray(` Payload: ${JSON.stringify(parsedPayload)}`));
|
||||
console.log('');
|
||||
|
||||
const executeOptions = postInstall
|
||||
? { appPath, postInstall: true as const, payload: parsedPayload }
|
||||
: functionUniversalIdentifier
|
||||
? { appPath, functionUniversalIdentifier, payload: parsedPayload }
|
||||
: { appPath, functionName: functionName!, payload: parsedPayload };
|
||||
|
||||
const result = await functionExecute(executeOptions);
|
||||
|
||||
if (!result.success) {
|
||||
switch (result.error.code) {
|
||||
case APP_ERROR_CODES.MANIFEST_NOT_FOUND: {
|
||||
console.error(chalk.red('Failed to build manifest.'));
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.FETCH_FUNCTIONS_FAILED: {
|
||||
console.error(
|
||||
chalk.red('Failed to fetch functions:'),
|
||||
result.error.message,
|
||||
);
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.FUNCTION_NOT_FOUND: {
|
||||
console.error(chalk.red(result.error.message));
|
||||
console.log('');
|
||||
|
||||
const availableFunctions = (result.error.details
|
||||
?.availableFunctions ?? []) as Array<{
|
||||
name: string;
|
||||
universalIdentifier: string;
|
||||
}>;
|
||||
|
||||
if (availableFunctions.length > 0) {
|
||||
console.log(chalk.cyan('Available functions:'));
|
||||
availableFunctions.forEach((logicFunction) => {
|
||||
console.log(
|
||||
` - ${chalk.white(logicFunction.name)} (${logicFunction.universalIdentifier})`,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
console.log(
|
||||
chalk.yellow(
|
||||
'No functions found for this application. Have you synced your app with `yarn app:dev`?',
|
||||
),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FUNCTION_ERROR_CODES.EXECUTION_FAILED: {
|
||||
console.error(chalk.red('Execution failed:'), result.error.message);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
console.error(chalk.red(result.error.message));
|
||||
}
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const executionResult = result.data;
|
||||
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
console.log(chalk.cyan('Execution Result'));
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
|
||||
const statusColor =
|
||||
executionResult.status === 'SUCCESS' ? chalk.green : chalk.red;
|
||||
console.log(
|
||||
`${chalk.bold('Status:')} ${statusColor(executionResult.status)}`,
|
||||
);
|
||||
|
||||
console.log(`${chalk.bold('Duration:')} ${executionResult.duration}ms`);
|
||||
|
||||
if (isDefined(executionResult.data)) {
|
||||
console.log('');
|
||||
console.log(chalk.bold('Data:'));
|
||||
console.log(chalk.white(JSON.stringify(executionResult.data, null, 2)));
|
||||
}
|
||||
|
||||
if (executionResult.error) {
|
||||
console.log('');
|
||||
console.log(chalk.bold.red('Error:'));
|
||||
console.log(chalk.red(` Type: ${executionResult.error.errorType}`));
|
||||
console.log(
|
||||
chalk.red(` Message: ${executionResult.error.errorMessage}`),
|
||||
);
|
||||
if (executionResult.error.stackTrace) {
|
||||
console.log('');
|
||||
console.log(chalk.gray('Stack trace:'));
|
||||
console.log(chalk.gray(executionResult.error.stackTrace));
|
||||
}
|
||||
}
|
||||
|
||||
if (executionResult.logs) {
|
||||
console.log('');
|
||||
console.log(chalk.bold('Logs:'));
|
||||
console.log(chalk.gray(executionResult.logs));
|
||||
}
|
||||
|
||||
console.log(chalk.cyan('─'.repeat(60)));
|
||||
|
||||
if (executionResult.status !== 'SUCCESS') {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user