|
1 | 1 | import { ITool } from ".."; |
| 2 | +import { type AuthProvider, type Authorization } from "./integrations"; |
2 | 3 |
|
3 | 4 | /** |
4 | 5 | * Represents an incoming webhook request. |
@@ -125,35 +126,68 @@ export abstract class Network extends ITool { |
125 | 126 | * Generates a unique HTTP endpoint that will invoke the callback function |
126 | 127 | * when requests are received. The callback receives the WebhookRequest plus any extraArgs. |
127 | 128 | * |
128 | | - * For provider-specific webhooks (e.g., Slack), use the provider and authorization |
129 | | - * parameters to enable automatic routing based on provider-specific identifiers. |
| 129 | + * **Provider-Specific Behavior:** |
| 130 | + * - **Slack**: Uses provider-specific routing via team_id. Requires `authorization` parameter. |
| 131 | + * - **Gmail** (Google with Gmail scopes): Returns a Google Pub/Sub topic name instead of a webhook URL. |
| 132 | + * The topic name (e.g., "projects/plot-prod/topics/gmail-webhook-abc123") should be passed |
| 133 | + * to the Gmail API's `users.watch` endpoint. Requires `authorization` parameter with Gmail scopes. |
| 134 | + * - **Default**: Returns a standard webhook URL for all other cases. |
130 | 135 | * |
131 | 136 | * @param options - Webhook creation options |
132 | 137 | * @param options.callback - Function receiving (request, ...extraArgs) |
133 | 138 | * @param options.extraArgs - Additional arguments to pass to the callback (type-checked) |
134 | 139 | * @param options.provider - Optional provider for provider-specific webhook routing |
135 | | - * @param options.authorization - Optional authorization for provider-specific webhooks (required for Slack) |
136 | | - * @returns Promise resolving to the webhook URL |
| 140 | + * @param options.authorization - Optional authorization for provider-specific webhooks (required for Slack and Gmail) |
| 141 | + * @returns Promise resolving to the webhook URL, or for Gmail, a Pub/Sub topic name |
| 142 | + * |
| 143 | + * @example |
| 144 | + * ```typescript |
| 145 | + * // Gmail webhook - returns Pub/Sub topic name |
| 146 | + * const topicName = await this.tools.network.createWebhook({ |
| 147 | + * callback: this.onGmailNotification, |
| 148 | + * provider: AuthProvider.Google, |
| 149 | + * authorization: gmailAuth, |
| 150 | + * extraArgs: ["inbox"] |
| 151 | + * }); |
| 152 | + * // topicName: "projects/plot-prod/topics/gmail-webhook-abc123" |
| 153 | + * |
| 154 | + * // Pass topic name to Gmail API |
| 155 | + * await gmailApi.users.watch({ |
| 156 | + * userId: 'me', |
| 157 | + * requestBody: { |
| 158 | + * topicName: topicName, // Use the returned topic name |
| 159 | + * labelIds: ['INBOX'] |
| 160 | + * } |
| 161 | + * }); |
| 162 | + * ``` |
137 | 163 | */ |
138 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
139 | 164 | abstract createWebhook< |
140 | 165 | TCallback extends (request: WebhookRequest, ...args: any[]) => any |
141 | 166 | >(options: { |
142 | 167 | callback: TCallback; |
143 | 168 | extraArgs?: TCallback extends (req: any, ...rest: infer R) => any ? R : []; |
144 | | - provider?: any; // AuthProvider from integrations, but we can't import it here |
145 | | - authorization?: any; // Authorization from integrations |
| 169 | + provider?: AuthProvider; |
| 170 | + authorization?: Authorization; |
146 | 171 | }): Promise<string>; |
147 | 172 |
|
148 | 173 | /** |
149 | 174 | * Deletes an existing webhook endpoint. |
150 | 175 | * |
151 | | - * Removes the webhook endpoint and stops processing requests to that URL. |
152 | | - * Any subsequent requests to the deleted webhook will return 404. |
| 176 | + * Removes the webhook endpoint and stops processing requests. |
| 177 | + * Works with all webhook types (standard, Slack, and Gmail). |
| 178 | + * |
| 179 | + * **For Gmail webhooks:** Also deletes the associated Google Pub/Sub topic and subscription. |
| 180 | + * |
| 181 | + * **For Slack webhooks:** Removes the callback registration for the specific team. |
| 182 | + * |
| 183 | + * **For standard webhooks:** Removes the webhook endpoint. Any subsequent requests |
| 184 | + * to the deleted webhook will return 404. |
153 | 185 | * |
154 | | - * @param url - The webhook URL to delete |
| 186 | + * @param url - The webhook identifier returned from `createWebhook()`. |
| 187 | + * This can be a URL (standard webhooks), a Pub/Sub topic name (Gmail), |
| 188 | + * or an opaque identifier (Slack). Always pass the exact value returned |
| 189 | + * from `createWebhook()`. |
155 | 190 | * @returns Promise that resolves when the webhook is deleted |
156 | 191 | */ |
157 | | - // eslint-disable-next-line @typescript-eslint/no-unused-vars |
158 | 192 | abstract deleteWebhook(url: string): Promise<void>; |
159 | 193 | } |
0 commit comments