@message-queue-toolkit/core
Version:
Useful utilities, interfaces and base classes for message queue handling. Supports AMQP and SQS with a common abstraction on top currently
80 lines (79 loc) • 3.14 kB
TypeScript
import { type CommonLogger, type ErrorReporter } from '@lokalise/node-core';
import { type StartupResourcePollingConfig } from '../types/queueOptionsTypes.ts';
/**
* Context passed to error callbacks indicating whether the error is final.
*/
export type PollingErrorContext = {
/**
* If true, the operation has stopped and will not retry.
* If false, this is a transient error and the operation will continue.
*/
isFinal: boolean;
};
/**
* Callback invoked when a polling operation fails.
*/
export type PollingErrorCallback = (error: Error, context: PollingErrorContext) => void;
export type StartupResourcePollingCheckResult<T> = {
isAvailable: true;
result: T;
} | {
isAvailable: false;
};
export type WaitForResourceOptions<T> = {
/**
* Startup resource polling configuration
*/
config: StartupResourcePollingConfig;
/**
* Function that checks if the resource is available.
* Should return { isAvailable: true, result: T } when resource exists,
* or { isAvailable: false } when resource doesn't exist.
* Should throw on unexpected errors.
*/
checkFn: () => Promise<StartupResourcePollingCheckResult<T>>;
/**
* Human-readable name of the resource for logging
*/
resourceName: string;
/**
* Logger instance for progress logging
*/
logger?: CommonLogger;
/**
* Error reporter for reporting timeout errors when throwOnTimeout is false.
* Required when config.throwOnTimeout is false.
*/
errorReporter?: ErrorReporter;
/**
* Callback invoked when resource becomes available in non-blocking mode.
* Only used when config.nonBlocking is true and the resource was not immediately available.
*/
onResourceAvailable?: (result: T) => void;
/**
* Callback invoked when background polling fails in non-blocking mode.
* This can happen due to polling timeout or unexpected errors during polling.
* Only used when config.nonBlocking is true.
*/
onError?: PollingErrorCallback;
};
export declare class StartupResourcePollingTimeoutError extends Error {
readonly resourceName: string;
readonly timeoutMs: number;
constructor(resourceName: string, timeoutMs: number);
}
/**
* Waits for a resource to become available by polling.
* This is used for startup resource polling mode where resources may not exist at startup.
*
* @param options - Configuration and check function
* @returns The result from the check function when resource becomes available,
* or undefined if nonBlocking is true and resource was not immediately available
* @throws StartupResourcePollingTimeoutError if timeout is reached and throwOnTimeout is true (default)
*/
export declare function waitForResource<T>(options: WaitForResourceOptions<T>): Promise<T | undefined>;
/**
* Helper to check if startup resource polling is enabled.
* Returns true only when config is provided and enabled is explicitly true.
*/
export declare function isStartupResourcePollingEnabled(config?: StartupResourcePollingConfig): config is StartupResourcePollingConfig;