All files / web/src/api triggers.ts

73.46% Statements 36/49
69.23% Branches 9/13
100% Functions 5/5
73.46% Lines 36/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x     1x   1x 4x       4x 1x   1x 1x       1x 1x   1x 4x     4x 4x 4x   1x 2x 2x 2x 2x           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   1x 1x 1x  
import { apiClient } from './config'
import { isTauri, invokeCommand } from './utils'
import type { TriggerStatus } from '@/types/generated/TriggerStatus'
import type { TestWorkflowResponse } from '@/types/api'
import { API_ENDPOINTS } from '@/constants'
 
export const activateWorkflow = async (id: string): Promise<void> => {
  if (isTauri()) {
    await invokeCommand('activate_workflow', { workflow_id: id })
    return
  }
  await apiClient.put(API_ENDPOINTS.TRIGGER.ACTIVATE(id))
}
 
export const deactivateWorkflow = async (id: string): Promise<void> => {
  if (isTauri()) {
    await invokeCommand('deactivate_workflow', { workflow_id: id })
    return
  }
  await apiClient.put(API_ENDPOINTS.TRIGGER.DEACTIVATE(id))
}
 
export const getTriggerStatus = async (id: string): Promise<TriggerStatus | null> => {
  if (isTauri()) {
    return invokeCommand<TriggerStatus | null>('get_trigger_status', { workflow_id: id })
  }
  const response = await apiClient.get<TriggerStatus>(API_ENDPOINTS.TRIGGER.STATUS(id))
  return response.data || null
}
 
export const testWorkflow = async (
  id: string,
  testData?: any
): Promise<TestWorkflowResponse> => {
  if (isTauri()) {
    return invokeCommand<TestWorkflowResponse>('test_workflow', {
      workflow_id: id,
      test_data: testData || {}
    })
  }
  const payload = testData ?? {}
  const response = await apiClient.post<TestWorkflowResponse>(
    API_ENDPOINTS.TRIGGER.TEST(id),
    payload,
    {
      headers: {
        'Content-Type': 'application/json',
      },
    }
  )
  return response.data
}
 
export const getWebhookUrl = (id: string): string => {
  return `${apiClient.defaults.baseURL}${API_ENDPOINTS.TRIGGER.WEBHOOK(id)}`
}