UNPKG

alepha

Version:

Alepha is a convention-driven TypeScript framework for building robust, end-to-end type-safe applications, from serverless APIs to full-stack React apps.

171 lines (169 loc) 5.69 kB
import * as _alepha_core1 from "alepha"; import * as _alepha_core2 from "alepha"; import * as _alepha_core0 from "alepha"; import { Alepha, Descriptor, KIND, Service, Static, TSchema } from "alepha"; import { DateTimeProvider, DurationLike } from "alepha/datetime"; //#region src/providers/TopicProvider.d.ts /** * Base class for topic providers. */ declare abstract class TopicProvider { protected readonly alepha: Alepha; /** * Publish a message to a topic. * * @param topic - The topic to publish to. * @param message - The message to publish. */ abstract publish(topic: string, message: string): Promise<void>; /** * Subscribe to a topic. * * @param topic - The topic to subscribe to. * @param callback - The callback to call when a message is received. */ abstract subscribe(topic: string, callback: SubscribeCallback): Promise<UnSubscribeFn>; /** * Unsubscribe from a topic. * * @param topic - The topic to unsubscribe from. */ abstract unsubscribe(topic: string): Promise<void>; /** * Returns the list of $subscribers for this provider. */ protected subscribers(): Array<() => Promise<unknown>>; } type SubscribeCallback = (message: string) => Promise<void> | void; type UnSubscribeFn = () => Promise<void>; //# sourceMappingURL=TopicProvider.d.ts.map //#endregion //#region src/descriptors/$topic.d.ts /** * Create a new topic. */ declare const $topic: { <T extends TopicMessageSchema>(options: TopicDescriptorOptions<T>): TopicDescriptor<T>; [KIND]: typeof TopicDescriptor; }; interface TopicDescriptorOptions<T extends TopicMessageSchema> { /** * Topic key. * * If not provided, the propertyKey is used as the topic name. */ name?: string; /** * Describe the topic. For documentation purposes. */ description?: string; /** * Override the default topic provider. * * If not provided, the default provider is used. * If "memory" is provided, the default in-memory provider is used. * If a class is provided, it must extend `TopicProvider`. */ provider?: "memory" | Service<TopicProvider>; /** * Topic message schema. */ schema: T; /** * Add a subscriber handler. */ handler?: TopicHandler<T>; } declare class TopicDescriptor<T extends TopicMessageSchema> extends Descriptor<TopicDescriptorOptions<T>> { protected readonly log: _alepha_core1.Logger; protected readonly dateTimeProvider: DateTimeProvider; readonly provider: TopicProvider; get name(): string; publish(payload: TopicMessage<T>["payload"]): Promise<void>; subscribe(handler: TopicHandler<T>): Promise<UnSubscribeFn>; wait(options?: TopicWaitOptions<T>): Promise<TopicMessage<T>>; protected $provider(): TopicProvider; protected parseMessage(message: string): TopicMessage<T>; } interface TopicMessage<T extends TopicMessageSchema> { payload: Static<T["payload"]>; } interface TopicWaitOptions<T extends TopicMessageSchema> { timeout?: DurationLike; filter?: (message: { payload: Static<T["payload"]>; }) => boolean; } interface TopicMessageSchema { headers?: TSchema; payload: TSchema; } type TopicHandler<T extends TopicMessageSchema = TopicMessageSchema> = (message: TopicMessage<T>) => unknown; //# sourceMappingURL=$topic.d.ts.map //#endregion //#region src/descriptors/$subscriber.d.ts /** * Subscribe to a $topic. */ declare const $subscriber: { <T extends TopicMessageSchema>(options: SubscriberDescriptorOptions<T>): SubscriberDescriptor<T>; [KIND]: typeof SubscriberDescriptor; }; interface SubscriberDescriptorOptions<T extends TopicMessageSchema> { topic: TopicDescriptor<T>; handler: TopicHandler<T>; } declare class SubscriberDescriptor<T extends TopicMessageSchema> extends Descriptor<SubscriberDescriptorOptions<T>> {} //# sourceMappingURL=$subscriber.d.ts.map //#endregion //#region src/errors/TopicTimeoutError.d.ts declare class TopicTimeoutError extends Error { readonly topic: string; readonly timeout: number; constructor(topic: string, timeout: number); } //# sourceMappingURL=TopicTimeoutError.d.ts.map //#endregion //#region src/providers/MemoryTopicProvider.d.ts declare class MemoryTopicProvider extends TopicProvider { protected readonly log: _alepha_core2.Logger; protected readonly subscriptions: Record<string, SubscribeCallback[]>; protected readonly start: _alepha_core2.HookDescriptor<"start">; /** * Publish a message to a topic. * * @param topic * @param message */ publish(topic: string, message: string): Promise<void>; /** * Subscribe to a topic. * * @param topic - The topic to subscribe to. * @param callback */ subscribe(topic: string, callback: SubscribeCallback): Promise<UnSubscribeFn>; /** * Unsubscribe from a topic. * * @param topic - The topic to unsubscribe from. */ unsubscribe(topic: string): Promise<void>; } //# sourceMappingURL=MemoryTopicProvider.d.ts.map //#endregion //#region src/index.d.ts /** * Generic interface for pub/sub messaging. * Gives you the ability to create topics and subscribers. * This module provides only a memory implementation of the topic provider. * * @see {@link $topic} * @see {@link $subscriber} * @module alepha.topic */ declare const AlephaTopic: _alepha_core0.Service<_alepha_core0.Module>; //# sourceMappingURL=index.d.ts.map //#endregion export { $subscriber, $topic, AlephaTopic, MemoryTopicProvider, SubscribeCallback, SubscriberDescriptor, SubscriberDescriptorOptions, TopicDescriptor, TopicDescriptorOptions, TopicHandler, TopicMessage, TopicMessageSchema, TopicProvider, TopicTimeoutError, TopicWaitOptions, UnSubscribeFn }; //# sourceMappingURL=index.d.ts.map