import type { Context } from "../context.ts"; import { type CloudflareApi, type CloudflareApiOptions } from "./api.ts"; import { type R2Bucket } from "./bucket.ts"; import { type Queue } from "./queue.ts"; /** * Event types that can trigger R2 bucket notifications */ export type R2BucketNotificationEventType = "object-create" | "object-delete"; /** * Message payload sent to a Queue when an R2 bucket notification is triggered * * @see https://developers.cloudflare.com/r2/buckets/event-notifications/#message-format */ export interface R2BucketNotificationMessage { /** * The Cloudflare account ID that the event is associated with */ account: string; /** * The type of action that triggered the event notification * Example actions include: PutObject, CopyObject, CompleteMultipartUpload, DeleteObject */ action: string; /** * The name of the bucket where the event occurred */ bucket: string; /** * Details about the object involved in the event */ object: { /** * The key (or name) of the object within the bucket */ key: string; /** * The size of the object in bytes (not present for object-delete events) */ size?: number; /** * The entity tag (eTag) of the object (not present for object-delete events) */ eTag?: string; }; /** * The time when the action that triggered the event occurred (ISO 8601 format) */ eventTime: string; /** * Details about the source of a copied object (only present for CopyObject events) */ copySource?: { /** * The bucket that contained the source object */ bucket: string; /** * The name of the source object */ object: string; }; } /** * Base properties shared by all R2 Bucket Notification configurations */ interface R2BucketNotificationBaseProps extends CloudflareApiOptions { /** * The R2 bucket to attach the notification rule to. * Can be either a bucket name (string) or an R2Bucket resource. */ bucket: string | R2Bucket; /** * The queue that will receive notification messages. * Can be either a queue name (string) or a Queue resource. */ queue: string | Queue; /** * The type of events that will trigger notifications. * - "object-create": Triggered when objects are created or overwritten (PutObject, CompleteMultipartUpload) * - "object-delete": Triggered when objects are deleted (DeleteObject, CopyObject, LifecycleDeletion) */ eventTypes: R2BucketNotificationEventType[]; /** * Optional description for the notification rule to help identify it. */ description?: string; /** * The jurisdiction where the bucket exists. * Required for EU or FedRAMP jurisdictions. */ jurisdiction?: "default" | "eu" | "fedramp"; /** * Whether to delete the notification rule when the resource is removed from Alchemy. * @default true */ delete?: boolean; /** * Whether to adopt an existing notification rule if one already exists with the same configuration. * @default false */ adopt?: boolean; } /** * Props with single prefix and suffix (creates one rule) */ interface R2BucketNotificationSingleProps extends R2BucketNotificationBaseProps { /** * Optional prefix filter - only objects with keys starting with this prefix will trigger notifications. */ prefix?: string; /** * Optional suffix filter - only objects with keys ending with this suffix will trigger notifications. */ suffix?: string; } /** * Props with multiple prefixes (creates one rule per prefix) */ interface R2BucketNotificationMultiplePrefixProps extends R2BucketNotificationBaseProps { /** * Array of prefix filters - creates one notification rule per prefix. */ prefix: string[]; /** * Optional suffix filter applied to all rules. */ suffix?: string; } /** * Props with multiple suffixes (creates one rule per suffix) */ interface R2BucketNotificationMultipleSuffixProps extends R2BucketNotificationBaseProps { /** * Optional prefix filter applied to all rules. */ prefix?: string; /** * Array of suffix filters - creates one notification rule per suffix. */ suffix: string[]; } /** * Properties for creating or updating R2 Bucket Notification rules. * * Either prefix OR suffix can be an array (creating multiple rules), but not both. */ export type R2BucketNotificationProps = R2BucketNotificationSingleProps | R2BucketNotificationMultiplePrefixProps | R2BucketNotificationMultipleSuffixProps; /** * Output returned after R2 Bucket Notification rule creation/update */ export type R2BucketNotification = Omit & { /** * Resource type identifier */ type: "r2_bucket_notification"; /** * The Cloudflare-assigned rule ID(s) for this notification. * When prefix or suffix is an array, this contains multiple rule IDs. */ ruleId: string | string[]; /** * The name of the bucket this notification is attached to */ bucketName: string; /** * The name of the queue that receives notification messages */ queueName: string; /** * Prefix filter(s) - normalized to always be a string or array matching input */ prefix?: string | string[]; /** * Suffix filter(s) - normalized to always be a string or array matching input */ suffix?: string | string[]; /** * Time when the notification rule was created */ createdAt?: string; }; /** * Type guard to check if a resource is an R2BucketNotification */ export declare function isR2BucketNotification(resource: any): resource is R2BucketNotification; /** * Creates an event notification rule for an R2 bucket that sends messages to a Queue. * * Event notifications allow you to trigger automated workflows when objects are created, * modified, or deleted in your R2 bucket. Messages are delivered to a Cloudflare Queue * where they can be processed by a consumer Worker. * * @example * ## Basic object-create notification * * Send notifications to a queue when objects are created in a bucket: * * ```ts * import { R2Bucket, Queue, R2BucketNotification } from "alchemy/cloudflare"; * * const bucket = await R2Bucket("uploads"); * const queue = await Queue("upload-events"); * * await R2BucketNotification("upload-notifications", { * bucket, * queue, * eventTypes: ["object-create"], * }); * ``` * * @example * ## Filtered notifications with prefix and suffix * * Only trigger notifications for specific object patterns: * * ```ts * import { R2Bucket, Queue, R2BucketNotification } from "alchemy/cloudflare"; * * const bucket = await R2Bucket("documents"); * const queue = await Queue("pdf-processing"); * * await R2BucketNotification("pdf-uploads", { * bucket, * queue, * eventTypes: ["object-create"], * prefix: "incoming/", * suffix: ".pdf", * description: "Process newly uploaded PDF files", * }); * ``` * * @example * ## Multiple suffixes for different file types * * Create rules for multiple file extensions at once: * * ```ts * import { R2Bucket, Queue, R2BucketNotification } from "alchemy/cloudflare"; * * const bucket = await R2Bucket("media"); * const queue = await Queue("audio-processing"); * * await R2BucketNotification("audio-uploads", { * bucket, * queue, * eventTypes: ["object-create"], * prefix: "audio/", * suffix: [".mp3", ".wav", ".flac"], * }); * ``` * * @example * ## Multiple event types with typed queue * * Listen for both create and delete events with type-safe message handling: * * ```ts * import { R2Bucket, Queue, R2BucketNotification, R2BucketNotificationMessage } from "alchemy/cloudflare"; * * const bucket = await R2Bucket("assets"); * const queue = await Queue("asset-events"); * * await R2BucketNotification("asset-notifications", { * bucket, * queue, * eventTypes: ["object-create", "object-delete"], * }); * ``` * * @example * ## Process notifications with a Worker * * Complete example showing bucket notifications processed by a Worker: * * ```ts * import { R2Bucket, Queue, R2BucketNotification, Worker, R2BucketNotificationMessage } from "alchemy/cloudflare"; * * const bucket = await R2Bucket("uploads"); * const queue = await Queue("upload-events"); * * await R2BucketNotification("upload-notifications", { * bucket, * queue, * eventTypes: ["object-create"], * }); * * await Worker("processor", { * entrypoint: "./src/processor.ts", * eventSources: [queue], * }); * ``` * * @see https://developers.cloudflare.com/r2/buckets/event-notifications/ */ export declare const R2BucketNotification: (((this: any, id: string, props?: {}) => never) & (new (_: never) => never)) | ((this: Context, _id: string, props: R2BucketNotificationProps) => Promise); interface NotificationRuleInfo { ruleId: string; queueId: string; queueName: string; actions: string[]; prefix: string; suffix: string; createdAt?: string; } /** * List all notification rules for an R2 bucket */ export declare function listR2BucketNotifications(api: CloudflareApi, bucketName: string, options?: { jurisdiction?: string; }): Promise; export {}; //# sourceMappingURL=r2-bucket-notification.d.ts.map