Skip to main content

@rtorcato/cf-common

Interfaces​

QueueProducer​

Defined in: index.ts:7

A typed producer bound to one Queue binding.

Type Parameters​

T​

T

Methods​

send()​

send(body, options?): Promise<void>

Defined in: index.ts:9

Enqueue one message. Throws QUEUE_SEND_FAILED if the binding rejects it.

Parameters​
body​

T

options?​

QueueSendOptions

Returns​

Promise<void>

sendBatch()​

sendBatch(bodies, options?): Promise<void>

Defined in: index.ts:15

Enqueue many messages, split into calls of at most 100 (Cloudflare's limit). Chunks are sent in order and sending stops at the first failure, so earlier chunks stay enqueued. Throws QUEUE_SEND_FAILED.

Parameters​
bodies​

Iterable<T>

options?​

QueueSendBatchOptions

Returns​

Promise<void>


QueueOptions​

Defined in: index.ts:58

Properties​

retryDelay?​

optional retryDelay?: number | ((attempts) => number)

Defined in: index.ts:64

Seconds to wait before a failed message is redelivered. A function gets the message's attempts (1 on the first delivery) so you can back off. Defaults to Cloudflare's immediate redelivery.

Type Aliases​

QueueHandler​

QueueHandler<T, Env> = (body, message, env, ctx) => void | Promise<void>

Defined in: index.ts:68

Handles one message body. Throw to retry that message.

Type Parameters​

T​

T

Env​

Env

Parameters​

body​

T

message​

Message<T>

env​

Env

ctx​

ExecutionContext

Returns​

void | Promise<void>

Functions​

createProducer()​

createProducer<T>(queue): QueueProducer<T>

Defined in: index.ts:27

Wrap a Queue producer binding so send and sendBatch take plain bodies and failures surface as a CloudflareError.

Example​

const jobs = createProducer<EmailJob>(env.EMAILS)
await jobs.send({ to: 'ada@example.com' }, { delaySeconds: 30 })
await jobs.sendBatch(recipients.map((to) => ({ to })))

Type Parameters​

T​

T = unknown

Parameters​

queue​

Queue<T>

Returns​

QueueProducer<T>


defineQueue()​

defineQueue<T, Env>(handler, options?): (batch, env, ctx) => Promise<void>

Defined in: index.ts:89

Build a queue handler that runs handler once per message and settles each one on its own: ack() when it returns, retry() when it throws. One bad message no longer retries the whole batch. Messages run concurrently, and once a message exhausts max_retries Cloudflare moves it to the dead-letter queue, if you configured one.

Example​

export default {
queue: defineQueue<EmailJob, Env>(async (job, _msg, env) => sendEmail(env, job), {
retryDelay: (attempts) => 2 ** attempts * 10,
}),
}

Type Parameters​

T​

T = unknown

Env​

Env = unknown

Parameters​

handler​

QueueHandler<T, Env>

options?​

QueueOptions = {}

Returns​

(batch, env, ctx) => Promise<void>