1import type * as Effect from "effect/Effect";
2import type * as Stream from "effect/Stream";
3import {
4 consumeBusEvents,
5 type EventRecord,
6 type EventRouteProps,
7} from "../EventBridge/EventSource.ts";
8
9/**
10 * The `detail` payload Amazon QuickSight delivers to EventBridge when an
11 * asset (dashboard, analysis, dataset, data source, template, theme, folder)
12 * is created, updated, or deleted. Detail-types are strings like
13 * `QuickSight Dashboard Creation Successful` or
14 * `QuickSight Dataset Update Failed`. Which fields are present depends on
15 * the asset kind, so everything is optional.
16 */
17export interface QuickSightAssetEventDetail {
18 /** The account the asset lives in. */
19 awsAccountId?: string;
20 /** Id of the affected asset (e.g. the dashboard or dataset id). */
21 resourceId?: string;
22 /** ARN of the affected asset. */
23 arn?: string;
24 /** Display name of the affected asset. */
25 name?: string;
26 /** Version number for versioned assets (dashboards, templates, themes). */
27 versionNumber?: number;
28 /** Additional event fields (the schema grows over time). */
29 [key: string]: unknown;
30}
31
32/** A QuickSight asset EventBridge event delivered to the handler. */
33export type QuickSightAssetEvent = EventRecord<QuickSightAssetEventDetail>;
34
35export interface AssetEventSourceProps extends EventRouteProps {
36 /**
37 * Logical id for the backing EventBridge rule.
38 * @default "QuickSightAssetEvents"
39 */
40 id?: string;
41 /**
42 * Only deliver events with these `detail-type` values (e.g.
43 * `["QuickSight Dashboard Creation Successful"]`).
44 * @default all asset CUD events (CloudTrail service events excluded)
45 */
46 detailTypes?: readonly string[];
47}
48
49/**
50 * Event source connecting Amazon QuickSight asset events to the hosting
51 * compute. QuickSight (Enterprise Edition) publishes create/update/delete
52 * outcomes for dashboards, analyses, datasets, data sources, templates,
53 * themes, and folders to the account's default EventBridge bus (source
54 * `aws.quicksight`, detail-types like `QuickSight Dashboard Creation
55 * Successful`); this subscribes the host Function to those events so it can
56 * drive continuous-deployment, replication, or backup automation.
57 *
58 * QuickSight publishes to EventBridge automatically — no additional resource
59 * is created besides the EventBridge rule targeting the host. When
60 * `detailTypes` is omitted, the rule excludes `AWS Service Event via
61 * CloudTrail` so only the native asset events are delivered. Provide the
62 * host-specific implementation layer (e.g. `AWS.Lambda.EventSource`) on the
63 * Function effect.
64 *
65 * ### Consuming Asset Events
66 * **Example:** React To Dashboard Publishes
67 * ```typescript
68 * import * as AWS from "alchemy/AWS";
69 *
70 * export default DashboardReactor.make(
71 * { main: import.meta.url },
72 * Effect.gen(function* () {
73 * yield* AWS.QuickSight.consumeAssetEvents(
74 * {
75 * detailTypes: [
76 * "QuickSight Dashboard Creation Successful",
77 * "QuickSight Dashboard Update Successful",
78 * ],
79 * },
80 * (events) =>
81 * Stream.runForEach(events, (event) =>
82 * Effect.log(`dashboard ${event.detail.resourceId} published`),
83 * ),
84 * );
85 * return {};
86 * }).pipe(Effect.provide(AWS.Lambda.EventSource)),
87 * );
88 * ```
89 */
90export const consumeAssetEvents = <StreamReq = never, Req = never>(
91 props: AssetEventSourceProps,
92 process: (
93 events: Stream.Stream<QuickSightAssetEvent, never, StreamReq>,
94 ) => Effect.Effect<void, never, Req>,
95) =>
96 consumeBusEvents(
97 props.id ?? "QuickSightAssetEvents",
98 {
99 source: ["aws.quicksight"],
100 "detail-type": props.detailTypes
101 ? [...props.detailTypes]
102 : [{ "anything-but": ["AWS Service Event via CloudTrail"] }],
103 },
104 { description: props.description, state: props.state },
105 process,
106 );