All files / sdk/models activeTriggers.ts

65% Statements 13/20
72.72% Branches 8/11
80% Functions 4/5
65% Lines 13/20

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104  11x   11x                             11x       25x                       1x 1x         1x                               2x 2x       2x                                                         1x 1x           1x            
import { GetActiveTriggersData } from "../client/types.gen";
import apiClient from "../client/client";
import { BackendClient } from "./backendClient";
import { CEG } from "../utils/error";
 
type TActiveTrigger = {
  id: string;
  connectionId: string;
  triggerName: string;
  triggerData: string;
  triggerConfig: Record<string, any>;
  state: Record<string, any>;
  createdAt: string;
  updatedAt: string;
  disabledAt: string | null;
  disabledReason: string | null;
};
 
export class ActiveTriggers {
  backendClient: BackendClient;
 
  constructor(backendClient: BackendClient) {
    this.backendClient = backendClient;
  }
  /**
   * Retrieves details of a specific active trigger in the Composio platform by providing its trigger name.
   *
   * The response includes the trigger's name, description, input parameters, expected response, associated app information, and enabled status.
   *
   * @param {GetActiveTriggerData} data The data for the request.
   * @returns {CancelablePromise<GetActiveTriggerResponse>} A promise that resolves to the details of the active trigger.
   * @throws {ApiError} If the request fails.
   */
  async get({ triggerId }: { triggerId: string }) {
    try {
      const { data } = await apiClient.triggers.getActiveTriggers({
        query: {
          triggerIds: `${triggerId}`,
        },
      });
      return data?.triggers[0];
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Retrieves a list of all active triggers in the Composio platform.
   *
   * This method allows you to fetch a list of all the available active triggers. It supports pagination to handle large numbers of triggers. The response includes an array of trigger objects, each containing information such as the trigger's name, description, input parameters, expected response, associated app information, and enabled status.
   *
   * @param {ListActiveTriggersData} data The data for the request.
   * @returns {CancelablePromise<ListActiveTriggersResponse>} A promise that resolves to the list of all active triggers.
   * @throws {ApiError} If the request fails.
   */
  async list(data: GetActiveTriggersData = {}) {
    try {
      const { data: response } = await apiClient.triggers.getActiveTriggers({
        query: data,
      });
 
      return response?.triggers || [];
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Enables the previously disabled trigger.
   *
   * @param {Object} data The data for the request.
   * @param {string} data.triggerId Id of the trigger
   * @returns {CancelablePromise<Record<string, any>>} A promise that resolves to the response of the enable request.
   * @throws {ApiError} If the request fails.
   */
  async enable(data: { triggerId: string }): Promise<boolean> {
    try {
      await apiClient.triggers.switchTriggerInstanceStatus({
        path: data,
        body: {
          enabled: true,
        },
      });
      return true;
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  async disable(data: { triggerId: string }) {
    try {
      await apiClient.triggers.switchTriggerInstanceStatus({
        path: data,
        body: {
          enabled: false,
        },
      });
      return true;
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
}