UNPKG

@plexus-interop/cli

Version:

Plexus Interop CLI tools

154 lines (121 loc) 6.48 kB
import { InternalGenericClientApi, ClientApiBuilder, MethodInvocationContext, Completion, ClientConnectRequest, StreamingInvocationClient, GenericClientApi, InvocationRequestInfo, InvocationClient, GenericRequest, GenericClientApiBase } from '@plexus-interop/client'; import { ProvidedMethodReference, ServiceDiscoveryRequest, ServiceDiscoveryResponse, MethodDiscoveryRequest, MethodDiscoveryResponse, GenericClientApiBuilder, ValueHandler } from '@plexus-interop/client'; import { TransportConnection, UniqueId } from '@plexus-interop/transport-common'; import { Arrays, Observer } from '@plexus-interop/common'; import { InvocationObserver, InvocationObserverConverter, ContainerAwareClientAPIBuilder } from '@plexus-interop/client'; import * as plexus from './plexus-messages'; /** * Proxy interface of GreetingService service, to be consumed by Client API */ export abstract class GreetingServiceProxy { public abstract unary(request: plexus.interop.samples.IGreetingRequest): Promise<plexus.interop.samples.IGreetingResponse>; public abstract serverStreaming(request: plexus.interop.samples.IGreetingRequest, responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<InvocationClient>; public abstract clientStreaming(responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<StreamingInvocationClient<plexus.interop.samples.IGreetingRequest>>; public abstract duplexStreaming(responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<StreamingInvocationClient<plexus.interop.samples.IGreetingRequest>>; } /** * Internal Proxy implementation for GreetingService service */ export class GreetingServiceProxyImpl implements GreetingServiceProxy { constructor(private readonly genericClient: GenericClientApi) { } public unary(request: plexus.interop.samples.IGreetingRequest): Promise<plexus.interop.samples.IGreetingResponse> { const invocationInfo: InvocationRequestInfo = { methodId: 'Unary', serviceId: 'interop.samples.GreetingService' }; return new Promise((resolve, reject) => { this.genericClient.sendUnaryRequest(invocationInfo, request, { value: responsePayload => resolve(responsePayload), error: e => reject(e) }, plexus.interop.samples.GreetingRequest, plexus.interop.samples.GreetingResponse); }); } public serverStreaming(request: plexus.interop.samples.IGreetingRequest, responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<InvocationClient> { const invocationInfo: InvocationRequestInfo = { methodId: 'ServerStreaming', serviceId: 'interop.samples.GreetingService' }; return this.genericClient.sendServerStreamingRequest( invocationInfo, request, responseObserver, plexus.interop.samples.GreetingRequest, plexus.interop.samples.GreetingResponse ); } public clientStreaming(responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<StreamingInvocationClient<plexus.interop.samples.IGreetingRequest>> { const invocationInfo: InvocationRequestInfo = { methodId: 'ClientStreaming', serviceId: 'interop.samples.GreetingService' }; return this.genericClient.sendBidirectionalStreamingRequest( invocationInfo, responseObserver, plexus.interop.samples.GreetingRequest, plexus.interop.samples.GreetingResponse); } public duplexStreaming(responseObserver: InvocationObserver<plexus.interop.samples.IGreetingResponse>): Promise<StreamingInvocationClient<plexus.interop.samples.IGreetingRequest>> { const invocationInfo: InvocationRequestInfo = { methodId: 'DuplexStreaming', serviceId: 'interop.samples.GreetingService' }; return this.genericClient.sendBidirectionalStreamingRequest( invocationInfo, responseObserver, plexus.interop.samples.GreetingRequest, plexus.interop.samples.GreetingResponse); } } /** * Main client API */ export interface GreetingClientClient extends GenericClientApi { getGreetingServiceProxy(): GreetingServiceProxy; } /** * Client's API internal implementation */ class GreetingClientClientImpl extends GenericClientApiBase implements GreetingClientClient { public constructor( private readonly genericClient: GenericClientApi, private readonly greetingServiceProxy: GreetingServiceProxy ) { super(genericClient); } public getGreetingServiceProxy(): GreetingServiceProxy { return this.greetingServiceProxy; } } /** * Client API builder */ export class GreetingClientClientBuilder implements ClientApiBuilder<GreetingClientClient, GreetingClientClientBuilder> { protected genericBuilder: GenericClientApiBuilder = new ContainerAwareClientAPIBuilder() .withApplicationId('interop.samples.GreetingClient'); public withClientApiDecorator(clientApiDecorator: (client: InternalGenericClientApi) => Promise<GenericClientApi>): GreetingClientClientBuilder { this.genericBuilder.withClientApiDecorator(clientApiDecorator); return this; } public withClientExtension(extension: (builder: ClientApiBuilder<GreetingClientClient, GreetingClientClientBuilder>) => void): GreetingClientClientBuilder { extension(this); return this; } public withTransportConnectionProvider(provider: () => Promise<TransportConnection>): GreetingClientClientBuilder { this.genericBuilder.withTransportConnectionProvider(provider); return this; } public withAppInstanceId(appInstanceId: UniqueId): GreetingClientClientBuilder { this.genericBuilder.withAppInstanceId(appInstanceId); return this; } public withAppId(appId: string): GreetingClientClientBuilder { this.genericBuilder.withApplicationId(appId); return this; } public connect(): Promise<GreetingClientClient> { return this.genericBuilder .connect() .then(genericClient => new GreetingClientClientImpl( genericClient, new GreetingServiceProxyImpl(genericClient) )); } }