From df7aba9e24e3e9fd4ab3011b5224efbdfe450922 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 12:46:57 +0530 Subject: [PATCH 1/8] feat: add fetch by trigger ids --- js/src/sdk/models/triggers.spec.ts | 13 +++++++++++-- js/src/sdk/models/triggers.ts | 24 ++++++++++++++++++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/js/src/sdk/models/triggers.spec.ts b/js/src/sdk/models/triggers.spec.ts index 1ebbb1fb595..23e4ff892c3 100644 --- a/js/src/sdk/models/triggers.spec.ts +++ b/js/src/sdk/models/triggers.spec.ts @@ -31,11 +31,11 @@ describe("Apps class tests", () => { it("should retrieve a list of triggers for a specific app", async () => { const triggerList = await triggers.list({ - appNames: "github", + appNames: ["gmail"], }); // this is breaking for now expect(triggerList.length).toBeGreaterThan(0); - expect(triggerList[0].appName).toBe("github"); + expect(triggerList[0].appName).toBe("gmail"); }); }); @@ -94,6 +94,15 @@ describe("Apps class tests subscribe", () => { expect(trigger.status).toBe("success"); }); + + it("should retrieve a list of triggers for a specific triggerId", async () => { + const triggerList = await triggers.list({ + triggerIds: ["GMAIL_NEW_GMAIL_MESSAGE"], + }); + expect(triggerList.length).toBeGreaterThan(0); + expect(triggerList[0].name).toBe("GMAIL_NEW_GMAIL_MESSAGE"); + }); + // it("should subscribe to a trigger and receive a trigger", async () => { // function waitForTriggerReceived() { // return new Promise((resolve) => { diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index cf3ee61cd27..04acab7719b 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -3,13 +3,23 @@ import logger from "../../utils/logger"; import { BackendClient } from "./backendClient"; import apiClient from "../client/client"; - +import z from "zod"; import { CEG } from "../utils/error"; import { ListTriggersData } from "../client"; import { TELEMETRY_LOGGER } from "../utils/telemetry"; import { TELEMETRY_EVENTS } from "../utils/telemetry/events"; -type RequiredQuery = ListTriggersData["query"]; + type RequiredQuery = ListTriggersData["query"]; + +const ZTriggerQuery = z.object({ + triggerIds: z.array(z.string()).optional(), + appNames: z.array(z.string()).optional(), + connectedAccountsIds: z.array(z.string()).optional(), + integrationIds: z.array(z.string()).optional(), + showEnabledOnly: z.boolean().optional(), +}); + +type TTriggerQuery = z.infer; export class Triggers { trigger_to_client_event = "trigger_to_client"; @@ -29,18 +39,24 @@ export class Triggers { * @returns {CancelablePromise} A promise that resolves to the list of all triggers. * @throws {ApiError} If the request fails. */ - async list(data: RequiredQuery = {}) { + async list(data: TTriggerQuery = {}) { TELEMETRY_LOGGER.manualTelemetry(TELEMETRY_EVENTS.SDK_METHOD_INVOKED, { method: "list", file: this.fileName, params: { data }, }); + const validatedData = ZTriggerQuery.parse(data); try { const { data: response } = await apiClient.triggers.listTriggers({ query: { - appNames: data?.appNames, + appNames: validatedData?.appNames?.join(","), + triggerIds: validatedData?.triggerIds?.join(","), + connectedAccountIds: validatedData?.connectedAccountsIds?.join(","), + integrationIds: validatedData?.integrationIds?.join(","), + showEnabledOnly: validatedData?.showEnabledOnly, }, }); + return response || []; } catch (error) { throw CEG.handleAllError(error); From 1786e62e523f0e085203f17d7ee0e7d97609d802 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 12:49:09 +0530 Subject: [PATCH 2/8] feat: add invariant --- js/src/sdk/models/triggers.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index 04acab7719b..d6075cc6cd6 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -8,6 +8,7 @@ import { CEG } from "../utils/error"; import { ListTriggersData } from "../client"; import { TELEMETRY_LOGGER } from "../utils/telemetry"; import { TELEMETRY_EVENTS } from "../utils/telemetry/events"; +import { SDK_ERROR_CODES } from "../utils/errors/src/constants"; type RequiredQuery = ListTriggersData["query"]; @@ -45,15 +46,22 @@ export class Triggers { file: this.fileName, params: { data }, }); - const validatedData = ZTriggerQuery.parse(data); + const {appNames, triggerIds, connectedAccountsIds, integrationIds, showEnabledOnly} = ZTriggerQuery.parse(data); + + if(!appNames && !triggerIds && !connectedAccountsIds && !integrationIds) { + throw CEG.getCustomError(SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, { + message: "At least one of appNames, triggerIds, connectedAccountsIds, integrationIds is required", + }); + } + try { const { data: response } = await apiClient.triggers.listTriggers({ query: { - appNames: validatedData?.appNames?.join(","), - triggerIds: validatedData?.triggerIds?.join(","), - connectedAccountIds: validatedData?.connectedAccountsIds?.join(","), - integrationIds: validatedData?.integrationIds?.join(","), - showEnabledOnly: validatedData?.showEnabledOnly, + appNames: appNames?.join(","), + triggerIds: triggerIds?.join(","), + connectedAccountIds: connectedAccountsIds?.join(","), + integrationIds: integrationIds?.join(","), + showEnabledOnly: showEnabledOnly, }, }); From 7027ecc79d3c30dad1e77316a6aabe2f3afb343a Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 12:56:33 +0530 Subject: [PATCH 3/8] feat: prettier --- js/src/sdk/models/triggers.spec.ts | 1 - js/src/sdk/models/triggers.ts | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/js/src/sdk/models/triggers.spec.ts b/js/src/sdk/models/triggers.spec.ts index 23e4ff892c3..8826f51569b 100644 --- a/js/src/sdk/models/triggers.spec.ts +++ b/js/src/sdk/models/triggers.spec.ts @@ -94,7 +94,6 @@ describe("Apps class tests subscribe", () => { expect(trigger.status).toBe("success"); }); - it("should retrieve a list of triggers for a specific triggerId", async () => { const triggerList = await triggers.list({ triggerIds: ["GMAIL_NEW_GMAIL_MESSAGE"], diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index d6075cc6cd6..ff46ff61218 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -10,7 +10,7 @@ import { TELEMETRY_LOGGER } from "../utils/telemetry"; import { TELEMETRY_EVENTS } from "../utils/telemetry/events"; import { SDK_ERROR_CODES } from "../utils/errors/src/constants"; - type RequiredQuery = ListTriggersData["query"]; +type RequiredQuery = ListTriggersData["query"]; const ZTriggerQuery = z.object({ triggerIds: z.array(z.string()).optional(), @@ -46,11 +46,18 @@ export class Triggers { file: this.fileName, params: { data }, }); - const {appNames, triggerIds, connectedAccountsIds, integrationIds, showEnabledOnly} = ZTriggerQuery.parse(data); - - if(!appNames && !triggerIds && !connectedAccountsIds && !integrationIds) { + const { + appNames, + triggerIds, + connectedAccountsIds, + integrationIds, + showEnabledOnly, + } = ZTriggerQuery.parse(data); + + if (!appNames && !triggerIds && !connectedAccountsIds && !integrationIds) { throw CEG.getCustomError(SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, { - message: "At least one of appNames, triggerIds, connectedAccountsIds, integrationIds is required", + message: + "At least one of appNames, triggerIds, connectedAccountsIds, integrationIds is required", }); } From 1dc542a566548a42ced2f371a79448c45b40e6f3 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 13:02:52 +0530 Subject: [PATCH 4/8] feat: remove invariant --- js/src/sdk/models/triggers.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index ff46ff61218..3386e1f77f6 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -54,13 +54,6 @@ export class Triggers { showEnabledOnly, } = ZTriggerQuery.parse(data); - if (!appNames && !triggerIds && !connectedAccountsIds && !integrationIds) { - throw CEG.getCustomError(SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, { - message: - "At least one of appNames, triggerIds, connectedAccountsIds, integrationIds is required", - }); - } - try { const { data: response } = await apiClient.triggers.listTriggers({ query: { From c382661d8dc9206de7c4fcf4fbbd5a05eb2c4ef0 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 13:49:10 +0530 Subject: [PATCH 5/8] feat: trigger --- js/src/sdk/models/triggers.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index 3386e1f77f6..61060b171a3 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -46,15 +46,16 @@ export class Triggers { file: this.fileName, params: { data }, }); - const { - appNames, - triggerIds, - connectedAccountsIds, - integrationIds, - showEnabledOnly, - } = ZTriggerQuery.parse(data); try { + const { + appNames, + triggerIds, + connectedAccountsIds, + integrationIds, + showEnabledOnly, + } = ZTriggerQuery.parse(data); + const { data: response } = await apiClient.triggers.listTriggers({ query: { appNames: appNames?.join(","), From 103dea6efeaf2848c86fd6ec1b8ab0d7b92c3001 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 13:49:47 +0530 Subject: [PATCH 6/8] feat: trigger spec --- js/src/sdk/models/triggers.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/src/sdk/models/triggers.spec.ts b/js/src/sdk/models/triggers.spec.ts index 8826f51569b..c41a9ce8e21 100644 --- a/js/src/sdk/models/triggers.spec.ts +++ b/js/src/sdk/models/triggers.spec.ts @@ -30,12 +30,13 @@ describe("Apps class tests", () => { }); it("should retrieve a list of triggers for a specific app", async () => { + const appName = "github"; const triggerList = await triggers.list({ - appNames: ["gmail"], + appNames: [appName], }); // this is breaking for now expect(triggerList.length).toBeGreaterThan(0); - expect(triggerList[0].appName).toBe("gmail"); + expect(triggerList[0].appName).toBe(appName); }); }); From 06fd71d7a461c2d40c0876387a358a6c6579e2cf Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 15:58:34 +0530 Subject: [PATCH 7/8] feat: ENG-2910 --- js/src/sdk/models/triggers.spec.ts | 9 +++++++++ js/src/sdk/models/triggers.ts | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/js/src/sdk/models/triggers.spec.ts b/js/src/sdk/models/triggers.spec.ts index c41a9ce8e21..0920c0601bc 100644 --- a/js/src/sdk/models/triggers.spec.ts +++ b/js/src/sdk/models/triggers.spec.ts @@ -5,6 +5,7 @@ import { Triggers } from "./triggers"; import { ConnectedAccounts } from "./connectedAccounts"; import { Entity } from "./Entity"; import { Actions } from "./actions"; +import { SDK_ERROR_CODES } from "../utils/errors/src/constants"; describe("Apps class tests", () => { let backendClient; @@ -103,6 +104,14 @@ describe("Apps class tests subscribe", () => { expect(triggerList[0].name).toBe("GMAIL_NEW_GMAIL_MESSAGE"); }); + it("try to subscribe to a trigger that doesn't exist", async () => { + await expect( + triggers.subscribe(async () => {}, { + triggerId: "GMAIL_NEW_GMAIL_MESSAGE_2", + }) + ).rejects.toThrow("No triggers found for the given filters"); + }); + // it("should subscribe to a trigger and receive a trigger", async () => { // function waitForTriggerReceived() { // return new Promise((resolve) => { diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index 61060b171a3..8aa5e569887 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -187,9 +187,28 @@ export class Triggers { params: { filters }, }); if (!fn) throw new Error("Function is required for trigger subscription"); - //@ts-ignore const clientId = await this.backendClient.getClientId(); - //@ts-ignore + + const availableTriggers = await this.list({ + appNames: filters.appName ? [filters.appName] : undefined, + triggerIds: filters.triggerId ? [filters.triggerId] : undefined, + connectedAccountsIds: filters.connectionId + ? [filters.connectionId] + : undefined, + integrationIds: filters.integrationId + ? [filters.integrationId] + : undefined, + }); + + if (availableTriggers.length === 0) { + throw CEG.getCustomError(SDK_ERROR_CODES.COMMON.INVALID_PARAMS_PASSED, { + message: "No triggers match the specified filters", + description: "The provided filters did not return any triggers.", + possibleFix: + "Verify the filters and ensure a trigger is set up with the specified criteria.", + }); + } + await PusherUtils.getPusherClient( this.backendClient.baseUrl, this.backendClient.apiKey From 360b0bb096c3a873bc537d238402e1464ce96885 Mon Sep 17 00:00:00 2001 From: Himanshu Dixit Date: Thu, 19 Dec 2024 17:22:54 +0530 Subject: [PATCH 8/8] feat: update trigger --- js/src/sdk/models/triggers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/js/src/sdk/models/triggers.ts b/js/src/sdk/models/triggers.ts index b25f1bcb6e3..056398b51b2 100644 --- a/js/src/sdk/models/triggers.ts +++ b/js/src/sdk/models/triggers.ts @@ -2,13 +2,13 @@ import logger from "../../utils/logger"; import { PusherUtils, TriggerData } from "../utils/pusher"; import { BackendClient } from "./backendClient"; -import apiClient from "../client/client"; import z from "zod"; -import { CEG } from "../utils/error"; import { ListTriggersData } from "../client"; +import apiClient from "../client/client"; +import { CEG } from "../utils/error"; +import { SDK_ERROR_CODES } from "../utils/errors/src/constants"; import { TELEMETRY_LOGGER } from "../utils/telemetry"; import { TELEMETRY_EVENTS } from "../utils/telemetry/events"; -import { SDK_ERROR_CODES } from "../utils/errors/src/constants"; type RequiredQuery = ListTriggersData["query"];