All files / sdk/models triggers.ts

78.57% Statements 22/28
18.18% Branches 4/22
83.33% Functions 10/12
84.61% Lines 22/26

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  8x 8x     8x         8x 14x       14x                             2x       2x                         2x                   2x       1x         1x       2x         2x                         1x   1x   1x   1x                           1x 1x                 1x 1x        
 
import { TriggerData, PusherUtils } from "../utils/pusher";
import logger from "../../utils/logger";
import {BackendClient} from "./backendClient"
 
import apiClient from "../client/client"
import { TriggersControllerListTriggersData, TriggersControllerListTriggersResponse } from "../client";
 
type RequiredQuery = TriggersControllerListTriggersData["query"];
 
export class Triggers {
    trigger_to_client_event = "trigger_to_client";
 
    backendClient: BackendClient;
    constructor(backendClient: BackendClient) {
        this.backendClient = backendClient;
    }
 
    /**
     * Retrieves a list of all triggers in the Composio platform.
     * 
     * This method allows you to fetch a list of all the available 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 {ListTriggersData} data The data for the request.
     * @returns {CancelablePromise<ListTriggersResponse>} A promise that resolves to the list of all triggers.
     * @throws {ApiError} If the request fails.
     */
    //@ts-ignore
    list(data?: RequiredQuery={} ): Promise<TriggersControllerListTriggersResponse> {
        //@ts-ignore
        return apiClient.triggers.listTriggers({
            query: {
                appNames: data?.appNames,
            }
        }).then(res => res.data)
    }
 
    /**
     * Setup a trigger for a connected account.
     * 
     * @param {SetupTriggerData} data The data for the request.
     * @returns {CancelablePromise<SetupTriggerResponse>} A promise that resolves to the setup trigger response.
     * @throws {ApiError} If the request fails.
     */
    //@ts-ignore
    async setup(connectedAccountId, triggerName, config: Record<string, any>):{status:"string",triggerId:string}{
        //@ts-ignore
        const {data,error} = await apiClient.triggers.enableTrigger({
            path:{
                connectedAccountId,
                triggerName
            },
            body: {
                triggerConfig: config
            }
        })
 
        return data as unknown as {status:"string",triggerId:string};
    }
 
    enable(data: { triggerId: any }): any {
        return apiClient.triggers.switchTriggerInstanceStatus({
            path: data,
            body: {
                enabled: true
            }
        }).then(res => res.data)
    }
 
    disable(data: { triggerId: any }): any {
        return apiClient.triggers.switchTriggerInstanceStatus({
            path: data,
            body: {
                enabled: false
            }
        }).then(res => res.data)
    }
 
    async subscribe(fn: (data: TriggerData) => void, filters:{
        appName?: string,
        triggerId?  : string;
        connectionId?: string;
        integrationId?: string;
        triggerName?: string;
        triggerData?: string;
        entityId?: string;
    }={}) {
 
        Iif(!fn) throw new Error("Function is required for trigger subscription");
        //@ts-ignore
        const clientId = await this.backendClient.getClientId();
        //@ts-ignore
        await PusherUtils.getPusherClient(this.backendClient.baseUrl, this.backendClient.apiKey);
 
        const shouldSendTrigger = (data: TriggerData) => {
           if(Object.keys(filters).length === 0) return true;
            else{
                return (
                    (!filters.appName || data.appName === filters.appName) &&
                    (!filters.triggerId || data.metadata.id === filters.triggerId) &&
                    (!filters.connectionId || data.metadata.connectionId === filters.connectionId) &&
                    (!filters.triggerName || data.metadata.triggerName === filters.triggerName) &&
                    (!filters.entityId || data.metadata.connection.clientUniqueUserId === filters.entityId) &&
                    (!filters.integrationId || data.metadata.connection.integrationId === filters.integrationId)
                );
            }
        }
        
        logger.info("Subscribing to triggers",filters)
        PusherUtils.triggerSubscribe(clientId, (data: TriggerData) => {
            Iif (shouldSendTrigger(data)) {
                fn(data);
            }
        });
    }
 
    async unsubscribe() {
        //@ts-ignore
        const clientId = await this.backendClient.getClientId();
        PusherUtils.triggerUnsubscribe(clientId);
    }
}