UNPKG

@nestjs/microservices

Version:

Microservices for NestJS: message and event handlers over Kafka, RabbitMQ, NATS, Redis, MQTT, gRPC or TCP.

92 lines (91 loc) • 5.07 kB
import { Observable, Observer, ReplaySubject } from 'rxjs'; import { ClientOptions, MsPattern, PacketId, ReadPacket, WritePacket } from '../interfaces/index.js'; import { ProducerDeserializer } from '../interfaces/deserializer.interface.js'; import { ProducerSerializer } from '../interfaces/serializer.interface.js'; /** * @publicApi */ export declare abstract class ClientProxy<EventsMap extends Record<never, Function> = Record<never, Function>, Status extends string = string> { protected onDispatchHook?: (packet: ReadPacket) => void; protected routingMap: Map<string, Function>; protected serializer: ProducerSerializer; protected deserializer: ProducerDeserializer; protected _status$: ReplaySubject<Status>; /** * Returns an observable that emits status changes. */ get status(): Observable<Status>; /** * Establishes the connection to the underlying server/broker. */ abstract connect(): Promise<any>; /** * Closes the underlying connection to the server/broker. */ abstract close(): any; /** * Registers an event listener for the given event. * @param event Event name * @param callback Callback to be executed when the event is emitted */ on<EventKey extends keyof EventsMap = keyof EventsMap, EventCallback extends EventsMap[EventKey] = EventsMap[EventKey]>(event: EventKey, callback: EventCallback): void; /** * Returns an instance of the underlying server/broker instance, * or a group of servers if there are more than one. */ abstract unwrap<T>(): T; /** * Send a message to the server/broker. * Used for message-driven communication style between microservices. * @param pattern Pattern to identify the message * @param data Data to be sent * @returns Observable with the result */ send<TResult = any, TInput = any>(pattern: any, data: TInput): Observable<TResult>; /** * Emits an event to the server/broker. * Used for event-driven communication style between microservices. * @param pattern Pattern to identify the event * @param data Data to be sent * @returns Observable that completes when the event is successfully emitted */ emit<TResult = any, TInput = any>(pattern: any, data: TInput): Observable<TResult>; /** * Sets a hook that is called with every outgoing packet - requests and * events alike - right before it is handed to the transport. The hook may * assign `packet.metadata`; it must not replace `pattern` or `data`. * * The client-side counterpart of `Server#setOnProcessingStartHook`: what one * side attaches here, the other reads from the RPC context, which is what * lets instrumentation carry a trace id across a hop without touching the * application's payloads. * * TCP and Redis carry the metadata inside the packet. NATS, Kafka, RabbitMQ, * MQTT 5 and gRPC carry it as headers, under the names the hook gave it, and * never in the payload. There, metadata only fills in what the message does * not set itself: a header set on the record (`NatsRecordBuilder`, * `RmqRecordBuilder`, `MqttRecordBuilder`, a Kafka message's `headers`, the * `Metadata` passed to a gRPC call) or in the client options (`headers`, * `userProperties`) wins over a metadata entry of the same name. * * An entry the transport cannot carry (a NATS header value with a line * break, a gRPC metadata key with a space) fails the call, as the same * header set on the record would. MQTT 3 has no header channel, so metadata * does not travel over it, and it is left off for a NATS server without * header support. */ setOnDispatchHook(hook: (packet: ReadPacket) => void): void; protected createPacket<TInput>(pattern: any, data: TInput): ReadPacket; protected abstract publish(packet: ReadPacket, callback: (packet: WritePacket) => void): () => void; protected abstract dispatchEvent<T = any>(packet: ReadPacket): Promise<T>; protected createObserver<T>(observer: Observer<T>): (packet: WritePacket) => void; protected serializeError(err: any): any; protected serializeResponse(response: any): any; protected assignPacketId(packet: ReadPacket): ReadPacket & PacketId; protected connect$(instance: any, errorEvent?: string, connectEvent?: string): Observable<any>; protected getOptionsProp<Options extends ClientOptions['options'], Attribute extends keyof Options>(obj: Options, prop: Attribute): Options[Attribute]; protected getOptionsProp<Options extends ClientOptions['options'], Attribute extends keyof Options, DefaultValue extends Options[Attribute] = Options[Attribute]>(obj: Options, prop: Attribute, defaultValue: DefaultValue): Required<Options>[Attribute]; protected normalizePattern(pattern: MsPattern): string; protected initializeSerializer(options: ClientOptions['options']): void; protected initializeDeserializer(options: ClientOptions['options']): void; }