@mondaycom/apps-cli
Version:
A cli tool to manage apps (and monday-code projects) in monday.com
67 lines (66 loc) • 2.72 kB
JavaScript
import { APP_VERSION_STATUS } from '../consts/app-versions.js';
import { getAppVersionsByAppIdUrl, listAppVersionsByAppIdUrl } from '../consts/urls.js';
import { execute } from './api-service.js';
import { getAppVersionSchema, listAppVersionsSchema } from './schemas/app-versions-schemas.js';
import logger from '../utils/logger.js';
import { HttpError } from '../types/errors/index.js';
import { HttpMethodTypes } from '../types/services/api-service.js';
import { appsUrlBuilder } from '../utils/urls-builder.js';
export const listAppVersionsByAppId = async (appId) => {
try {
const path = listAppVersionsByAppIdUrl(appId);
const url = appsUrlBuilder(path);
const response = await execute({
url,
headers: { Accept: 'application/json' },
method: HttpMethodTypes.GET,
}, listAppVersionsSchema);
const sortedAppVersions = response.appVersions?.sort((a, b) => b.id - a.id);
return sortedAppVersions;
}
catch (error) {
if (error instanceof HttpError) {
logger.error(error.message);
throw error;
}
throw new Error('Failed to list app versions.');
}
};
export const defaultVersionByAppId = async (appId, options = {}) => {
const defaults = { useLiveVersion: false };
options = { ...defaults, ...options };
logger.info(`Getting the latest valid version for app id - ${appId}`);
const appVersions = await listAppVersionsByAppId(appId);
const latestVersion = appVersions.sort((a, b) => b.id - a.id)[0];
const allowedStatuses = options?.useLiveVersion
? [APP_VERSION_STATUS.LIVE, APP_VERSION_STATUS.DRAFT]
: [APP_VERSION_STATUS.DRAFT];
const validVersion = allowedStatuses.includes(latestVersion.status) ? latestVersion : undefined;
if (validVersion) {
logger.info(`Using version - ${validVersion?.id} for app id - ${appId}`);
}
else {
logger.info(options?.customLogMessage || `No valid version found for app id - ${appId}`);
}
return validVersion;
};
export const getAppVersionById = async (appVersionId) => {
try {
const path = getAppVersionsByAppIdUrl(appVersionId);
const url = appsUrlBuilder(path);
logger.debug(`fetching logs url: ${url}`);
const response = await execute({
url,
headers: { Accept: 'application/json' },
method: HttpMethodTypes.GET,
}, getAppVersionSchema);
return response.appVersion;
}
catch (error) {
if (error instanceof HttpError) {
logger.error(error.message);
throw error;
}
throw new Error('Failed to list app versions.');
}
};