Skip to content

Commit

Permalink
fix: return grpc-web response without toObject method (#728)
Browse files Browse the repository at this point in the history
fix #636
  • Loading branch information
relaxgo authored Dec 12, 2022
1 parent 6c3823b commit 7431aa8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
5 changes: 3 additions & 2 deletions integration/grpc-web-no-streaming-observable/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
},
Expand Down
7 changes: 4 additions & 3 deletions integration/grpc-web-no-streaming/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
},
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 16 additions & 11 deletions integration/grpc-web/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
},
Expand All @@ -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;
},
};
},
Expand Down Expand Up @@ -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;
},
};
},
Expand All @@ -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;
},
};
},
Expand All @@ -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;
},
};
},
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/generate-grpc-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; },
};
}
}`;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7431aa8

Please sign in to comment.