1import type * as runtime from "@cloudflare/workers-types";
2import * as Effect from "effect/Effect";
3import * as Binding from "../../Binding.ts";
4import type { RuntimeContext } from "../../RuntimeContext.ts";
5import type { Queue } from "./Queue.ts";
6import type { SendError, SendMessage, SendOptions } from "./QueueTypes.ts";
7
8/**
9 * Binding service that turns a {@link Queue} resource into a typed
10 * {@link WriteQueueClient} you can call from a Worker's runtime Effect.
11 *
12 * The Cloudflare Worker queue binding is producer-only — `send` for a
13 * single message and `sendBatch` for many in one call. Messages can be
14 * any JSON-serializable value.
15 * ### Sending Messages
16 * **Example:** Producer route
17 * ```typescript
18 * const queue = yield* Cloudflare.Queues.WriteQueue(Queue);
19 *
20 * return {
21 * fetch: Effect.gen(function* () {
22 * yield* queue.send({ text: "hi", sentAt: Date.now() });
23 * return HttpServerResponse.empty({ status: 202 });
24 * }),
25 * };
26 * ```
27 *
28 * **Example:** Sending a batch
29 * ```typescript
30 * yield* queue.sendBatch([
31 * { body: { event: "click", id: 1 } },
32 * { body: { event: "click", id: 2 } },
33 * { body: "raw text", contentType: "text" },
34 * ]);
35 * ```
36 *
37 * Provide {@link WriteQueueBinding} (native Worker binding) or
38 * {@link WriteQueueHttp} (scoped HTTP token) in the worker's runtime
39 * layer to resolve the underlying queue at request time.
40 *
41 * `WriteQueue` is a single identifier that is simultaneously the binding's
42 * Context tag, its type, and the callable —
43 * `yield* Cloudflare.Queues.WriteQueue(queue)`.
44 *
45 * @binding
46 * @product Queues
47 * @category Storage & Databases
48 */
49export interface WriteQueue extends Binding.Service<
50 WriteQueue,
51 "Cloudflare.Queues.WriteQueue",
52 (queue: Queue) => Effect.Effect<WriteQueueClient>
53> {}
54
55export const WriteQueue = Binding.Service<WriteQueue>(
56 "Cloudflare.Queues.WriteQueue",
57);
58
59export interface WriteQueueClient {
60 raw: Effect.Effect<runtime.Queue<unknown>, never, RuntimeContext>;
61 send(
62 body: unknown,
63 options?: SendOptions,
64 ): Effect.Effect<void, SendError, RuntimeContext>;
65 sendBatch(
66 messages: ReadonlyArray<SendMessage>,
67 ): Effect.Effect<void, SendError, RuntimeContext>;
68}