1import type * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
2import type * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4import type { RuntimeContext } from "../../RuntimeContext.ts";
5import type { Topic } from "./Topic.ts";
6
7/** A message to publish. `data` is base64-encoded for you. */
8export interface PublishMessage {
9 /** Message body; strings are UTF-8 encoded. */
10 data: string | Uint8Array;
11 /** Optional message attributes. */
12 attributes?: Record<string, string>;
13 /** Ordering key; requires message ordering on the subscription. */
14 orderingKey?: string;
15}
16
17/** Publish-only client for one Pub/Sub topic. */
18export interface WriteTopicClient {
19 /** Publish one message and return its server-assigned message id. */
20 publish(
21 message: PublishMessage,
22 ): Effect.Effect<string, pubsub.PublishProjectsTopicsError, RuntimeContext>;
23 /** Publish messages in one request; ids are returned in input order. */
24 publishBatch(
25 messages: ReadonlyArray<PublishMessage>,
26 ): Effect.Effect<string[], pubsub.PublishProjectsTopicsError, RuntimeContext>;
27}
28
29/**
30 * Publish access to a Pub/Sub {@link Topic}: `publish`, `publishBatch`.
31 * Grants `roles/pubsub.publisher` on the topic only. Topics have no runtime
32 * read — consume through a subscription with `ReadSubscription`.
33 *
34 * ### Publishing messages
35 * **Example:** Publish one message
36 * ```typescript
37 * const events = yield* GCP.PubSub.WriteTopic(topic);
38 * const messageId = yield* events.publish({
39 * data: JSON.stringify({ type: "signup" }),
40 * attributes: { source: "api" },
41 * });
42 * // …provided with Effect.provide(GCP.PubSub.WriteTopicHttp)
43 * ```
44 *
45 * **Example:** Publish a batch
46 * ```typescript
47 * const events = yield* GCP.PubSub.WriteTopic(topic);
48 * const ids = yield* events.publishBatch([{ data: "a" }, { data: "b" }]);
49 * ```
50 *
51 * @binding
52 * @category PubSub
53 */
54export interface WriteTopic extends Binding.Service<
55 WriteTopic,
56 "GCP.PubSub.WriteTopic",
57 (topic: Topic) => Effect.Effect<WriteTopicClient>
58> {}
59
60export const WriteTopic = Binding.Service<WriteTopic>("GCP.PubSub.WriteTopic");