import { ServiceDefinition, MethodDefinition, ServerUnaryCall, ServerReadableStream, ServerWritableStream, ServerDuplexStream, status, Metadata, ServerCredentials, Server, } from "@grpc/grpc-js"; import { Observable } from "rxjs"; export class GrpcHostBuilder { /** * @param options gRPC native options https://grpc.io/grpc/cpp/group__grpc__arg__keys.html */ constructor(options?: object); /** * Changes default loggers factory * @param createLogger Factory method for loggers creation. */ useLoggersFactory(loggersFactory: (options?: object) => Logging.ILogger): GrpcHostBuilder; /** * Adds new interceptor to pipeline. * @param interceptor New interceptor. * @param interceptorArguments Interceptor additional arguments. */ addInterceptor( /** * @param call Server call. * @param methodDefinition Metadata for method implementation. * @param next Next layers executor. * @param arguments Interceptor additional arguments. */ interceptor: ( call: ServiceCall, methodDefinition: MethodDefinition, next: handleServiceCall, ...arguments: any[] ) => Promise, ...interceptorArguments: any[] ): GrpcHostBuilder; /** * Adds new interceptor to pipeline. * @param interceptor Constructor for new interceptor. * @param interceptorArguments Interceptor additional arguments. */ addInterceptor( interceporConstructor: new (serverContext: ServerContext, ...arguments: any[]) => IInterceptor, ...interceptorArguments: any[] ): GrpcHostBuilder; /** * Adds implementation of a new service. * @param definition Definition of the service. * @param implementation Implementation of the service. */ addService( definition: ServiceDefinition, implementation: ImplementationType ): GrpcHostBuilder; /** * Binds server to endpoint. * @param grpcBind Bind for gRPC server in format "address:port". * @param [credentials = ServerCredentials.createInsecure()] Server credentials */ bind(grpcBind: string, credentials?: ServerCredentials): GrpcHostBuilder; /** * Builds the server. */ buildAsync(): Promise; } type ServerContext = { createLogger: (options?: object) => Logging.ILogger; }; type ServiceCall = | ServerUnaryCall | ServerReadableStream | ServerWritableStream | ServerDuplexStream; type handleServiceCall = | handleUnaryCall | handleClientStreamingCall | handleServerStreamingCall | handleBidiStreamingCall; type handleUnaryCall = (call: ServerUnaryCall) => Promise; // prettier-ignore type handleClientStreamingCall = (call: ServerReadableStream) => Promise; // prettier-ignore type handleServerStreamingCall = (call: ServerWritableStream) => Promise; // prettier-ignore type handleBidiStreamingCall = (call: ServerDuplexStream) => Promise; // prettier-ignore /** * Used for calls that are streaming from the client side. */ export interface ServerIngoingStreamingCall { /** * Indicates if the call has been cancelled */ cancelled: boolean; /** * The request metadata from the client */ metadata: Metadata; /** * Client streaming data */ source: Observable; /** * Get the endpoint this call/stream is connected to. * @return The URI of the endpoint */ getPeer(): string; /** * Send the initial metadata for a writable stream. * @param responseMetadata Metadata to send */ sendMetadata(responseMetadata: Metadata): void; } /** * Used for calls that are streaming from the server side. */ export interface ServerOutgoingStreamingCall { /** * Indicates if the call has been cancelled */ cancelled: boolean; /** * The request metadata from the client */ metadata: Metadata; /** * The request message from the client */ request: RequestType; /** * Get the endpoint this call/stream is connected to. * @return The URI of the endpoint */ getPeer(): string; /** * Send the initial metadata for a writable stream. * @param responseMetadata Metadata to send */ sendMetadata(responseMetadata: Metadata): void; } /** * Used for calls that are bidirectional streaming. */ export interface ServerBidiStreamingCall { /** * Indicates if the call has been cancelled */ cancelled: boolean; /** * The request metadata from the client */ metadata: Metadata; /** * Client streaming data */ source: Observable; /** * Get the endpoint this call/stream is connected to. * @return The URI of the endpoint */ getPeer(): string; /** * Send the initial metadata for a writable stream. * @param responseMetadata Metadata to send */ sendMetadata(responseMetadata: Metadata): void; } type serviceMethodImplementation = | serviceUnaryMethodImplementation | serviceClientStreamingMethodImplementation | serviceServerStreamingMethodImplementation | serviceBidiStreamingMethodImplementation; type serviceUnaryMethodImplementation = (call: ServerUnaryCall) => Promise; // prettier-ignore type serviceClientStreamingMethodImplementation = (call: ServerIngoingStreamingCall) => Promise; // prettier-ignore type serviceServerStreamingMethodImplementation = (call: ServerOutgoingStreamingCall) => Promise>; // prettier-ignore type serviceBidiStreamingMethodImplementation = (call: ServerBidiStreamingCall) => Promise>; // prettier-ignore type UntypedServiceImplementation = { [name: string]: serviceMethodImplementation }; export interface IInterceptor { /** * Interceptor implementation. * @param call Server call. * @param methodDefinition Metadata for method implementation. * @param next Next layers executor. */ invoke( call: ServiceCall, methodDefinition: MethodDefinition, next: handleServiceCall ): Promise; } export namespace Logging { export interface ILogger { error(message: string, payload?: object): void; warn(message: string, payload?: object): void; info(message: string, payload?: object): void; verbose(message: string, payload?: object): void; debug(message: string, payload?: object): void; silly(message: string, payload?: object): void; } }