Skip to content

Commit

Permalink
chore: make create client dynamic (#2368)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Oct 10, 2022
1 parent 88b720b commit 3b3c05b
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions packages/grpc/src/comsumer/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,41 @@ export class GRPCClients extends Map {
return;
}
for (const cfg of this.grpcConfig['services']) {
const packageDefinition = await loadProto({
loaderOptions: cfg.loaderOptions,
protoPath: cfg.protoPath,
});
const allProto = loadPackageDefinition(packageDefinition);
const packageProto: any = finePackageProto(allProto, cfg.package);
for (const definition in packageDefinition) {
if (!packageDefinition[definition]['format']) {
const serviceName = definition.replace(`${cfg.package}.`, '');
const connectionService = new packageProto[serviceName](
cfg.url,
credentials.createInsecure(),
cfg.clientOptions
);
for (const methodName of Object.keys(packageDefinition[definition])) {
const originMethod = connectionService[methodName];
connectionService[methodName] = (
clientOptions: IClientOptions = {}
) => {
return this.getClientRequestImpl(
connectionService,
originMethod,
clientOptions
);
};
connectionService[Utils.camelCase(methodName)] =
connectionService[methodName];
}
this.set(definition, connectionService);
await this.createClient(cfg);
}
}

async createClient<T>(options: IGRPCClientServiceOptions): Promise<T> {
const packageDefinition = await loadProto({
loaderOptions: options.loaderOptions,
protoPath: options.protoPath,
});
const allProto = loadPackageDefinition(packageDefinition);
const packageProto: any = finePackageProto(allProto, options.package);
for (const definition in packageDefinition) {
if (!packageDefinition[definition]['format']) {
const serviceName = definition.replace(`${options.package}.`, '');
const connectionService = new packageProto[serviceName](
options.url,
credentials.createInsecure(),
options.clientOptions
);
for (const methodName of Object.keys(packageDefinition[definition])) {
const originMethod = connectionService[methodName];
connectionService[methodName] = (
clientOptions: IClientOptions = {}
) => {
return this.getClientRequestImpl(
connectionService,
originMethod,
clientOptions
);
};
connectionService[Utils.camelCase(methodName)] =
connectionService[methodName];
}
this.set(definition, connectionService);
return connectionService;
}
}
}
Expand Down

0 comments on commit 3b3c05b

Please sign in to comment.