@vercel/queue 0.0.0-alpha.3 → 0.0.0-alpha.5

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
@@ -80,8 +80,9 @@ interface QueueClientOptions {
80
80
  /**
81
81
  * Vercel function OIDC token
82
82
  * Can be obtained from x-vercel-oidc-token header in Vercel Serverless Functions
83
+ * If not provided, will automatically attempt to retrieve from Vercel Function environment
83
84
  */
84
- token: string;
85
+ token?: string;
85
86
  }
86
87
  /**
87
88
  * Callback configuration for a consumer group
@@ -100,15 +101,10 @@ interface CallbackConfig {
100
101
  */
101
102
  frequency?: number;
102
103
  }
103
- interface SendMessageOptions<T = unknown> {
104
- /**
105
- * The queue name to send the message to
106
- */
107
- queueName: string;
108
- /**
109
- * The message payload
110
- */
111
- payload: T;
104
+ /**
105
+ * Shared options for publishing messages
106
+ */
107
+ interface PublishOptions {
112
108
  /**
113
109
  * Unique key to prevent duplicate message submissions
114
110
  * @default random UUID
@@ -125,10 +121,19 @@ interface SendMessageOptions<T = unknown> {
125
121
  * Callback configuration
126
122
  * - If a single CallbackConfig is provided, it will use the "default" consumer group
127
123
  * - If an object is provided, keys are consumer group names with their respective callback configs
128
- * Format: { consumerGroup: { url, delay?, frequency? } } or { url, delay?, frequency? }
129
124
  */
130
125
  callback?: Record<string, CallbackConfig> | CallbackConfig;
131
126
  }
127
+ interface SendMessageOptions<T = unknown> extends PublishOptions {
128
+ /**
129
+ * The queue name to send the message to
130
+ */
131
+ queueName: string;
132
+ /**
133
+ * The message payload
134
+ */
135
+ payload: T;
136
+ }
132
137
  interface SendMessageResponse {
133
138
  /**
134
139
  * The generated message ID
@@ -250,10 +255,18 @@ interface MessageTimeoutResult {
250
255
  * Result returned by message handlers
251
256
  */
252
257
  type MessageHandlerResult = void | MessageTimeoutResult;
258
+ /**
259
+ * Message metadata provided to handlers
260
+ */
261
+ interface MessageMetadata {
262
+ messageId: string;
263
+ deliveryCount: number;
264
+ timestamp: string;
265
+ }
253
266
  /**
254
267
  * Message handler function type
255
268
  */
256
- type MessageHandler<T = unknown> = (message: Message<T>) => Promise<MessageHandlerResult> | MessageHandlerResult;
269
+ type MessageHandler<T = unknown> = (message: T, metadata: MessageMetadata) => Promise<MessageHandlerResult> | MessageHandlerResult;
257
270
  /**
258
271
  * Options for creating a ConsumerGroup instance
259
272
  */
@@ -302,16 +315,6 @@ interface ReceiveMessageByIdOptions<T = unknown> {
302
315
  interface ReceiveMessageByIdResponse<T = unknown, TSkipPayload extends boolean = false> {
303
316
  message: TSkipPayload extends true ? Message<void> : Message<T>;
304
317
  }
305
- interface FifoOrderingError {
306
- /**
307
- * Error message describing the FIFO ordering violation
308
- */
309
- error: string;
310
- /**
311
- * The message ID that must be processed first
312
- */
313
- nextMessageId: string;
314
- }
315
318
  /**
316
319
  * Error thrown when a message is not found (404)
317
320
  */
@@ -326,11 +329,10 @@ declare class MessageNotAvailableError extends Error {
326
329
  constructor(messageId: string, reason?: string);
327
330
  }
328
331
  /**
329
- * Error thrown when there's a FIFO ordering violation (409 with nextMessageId)
332
+ * Error thrown when there's a FIFO ordering violation (409)
330
333
  */
331
334
  declare class FifoOrderingViolationError extends Error {
332
- readonly nextMessageId: string;
333
- constructor(messageId: string, nextMessageId: string, reason: string);
335
+ constructor(messageId: string, reason: string);
334
336
  }
335
337
  /**
336
338
  * Error thrown when message data is corrupted or can't be parsed
@@ -373,8 +375,7 @@ declare class BadRequestError extends Error {
373
375
  * Error thrown when there's a failed dependency (424) - FIFO ordering violation in receive by ID
374
376
  */
375
377
  declare class FailedDependencyError extends Error {
376
- readonly nextMessageId: string;
377
- constructor(messageId: string, nextMessageId: string);
378
+ constructor(messageId: string);
378
379
  }
379
380
  /**
380
381
  * Error thrown for internal server errors (500)
@@ -418,19 +419,26 @@ declare class InvalidCallbackError extends Error {
418
419
  declare class QueueClient {
419
420
  private baseUrl;
420
421
  private token;
422
+ /**
423
+ * Internal default instance for use by createTopic and other convenience functions
424
+ * @internal
425
+ */
426
+ private static _defaultInstance;
421
427
  /**
422
428
  * Create a new Vercel Queue Service client
423
- * @param options Client configuration options
429
+ * @param options Client configuration options (optional - will auto-detect Vercel Function environment)
424
430
  */
425
- constructor(options: QueueClientOptions);
431
+ constructor(options?: QueueClientOptions);
426
432
  /**
427
- * Create a QueueClient automatically configured for Vercel Functions
428
- * This method automatically retrieves the OIDC token from the Vercel Function environment
429
- * Always creates a fresh instance since OIDC tokens expire after 15 minutes
430
- * @param baseUrl Optional base URL override
431
- * @returns Promise resolving to a new QueueClient instance
433
+ * Get the default client instance for internal use by convenience functions
434
+ * @internal
432
435
  */
433
- static fromVercelFunction(baseUrl?: string): Promise<QueueClient>;
436
+ static _getDefaultInstance(): QueueClient;
437
+ /**
438
+ * Synchronously get OIDC token from environment
439
+ * Used internally by constructor - mirrors the logic from getVercelOidcToken but synchronously
440
+ */
441
+ private getVercelOidcTokenSync;
434
442
  /**
435
443
  * Send a message to a queue
436
444
  * @param options Send message options
@@ -465,7 +473,6 @@ declare class QueueClient {
465
473
  * @throws {MessageLockedError} When the message is temporarily locked (423)
466
474
  * @throws {FailedDependencyError} When FIFO ordering is violated (424)
467
475
  * @throws {MessageNotAvailableError} When message exists but isn't available (409)
468
- * @throws {FifoOrderingViolationError} When FIFO ordering is violated (409 with nextMessageId)
469
476
  * @throws {MessageCorruptedError} When message data is corrupted
470
477
  * @throws {BadRequestError} When request parameters are invalid
471
478
  * @throws {UnauthorizedError} When authentication fails
@@ -504,6 +511,15 @@ declare class QueueClient {
504
511
  changeVisibility(options: ChangeVisibilityOptions): Promise<ChangeVisibilityResponse>;
505
512
  }
506
513
 
514
+ /**
515
+ * Options for the consume method
516
+ */
517
+ interface ConsumeOptions {
518
+ /** The specific message ID to consume (if not provided, consumes next available message) */
519
+ messageId?: string;
520
+ /** Whether to skip downloading the payload (only allowed when messageId is provided) */
521
+ skipPayload?: boolean;
522
+ }
507
523
  /**
508
524
  * A ConsumerGroup represents a named group of consumers that process messages from a topic
509
525
  */
@@ -548,75 +564,34 @@ declare class ConsumerGroup<T = unknown> {
548
564
  */
549
565
  private processMessage;
550
566
  /**
551
- * Start continuous processing of messages from the topic
552
- * @param signal AbortSignal to control when to stop processing
553
- * @param handler Function to process each message
554
- * @param options Processing options
555
- * @returns Promise that resolves when processing stops (due to signal or error)
556
- */
557
- subscribe(signal: AbortSignal, handler: MessageHandler<T>, options?: {
558
- pollingInterval?: number;
559
- }): Promise<void>;
560
- /**
561
- * Receive and process a specific message by its ID with full payload
562
- * @param messageId The ID of the message to receive and process
563
- * @param handler Function to process the message with full payload
564
- * @returns Promise that resolves when the message is processed or rejects with specific errors
565
- * @throws {MessageNotFoundError} When the message doesn't exist (404)
566
- * @throws {MessageNotAvailableError} When the message exists but isn't available for processing (409)
567
- * @throws {MessageLockedError} When the message is temporarily locked (423)
568
- * @throws {FifoOrderingViolationError} When there's a FIFO ordering violation (409 with nextMessageId)
569
- * @throws {FailedDependencyError} When FIFO ordering is violated (424)
570
- * @throws {MessageCorruptedError} When the message data is corrupted
571
- * @throws {BadRequestError} When request parameters are invalid
572
- * @throws {UnauthorizedError} When authentication fails
573
- * @throws {ForbiddenError} When access is denied
574
- * @throws {InternalServerError} When server encounters an error
575
- */
576
- receiveMessage(messageId: string, handler: MessageHandler<T>): Promise<void>;
577
- /**
578
- * Receive and process the next available message from the queue
567
+ * Consume the next available message from the queue
579
568
  * @param handler Function to process the message
580
- * @returns Promise that resolves when the message is processed or rejects with specific errors
581
- * @throws {QueueEmptyError} When no messages are available in the queue (204)
582
- * @throws {MessageLockedError} When the next message in a FIFO queue is locked (423)
583
- * @throws {BadRequestError} When request parameters are invalid
584
- * @throws {UnauthorizedError} When authentication fails
585
- * @throws {ForbiddenError} When access is denied
586
- * @throws {InternalServerError} When server encounters an error
569
+ * @returns Promise that resolves when the message is processed
570
+ * @throws All the same errors as the underlying client methods
587
571
  */
588
- receiveNextMessage(handler: MessageHandler<T>): Promise<void>;
572
+ consume(handler: MessageHandler<T>): Promise<void>;
589
573
  /**
590
- * Receive and process multiple next available messages from the queue
591
- * @param limit Number of messages to process (1-10)
592
- * @param handler Function to process each message
593
- * @returns Promise that resolves to an array of PromiseSettledResult (same as Promise.allSettled)
594
- * @throws {InvalidLimitError} When limit parameter is not between 1 and 10
595
- * @throws {QueueEmptyError} When no messages are available in the queue (204)
596
- * @throws {MessageLockedError} When the next message in a FIFO queue is locked (423)
597
- * @throws {BadRequestError} When request parameters are invalid
598
- * @throws {UnauthorizedError} When authentication fails
599
- * @throws {ForbiddenError} When access is denied
600
- * @throws {InternalServerError} When server encounters an error
574
+ * Consume a specific message by its ID with full payload
575
+ * @param handler Function to process the message
576
+ * @param options Consume options with messageId specified
577
+ * @returns Promise that resolves when the message is processed
578
+ * @throws All the same errors as the underlying client methods
601
579
  */
602
- receiveNextMessages(limit: number, handler: MessageHandler<T>): Promise<PromiseSettledResult<void>[]>;
580
+ consume(handler: MessageHandler<T>, options: {
581
+ messageId: string;
582
+ skipPayload?: false | undefined;
583
+ }): Promise<void>;
603
584
  /**
604
- * Handle a specific message by its ID without downloading the payload (metadata only)
605
- * @param messageId The ID of the message to handle
585
+ * Consume a specific message by its ID without downloading the payload (metadata only)
606
586
  * @param handler Function to process the message metadata (payload will be void)
607
- * @returns Promise that resolves when the message is handled or rejects with specific errors
608
- * @throws {MessageNotFoundError} When the message doesn't exist (404)
609
- * @throws {MessageNotAvailableError} When the message exists but isn't available for processing (409)
610
- * @throws {MessageLockedError} When the message is temporarily locked (423)
611
- * @throws {FifoOrderingViolationError} When there's a FIFO ordering violation (409 with nextMessageId)
612
- * @throws {FailedDependencyError} When FIFO ordering is violated (424)
613
- * @throws {MessageCorruptedError} When the message data is corrupted
614
- * @throws {BadRequestError} When request parameters are invalid
615
- * @throws {UnauthorizedError} When authentication fails
616
- * @throws {ForbiddenError} When access is denied
617
- * @throws {InternalServerError} When server encounters an error
587
+ * @param options Consume options with messageId and skipPayload specified
588
+ * @returns Promise that resolves when the message is processed
589
+ * @throws All the same errors as the underlying client methods
618
590
  */
619
- handleMessage(messageId: string, handler: MessageHandler<void>): Promise<void>;
591
+ consume(handler: MessageHandler<void>, options: {
592
+ messageId: string;
593
+ skipPayload: true;
594
+ }): Promise<void>;
620
595
  /**
621
596
  * Get the consumer group name
622
597
  */
@@ -651,11 +626,7 @@ declare class Topic<T = unknown> {
651
626
  * @throws {ForbiddenError} When access is denied (environment mismatch)
652
627
  * @throws {InternalServerError} When server encounters an error
653
628
  */
654
- publish(payload: T, options?: {
655
- idempotencyKey?: string;
656
- retentionSeconds?: number;
657
- callback?: Record<string, CallbackConfig> | CallbackConfig;
658
- }): Promise<{
629
+ publish(payload: T, options?: PublishOptions): Promise<{
659
630
  messageId: string;
660
631
  }>;
661
632
  /**
@@ -676,13 +647,79 @@ declare class Topic<T = unknown> {
676
647
  }
677
648
 
678
649
  /**
679
- * Create a new Topic instance
680
- * @param client QueueClient instance to use for API calls
650
+ * Create a new Topic instance using the default QueueClient
651
+ * For custom client configuration, use `new Topic(customClient, topicName, transport)` directly
681
652
  * @param topicName Name of the topic
682
653
  * @param transport Optional serializer/deserializer for the payload (defaults to JSON)
683
654
  * @returns A Topic instance
684
655
  */
685
- declare function createTopic<T = unknown>(client: QueueClient, topicName: string, transport?: Transport<T>): Topic<T>;
656
+ declare function createTopic<T = unknown>(topicName: string, transport?: Transport<T>): Topic<T>;
657
+ /**
658
+ * Options for the send function
659
+ */
660
+ interface SendOptions<T = unknown> extends PublishOptions {
661
+ /**
662
+ * Serializer/deserializer for the payload
663
+ * @default JsonTransport instance
664
+ */
665
+ transport?: Transport<T>;
666
+ }
667
+ /**
668
+ * Send a message to a topic (shorthand for topic creation and publishing)
669
+ * Uses the default QueueClient with automatic OIDC token detection
670
+ * @param topicName Name of the topic to send to
671
+ * @param payload The data to send
672
+ * @param options Optional send options including transport and publish settings
673
+ * @returns Promise with the message ID
674
+ * @throws {BadRequestError} When request parameters are invalid
675
+ * @throws {UnauthorizedError} When authentication fails
676
+ * @throws {ForbiddenError} When access is denied (environment mismatch)
677
+ * @throws {InternalServerError} When server encounters an error
678
+ */
679
+ declare function send<T = unknown>(topicName: string, payload: T, options?: SendOptions<T>): Promise<{
680
+ messageId: string;
681
+ }>;
682
+ /**
683
+ * Options for the receive function
684
+ */
685
+ interface ReceiveOptions<T = unknown> extends ConsumerGroupOptions<T>, ConsumeOptions {
686
+ }
687
+ /**
688
+ * Receive a message from a topic (shorthand for topic and consumer group creation)
689
+ * Uses the default QueueClient with automatic OIDC token detection
690
+ * @param topicName Name of the topic to receive from
691
+ * @param consumerGroup Name of the consumer group
692
+ * @param handler Function to process the message
693
+ * @returns Promise that resolves when the message is processed
694
+ * @throws All the same errors as the underlying client methods
695
+ */
696
+ declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options?: ReceiveOptions<T>): Promise<void>;
697
+ /**
698
+ * Receive a specific message by its ID with full payload
699
+ * @param topicName Name of the topic to receive from
700
+ * @param consumerGroup Name of the consumer group
701
+ * @param handler Function to process the message
702
+ * @param options Receive options with messageId specified
703
+ * @returns Promise that resolves when the message is processed
704
+ * @throws All the same errors as the underlying client methods
705
+ */
706
+ declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<T>, options: ReceiveOptions<T> & {
707
+ messageId: string;
708
+ skipPayload?: false | undefined;
709
+ }): Promise<void>;
710
+ /**
711
+ * Receive a specific message by its ID without downloading the payload (metadata only)
712
+ * @param topicName Name of the topic to receive from
713
+ * @param consumerGroup Name of the consumer group
714
+ * @param handler Function to process the message metadata (payload will be void)
715
+ * @param options Receive options with messageId and skipPayload specified
716
+ * @returns Promise that resolves when the message is processed
717
+ * @throws All the same errors as the underlying client methods
718
+ */
719
+ declare function receive<T = unknown>(topicName: string, consumerGroup: string, handler: MessageHandler<void>, options: ReceiveOptions<T> & {
720
+ messageId: string;
721
+ skipPayload: true;
722
+ }): Promise<void>;
686
723
 
687
724
  /**
688
725
  * Queue Callback utilities for handling incoming webhook payloads
@@ -721,18 +758,6 @@ declare function createTopic<T = unknown>(client: QueueClient, topicName: string
721
758
  * ```
722
759
  */
723
760
  declare function parseCallbackRequest(request: Request): CallbackMessageOptions;
724
- /**
725
- * Message metadata provided to handlers
726
- */
727
- interface MessageMetadata {
728
- messageId: string;
729
- deliveryCount: number;
730
- timestamp: string;
731
- }
732
- /**
733
- * Handler function signature
734
- */
735
- type Handler<T = unknown> = (payload: T, metadata: MessageMetadata) => Promise<MessageHandlerResult> | MessageHandlerResult;
736
761
  /**
737
762
  * Configuration object with handlers for different topics
738
763
  * Each topic can have either:
@@ -740,8 +765,8 @@ type Handler<T = unknown> = (payload: T, metadata: MessageMetadata) => Promise<M
740
765
  * - An object with handlers for specific consumer groups
741
766
  */
742
767
  type CallbackHandlers = {
743
- [topicName: string]: Handler | {
744
- [consumerGroup: string]: Handler;
768
+ [topicName: string]: MessageHandler | {
769
+ [consumerGroup: string]: MessageHandler;
745
770
  };
746
771
  };
747
772
  /**
@@ -770,6 +795,4 @@ type CallbackHandlers = {
770
795
  */
771
796
  declare function handleCallback(handlers: CallbackHandlers): (request: Request) => Promise<Response>;
772
797
 
773
- declare function getVercelOidcToken(): Promise<string>;
774
-
775
- export { BadRequestError, BufferTransport, type CallbackConfig, type CallbackMessageOptions, type ChangeVisibilityOptions, type ChangeVisibilityResponse, ConsumerGroup, type ConsumerGroupOptions, type DeleteMessageOptions, type DeleteMessageResponse, FailedDependencyError, type FifoOrderingError, FifoOrderingViolationError, ForbiddenError, InternalServerError, InvalidCallbackError, InvalidLimitError, JsonTransport, type Message, MessageCorruptedError, type MessageHandler, type MessageHandlerResult, MessageLockedError, type MessageMetadata, MessageNotAvailableError, MessageNotFoundError, type MessageTimeoutResult, QueueClient, type QueueClientOptions, QueueEmptyError, type ReceiveMessageByIdOptions, type ReceiveMessageByIdResponse, type ReceiveMessagesOptions, type SendMessageOptions, type SendMessageResponse, StreamTransport, Topic, type Transport, UnauthorizedError, createTopic, getVercelOidcToken, handleCallback, parseCallbackRequest };
798
+ export { BadRequestError, BufferTransport, type CallbackConfig, type CallbackMessageOptions, type ChangeVisibilityOptions, type ChangeVisibilityResponse, type ConsumeOptions, ConsumerGroup, type ConsumerGroupOptions, type DeleteMessageOptions, type DeleteMessageResponse, FailedDependencyError, FifoOrderingViolationError, ForbiddenError, InternalServerError, InvalidCallbackError, InvalidLimitError, JsonTransport, type Message, MessageCorruptedError, type MessageHandler, type MessageHandlerResult, MessageLockedError, type MessageMetadata, MessageNotAvailableError, MessageNotFoundError, type MessageTimeoutResult, type PublishOptions, QueueClient, type QueueClientOptions, QueueEmptyError, type ReceiveMessageByIdOptions, type ReceiveMessageByIdResponse, type ReceiveMessagesOptions, type ReceiveOptions, type SendMessageOptions, type SendMessageResponse, type SendOptions, StreamTransport, Topic, type Transport, UnauthorizedError, createTopic, handleCallback, parseCallbackRequest, receive, send };