| 1 | import * as eventbridge from "@distilled.cloud/aws/eventbridge"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Binding from "../../Binding.ts"; |
| 4 | import type { EventBus } from "./EventBus.ts"; |
| 5 | |
| 6 | export interface PutEventsRequest extends Omit< |
| 7 | eventbridge.PutEventsRequest, |
| 8 | "Entries" |
| 9 | > { |
| 10 | Entries: Array<Omit<eventbridge.PutEventsRequestEntry, "EventBusName">>; |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Publishes events to an EventBridge event bus (`events:PutEvents`). |
| 15 | * |
| 16 | * Bind this operation to an {@link EventBus} inside a function runtime to get |
| 17 | * a callable that automatically injects the bus name into every entry. Omit |
| 18 | * the bus argument to publish to the account's default event bus. Provide the |
| 19 | * `PutEventsHttp` layer on the Function to satisfy the binding. |
| 20 | * ### Publishing Events |
| 21 | * **Example:** Publish an Event from a Handler |
| 22 | * ```typescript |
| 23 | * // init — bind the bus (provide AWS.EventBridge.PutEventsHttp on the Function) |
| 24 | * const putEvents = yield* AWS.EventBridge.PutEvents(bus); |
| 25 | * |
| 26 | * return { |
| 27 | * fetch: Effect.gen(function* () { |
| 28 | * // runtime — publish an event |
| 29 | * const result = yield* putEvents({ |
| 30 | * Entries: [ |
| 31 | * { |
| 32 | * Source: "my.app", |
| 33 | * DetailType: "OrderCreated", |
| 34 | * Detail: JSON.stringify({ orderId: "123" }), |
| 35 | * }, |
| 36 | * ], |
| 37 | * }); |
| 38 | * return HttpServerResponse.json({ |
| 39 | * failedEntryCount: result.FailedEntryCount ?? 0, |
| 40 | * }); |
| 41 | * }), |
| 42 | * }; |
| 43 | * ``` |
| 44 | * |
| 45 | * **Example:** Publish to the Default Event Bus |
| 46 | * ```typescript |
| 47 | * // omit the bus argument to target the account's default bus |
| 48 | * const putEvents = yield* AWS.EventBridge.PutEvents(); |
| 49 | * |
| 50 | * yield* putEvents({ |
| 51 | * Entries: [ |
| 52 | * { |
| 53 | * Source: "my.app", |
| 54 | * DetailType: "Heartbeat", |
| 55 | * Detail: JSON.stringify({ at: new Date().toISOString() }), |
| 56 | * }, |
| 57 | * ], |
| 58 | * }); |
| 59 | * ``` |
| 60 | * |
| 61 | * @binding |
| 62 | */ |
| 63 | export interface PutEvents extends Binding.Service< |
| 64 | PutEvents, |
| 65 | "AWS.EventBridge.PutEvents", |
| 66 | ( |
| 67 | bus?: EventBus, |
| 68 | ) => Effect.Effect< |
| 69 | ( |
| 70 | request: PutEventsRequest, |
| 71 | ) => Effect.Effect< |
| 72 | eventbridge.PutEventsResponse, |
| 73 | eventbridge.PutEventsError |
| 74 | > |
| 75 | > |
| 76 | > {} |
| 77 | export const PutEvents = Binding.Service<PutEvents>( |
| 78 | "AWS.EventBridge.PutEvents", |
| 79 | ); |