import { makeGenericClientConstructor, credentials, CallOptions } from "grpc"; import { RPCImpl } from "protobufjs"; import { delivery } from "../protos/protos"; const ClientConstructor = makeGenericClientConstructor({}, "", {}); export type GrpcClientOptions = { timeout: number }; export const makeBaseClient = ( url: string, pathPrefix: string, options: GrpcClientOptions | null = null ): RPCImpl => { const client = new ClientConstructor(url, credentials.createInsecure()); return (method, req, callback) => { const callOptions: CallOptions = {}; if (options?.timeout) { callOptions.deadline = Date.now() + options.timeout; } // prefix is needed until https://github.com/protobufjs/protobuf.js/issues/1229 is resolved client.makeUnaryRequest( `${pathPrefix}${method.name}`, (arg) => arg as any, (arg) => arg, req, null, callOptions, callback ); }; }; export const createDeliveryCampaignsClient = (url: string): delivery.Campaigns => { return delivery.Campaigns.create(makeBaseClient(url, "/delivery.Campaigns/", { timeout: 100 })); };