All files / src/sdk/models activeTriggers.ts

72.72% Statements 24/33
76.92% Branches 10/13
80% Functions 4/5
72.72% Lines 24/33

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146    11x 11x         11x 11x 11x             11x       26x   26x 26x                           1x         1x 1x 1x             1x                               2x         2x 2x 2x         2x                                                                                     1x         1x 1x 1x             1x                
import { Client } from "@hey-api/client-axios";
import { z } from "zod";
import apiClient from "../client/client";
import {
  ZActiveTriggerItemRes,
  ZActiveTriggersQuery,
  ZTriggerItemParam,
} from "../types/activeTrigger";
import { CEG } from "../utils/error";
import { TELEMETRY_LOGGER } from "../utils/telemetry";
import { TELEMETRY_EVENTS } from "../utils/telemetry/events";
import { AxiosBackendClient } from "./backendClient";
 
export type TriggerItemParam = z.infer<typeof ZTriggerItemParam>;
export type GetActiveTriggersData = z.infer<typeof ZActiveTriggersQuery>;
export type TriggerItemRes = z.infer<typeof ZActiveTriggerItemRes>;
export type TriggerChangeResponse = { status: string };
export class ActiveTriggers {
  // Remove this as we might not need it
  private backendClient: AxiosBackendClient;
  private client: Client;
  private fileName: string = "js/src/sdk/models/activeTriggers.ts";
  constructor(backendClient: AxiosBackendClient, client: Client) {
    this.backendClient = backendClient;
    this.client = client;
  }
 
  /** Missing type */
  /**
   * 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 {TriggerItemParam} data The data for the request.
   * @returns {Promise<TriggerItemRes>} A promise that resolves to the details of the active trigger.
   * @throws {ComposioError} If the request fails.
   */
  async get({ triggerId }: TriggerItemParam) {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "get",
      file: this.fileName,
      params: { triggerId },
    });
    try {
      const parsedData = ZTriggerItemParam.parse({ triggerId });
      const { data } = await apiClient.triggers.getActiveTriggers({
        client: this.client,
        query: {
          triggerIds: `${parsedData.triggerId}`,
        },
      });
 
      return data?.triggers?.[0] as unknown as TriggerItemRes;
    } 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 {GetActiveTriggersData} data The data for the request.
   * @returns {Promise<ZActiveTriggerItemRes[]>} A promise that resolves to the list of all active triggers.
   * @throws {ComposioError} If the request fails.
   */
  async list(data: GetActiveTriggersData = {}): Promise<TriggerItemRes[]> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "list",
      file: this.fileName,
      params: { data },
    });
    try {
      const parsedData = ZActiveTriggersQuery.parse(data);
      const { data: response } = await apiClient.triggers.getActiveTriggers({
        client: this.client,
        query: parsedData,
      });
 
      return response?.triggers as unknown as TriggerItemRes[];
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Enables the previously disabled trigger.
   *
   * @param {TriggerItemParam} data The data for the request.
   * @returns {Promise<{status: string}>} A promise that resolves to the response of the enable request.
   * @throws {ComposioError} If the request fails.
   */
  async enable(data: TriggerItemParam): Promise<{ status: string }> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "enable",
      file: this.fileName,
      params: { data },
    });
    try {
      const parsedData = ZTriggerItemParam.parse(data);
      await apiClient.triggers.switchTriggerInstanceStatus({
        client: this.client,
        path: { triggerId: parsedData.triggerId },
        body: {
          enabled: true,
        },
      });
      return {
        status: "success",
      };
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
 
  /**
   * Disables the previously enabled trigger.
   *
   * @param {TriggerItemParam} data The data for the request.
   * @returns {Promise<{status: string}>} A promise that resolves to the response of the disable request.
   */
  async disable(data: TriggerItemParam): Promise<TriggerChangeResponse> {
    TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, {
      method: "disable",
      file: this.fileName,
      params: { data },
    });
    try {
      const parsedData = ZTriggerItemParam.parse(data);
      await apiClient.triggers.switchTriggerInstanceStatus({
        client: this.client,
        path: parsedData,
        body: {
          enabled: false,
        },
      });
      return {
        status: "success",
      };
    } catch (error) {
      throw CEG.handleAllError(error);
    }
  }
}