diff --git a/packages/sdk/CHANGELOG.md b/packages/sdk/CHANGELOG.md index 620a767cf4aa6..b46f39a724014 100644 --- a/packages/sdk/CHANGELOG.md +++ b/packages/sdk/CHANGELOG.md @@ -2,6 +2,12 @@ # Changelog +## [1.2.0] - 2025-01-23 + +### Added + +- New methods and types to interact with the deployed triggers API + ## [1.1.6] - 2025-01-21 ### Changed diff --git a/packages/sdk/package.json b/packages/sdk/package.json index f063da12aa429..9d1251f46277f 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/sdk", - "version": "1.1.6", + "version": "1.2.0", "description": "Pipedream SDK", "main": "dist/server/server/index.js", "module": "dist/server/server/index.js", diff --git a/packages/sdk/src/shared/component.ts b/packages/sdk/src/shared/component.ts index 8f539bd381d06..d35de1bbd1f0e 100644 --- a/packages/sdk/src/shared/component.ts +++ b/packages/sdk/src/shared/component.ts @@ -108,3 +108,25 @@ export type V1DeployedComponent = { // eslint name_slug: string; callback_observations?: unknown; }; + +export type V1EmittedEvent = { + /** + * The event's payload. + */ + e: Record; // eslint-disable-line @typescript-eslint/no-explicit-any + + /** + * The event's type (set to "emit" currently). + */ + k: string; + + /** + * The event's timestamp in epoch milliseconds. + */ + ts: number; + + /** + * The event's unique ID. + */ + id: string; +} diff --git a/packages/sdk/src/shared/index.ts b/packages/sdk/src/shared/index.ts index 6376f0ed2767d..68f18745802a7 100644 --- a/packages/sdk/src/shared/index.ts +++ b/packages/sdk/src/shared/index.ts @@ -4,6 +4,7 @@ import type { ConfiguredProps, V1Component, V1DeployedComponent, + V1EmittedEvent, } from "./component.js"; export * from "./component.js"; import { version as sdkVersion } from "../version.js"; @@ -553,12 +554,166 @@ export type DeployTriggerOpts = ExternalUserId & { */ dynamicPropsId?: string; + /** + * The ID of the workflow that the trigger will use to send the events it + * generates. + */ + workflowId?: string; + /** * The webhook URL that the trigger will use to send the events it generates. */ webhookUrl?: string; }; +/** + * The request options for deleting a deployed trigger owned by a particular + * user. + */ +export type DeleteTriggerOpts = { + /** + * The ID of the trigger you're deleting (`dc_xxxxxxx` for example ). + */ + id: string; + + /** + * The end user ID, for whom you deployed the trigger. + */ + externalUserId: string; + + /** + * When explicitly set, the API will ignore any errors that occur during the + * deactivation hook of the trigger, effectively forcing the deletion of the + * trigger. + */ + ignoreHookErrors?: boolean; +}; + +/** + * The request options for retrieving a deployed trigger owned by a particular + * user. + */ +export type GetTriggerOpts = { + /** + * The ID of the trigger you're retrieving. + */ + id: string; + + /** + * Your end user ID, for whom you deployed the trigger. + */ + externalUserId: string; +}; + +/** + * The request options for retrieving the events emitted by a deployed trigger. + */ +export type GetTriggerEventsOpts = GetTriggerOpts & { + /** + * The number of events to retrieve (defaults to 20 if not provided). + */ + limit?: number; +}; + +/** + * The response from retrieving the events emitted by a deployed trigger. + */ +export type GetTriggerEventsResponse = { + /** + * The list of events emitted by the trigger. + */ + data: V1EmittedEvent[]; +}; + +/** + * The request options for retrieving the workflows that listen to events + * emitted by a specific trigger. + */ +export type GetTriggerWorkflowsOpts = GetTriggerOpts; + +/** + * The response from retrieving the workflows that listen to events emitted by a + * specific trigger. + */ +export type GetTriggerWorkflowsResponse = { + /** + * The list of workflow IDs that listen to events emitted by the trigger. + */ + workflow_ids: string[]; +}; + +/** + * The request options for updating the workflows that listen to events emitted + * by a specific trigger. + */ +export type UpdateTriggerWorkflowsOpts = GetTriggerOpts & { + /** + * The workflow IDs that should to events emitted by the trigger. + */ + workflowIds: string[]; +} + +/** + * The request options for retrieving the webhooks that listen to events + * emitted by a specific trigger. + */ +export type GetTriggerWebhooksOpts = GetTriggerOpts; + +/** + * The response from retrieving the webhooks that listen to events emitted by a + * specific trigger. + */ +export type GetTriggerWebhooksResponse = { + /** + * The list of webhook URLs that listen to events emitted by the trigger. + */ + webhook_urls: string[]; +}; + +/** + * The request options for updating the webhooks that listen to events emitted + * by a specific trigger. + */ +export type UpdateTriggerWebhooksOpts = GetTriggerOpts & { + /** + * The webhook URLs that should to events emitted by the trigger. + */ + webhookUrls: string[]; +} + +/** + * The request options for retrieving a list of deployed triggers for a + * particular user. + */ +export type GetTriggersOpts = RelationOpts & { + /** + * Your end user ID, for whom you deployed the trigger. + */ + externalUserId: string; +}; + +export type UpdateTriggerOpts = { + /** + * The ID of the trigger you're updating. + */ + id: string; + + /** + * Your end user ID, for whom you deployed the trigger. + */ + externalUserId: string; + + /** + * The state to which the trigger should be updated. + */ + active?: boolean; + + /** + * The new name of the trigger. + */ + name?: string; +}; + /** * Different ways in which customers can authorize requests to HTTP endpoints */ @@ -590,7 +745,7 @@ export interface RequestOptions extends Omit { /** * Query parameters to include in the request URL. */ - params?: Record; + params?: Record; /** * Headers to include in the request. @@ -1172,6 +1327,221 @@ export abstract class BaseClient { return this.deployTrigger(opts); } + /** + * Deletes a specific trigger. + * + * @param opts - The options for deleting the trigger. + * @returns No content + */ + public deleteTrigger(opts: DeleteTriggerOpts) { + const { + id, + externalUserId, + ignoreHookErrors = null, + } = opts; + + return this.makeConnectRequest(`/deployed-triggers/${id}`, { + method: "DELETE", + params: { + external_user_id: externalUserId, + ignore_hook_errors: ignoreHookErrors, + }, + }); + } + + /** + * Retrieves the metadata for a specific trigger. + * + * @param opts - The options for retrieving the trigger. + * @returns A promise resolving to the trigger metadata. + */ + public getTrigger(opts: GetTriggerOpts) { + const { + id, + externalUserId, + } = opts; + + return this.makeConnectRequest(`/deployed-triggers/${id}`, { + method: "GET", + params: { + external_user_id: externalUserId, + }, + }); + } + + /** + * Retrieves the metadata for all deployed triggers + * + * @param opts - The options for retrieving the triggers. + * @returns A promise resolving to a list of the trigger metadata. + */ + public getTriggers(opts: GetTriggersOpts) { + const { externalUserId } = opts; + + return this.makeConnectRequest("/deployed-triggers", { + method: "GET", + params: { + external_user_id: externalUserId, + }, + }); + } + + /** + * Updates a specific trigger. + * + * @param opts - The options for updating the trigger. + * @returns A promise resolving to the trigger metadata. + */ + public updateTrigger(opts: UpdateTriggerOpts) { + const { + id, + externalUserId, + active = null, + name = null, + } = opts; + + return this.makeConnectRequest(`/deployed-triggers/${id}`, { + method: "PUT", + params: { + external_user_id: externalUserId, + }, + body: { + active, + name, + }, + }); + } + + /** + * Retrieves the last events emitted by a specific trigger. + * + * @param opts - The options for retrieving the trigger events. + * @returns A promise resolving to a list of emitted events. + */ + public getTriggerEvents(opts: GetTriggerEventsOpts) { + const { + id, + externalUserId, + limit = null, + } = opts; + + return this.makeConnectRequest( + `/deployed-triggers/${id}/events`, { + method: "GET", + params: { + external_user_id: externalUserId, + n: limit, + }, + }, + ); + } + + /** + * Retrieves the list of workflows to which the trigger emits events. + * + * @param opts - The options for retrieving the listening workflows. + * @returns A promise resolving to a list of workflows. + */ + public getTriggerWorkflows(opts: GetTriggerWorkflowsOpts) { + const { + id, + externalUserId, + } = opts; + + return this.makeConnectRequest( + `/deployed-triggers/${id}/pipelines`, { + method: "GET", + params: { + external_user_id: externalUserId, + }, + }, + ); + } + + /** + * Updates the list of workflows to which the trigger will emit events. + * + * @param opts - The options for updating the listening workflows. + * @throws If `workflowIds` is not an array. + * @returns A promise resolving to a list of workflows. + */ + public updateTriggerWorkflows(opts: UpdateTriggerWorkflowsOpts) { + const { + id, + externalUserId, + workflowIds, + } = opts; + + if (!Array.isArray(workflowIds)) { + throw new Error("workflowIds must be an array"); + } + + return this.makeConnectRequest( + `/deployed-triggers/${id}/pipelines`, { + method: "PUT", + params: { + external_user_id: externalUserId, + }, + body: { + workflow_ids: workflowIds, + }, + }, + ); + } + + /** + * Retrieves the list of webhooks to which the trigger emits events. + * + * @param opts - The options for retrieving the listening webhooks. + * @returns A promise resolving to a list of webhooks. + */ + public getTriggerWebhooks(opts: GetTriggerWebhooksOpts) { + const { + id, + externalUserId, + } = opts; + + return this.makeConnectRequest( + `/deployed-triggers/${id}/webhooks`, { + method: "GET", + params: { + external_user_id: externalUserId, + }, + }, + ); + } + + /** + * Updates the list of webhooks to which the trigger will emit events. + * + * @param opts - The options for updating the listening webhooks. + * @throws If `webhookUrls` is not an array. + * @returns A promise resolving to a list of webhooks. + */ + public updateTriggerWebhooks(opts: UpdateTriggerWebhooksOpts) { + const { + id, + externalUserId, + webhookUrls, + } = opts; + + if (!Array.isArray(webhookUrls)) { + throw new Error("webhookUrls must be an array"); + } + + return this.makeConnectRequest( + `/deployed-triggers/${id}/webhooks`, { + method: "PUT", + params: { + external_user_id: externalUserId, + }, + body: { + webhook_urls: webhookUrls, + }, + }, + ); + } + /** * Builds a full workflow URL based on the input. * diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 33c1996281eff..abe70f911e917 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25107,22 +25107,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at . superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net + deprecated: Please downgrade to v7.1.5 if you need IE/ActiveXObject support OR upgrade to v8.0.0 as we no longer support IE and published an incorrect patch version (see https://github.com/visionmedia/superagent/issues/1731) supports-color@2.0.0: resolution: {integrity: sha512-KKNVtd6pCYgPIKU4cp2733HWYCpplQhddZLBUryaAHou723x+FRzQ5Df824Fj+IyyuiQTRoub4SnIFfIcrp70g==}