Skip to content

Commit

Permalink
fix addGrpcMetadata for generic RPC client
Browse files Browse the repository at this point in the history
  • Loading branch information
vojty committed Jan 19, 2023
1 parent 2be03eb commit 98e720e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/generate-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,11 @@ export function generateService(
params.push(code`metadata?: grpc.Metadata`);
} else if (options.addGrpcMetadata) {
const Metadata = imp("Metadata@@grpc/grpc-js");
const q = options.addNestjsRestParameter ? "" : "?";
params.push(code`metadata${q}: ${Metadata}`);
params.push(code`metadata?: ${Metadata}`);
} else if (options.metadataType) {
const Metadata = imp(options.metadataType);
params.push(code`metadata?: ${Metadata}`);
}
if (options.addNestjsRestParameter) {
params.push(code`...rest: any`);
}

chunks.push(
code`${methodDesc.formattedName}(${joinCode(params, { on: "," })}): ${responsePromiseOrObservable(
Expand Down Expand Up @@ -117,9 +113,11 @@ function generateRegularRpcMethod(ctx: Context, methodDesc: MethodDescriptorProt
const params = [
...(options.context ? [code`ctx: Context`] : []),
code`request: ${inputType}`,
...(options.addGrpcMetadata ? [code`metadata?: Metadata`] : []),
...(options.useAbortSignal ? [code`abortSignal?: AbortSignal`] : []),
];
const maybeCtx = options.context ? "ctx," : "";
const maybeMetadata = options.addGrpcMetadata ? "metadata," : "";
const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";

let encode = code`${rawInputType}.encode(request).finish()`;
Expand Down Expand Up @@ -169,6 +167,7 @@ function generateRegularRpcMethod(ctx: Context, methodDesc: MethodDescriptorProt
this.service,
"${methodDesc.name}",
data,
${maybeMetadata}
${maybeAbortSignal}
);
return ${decode};
Expand Down Expand Up @@ -335,6 +334,7 @@ export function generateRpcType(ctx: Context, hasStreamingMethods: boolean): Cod
const { options } = ctx;
const maybeContext = options.context ? "<Context>" : "";
const maybeContextParam = options.context ? "ctx: Context," : "";
const maybeMetadataParam = options.addGrpcMetadata ? `metadata?: Metadata,` : "";
const maybeAbortSignalParam = options.useAbortSignal ? "abortSignal?: AbortSignal," : "";
const methods = [[code`request`, code`Uint8Array`, code`Promise<Uint8Array>`]];
if (hasStreamingMethods) {
Expand All @@ -356,6 +356,7 @@ export function generateRpcType(ctx: Context, hasStreamingMethods: boolean): Cod
service: string,
method: string,
data: ${method[1]},
${maybeMetadataParam}
${maybeAbortSignalParam}
): ${method[2]};`);
});
Expand Down

0 comments on commit 98e720e

Please sign in to comment.