import { CommandContext, CommandReply } from './command'; import { GrpcClientLookup } from './grpc-util'; import { Metadata } from './metadata'; import { Reply } from './reply'; import { Component, ComponentOptions, PreStartSettings } from './kalix'; /** * An Action. * * @public */ export declare class Action implements Component { readonly serviceName: string; readonly service: protobuf.Service; readonly options: Required; clients?: GrpcClientLookup; /** * The command handlers. * * @remarks * The names of the properties must match the names of the service calls specified in the gRPC descriptor. */ commandHandlers: CommandHandlers; /** * Create a new action. * * @param desc - A descriptor or list of descriptors to parse, containing the service to serve * @param serviceName - The fully qualified name of the service that provides this interface * @param options - The options for this action */ constructor(desc: string | string[], serviceName: string, options?: Action.Options); preStart(settings: PreStartSettings): void; /** * Lookup a protobuf message type. * * This is provided as a convenience to lookup protobuf message types for use with events and snapshots. * * @param messageType - The fully qualified name of the type to lookup * @returns The protobuf message type */ lookupType(messageType: string): protobuf.Type; /** * Set the command handlers for this action. * * @param commandHandlers - The command handlers * @returns This action */ setCommandHandlers(commandHandlers: CommandHandlers): Action; } /** * @public */ export declare namespace Action { interface ActionContext { /** * Whether the client is still connected. */ readonly cancelled: boolean; /** * The metadata associated with the command. */ readonly metadata: Metadata; /** * Register an event handler. * * @param eventType - The type of the event * @param callback - The callback to handle the event * @returns nothing or a Reply for {@link StreamedInContext.end} events */ on(eventType: string, callback: (...args: any[]) => Reply | void): void; } /** * Context for an action command. * * @typeParam Response - The type of the response message */ interface ActionCommandContext extends ActionContext, CommandContext { /** * Write a message. * * @param message - The protobuf message to write * @param metadata - The metadata associated with the message */ write: (message: Response, metadata?: Metadata) => void; } /** * Context for a unary action command. * * @typeParam Response - The type of the response message */ interface UnaryCommandContext extends ActionCommandContext { } /** * Context for an action command that handles streamed messages in. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message */ interface StreamedInContext extends ActionCommandContext { /** * Cancel the incoming stream of messages. */ cancel: () => void; /** * Register an event handler for {@link StreamedInContext.data} events. * * @param eventType - 'data' * @param callback - the callback for each new message * @see {@link StreamedInContext.data} */ on(eventType: 'data', callback: (message: Request) => void): void; /** * Register an event handler for {@link StreamedInContext.end} events. * * @param eventType - 'end' * @param callback - the callback for when the input stream ends * @returns nothing or a Reply that is returned as the response from the action * @see {@link StreamedInContext.end} */ on(eventType: 'end', callback: () => Reply | void): void; } namespace StreamedInContext { /** * A data event. * * Emitted when a new message arrives. * * @eventProperty */ const data = "data"; /** * A stream end event. * * Emitted when the input stream terminates. * * @remarks * If a callback is registered and that returns a Reply, then that is returned as a response from the action. * * @eventProperty */ const end = "end"; } /** * Context for a streamed in action command. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message */ interface StreamedInCommandContext extends StreamedInContext { } /** * Context for an action command that returns a streamed message out. * * @typeParam Response - The type of the response message */ interface StreamedOutContext extends ActionCommandContext { /** * Send a reply * * @param reply - The reply to send */ reply: (reply: Reply) => void; /** * Terminate the outgoing stream of messages. */ end: () => void; /** * Register an event handler for {@link StreamedOutContext.cancelled} events. * * @param eventType - 'cancelled' * @param callback - the callback for when the stream is cancelled * @see {@link StreamedInContext.cancelled} */ on(eventType: 'cancelled', callback: () => void): void; } namespace StreamedOutContext { /** * A cancelled event. * * @eventProperty */ const cancelled = "cancelled"; } /** * Context for a streamed out action command. * * @typeParam Response - The type of the response message */ interface StreamedOutCommandContext extends StreamedOutContext { } /** * Context for a streamed action command. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message */ interface StreamedCommandContext extends StreamedInContext, StreamedOutContext { /** * Register an event handler for {@link StreamedInContext.data} events. * * @param eventType - 'data' * @param callback - the callback for each new message * @see {@link StreamedInContext.data} */ on(eventType: 'data', callback: (message: Request) => void): void; /** * Register an event handler for {@link StreamedInContext.end} events. * * @param eventType - 'end' * @param callback - the callback for when the input stream ends * @returns nothing or a Reply that is returned as the response from the action * @see {@link StreamedInContext.end} */ on(eventType: 'end', callback: () => Reply | void): void; /** * Register an event handler for {@link StreamedOutContext.cancelled} events. * * @param eventType - 'cancelled' * @param callback - the callback for when the stream is cancelled * @see {@link StreamedInContext.cancelled} */ on(eventType: 'cancelled', callback: () => void): void; } /** * The command handlers. * * @remarks * The names of the properties must match the names of the service calls specified in the gRPC descriptor. */ type CommandHandlers = { [commandName: string]: CommandHandler; }; /** * An action command handler. */ type CommandHandler = UnaryCommandHandler | StreamedInCommandHandler | StreamedOutCommandHandler | StreamedCommandHandler; /** * A unary action command handler. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message * @param message - The command message, this will be of the type of the gRPC service call input type * @param context - The command context * @returns The message to reply with, it must match the gRPC service call output type for * this command. If replying by using context.write, undefined must be returned. */ type UnaryCommandHandler = (message: Request, context: UnaryCommandContext) => Promise> | CommandReply; /** * A streamed in action command handler. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message * @param context - The command context * @returns The message to reply with, it must match the gRPC service call output type for * this command. If replying by using context.write, undefined must be returned. */ type StreamedInCommandHandler = (context: StreamedInCommandContext) => Promise> | CommandReply; /** * A streamed out command handler. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message * @param message - The command message, this will be of the type of the gRPC service call input type * @param context - The command context */ type StreamedOutCommandHandler = (message: Request, context: StreamedOutCommandContext) => void; /** * A streamed command handler. * * @typeParam Request - The type of the request message * @typeParam Response - The type of the response message * @param context - The command context */ type StreamedCommandHandler = (context: StreamedCommandContext) => void; interface Options extends Omit { } }