@vercel/queue 0.0.0-alpha.8 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,37 +1,83 @@
1
1
  /**
2
- * Serializer/Deserializer interface for message payloads
2
+ * Serializer/Deserializer interface for message payloads.
3
+ *
4
+ * Built-in implementations:
5
+ * - `JsonTransport` - JSON serialization (default, buffers entire payload)
6
+ * - `BufferTransport` - Binary data (buffers entire payload)
7
+ * - `StreamTransport` - Pass-through streaming (no buffering, ideal for large payloads)
3
8
  */
4
9
  interface Transport<T = unknown> {
5
10
  /**
6
- * Serialize a value to a buffer or stream for transmission
11
+ * Serialize a value to a buffer or stream for transmission.
12
+ * The result is sent as the request body with the `contentType` header.
7
13
  */
8
14
  serialize(value: T): Buffer | ReadableStream<Uint8Array>;
9
15
  /**
10
- * Deserialize a readable stream back to the original value
16
+ * Deserialize a readable stream back to the original value.
17
+ * Called when receiving messages from the queue.
11
18
  */
12
19
  deserialize(stream: ReadableStream<Uint8Array>): Promise<T>;
13
20
  /**
14
- * Optional cleanup method for deserialized payloads that may contain resources
15
- * Should be called when the payload is no longer needed, especially in error cases
21
+ * Optional cleanup method for deserialized payloads that may contain resources.
22
+ * Called automatically by ConsumerGroup on error; manual cleanup required for direct client usage.
16
23
  * @param payload The deserialized payload to clean up
17
24
  */
18
25
  finalize?(payload: T): Promise<void>;
19
26
  /**
20
- * MIME type for this serialization format
27
+ * MIME type for this serialization format.
28
+ * Sent as the Content-Type header when publishing messages.
21
29
  */
22
30
  contentType: string;
23
31
  }
24
32
  /**
25
- * Built-in JSON serializer/deserializer
26
- * This implementation reads the entire stream into memory for JSON parsing
33
+ * JSON serializer/deserializer (default transport).
34
+ *
35
+ * Ideal for structured data. Buffers the entire payload in memory for parsing.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // Default (JsonTransport is used automatically)
40
+ * const queue = new QueueClient({ region: process.env.QUEUE_REGION! });
41
+ * await queue.send("topic", { data: "example" });
42
+ *
43
+ * // With custom serialization
44
+ * const queue = new QueueClient({
45
+ * region: process.env.QUEUE_REGION!,
46
+ * transport: new JsonTransport({
47
+ * replacer: (key, value) => key === "password" ? undefined : value,
48
+ * reviver: (key, value) => key === "date" ? new Date(value) : value,
49
+ * }),
50
+ * });
51
+ * await queue.send("topic", { data: "example" });
52
+ * ```
27
53
  */
28
54
  declare class JsonTransport<T = unknown> implements Transport<T> {
29
55
  readonly contentType = "application/json";
56
+ readonly replacer?: Parameters<typeof JSON.stringify>[1];
57
+ readonly reviver?: Parameters<typeof JSON.parse>[1];
58
+ /**
59
+ * Create a new JsonTransport.
60
+ * @param options - Optional JSON serialization options
61
+ * @param options.replacer - Custom replacer for JSON.stringify
62
+ * @param options.reviver - Custom reviver for JSON.parse
63
+ */
64
+ constructor(options?: {
65
+ replacer?: Parameters<typeof JSON.stringify>[1];
66
+ reviver?: Parameters<typeof JSON.parse>[1];
67
+ });
30
68
  serialize(value: T): Buffer;
31
69
  deserialize(stream: ReadableStream<Uint8Array>): Promise<T>;
32
70
  }
33
71
  /**
34
- * Built-in Buffer serializer/deserializer (reads entire stream into a Buffer)
72
+ * Binary data serializer/deserializer.
73
+ *
74
+ * Ideal for binary data that fits in memory. Buffers the entire payload.
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const queue = new QueueClient({ region: "iad1", transport: new BufferTransport() });
79
+ * await queue.send("binary-topic", myBuffer);
80
+ * ```
35
81
  */
