UNPKG

@mondaycom/apps-cli

Version:

A cli tool to manage apps (and monday-code projects) in monday.com

137 lines (136 loc) 5.06 kB
import { getAppFeaturesUrl, getCreateAppFeatureReleaseUrl, getCreateAppFeatureUrl } from '../consts/urls.js'; import { execute } from './api-service.js'; import { createAppFeatureReleaseSchema, createAppFeatureSchema, listAppFeaturesSchema, } from './schemas/app-features-schemas.js'; import { AppFeatureType, AppReleaseSingleBuildCategory, BUILD_TYPES, } from '../types/services/app-features-service.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 listAppFeaturesByAppVersionId = async (appVersionId, options) => { try { const path = getAppFeaturesUrl(appVersionId, options?.includeTypes); const url = appsUrlBuilder(path); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.GET, }, listAppFeaturesSchema); const sortedAppFeatures = response.appFeatures?.sort((a, b) => b.id - a.id); const excludedFeatureTypes = new Set([AppFeatureType.AppFeatureOauth, ...(options?.excludeTypes || [])]); const filteredAppFeatures = sortedAppFeatures.filter(appFeature => { const appFeatureType = appFeature.type; const shouldBeExcluded = excludedFeatureTypes.has(appFeatureType); return !shouldBeExcluded; }); return filteredAppFeatures; } catch (error) { if (error instanceof HttpError) { logger.error(error.message); throw error; } throw new Error('Failed to list app features.'); } }; const prepareReleaseByBuildType = (buildType, customUrl) => { switch (buildType) { case BUILD_TYPES.CUSTOM_URL: { return { kind: 'iframe', data: { url: customUrl, }, }; } case BUILD_TYPES.MONDAY_CODE: { return { kind: 'single_build', appReleaseCategory: AppReleaseSingleBuildCategory.MondayCode, data: { url: customUrl, }, }; } case BUILD_TYPES.MONDAY_CODE_CDN: { return { kind: 'single_build', appReleaseCategory: AppReleaseSingleBuildCategory.view, data: { url: customUrl, }, }; } default: { throw new Error('Invalid build type'); } } }; export const createAppFeature = async ({ appId, appVersionId, appFeatureType, options, }) => { try { const path = getCreateAppFeatureUrl(appId, appVersionId); const url = appsUrlBuilder(path); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.POST, body: { name: options?.name, type: appFeatureType, data: { description: options?.description, }, }, }, createAppFeatureSchema); const appFeature = response.app_feature; return appFeature; } catch (error) { if (error instanceof HttpError) { logger.error(error.message); throw error; } throw new Error('Failed to create app feature.'); } }; export const createAppFeatureRelease = async ({ appId, appVersionId, appFeatureId, customUrl, buildType, }) => { try { const path = getCreateAppFeatureReleaseUrl(appId, appVersionId, appFeatureId); const url = appsUrlBuilder(path); const release = prepareReleaseByBuildType(buildType, customUrl); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.POST, body: release, }, createAppFeatureReleaseSchema); const updatedAppFeature = response.app_feature; return updatedAppFeature; } catch (error) { if (error instanceof HttpError) { logger.error(error.message); throw error; } throw new Error('Failed to list app features.'); } }; export const createAppFeatureWithRelease = async ({ appId, appVersionId, appFeatureType, build, options, }) => { const appFeature = await createAppFeature({ appId, appVersionId, appFeatureType, options }); if (!build) return appFeature; try { await createAppFeatureRelease({ appId, appVersionId, appFeatureId: appFeature.id, buildType: build.buildType, customUrl: build.url, }); } catch (error) { logger.debug(error.message); logger.error('Failed to connect app feature to release.'); throw error; } return appFeature; };