The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Binding from "alchemy/Binding"; |
| 3 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type { Queue } from "./Queue.ts"; |
| 6 | import 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 | */ |
| 49 | export interface WriteQueue extends Binding.Service< |
| 50 | WriteQueue, |
| 51 | "Cloudflare.Queues.WriteQueue", |
| 52 | (queue: Queue) => Effect.Effect<WriteQueueClient> |
| 53 | > {} |
| 54 | |
| 55 | export const WriteQueue = Binding.Service<WriteQueue>("Cloudflare.Queues.WriteQueue"); |
| 56 | |
| 57 | export interface WriteQueueClient { |
| 58 | raw: Effect.Effect<runtime.Queue<unknown>, never, RuntimeContext>; |
| 59 | send(body: unknown, options?: SendOptions): Effect.Effect<void, SendError, RuntimeContext>; |
| 60 | sendBatch(messages: ReadonlyArray<SendMessage>): Effect.Effect<void, SendError, RuntimeContext>; |
| 61 | } |