36
82
  declare class BufferTransport implements Transport<Buffer> {
37
83
  readonly contentType = "application/octet-stream";
@@ -39,31 +85,36 @@ declare class BufferTransport implements Transport<Buffer> {
39
85
  deserialize(stream: ReadableStream<Uint8Array>): Promise<Buffer>;
40
86
  }
41
87
  /**
42
- * Stream serializer/deserializer (pass-through for streaming binary data)
43
- * This is ideal for large payloads that don't need to be buffered in memory
88
+ * Pass-through stream serializer/deserializer.
89
+ *
90
+ * Ideal for large files and real-time streams. Does not buffer data in memory.
44
91
  *
45
- * IMPORTANT: When using StreamTransport, you must call finalize(payload) when done
46
- * processing the message to prevent resource leaks, especially in error cases.
47
- * ConsumerGroup handles this automatically, but direct QueueClient usage requires manual cleanup.
92
+ * **Important:** When using StreamTransport, you must call `finalize(payload)` when done
93
+ * processing to prevent resource leaks. ConsumerGroup handles this automatically;
94
+ * direct client usage requires manual cleanup.
48
95
  *
49
- * Example usage:
96
+ * @example
50
97
  * ```typescript
51
- * const transport = new StreamTransport();
52
- * try {
53
- * for await (const message of client.receiveMessages(options, transport)) {
54
- * // Process the stream...
55
- * const reader = message.payload.getReader();
56
- * // ... handle stream data
57
- * }
58
- * } catch (error) {
59
- * // Cleanup is handled automatically by ConsumerGroup
60
- * // or manually: await transport.finalize(message.payload);
61
- * }
98
+ * const queue = new QueueClient({ region: "iad1", transport: new StreamTransport() });
99
+ *
100
+ * // Sending a stream
101
+ * await queue.send("large-file", myReadableStream);
102
+ *
103
+ * // Receiving - cleanup handled automatically
104
+ * await queue.receive("large-file", "processor", async (stream, meta) => {
105
+ * const reader = stream.getReader();
106
+ * // Process chunks...
107
+ * });
108
+ * ```
62
109
  */
63
110
  declare class StreamTransport implements Transport<ReadableStream<Uint8Array>> {
64
111
  readonly contentType = "application/octet-stream";
65
112
  serialize(value: ReadableStream<Uint8Array>): ReadableStream<Uint8Array>;
66
113
  deserialize(stream: ReadableStream<Uint8Array>): Promise<ReadableStream<Uint8Array>>;
114
+ /**
115
+ * Consume any remaining stream data to prevent resource leaks.
116
+ * Called automatically by ConsumerGroup; manual call required for direct client usage.
117
+ */
67
118
  finalize(payload: ReadableStream<Uint8Array>): Promise<void>;
68
119
  }
69
120
 
@@ -72,120 +123,271 @@ declare class StreamTransport implements Transport<ReadableStream<Uint8Array>> {
72
123
  */
73
124
 
74
125
  /**
75
- * Shared options for publishing messages
126
+ * Vercel region code. The 20 known codes match
127
+ * {@link https://dcs.vercel-infra.com/ | dcs.vercel-infra.com}.
128
+ * Arbitrary strings are also accepted for forward compatibility
129
+ * with regions added after this SDK version.
76
130
  */
77
- interface PublishOptions {
131
+ type VercelRegion = "arn1" | "bom1" | "cdg1" | "cle1" | "cpt1" | "dub1" | "dxb1" | "fra1" | "gru1" | "hkg1" | "hnd1" | "iad1" | "icn1" | "kix1" | "lhr1" | "pdx1" | "sfo1" | "sin1" | "syd1" | "yul1" | (string & {});
132
+ /**
133
+ * Resolves a region code to a base URL for the Vercel Queue Service API.
134
+ *
135
+ * Default: `` (region) => `https://${region}.vercel-queue.com` ``
136
+ */
137
+ type BaseUrlResolver = (region: string) => string;
138
+ interface QueueClientOptions {
139
+ /**
140
+ * Vercel region code for API routing.
141
+ *
142
+ * Requests are sent to the regional endpoint resolved by
143
+ * {@link BaseUrlResolver} (default: `https://${region}.vercel-queue.com`).
144
+ *
145
+ * Use a `QUEUE_REGION` env var set to `${VERCEL_REGION}` in production
146
+ * and a fixed region (e.g. `"iad1"`) in development.
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * new QueueClient({ region: process.env.QUEUE_REGION! })
151
+ * ```
152
+ */
153
+ region: VercelRegion;
154
+ /**
155
+ * Custom resolver that maps a region code to a base URL.
156
+ * @default (region) => `https://${region}.vercel-queue.com`
157
+ */
158
+ resolveBaseUrl?: BaseUrlResolver;
78
159
  /**
79
- * Unique key to prevent duplicate message submissions
80
- * @default random UUID
160
+ * Authentication token for the Vercel Queue Service API.
161
+ * If not provided, the client will attempt to get a token via OIDC
162
+ * when running in a Vercel Function environment.
163
+ */
164
+ token?: string;
165
+ /**
166
+ * Custom headers to include in all requests.
167
+ */
168
+ headers?: Record<string, string>;
169
+ /**
170
+ * Deployment ID for message routing and lease validation.
171
+ *
172
+ * - `undefined` (default): auto-detect from `VERCEL_DEPLOYMENT_ID` env var;
173
+ * sent messages are pinned to this deployment.
174
+ * - `null`: explicitly unpinned — no deployment ID is sent on any request.
175
+ * - `"dpl_xxx"`: explicit value used for both send pinning and consume identification.
176
+ *
177
+ * Ignored in development mode (deployment ID is never sent locally).
178
+ */
179
+ deploymentId?: string | null;
180
+ /**
181
+ * Serializer/deserializer for message payloads.
182
+ * Used for all send and receive operations.
183
+ * @default JsonTransport
184
+ */
185
+ transport?: Transport;
186
+ }
187
+ /**
188
+ * Options for sending messages.
189
+ */
190
+ interface SendOptions {
191
+ /**
192
+ * Unique key to prevent duplicate message submissions.
193
+ * Deduplication window: `min(message retention, 24 hours)`.
81
194
  */
82
195
  idempotencyKey?: string;
83
196
  /**
84
- * Message retention time in seconds
197
+ * Message retention time in seconds. After this period, the message expires.
85
198
  * @default 86400 (24 hours)
86
- * @min 60
87
- * @max 86400
199
+ * @minimum 60 (1 minute)
200
+ * @maximum 86400 (24 hours)
88
201
  */
89
202
  retentionSeconds?: number;
90
- }
91
- interface SendMessageOptions<T = unknown> extends PublishOptions {
92
203
  /**
93
- * The queue name to send the message to
204
+ * Delay delivery of the message by this many seconds.
205
+ * The message will not be visible to consumers until the delay has passed.
206
+ * @default 0
207
+ * @minimum 0
208
+ * @maximum Value of retentionSeconds (delay cannot exceed retention)
94
209
  */
95
- queueName: string;
210
+ delaySeconds?: number;
96
211
  /**
97
- * The message payload
212
+ * Custom headers to include with this message.
213
+ * These headers are passed through to the VQS server.
98
214
  */
99
- payload: T;
215
+ headers?: Record<string, string>;
100
216
  }
101
- interface SendMessageResponse {
102
- /**
103
- * The generated message ID
104
- */
105
- messageId: string;
217
+ /**
218
+ * Result returned by `send()`.
219
+ *
220
+ * `messageId` is `null` when the server accepted the message for deferred
221
+ * processing (e.g. during a server-side outage) and no ID is available yet.
222
+ */
223
+ interface SendResult {
224
+ messageId: string | null;
106
225
  }
107
226
  interface Message<T = unknown> {
108
227
  /**
109
- * The message ID
228
+ * Unique message identifier. Used for receive-by-ID operations.
110
229
  */
111
230
  messageId: string;
112
231
  /**
113
- * The deserialized message payload
114
- * Note: If using streaming transports, ensure proper cleanup by calling transport.finalize(payload)
115
- * when done processing, especially in error cases
232
+ * The deserialized message payload.
233
+ * Note: If using StreamTransport, ensure proper cleanup by calling
234
+ * `transport.finalize(payload)` when done processing, especially in error cases.
116
235
  */
117
236
  payload: T;
118
237
  /**
119
- * Number of times this message has been delivered
238
+ * Number of times this message has been delivered.
239
+ * Starts at 1 for the first delivery, increments on each retry.
120
240
  */
121
241
  deliveryCount: number;
122
242
  /**
123
- * Timestamp when the message was created
243
+ * Timestamp when the message was created/published.
124
244
  */
125
- timestamp: string;
245
+ createdAt: Date;
126
246
  /**
127
- * MIME type of the message content
247
+ * Timestamp when the message expires.
248
+ * Only present for messages delivered via the v2beta binary callback path.
128
249
  */
129
- contentType: string;
250
+ expiresAt?: Date;
130
251
  /**
131
- * Unique ticket for this message delivery (required for delete/patch operations)
252
+ * MIME type of the message content.
253
+ * Set by the transport's `contentType` property during publish.
132
254
  */
133
- ticket: string;
134
- }
135
- /**
136
- * Result indicating the message should be timed out for retry later
137
- */
138
- interface MessageTimeoutResult {
255
+ contentType: string;
139
256
  /**
140
- * Time in seconds before the message becomes visible again
257
+ * Receipt handle (lease token) for this message delivery.
258
+ * Required for acknowledge and visibility extension operations.
259
+ * Valid only until the visibility timeout expires.
141
260
  */
142
- timeoutSeconds: number;
261
+ receiptHandle: string;
143
262
  }
144
- /**
145
- * Result returned by message handlers
146
- */
147
- type MessageHandlerResult = void | MessageTimeoutResult;
148
263
  /**
149
264
  * Message metadata provided to handlers
150
265
  */
151
266
  interface MessageMetadata {
152
267
  messageId: string;
153
268
  deliveryCount: number;
154
- timestamp: string;
269
+ createdAt: Date;
270
+ expiresAt?: Date;
271
+ topicName: string;
272
+ consumerGroup: string;
273
+ /** Vercel region the client is targeting. */
274
+ region: string;
155
275
  }
156
276
  /**
157
- * Message handler function type
277
+ * Instruction returned by a {@link RetryHandler} to control what happens
278
+ * to a message when the handler throws.
279
+ *
280
+ * - `{ afterSeconds: N }` — reschedule the message for redelivery after N seconds
281
+ * - `{ acknowledge: true }` — acknowledge the message so it is never retried
282
+ */
283
+ type RetryDirective = {
284
+ afterSeconds: number;
285
+ } | {
286
+ acknowledge: true;
287
+ };
288
+ /**
289
+ * Called when the message handler throws an error.
290
+ * Return a {@link RetryDirective} to reschedule or acknowledge the message,
291
+ * or return `undefined` to let the error propagate normally.
292
+ */
293
+ type RetryHandler = (error: unknown, metadata: MessageMetadata) => RetryDirective | void | undefined;
294
+ /**
295
+ * Message handler function type.
296
+ *
297
+ * Called once per message with the deserialized payload and metadata.
298
+ * Not called when the queue is empty — check the return value of `receive()` instead.
299
+ */
300
+ type MessageHandler<T = unknown> = (message: T, metadata: MessageMetadata) => Promise<void> | void;
301
+ /**
302
+ * Result returned by `receive()`. Use `ok` to check if messages were processed.
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * const result = await receive("topic", "group", handler);
307
+ * if (result.ok) {
308
+ * // Messages were processed successfully
309
+ * } else if (result.reason === "empty") {
310
+ * // Queue had no messages available
311
+ * }
312
+ * ```
313
+ */
314
+ type ReceiveResult = {
315
+ ok: true;
316
+ } | {
317
+ ok: false;
318
+ reason: "empty";
319
+ } | {
320
+ ok: false;
321
+ reason: "not_found";
322
+ messageId: string;
323
+ } | {
324
+ ok: false;
325
+ reason: "not_available";
326
+ messageId: string;
327
+ } | {
328
+ ok: false;
329
+ reason: "already_processed";
330
+ messageId: string;
331
+ };
332
+ /**
333
+ * Options for receiving a specific message by ID.
158
334
  */
159
- type MessageHandler<T = unknown> = (message: T, metadata: MessageMetadata) => Promise<MessageHandlerResult> | MessageHandlerResult;
335
+ interface ReceiveByIdOptions {
336
+ /** The specific message ID to consume */
337
+ messageId: string;
338
+ /**
339
+ * Message lock duration in seconds.
340
+ * @default 300 (5 minutes)
341
+ * @minimum 30
342
+ * @maximum 3600 (1 hour)
343
+ */
344
+ visibilityTimeoutSeconds?: number;
345
+ /**
346
+ * Called when the handler throws. Return `{ afterSeconds: N }` to reschedule
347
+ * the message for redelivery after N seconds, or return `undefined`
348
+ * to let the error propagate normally.
349
+ */
350
+ retry?: RetryHandler;
351
+ }
160
352
  /**
161
- * Options for creating a ConsumerGroup instance
353
+ * Options for receiving messages from the queue with an optional batch limit.
162
354
  */
163
- interface ConsumerGroupOptions<T = unknown> {
355
+ interface ReceiveBatchOptions {
164
356
  /**
165
- * Serializer/deserializer for the payload
166
- * @default JsonTransport instance
357
+ * Maximum number of messages to retrieve in a single request.
358
+ * @default 1
359
+ * @minimum 1
360
+ * @maximum 10
167
361
  */
168
- transport?: Transport<T>;
362
+ limit?: number;
169
363
  /**
170
- * Time in seconds that messages will be invisible to other consumers
171
- * @default 30
364
+ * Message lock duration in seconds.
365
+ * @default 300 (5 minutes)
366
+ * @minimum 30
367
+ * @maximum 3600 (1 hour)
172
368
  */
173
369
  visibilityTimeoutSeconds?: number;
174
370
  /**
175
- * How often to refresh the visibility timeout during processing (in seconds)
176
- * @default 10
371
+ * Called when the handler throws. Return `{ afterSeconds: N }` to reschedule
372
+ * the message for redelivery after N seconds, or return `undefined`
373
+ * to let the error propagate normally.
177
374
  */
178
- refreshInterval?: number;
375
+ retry?: RetryHandler;
179
376
  }
180
377
  /**
181
- * Error thrown when a message is not found (404)
378
+ * Options for the receive method. Either receive by message ID or receive
379
+ * from the queue with an optional limit. These options are mutually exclusive.
380
+ */
381
+ type ReceiveOptions = ReceiveByIdOptions | ReceiveBatchOptions;
382
+ /**
383
+ * Error thrown when a message does not exist or has expired.
182
384
  */
183
385
  declare class MessageNotFoundError extends Error {
184
386
  constructor(messageId: string);
185
387
  }
186
388
  /**
187
- * Error thrown when a message is not available for processing (409)
188
- * This can happen when the message is in the wrong state, already claimed, etc.
389
+ * Error thrown when a message exists but cannot be processed.
390
+ * This can happen when the message is in the wrong state or already claimed by another consumer.
189
391
  */
190
392
  declare class MessageNotAvailableError extends Error {
191
393
  constructor(messageId: string, reason?: string);
@@ -197,166 +399,252 @@ declare class MessageCorruptedError extends Error {
197
399
  constructor(messageId: string, reason: string);
198
400
  }
199
401
  /**
200
- * Error thrown when there are no messages available in the queue (204)
402
+ * Error thrown when there are no messages available in the queue.
201
403
  */
202
404
  declare class QueueEmptyError extends Error {
203
405
  constructor(queueName: string, consumerGroup: string);
204
406
  }
205
407
  /**
206
- * Error thrown when a message is temporarily locked (423)
408
+ * Error thrown when a message is temporarily locked by another consumer.
409
+ * The message is currently being processed and cannot be claimed.
207
410
  */
208
411
  declare class MessageLockedError extends Error {
412
+ /** Suggested retry delay in seconds, if provided by the server. */
209
413
  readonly retryAfter?: number;
210
414
  constructor(messageId: string, retryAfter?: number);
211
415
  }
212
416
  /**
213
- * Error thrown when authentication fails (401)
417
+ * Error thrown when authentication fails.
418
+ * This typically means the token is missing, invalid, or expired.
214
419
  */
215
420
  declare class UnauthorizedError extends Error {
216
421
  constructor(message?: string);
217
422
  }
218
423
  /**
219
- * Error thrown when access is forbidden (403)
424
+ * Error thrown when access is forbidden.
425
+ * This typically means the queue belongs to a different environment or project.
220
426
  */
221
427
  declare class ForbiddenError extends Error {
222
428
  constructor(message?: string);
223
429
  }
224
430
  /**
225
- * Error thrown for bad requests (400)
431
+ * Error thrown when request parameters are invalid.
226
432
  */
227
433
  declare class BadRequestError extends Error {
228
434
  constructor(message: string);
229
435
  }
230
436
  /**
231
- * Error thrown for internal server errors (500)
437
+ * Error thrown when the server encounters an unexpected error.
232
438
  */
233
439
  declare class InternalServerError extends Error {
234
440
  constructor(message?: string);
235
441
  }
236
442
  /**
237
- * Error thrown when batch limit parameter is invalid
443
+ * Error thrown when the batch limit parameter is outside the valid range.
444
+ * The limit must be between 1 and 10 (inclusive).
238
445
  */
239
446
  declare class InvalidLimitError extends Error {
240
447
  constructor(limit: number, min?: number, max?: number);
241
448
  }
242
-
243
449
  /**
244
- * Options for the consume method
450
+ * Error thrown when attempting to process a message that has already been successfully processed.
245
451
  */
246
- interface ConsumeOptions {
247
- /** The specific message ID to consume (if not provided, consumes next available message) */
248
- messageId?: string;
249
- /** Whether to skip downloading the payload (only allowed when messageId is provided) */
250
- skipPayload?: boolean;
452
+ declare class MessageAlreadyProcessedError extends Error {
453
+ constructor(messageId: string);
251
454
  }
252
-
253
455
  /**
254
- * Options for the send function
456
+ * Error thrown when a duplicate idempotency key is detected.
457
+ * The message was already sent within the deduplication window.
255
458
  */
256
- interface SendOptions<T = unknown> extends PublishOptions {
257
- /**
258
- * Serializer/deserializer for the payload
259
- * @default JsonTransport instance
260
- */
261
- transport?: Transport<T>;
459
+ declare class DuplicateMessageError extends Error {
460
+ readonly idempotencyKey?: string;
461
+ constructor(message: string, idempotencyKey?: string);
262
462
  }
263
463
  /**
264
- * Send a message to a topic (shorthand for topic creation and publishing)
265
- * Uses the default QueueClient with automatic OIDC token detection
266
- * @param topicName Name of the topic to send to
267
- * @param payload The data to send
268
- * @param options Optional send options including transport and publish settings
269
- * @returns Promise with the message ID
270
- * @throws {BadRequestError} When request parameters are invalid
271
- * @throws {UnauthorizedError} When authentication fails
272
- * @throws {ForbiddenError} When access is denied (environment mismatch)
273
- * @throws {InternalServerError} When server encounters an error
274
- */
275
- declare function send<T = unknown>(topicName: string, payload: T, options?: SendOptions<T>): Promise<{
276
- messageId: string;
277
- }>;
464
+ * Error thrown when consumer discovery fails.
465
+ * This typically means the deployment could not be reached or is not configured correctly.
466
+ */
467
+ declare class ConsumerDiscoveryError extends Error {
468
+ readonly deploymentId?: string;
469
+ constructor(message: string, deploymentId?: string);
470
+ }
278
471
  /**
279
- * Options for the receive function
472
+ * Error thrown when the consumer registry is not configured for the project.
280
473
  */
281
- interface ReceiveOptions<T = unknown> extends ConsumerGroupOptions<T>, ConsumeOptions {
474
+ declare class ConsumerRegistryNotConfiguredError extends Error {
475
+ constructor(message?: string);
476
+ }
477
+
478
+ declare class QueueClient {
479
+ constructor(options: QueueClientOptions);
480
+ /**
481
+ * Send a message to a topic.
482
+ *
483
+ * This is an arrow function property so it can be destructured:
484
+ * ```typescript
485
+ * const { send } = new QueueClient({ region: process.env.QUEUE_REGION! });
486
+ * await send("my-topic", payload);
487
+ * ```
488
+ *
489
+ * @param topicName - Name of the topic (pattern: `[A-Za-z0-9_-]+`)
490
+ * @param payload - The data to send (serialized via the configured transport)
491
+ * @param options - Optional send options (idempotencyKey, retentionSeconds, delaySeconds, headers)
492
+ * @returns `{ messageId }` — `messageId` is `null` when the server accepted
493
+ * the message for deferred processing (no ID available yet)
494
+ */
495
+ send: <T = unknown>(topicName: string, payload: T, options?: SendOptions) => Promise<SendResult>;
496
+ /**
497
+ * Receive and process messages from a topic.
498
+ *
499
+ * Each message is automatically locked, kept alive via periodic visibility
500
+ * extensions during processing, and acknowledged upon successful handler completion.
501
+ * The handler is not called when the queue is empty — check `result.ok` instead.
502
+ *
503
+ * This is an arrow function property so it can be destructured:
504
+ * ```typescript
505
+ * const { receive } = new QueueClient({ region: process.env.QUEUE_REGION! });
506
+ * const result = await receive("my-topic", "my-group", handler);
507
+ * if (!result.ok) console.log(result.reason);
508
+ * ```
509
+ *
510
+ * @param topicName - Name of the topic (pattern: `[A-Za-z0-9_-]+`)
511
+ * @param consumerGroup - Name of the consumer group (pattern: `[A-Za-z0-9_-]+`)
512
+ * @param handler - Function to process each message payload and metadata.
513
+ * Not called when the queue is empty.
514
+ * @param options - Optional receive options (visibilityTimeoutSeconds, limit, or messageId)
515
+ * @returns Discriminated result: `{ ok: true }` on success, `{ ok: false, reason }` otherwise
516
+ */
517
+ receive: <T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options?: ReceiveOptions) => Promise<ReceiveResult>;
518
+ /**
519
+ * Create a Web API route handler for processing queue callback messages.
520
+ *
521
+ * Parses incoming `Request` as a CloudEvent and invokes the handler.
522
+ * For use on Vercel — Vercel invokes this route when messages are available.
523
+ *
524
+ * This is an arrow function property so it can be destructured:
525
+ * ```typescript
526
+ * const { handleCallback } = new QueueClient({ region: process.env.QUEUE_REGION! });
527
+ * export const POST = handleCallback(handler);
528
+ * ```
529
+ *
530
+ * @param handler - Function to process the message payload and metadata
531
+ * @param options - Optional configuration
532
+ * @param options.visibilityTimeoutSeconds - Message lock duration (default: 300, max: 3600)
533
+ * @param options.retry - Called when the handler throws. Return `{ afterSeconds: N }` to
534
+ * reschedule the message for redelivery after N seconds.
535
+ * @returns A `(request: Request) => Promise<Response>` route handler
536
+ */
537
+ handleCallback: <T = unknown>(handler: MessageHandler<T>, options?: {
538
+ visibilityTimeoutSeconds?: number;
539
+ retry?: RetryHandler;
540
+ }) => ((request: Request) => Promise<Response>);
541
+ /**
542
+ * Create a Connect-style route handler for processing queue callback messages.
543
+ * For use on Vercel — Vercel invokes this route when messages are available.
544
+ *
545
+ * For frameworks using the `(req, res)` middleware pattern where `req.body`
546
+ * is pre-parsed (Next.js Pages Router, etc.).
547
+ *
548
+ * This is an arrow function property so it can be destructured:
549
+ * ```typescript
550
+ * const { handleNodeCallback } = new QueueClient({ region: process.env.QUEUE_REGION! });
551
+ * app.post("/api/queue", handleNodeCallback(handler));
552
+ * ```
553
+ *
554
+ * @param handler - Function to process the message payload and metadata
555
+ * @param options - Optional configuration
556
+ * @param options.visibilityTimeoutSeconds - Message lock duration (default: 300, max: 3600)
557
+ * @param options.retry - Called when the handler throws. Return `{ afterSeconds: N }` to
558
+ * reschedule the message for redelivery after N seconds.
559
+ * @returns A `(req, res) => Promise<void>` route handler
560
+ */
561
+ handleNodeCallback: <T = unknown>(handler: MessageHandler<T>, options?: {
562
+ visibilityTimeoutSeconds?: number;
563
+ retry?: RetryHandler;
564
+ }) => ((req: {
565
+ method?: string;
566
+ headers: Record<string, string | string[] | undefined>;
567
+ body?: unknown;
568
+ }, res: {
569
+ status(code: number): {
570
+ json(data: unknown): void;
571
+ end(): void;
572
+ };
573
+ end(): void;
574
+ }) => Promise<void>);
282
575
  }
576
+
283
577
  /**
284
- * Receive a message from a topic (shorthand for topic and consumer group creation)
285
- * Uses the default QueueClient with automatic OIDC token detection
286
- * @param topicName Name of the topic to receive from
287
- * @param consumerGroup Name of the consumer group
288
- * @param handler Function to process the message
289
- * @returns Promise that resolves when the message is processed
290
- * @throws All the same errors as the underlying client methods
291
- */
292
- declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options?: ReceiveOptions<T>): Promise<void>;
293
- /**
294
- * Receive a specific message by its ID with full payload
295
- * @param topicName Name of the topic to receive from
296
- * @param consumerGroup Name of the consumer group
297
- * @param handler Function to process the message
298
- * @param options Receive options with messageId specified
299
- * @returns Promise that resolves when the message is processed
300
- * @throws All the same errors as the underlying client methods
301
- */
302
- declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options: ReceiveOptions<T> & {
303
- messageId: string;
304
- skipPayload?: false | undefined;
305
- }): Promise<void>;
306
- /**
307
- * Receive a specific message by its ID without downloading the payload (metadata only)
308
- * @param topicName Name of the topic to receive from
309
- * @param consumerGroup Name of the consumer group
310
- * @param handler Function to process the message metadata (payload will be void)
311
- * @param options Receive options with messageId and skipPayload specified
312
- * @returns Promise that resolves when the message is processed
313
- * @throws All the same errors as the underlying client methods
314
- */
315
- declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<void>, options: ReceiveOptions<T> & {
316
- messageId: string;
317
- skipPayload: true;
318
- }): Promise<void>;
578
+ * Core queue callback utilities for handling incoming webhook payloads
579
+ * from Vercel triggers using the CloudEvent specification.
580
+ *
581
+ * This module provides the framework-agnostic core. For framework-specific
582
+ * wrappers, see `@vercel/queue/web` and `@vercel/queue/nextjs/pages`.
583
+ */
319
584
 
585
+ declare const CLOUD_EVENT_TYPE_V1BETA = "com.vercel.queue.v1beta";
586
+ declare const CLOUD_EVENT_TYPE_V2BETA = "com.vercel.queue.v2beta";
320
587
  /**
321
- * Configuration object with handlers for different topics and consumer groups
588
+ * Routing-only callback: the SDK must fetch the message by ID.
589
+ * Produced by v1beta structured mode and v2beta large-body mode.
322
590
  */
323
- type CallbackHandlers = {
324
- [topicName: string]: {
325
- [consumerGroup: string]: MessageHandler;
326
- };
591
+ type ParsedCallbackV1 = {
592
+ queueName: string;
593
+ consumerGroup: string;
594
+ messageId: string;
595
+ region?: string;
596
+ };
597
+ /**
598
+ * Full-message callback: payload and receipt handle are inlined,
599
+ * so the SDK can process directly without an extra fetch.
600
+ * Produced by v2beta small-body mode.
601
+ */
602
+ type ParsedCallbackV2 = {
603
+ queueName: string;
604
+ consumerGroup: string;
605
+ messageId: string;
606
+ region?: string;
607
+ receiptHandle: string;
608
+ deliveryCount?: number;
609
+ createdAt?: string;
610
+ expiresAt?: string;
611
+ contentType?: string;
612
+ visibilityDeadline?: string;
613
+ rawBody?: ReadableStream<Uint8Array>;
614
+ parsedPayload?: unknown;
327
615
  };
616
+ type ParsedCallbackRequest = ParsedCallbackV1 | ParsedCallbackV2;
328
617
  /**
329
- * Simplified queue callback handler for Next.js route handlers
618
+ * Parse a callback from a pre-parsed body and headers.
330
619
  *
331
- * Automatically extracts queue information from CloudEvent format
332
- * and routes to the appropriate handler based on topic and consumer group.
620
+ * For frameworks like Next.js Pages Router where the body has already been
621
+ * parsed, use this instead of {@link parseCallback}.
333
622
  *
334
- * @param handlers Object with topic-specific handlers organized by consumer groups
335
- * @returns A Next.js route handler function
623
+ * Detects the CloudEvent version from the `ce-type` header:
624
+ * - `com.vercel.queue.v2beta`: binary content mode (metadata in headers,
625
+ * payload in body). For small messages, the body is attached as `parsedPayload`.
626
+ * - Otherwise: structured content mode (v1beta, entire CloudEvent in JSON body)
336
627
  *
337
- * @example
338
- * ```typescript
339
- * // Single topic with multiple consumer groups
340
- * export const POST = handleCallback({
341
- * "image-processing": {
342
- * "compress": (message, metadata) => console.log("Compressing image", message),
343
- * "resize": (message, metadata) => console.log("Resizing image", message),
344
- * }
345
- * });
628
+ * @param body - The framework-parsed request body. Type depends on Content-Type
629
+ * and framework configuration:
630
+ * - v1beta: parsed CloudEvent JSON object
631
+ * - v2beta: the message payload as parsed by the framework (object, Buffer, or string)
632
+ * @param headers - HTTP headers
633
+ * @returns Parsed callback request with routing metadata and optional payload
634
+ */
635
+ declare function parseRawCallback(body: unknown, headers: Record<string, string | string[] | undefined>): ParsedCallbackRequest;
636
+ /**
637
+ * Parse and validate a CloudEvent callback from a Web API `Request` object.
346
638
  *
347
- * // Multiple topics with consumer groups
348
- * export const POST = handleCallback({
349
- * "user-events": {
350
- * "welcome": (user, metadata) => console.log("Welcoming user", user),
351
- * "analytics": (user, metadata) => console.log("Tracking user", user),
352
- * },
353
- * "order-events": {
354
- * "fulfillment": (order, metadata) => console.log("Fulfilling order", order),
355
- * "notifications": (order, metadata) => console.log("Notifying order", order),
356
- * }
357
- * });
358
- * ```
639
+ * Detects the CloudEvent version from the `ce-type` header:
640
+ * - `com.vercel.queue.v2beta`: binary content mode (metadata in headers,
641
+ * payload in body). For v2beta, the body is attached as `rawBody` (a
642
+ * ReadableStream) rather than being parsed.
643
+ * - Otherwise: structured content mode (v1beta, entire CloudEvent in JSON body)
644
+ *
645
+ * For frameworks that pre-parse the body (e.g. Next.js Pages Router),
646
+ * use {@link parseRawCallback} instead.
359
647
  */
360
- declare function handleCallback(handlers: CallbackHandlers): (request: Request) => Promise<Response>;
648
+ declare function parseCallback(request: Request): Promise<ParsedCallbackRequest>;
361
649
 
362
- export { BadRequestError, BufferTransport, ForbiddenError, InternalServerError, InvalidLimitError, JsonTransport, type Message, MessageCorruptedError, type MessageHandler, type MessageHandlerResult, MessageLockedError, type MessageMetadata, MessageNotAvailableError, MessageNotFoundError, type MessageTimeoutResult, type PublishOptions, QueueEmptyError, type ReceiveOptions, type SendMessageOptions, type SendMessageResponse, type SendOptions, StreamTransport, type Transport, UnauthorizedError, handleCallback, receive, send };
650
+ export { BadRequestError, type BaseUrlResolver, BufferTransport, CLOUD_EVENT_TYPE_V1BETA, CLOUD_EVENT_TYPE_V2BETA, ConsumerDiscoveryError, ConsumerRegistryNotConfiguredError, DuplicateMessageError, ForbiddenError, InternalServerError, InvalidLimitError, JsonTransport, type Message, MessageAlreadyProcessedError, MessageCorruptedError, type MessageHandler, MessageLockedError, type MessageMetadata, MessageNotAvailableError, MessageNotFoundError, type ParsedCallbackRequest, type ParsedCallbackV1, type ParsedCallbackV2, QueueClient, type QueueClientOptions, QueueEmptyError, type ReceiveBatchOptions, type ReceiveByIdOptions, type ReceiveOptions, type ReceiveResult, type RetryDirective, type RetryHandler, type SendOptions, type SendResult, StreamTransport, type Transport, UnauthorizedError, type VercelRegion, parseCallback, parseRawCallback };