@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
67 lines (66 loc) • 2.43 kB
JavaScript
import { CronExpressionParser } from 'cron-parser';
import { StatusCodes } from 'http-status-codes';
import logger from '../utils/logger.js';
import { isDefinedAndNotEmpty } from '../utils/validations.js';
export const printJobs = (jobs) => {
if (jobs.length === 0) {
logger.log('No scheduler jobs found.');
return;
}
logger.table(jobs);
};
export const validateCronExpression = (schedule) => {
if (!isDefinedAndNotEmpty(schedule)) {
throw new Error('Cron expression is required');
}
const fields = schedule.trim().split(/\s+/);
if (fields.length !== 5) {
throw new Error(`Invalid cronjob schedule format: Expected 5 fields (minute hour day month weekday), got ${fields.length}`);
}
try {
CronExpressionParser.parse(schedule);
}
catch (error) {
const message = error instanceof Error ? error.message : String(error);
throw new Error(`Invalid cronjob schedule format: ${message}`);
}
};
export const validateTargetUrl = (targetUrl) => {
if (!targetUrl) {
throw new Error('Target URL is required');
}
if (!targetUrl.startsWith('/')) {
throw new Error('Target URL must start with a slash');
}
// validate that the value after / is not an empty string
if (targetUrl.split('/')[1] === '') {
throw new Error('Target URL must not be empty');
}
};
export const handleHttpErrors = (error) => {
switch (error.code) {
case StatusCodes.NOT_FOUND: {
logger.error(error.message);
throw new Error('monday-code deployment not found for the requested app');
}
case StatusCodes.FORBIDDEN: {
logger.error(error.message);
throw new Error('You are not authorized to access the requested app');
}
case StatusCodes.BAD_REQUEST: {
logger.error(error.message);
throw new Error('Invalid request');
}
case StatusCodes.INTERNAL_SERVER_ERROR: {
logger.error(error.message);
throw new Error('Internal server error');
}
case StatusCodes.UPGRADE_REQUIRED: {
logger.error('Scheduler for monday code is in beta. check out our community slack announcement to join the beta group');
throw new Error('Beta access required');
}
default: {
throw new Error('Unknown error');
}
}
};