UNPKG

@mondaycom/apps-cli

Version:

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

110 lines (109 loc) 3.41 kB
import { appSchedulerUrl } from '../consts/urls.js'; import { execute } from '../services/api-service.js'; import { handleHttpErrors } from '../services/scheduler-service.utils.js'; import { HttpError } from '../types/errors/index.js'; import { HttpMethodTypes } from '../types/services/api-service.js'; import { addRegionToQuery } from '../utils/region.js'; import { appsUrlBuilder } from '../utils/urls-builder.js'; export const listJobs = async (appId, region) => { try { const path = appSchedulerUrl(appId); const url = appsUrlBuilder(path); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.GET, query: addRegionToQuery({}, region), }); return response.jobs; } catch (error) { if (error instanceof HttpError) { handleHttpErrors(error); } throw new Error('failed to list scheduler jobs'); } }; const createJob = async (appId, job, region) => { try { const path = appSchedulerUrl(appId); const url = appsUrlBuilder(path); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.POST, body: job, query: addRegionToQuery({}, region), }); return response.job; } catch (error) { if (error instanceof HttpError) { handleHttpErrors(error); } throw new Error('failed to create scheduler job'); } }; const deleteJob = async (appId, jobName, region) => { try { const path = `${appSchedulerUrl(appId)}/${jobName}`; const url = appsUrlBuilder(path); await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.DELETE, query: addRegionToQuery({}, region), }); } catch (error) { if (error instanceof HttpError) { handleHttpErrors(error); } throw new Error('failed to delete scheduler job'); } }; const runJob = async (appId, jobName, region) => { try { const path = `${appSchedulerUrl(appId)}/${jobName}/run`; const url = appsUrlBuilder(path); await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.POST, query: addRegionToQuery({}, region), }); } catch (error) { if (error instanceof HttpError) { handleHttpErrors(error); } throw new Error('failed to run scheduler job'); } }; const updateJob = async (appId, jobName, job, region) => { try { const path = `${appSchedulerUrl(appId)}/${jobName}`; const url = appsUrlBuilder(path); const response = await execute({ url, headers: { Accept: 'application/json' }, method: HttpMethodTypes.PUT, body: job, query: addRegionToQuery({}, region), }); return response.job; } catch (error) { if (error instanceof HttpError) { handleHttpErrors(error); } throw new Error('failed to update scheduler job'); } }; export const SchedulerService = { listJobs, createJob, deleteJob, runJob, updateJob, };