ts-proto 2.20.0 → 2.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.markdown +3 -1
- package/build/src/generate-nestjs.js +13 -5
- package/build/src/main.js +6 -2
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -481,7 +481,9 @@ With `--ts_proto_opt=protoJsonFormat=false`, strict proto3 compliance is disable
|
|
|
481
481
|
|
|
482
482
|
- With `--ts_proto_opt=nestJs=true`, the defaults will change to generate [NestJS protobuf](https://docs.nestjs.com/microservices/grpc) friendly types & service interfaces that can be used in both the client-side and server-side of NestJS protobuf implementations. See the [nestjs readme](NESTJS.markdown) for more information and implementation examples.
|
|
483
483
|
|
|
484
|
-
Specifically `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` will all be false, `lowerCaseServiceMethods` will be true and `outputServices` will be ignored.
|
|
484
|
+
Specifically `outputEncodeMethods`, `outputJsonMethods`, `outputPartialMethods`, and `outputClientImpl` will all be false, `lowerCaseServiceMethods` will be true and `outputServices` will be ignored.
|
|
485
|
+
|
|
486
|
+
If `outputPartialMethods=true` is set as well, the `FooServiceClient` methods will accept `DeepPartial` requests and the `FooServiceController` methods may return `DeepPartial` responses, since any missing fields are received as their default values.
|
|
485
487
|
|
|
486
488
|
Note that `addGrpcMetadata`, `addNestjsRestParameter`, `returnObservable` and `useNestjsFqnServiceName` will still be false.
|
|
487
489
|
|
|
@@ -34,20 +34,26 @@ function generateNestjsServiceController(ctx, fileDesc, sourceInfo, serviceDesc)
|
|
|
34
34
|
if (options.addNestjsRestParameter) {
|
|
35
35
|
params.push((0, ts_poet_1.code) `...rest: any`);
|
|
36
36
|
}
|
|
37
|
+
// NestJS encodes the response with @grpc/proto-loader, which leaves out any missing fields,
|
|
38
|
+
// i.e. the client receives them as their default values, so it's okay to accept partial responses
|
|
39
|
+
let outputType = (0, types_1.responseType)(ctx, methodDesc);
|
|
40
|
+
if (options.outputPartialMethods) {
|
|
41
|
+
outputType = (0, ts_poet_1.code) `${ctx.utils.DeepPartial}<${outputType}>`;
|
|
42
|
+
}
|
|
37
43
|
// Return observable for interface only configuration, passing returnObservable=true and methodDesc.serverStreaming=true
|
|
38
44
|
let returns;
|
|
39
45
|
if ((0, types_1.isEmptyType)(methodDesc.outputType)) {
|
|
40
46
|
returns = (0, ts_poet_1.code) `void | Promise<void>`;
|
|
41
47
|
}
|
|
42
48
|
else if (options.returnObservable || methodDesc.serverStreaming) {
|
|
43
|
-
returns = (0, ts_poet_1.code) `${(0, types_1.
|
|
49
|
+
returns = (0, ts_poet_1.code) `${(0, types_1.observableType)(ctx)}<${outputType}>`;
|
|
44
50
|
}
|
|
45
51
|
else {
|
|
46
52
|
// generate nestjs union type
|
|
47
53
|
returns = (0, ts_poet_1.code) `
|
|
48
|
-
|
|
49
|
-
| ${(0, types_1.
|
|
50
|
-
| ${
|
|
54
|
+
Promise<${outputType}>
|
|
55
|
+
| ${(0, types_1.observableType)(ctx)}<${outputType}>
|
|
56
|
+
| ${outputType}
|
|
51
57
|
`;
|
|
52
58
|
}
|
|
53
59
|
chunks.push((0, ts_poet_1.code) `
|
|
@@ -84,7 +90,9 @@ function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
84
90
|
if (options.context) {
|
|
85
91
|
params.push((0, ts_poet_1.code) `ctx: Context`);
|
|
86
92
|
}
|
|
87
|
-
|
|
93
|
+
// NestJS encodes the request with @grpc/proto-loader, which leaves out any missing fields,
|
|
94
|
+
// i.e. the server receives them as their default values, so it's okay to accept partial requests
|
|
95
|
+
params.push((0, ts_poet_1.code) `request: ${(0, types_1.requestType)(ctx, methodDesc, options.outputPartialMethods)}`);
|
|
88
96
|
// Use metadata as last argument for interface only configuration
|
|
89
97
|
if (options.addGrpcMetadata) {
|
|
90
98
|
const q = options.addNestjsRestParameter ? "" : "?";
|
package/build/src/main.js
CHANGED
|
@@ -1932,7 +1932,11 @@ function valueTypeFromJson(ctx, field, from) {
|
|
|
1932
1932
|
return (0, ts_poet_1.code) `BigInt(${from})`;
|
|
1933
1933
|
}
|
|
1934
1934
|
else if ((0, types_1.isBytesValueType)(field)) {
|
|
1935
|
-
|
|
1935
|
+
// BytesValue is a base64 string in JSON, like a plain `bytes` field.
|
|
1936
|
+
if (options.env === options_1.EnvOption.NODE) {
|
|
1937
|
+
return (0, ts_poet_1.code) `Buffer.from(${ctx.utils.bytesFromBase64}(${from}))`;
|
|
1938
|
+
}
|
|
1939
|
+
return (0, ts_poet_1.code) `${ctx.utils.bytesFromBase64}(${from})`;
|
|
1936
1940
|
}
|
|
1937
1941
|
else {
|
|
1938
1942
|
return (0, ts_poet_1.code) `${(0, case_1.capitalize)(valueType.toCodeString([]))}(${from})`;
|
|
@@ -2555,7 +2559,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
2555
2559
|
else if ((0, types_1.isMessage)(field) && !(0, types_1.isValueType)(ctx, field) && !(0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
2556
2560
|
return (0, ts_poet_1.code) `${(0, types_1.getMessageMethod)(ctx, field.typeName, "toJSON")}(${from})`;
|
|
2557
2561
|
}
|
|
2558
|
-
else if ((0, types_1.isBytes)(field)) {
|
|
2562
|
+
else if ((0, types_1.isBytes)(field) || (0, types_1.isBytesValueType)(field)) {
|
|
2559
2563
|
return (0, ts_poet_1.code) `${utils.base64FromBytes}(${from})`;
|
|
2560
2564
|
}
|
|
2561
2565
|
else if (isLongValueTypeAsObject(options, field)) {
|