// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. import { APIResource } from '../core/resource'; import * as FilesAPI from './files'; import * as SourcesAPI from './sources'; import * as UploadsAPI from './uploads'; import * as JobsAPI from './jobs/jobs'; import { Webhook as Webhook_ } from 'standardwebhooks'; import { APIPromise } from '../core/api-promise'; import { buildHeaders } from '../internal/headers'; import { RequestOptions } from '../internal/request-options'; import { path } from '../internal/utils/path'; export class Webhooks extends APIResource { /** * Create a new webhook for a project. The webhook will receive notifications for * specified events. * * @example * ```ts * const webhook = await client.webhooks.create({ * url: 'https://example.com/webhook', * }); * ``` */ create(body: WebhookCreateParams, options?: RequestOptions): APIPromise { return ( this._client.post('/api/webhooks', { body, ...options, __security: { projectAccessTokenAuth: true }, }) as APIPromise<{ data: Webhook }> )._thenUnwrap((obj) => obj.data); } /** * Retrieve details of a specific webhook configuration by its ID. The webhook must * belong to the current project. * * @example * ```ts * const webhook = await client.webhooks.retrieve('webhookId'); * ``` */ retrieve(webhookID: string, options?: RequestOptions): APIPromise { return ( this._client.get(path`/api/webhooks/${webhookID}`, { ...options, __security: { projectAccessTokenAuth: true }, }) as APIPromise<{ data: Webhook }> )._thenUnwrap((obj) => obj.data); } /** * Update the enabled status of a webhook. The webhook must belong to the current * project. * * @example * ```ts * await client.webhooks.update('webhookId'); * ``` */ update(webhookID: string, body: WebhookUpdateParams, options?: RequestOptions): APIPromise { return this._client.patch(path`/api/webhooks/${webhookID}`, { body, ...options, headers: buildHeaders([{ Accept: '*/*' }, options?.headers]), __security: { projectAccessTokenAuth: true }, }); } /** * Retrieve a list of all webhooks configured for the current project. Each webhook * includes its URL, enabled status, and subscribed events. * * @example * ```ts * const webhooks = await client.webhooks.list(); * ``` */ list(options?: RequestOptions): APIPromise { return this._client.get('/api/webhooks', { ...options, __security: { projectAccessTokenAuth: true } }); } /** * Permanently delete a webhook configuration. The webhook must belong to the * current project. This action cannot be undone. * * @example * ```ts * await client.webhooks.delete('webhookId'); * ``` */ delete(webhookID: string, options?: RequestOptions): APIPromise { return this._client.delete(path`/api/webhooks/${webhookID}`, { ...options, headers: buildHeaders([{ Accept: '*/*' }, options?.headers]), __security: { projectAccessTokenAuth: true }, }); } unwrap( body: string, { headers, key }: { headers: Record; key?: string }, ): UnwrapWebhookEvent { if (headers !== undefined) { const keyStr: string | null = key === undefined ? this._client.webhookKey : key; if (keyStr === null) throw new Error('Webhook key must not be null in order to unwrap'); const wh = new Webhook_(keyStr); wh.verify(body, headers); } return JSON.parse(body) as UnwrapWebhookEvent; } } export interface Webhook { /** * Unique identifier of the webhook */ id: string; /** * Whether the webhook is currently enabled */ enabled: boolean; /** * Array of event types this webhook subscribes to */ events: Array< 'job.completed' | 'job.failed' | 'job.cancelled' | 'upload.completed' | 'upload.failed' | 'upload.expired' >; /** * ID of the project this webhook belongs to */ project_id: string; /** * URL where webhook events will be sent */ url: string; } /** * Response containing the list of all webhooks for a project */ export interface WebhookListResponse { /** * Data contains the webhook items */ data: Array; /** * Status indicates the response status "success" */ status: 'success'; } export interface NewEventWebhookEvent { /** * Unique identifier of the notification */ id: string; /** * Event-specific payload data */ data: | NewEventWebhookEvent.NotificationPayloadJobCompleted | NewEventWebhookEvent.NotificationPayloadJobFailed | NewEventWebhookEvent.NotificationPayloadUploadCompleted | NewEventWebhookEvent.NotificationPayloadUploadFailed; /** * Timestamp when the notification was sent */ date: string; /** * Type of event that triggered the notification. */ event: | 'job.completed' | 'job.failed' | 'job.cancelled' | 'upload.completed' | 'upload.failed' | 'upload.expired'; } export namespace NewEventWebhookEvent { /** * Payload data structure for job.completed events */ export interface NotificationPayloadJobCompleted { /** * List of files generated by the job */ files: Array; job: JobsAPI.Job; } /** * Payload data structure for job.failed and job.cancelled events */ export interface NotificationPayloadJobFailed { job: JobsAPI.Job; } /** * Payload data structure for upload.completed events */ export interface NotificationPayloadUploadCompleted { source: SourcesAPI.Source; upload: UploadsAPI.Upload; } /** * Payload data structure for upload.failed and upload.expired events */ export interface NotificationPayloadUploadFailed { upload: UploadsAPI.Upload; } } export interface UnwrapWebhookEvent { /** * Unique identifier of the notification */ id: string; /** * Event-specific payload data */ data: | UnwrapWebhookEvent.NotificationPayloadJobCompleted | UnwrapWebhookEvent.NotificationPayloadJobFailed | UnwrapWebhookEvent.NotificationPayloadUploadCompleted | UnwrapWebhookEvent.NotificationPayloadUploadFailed; /** * Timestamp when the notification was sent */ date: string; /** * Type of event that triggered the notification. */ event: | 'job.completed' | 'job.failed' | 'job.cancelled' | 'upload.completed' | 'upload.failed' | 'upload.expired'; } export namespace UnwrapWebhookEvent { /** * Payload data structure for job.completed events */ export interface NotificationPayloadJobCompleted { /** * List of files generated by the job */ files: Array; job: JobsAPI.Job; } /** * Payload data structure for job.failed and job.cancelled events */ export interface NotificationPayloadJobFailed { job: JobsAPI.Job; } /** * Payload data structure for upload.completed events */ export interface NotificationPayloadUploadCompleted { source: SourcesAPI.Source; upload: UploadsAPI.Upload; } /** * Payload data structure for upload.failed and upload.expired events */ export interface NotificationPayloadUploadFailed { upload: UploadsAPI.Upload; } } export interface WebhookCreateParams { /** * Url is the endpoint that will receive webhook notifications, which must be a * valid HTTP URL. */ url: string; /** * Enabled indicates whether the webhook is active. */ enabled?: boolean; /** * Events specifies the types of events that will trigger the webhook. */ events?: Array< 'job.completed' | 'job.failed' | 'job.cancelled' | 'upload.completed' | 'upload.failed' | 'upload.expired' >; } export interface WebhookUpdateParams { /** * Enabled indicates whether the webhook should be enabled or disabled. */ enabled?: boolean; /** * Events specifies the types of events that will trigger the webhook. */ events?: Array< 'job.completed' | 'job.failed' | 'job.cancelled' | 'upload.completed' | 'upload.failed' | 'upload.expired' >; } export declare namespace Webhooks { export { type Webhook as Webhook, type WebhookListResponse as WebhookListResponse, type NewEventWebhookEvent as NewEventWebhookEvent, type UnwrapWebhookEvent as UnwrapWebhookEvent, type WebhookCreateParams as WebhookCreateParams, type WebhookUpdateParams as WebhookUpdateParams, }; }