From 7431aa8e5718ad8a8fe48651798f203995bf705b Mon Sep 17 00:00:00 2001 From: Jiajun Zhang Date: Sun, 11 Dec 2022 20:37:37 -0500 Subject: [PATCH] fix: return grpc-web response without toObject method (#728) fix stephenh/ts-proto#636 --- .../example.ts | 5 ++-- integration/grpc-web-no-streaming/example.ts | 7 ++--- integration/grpc-web/example.ts | 27 +++++++++++-------- src/generate-grpc-web.ts | 8 ++++-- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/integration/grpc-web-no-streaming-observable/example.ts b/integration/grpc-web-no-streaming-observable/example.ts index ea9642f05..1bfdb4221 100644 --- a/integration/grpc-web-no-streaming-observable/example.ts +++ b/integration/grpc-web-no-streaming-observable/example.ts @@ -335,10 +335,11 @@ export const DashStateUserSettingsDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashUserSettingsState.decode(data); return { - ...DashUserSettingsState.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, diff --git a/integration/grpc-web-no-streaming/example.ts b/integration/grpc-web-no-streaming/example.ts index 834263b73..17bc4bab3 100644 --- a/integration/grpc-web-no-streaming/example.ts +++ b/integration/grpc-web-no-streaming/example.ts @@ -333,10 +333,11 @@ export const DashStateUserSettingsDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashUserSettingsState.decode(data); return { - ...DashUserSettingsState.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -400,7 +401,7 @@ export class GrpcWebImpl { debug: this.options.debug, onEnd: function (response) { if (response.status === grpc.Code.OK) { - resolve(response.message); + resolve(response.message!.toObject()); } else { const err = new GrpcWebError(response.statusMessage, response.status, response.trailers); reject(err); diff --git a/integration/grpc-web/example.ts b/integration/grpc-web/example.ts index 75c03351e..ae32731ea 100644 --- a/integration/grpc-web/example.ts +++ b/integration/grpc-web/example.ts @@ -642,10 +642,11 @@ export const DashStateUserSettingsDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashUserSettingsState.decode(data); return { - ...DashUserSettingsState.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -664,10 +665,11 @@ export const DashStateActiveUserSettingsStreamDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashUserSettingsState.decode(data); return { - ...DashUserSettingsState.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -722,10 +724,11 @@ export const DashAPICredsCreateDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashCred.decode(data); return { - ...DashCred.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -744,10 +747,11 @@ export const DashAPICredsUpdateDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashCred.decode(data); return { - ...DashCred.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -766,10 +770,11 @@ export const DashAPICredsDeleteDesc: UnaryMethodDefinitionish = { } as any, responseType: { deserializeBinary(data: Uint8Array) { + const value = DashCred.decode(data); return { - ...DashCred.decode(data), + ...value, toObject() { - return this; + return value; }, }; }, @@ -838,7 +843,7 @@ export class GrpcWebImpl { debug: this.options.debug, onEnd: function (response) { if (response.status === grpc.Code.OK) { - resolve(response.message); + resolve(response.message!.toObject()); } else { const err = new GrpcWebError(response.statusMessage, response.status, response.trailers); reject(err); diff --git a/src/generate-grpc-web.ts b/src/generate-grpc-web.ts index 4edfd29bc..dda41b3eb 100644 --- a/src/generate-grpc-web.ts +++ b/src/generate-grpc-web.ts @@ -125,7 +125,11 @@ export function generateGrpcMethodDesc( // we want/what grpc-web's runtime needs. const responseFn = code`{ deserializeBinary(data: Uint8Array) { - return { ...${outputType}.decode(data), toObject() { return this; } }; + const value = ${outputType}.decode(data); + return { + ...value, + toObject() { return value; }, + }; } }`; @@ -246,7 +250,7 @@ function createPromiseUnaryMethod(ctx: Context): Code { debug: this.options.debug, onEnd: function (response) { if (response.status === grpc.Code.OK) { - resolve(response.message); + resolve(response.message!.toObject()); } else { const err = new ${ctx.utils.GrpcWebError}(response.statusMessage, response.status, response.trailers); reject(err);