@micro.ts/core
Version:
Microservice framework with Typescript
155 lines (154 loc) • 4.94 kB
TypeScript
import { Channel, Connection, ConsumeMessage, Options } from 'amqplib';
import { Action, BaseRouteDefinition, IAmqpExchangeConfig, QueueConsumeOptions } from '../../server/types';
import { AbstractBroker, ActionHandler, DefinitionHandlerPair } from '../AbstractBroker';
import { RequestMapper, RouteMapper } from '../IBroker';
import { AmqpClient, AmqpClientOptions } from './AmqpClient';
/**
* Configuration for amqp connection
*/
export declare type IAmqpConfig = string | Options.Connect;
/**
* Binding pair with the queue name and the binding pattern
*/
export declare type IAmqpBindingConfig = {
queue: string;
pattern: string;
};
/**
* Interface to pass into the AmqpClient to get a connection, and an existing channel if needed
*/
export interface IAmqpConnectionHooks {
/**
* Return an existing connection
*/
getConnection(): Connection;
/**
* Return an existing chanel
*/
getChannel(): Channel;
}
export interface TopicBasedAmqpConfig {
/**
* Connections string or connection options
*/
connection: IAmqpConfig;
/**
* Default topic exchange name
*/
topic: string;
}
export declare class AmqpBroker<T = IAmqpConfig> extends AbstractBroker<T> implements IAmqpConnectionHooks {
name: string;
protected connection: Connection;
protected channel: Channel;
private beforeAssertHooks;
/**
* Default exchange for all the queues
*/
private _defaultExchange;
addBeforeAssertHook(hook: () => Promise<void>): void;
private execBeforeAsssertHooks;
/**
* Getter for the default exchange
*/
get defaultExchange(): IAmqpExchangeConfig;
/**
* Setter for the default exchange
* @param value
*/
set defaultExchange(value: IAmqpExchangeConfig);
/**
* Bindings map created when adding queues
*/
private bindings;
private getPayload;
private convertPayload;
/**
* Map an amqp message on a given queue to Action object
* @param r
* @param queue
* @param routingKey
* @param json
*/
protected requestMapper: RequestMapper;
/**
* Default route mapping
*/
protected routeMapper: RouteMapper;
/**
* Creates an AMQP client which provides sendToQueue, publish, and rpc methods, to work with the AMQP server
* @param opts
*/
createClient(opts?: Partial<AmqpClientOptions>): Promise<AmqpClient>;
/**
* Return AMQP connection, available after the server is started
*/
getConnection(): Connection;
/**
* Returns the broker channel used to consume the app queues
*/
getChannel(): Channel;
/**
* Consume message from the asserted queue, find its corresponding handler, execute the handler,
* and if can reply, respond to the replyTo queue
* @param route
* @param message
* @param value
* @param json
*/
protected consumeMessage(route: string, message: ConsumeMessage | null, value: DefinitionHandlerPair[], json: boolean, options?: QueueConsumeOptions): Promise<void>;
/**
* Registered a single route definition and consumes messages from it
* Called when the server is started
* @param value
* @param route
*/
protected registerSingleRoute(value: DefinitionHandlerPair[], route: string): Promise<boolean>;
/**
* Add single route, get its exchanges config and save it in the binding map
* @param def
* @param handler
*/
addRoute(def: BaseRouteDefinition, handler: ActionHandler): string;
/**
* Assert every exchange
*/
private assertExchanges;
protected registerRoutes(): Promise<void>;
/**
* Reply if the message has replyTo queue and correlationId
* @param data
* @param replyToQueue
* @param correlationId
*/
protected rpcReply(data: Action, replyToQueue: string, correlationId: string, requestHeaders?: any): Promise<void>;
protected get connectionConfig(): IAmqpConfig;
/**
* Create connection and channel,
* assert exchanges and queues,
* create bindings,
* register consumers
*/
start(): Promise<void>;
/**
* Extract params for a given queue string
* @param queue
*/
protected extractQueueParamNames(queue: string): {
name: string;
param: boolean;
}[];
/**
* Get a queue pattern string from a given queue name,
* If exchange type is not topic return the queue name as a pattern,
* else replace all ':param' parts of the string with '#'
* @param queue
* @param type
*/
protected getQueuePattern(queue: string, type: 'topic' | 'direct' | 'fanout'): string;
/**
* Called immediately after broker configuration is set, either with a constructor configuration,
* or a config resolver from an IConfiguration instance
*/
protected construct(): void;
}