import { CallOptions, ClientMiddleware, composeClientMiddleware, } from 'nice-grpc-common'; import { AnyMethodDefinition, CompatServiceDefinition, NormalizedServiceDefinition, normalizeServiceDefinition, ServiceDefinition, } from '../service-definitions'; import {Channel} from './channel'; import {Client} from './Client'; import {createBidiStreamingMethod} from './createBidiStreamingMethod'; import {createClientStreamingMethod} from './createClientStreamingMethod'; import {createServerStreamingMethod} from './createServerStreamingMethod'; import {createUnaryMethod} from './createUnaryMethod'; export type ClientFactory = { /** * Attach a middleware to the client factory. * * This method returns a new client factory with the middleware attached. * * A middleware that is attached first, will be invoked last. * * @param middleware The middleware to attach. * @returns A new client factory with the middleware attached. */ use( middleware: ClientMiddleware, ): ClientFactory; /** * Create a client using the client factory. * * @param definition The service definition. * @param channel The channel to use. * @param defaultCallOptions Default call options per method, or `'*'` for all * methods. */ create( definition: Service, channel: Channel, defaultCallOptions?: DefaultCallOptions< NormalizedServiceDefinition, CallOptionsExt >, ): Client; }; export type DefaultCallOptions< Service extends ServiceDefinition, CallOptionsExt = {}, > = { [K in keyof Service | '*']?: CallOptions & Partial; }; /** * Create a client factory that can be used to create clients with middleware. */ export function createClientFactory(): ClientFactory { return createClientFactoryWithMiddleware(); } /** * Create a client with no middleware. * * This is the same as calling `createClientFactory().create()`. */ export function createClient( definition: Service, channel: Channel, defaultCallOptions?: DefaultCallOptions>, ): Client { return createClientFactory().create(definition, channel, defaultCallOptions); } function createClientFactoryWithMiddleware( middleware?: ClientMiddleware, ): ClientFactory { return { use(newMiddleware: ClientMiddleware) { return createClientFactoryWithMiddleware( middleware == null ? (newMiddleware as ClientMiddleware) : composeClientMiddleware(middleware, newMiddleware), ); }, create( definition: Service, channel: Channel, defaultCallOptions: DefaultCallOptions< NormalizedServiceDefinition, CallOptionsExt > = {}, ) { type NormalizedService = NormalizedServiceDefinition; const client = {} as { [K in keyof NormalizedService]: Function; }; const methodEntries = Object.entries( normalizeServiceDefinition(definition), ) as Array<[keyof NormalizedService, AnyMethodDefinition]>; for (const [methodName, methodDefinition] of methodEntries) { const defaultOptions = { ...defaultCallOptions['*'], ...defaultCallOptions[methodName], } as CallOptions; if (!methodDefinition.requestStream) { if (!methodDefinition.responseStream) { client[methodName] = createUnaryMethod( methodDefinition, channel, middleware, defaultOptions, ); } else { client[methodName] = createServerStreamingMethod( methodDefinition, channel, middleware, defaultOptions, ); } } else { if (!methodDefinition.responseStream) { client[methodName] = createClientStreamingMethod( methodDefinition, channel, middleware, defaultOptions, ); } else { client[methodName] = createBidiStreamingMethod( methodDefinition, channel, middleware, defaultOptions, ); } } } return client as Client; }, }; }