ts-proto 1.83.0 → 1.84.0
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/CHANGELOG.md +28 -0
- package/build/generate-nestjs.js +2 -3
- package/build/generate-services.js +1 -1
- package/build/main.js +43 -21
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
# [1.84.0](https://github.com/stephenh/ts-proto/compare/v1.83.3...v1.84.0) (2021-11-02)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* Reduce code size by using nullish coalescing operator in fromPartial ([#376](https://github.com/stephenh/ts-proto/issues/376)) ([19d2ded](https://github.com/stephenh/ts-proto/commit/19d2deda2cf7c47b1b56bfc65cf58653291dba4a))
|
|
7
|
+
|
|
8
|
+
## [1.83.3](https://github.com/stephenh/ts-proto/compare/v1.83.2...v1.83.3) (2021-10-28)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* fix codegen for maps with wrapper value type ([#370](https://github.com/stephenh/ts-proto/issues/370)) ([dd2481d](https://github.com/stephenh/ts-proto/commit/dd2481df0835faafc561aad4a4d0c9c2ff9d868a))
|
|
14
|
+
|
|
15
|
+
## [1.83.2](https://github.com/stephenh/ts-proto/compare/v1.83.1...v1.83.2) (2021-10-26)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* Add missing defaults to fromPartial if options.oneof is UNIONS ([#375](https://github.com/stephenh/ts-proto/issues/375)) ([21781e9](https://github.com/stephenh/ts-proto/commit/21781e98bb6117b540be8c3f2c38ac3ad5cbbb44))
|
|
21
|
+
|
|
22
|
+
## [1.83.1](https://github.com/stephenh/ts-proto/compare/v1.83.0...v1.83.1) (2021-09-17)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Bug Fixes
|
|
26
|
+
|
|
27
|
+
* deprecated grpc and replace with @grpc/grpc-js ([#362](https://github.com/stephenh/ts-proto/issues/362)) ([1a11b97](https://github.com/stephenh/ts-proto/commit/1a11b97e9b5e92e79fdd4e22d3e8ea4536af243b))
|
|
28
|
+
|
|
1
29
|
# [1.83.0](https://github.com/stephenh/ts-proto/compare/v1.82.5...v1.83.0) (2021-09-12)
|
|
2
30
|
|
|
3
31
|
|
package/build/generate-nestjs.js
CHANGED
|
@@ -6,12 +6,11 @@ const types_1 = require("./types");
|
|
|
6
6
|
const sourceInfo_1 = require("./sourceInfo");
|
|
7
7
|
const main_1 = require("./main");
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
|
-
const options_1 = require("./options");
|
|
10
9
|
function generateNestjsServiceController(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
11
10
|
var _a;
|
|
12
11
|
const { options } = ctx;
|
|
13
12
|
const chunks = [];
|
|
14
|
-
const Metadata =
|
|
13
|
+
const Metadata = ts_poet_1.imp('Metadata@@grpc/grpc-js');
|
|
15
14
|
utils_1.maybeAddComment(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
|
|
16
15
|
const t = options.context ? `<${main_1.contextTypeVar}>` : '';
|
|
17
16
|
chunks.push(ts_poet_1.code `
|
|
@@ -74,7 +73,7 @@ exports.generateNestjsServiceController = generateNestjsServiceController;
|
|
|
74
73
|
function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
75
74
|
const { options } = ctx;
|
|
76
75
|
const chunks = [];
|
|
77
|
-
const Metadata =
|
|
76
|
+
const Metadata = ts_poet_1.imp('Metadata@@grpc/grpc-js');
|
|
78
77
|
utils_1.maybeAddComment(sourceInfo, chunks);
|
|
79
78
|
const t = options.context ? `<${main_1.contextTypeVar}>` : ``;
|
|
80
79
|
chunks.push(ts_poet_1.code `
|
|
@@ -48,7 +48,7 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
48
48
|
params.push(ts_poet_1.code `metadata?: grpc.Metadata`);
|
|
49
49
|
}
|
|
50
50
|
else if (options.addGrpcMetadata) {
|
|
51
|
-
const Metadata = ts_poet_1.imp('Metadata
|
|
51
|
+
const Metadata = ts_poet_1.imp('Metadata@@grpc/grpc-js');
|
|
52
52
|
const q = options.addNestjsRestParameter ? '' : '?';
|
|
53
53
|
params.push(ts_poet_1.code `metadata${q}: ${Metadata}`);
|
|
54
54
|
}
|
package/build/main.js
CHANGED
|
@@ -632,10 +632,18 @@ function generateEncode(ctx, fullName, messageDesc) {
|
|
|
632
632
|
}
|
|
633
633
|
if (types_1.isRepeated(field)) {
|
|
634
634
|
if (types_1.isMapType(ctx, messageDesc, field)) {
|
|
635
|
+
const valueType = typeMap.get(field.typeName)[2].field[1];
|
|
635
636
|
const maybeTypeField = options.outputTypeRegistry ? `$type: '${field.typeName.slice(1)}',` : '';
|
|
637
|
+
const entryWriteSnippet = types_1.isValueType(ctx, valueType)
|
|
638
|
+
? ts_poet_1.code `
|
|
639
|
+
if (value !== undefined) {
|
|
640
|
+
${writeSnippet(`{ ${maybeTypeField} key: key as any, value }`)};
|
|
641
|
+
}
|
|
642
|
+
`
|
|
643
|
+
: writeSnippet(`{ ${maybeTypeField} key: key as any, value }`);
|
|
636
644
|
chunks.push(ts_poet_1.code `
|
|
637
645
|
Object.entries(message.${fieldName}).forEach(([key, value]) => {
|
|
638
|
-
${
|
|
646
|
+
${entryWriteSnippet}
|
|
639
647
|
});
|
|
640
648
|
`);
|
|
641
649
|
}
|
|
@@ -814,6 +822,10 @@ function generateFromJson(ctx, fullName, messageDesc) {
|
|
|
814
822
|
(options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.TIMESTAMP)) {
|
|
815
823
|
return ts_poet_1.code `${utils.fromJsonTimestamp}(${from})`;
|
|
816
824
|
}
|
|
825
|
+
else if (types_1.isValueType(ctx, valueType)) {
|
|
826
|
+
const type = types_1.basicTypeName(ctx, valueType);
|
|
827
|
+
return ts_poet_1.code `${from} as ${type}`;
|
|
828
|
+
}
|
|
817
829
|
else {
|
|
818
830
|
const type = types_1.basicTypeName(ctx, valueType);
|
|
819
831
|
return ts_poet_1.code `${type}.fromJSON(${from})`;
|
|
@@ -917,7 +929,7 @@ function generateToJson(ctx, fullName, messageDesc) {
|
|
|
917
929
|
else if (types_1.isTimestamp(valueType) && options.useDate === options_1.DateOption.TIMESTAMP) {
|
|
918
930
|
return ts_poet_1.code `${utils.fromTimestamp}(${from}).toISOString()`;
|
|
919
931
|
}
|
|
920
|
-
else if (types_1.isScalar(valueType)) {
|
|
932
|
+
else if (types_1.isScalar(valueType) || types_1.isValueType(ctx, valueType)) {
|
|
921
933
|
return ts_poet_1.code `${from}`;
|
|
922
934
|
}
|
|
923
935
|
else {
|
|
@@ -990,12 +1002,6 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
990
1002
|
fromPartial(${messageDesc.field.length > 0 ? 'object' : '_'}: ${utils.DeepPartial}<${fullName}>): ${fullName} {
|
|
991
1003
|
const message = { ...base${fullName} } as ${fullName};
|
|
992
1004
|
`);
|
|
993
|
-
// initialize all lists
|
|
994
|
-
messageDesc.field.filter(types_1.isRepeated).forEach((field) => {
|
|
995
|
-
const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
|
|
996
|
-
const name = case_1.maybeSnakeToCamel(field.name, options);
|
|
997
|
-
chunks.push(ts_poet_1.code `message.${name} = ${value};`);
|
|
998
|
-
});
|
|
999
1005
|
// add a check for each incoming field
|
|
1000
1006
|
messageDesc.field.forEach((field) => {
|
|
1001
1007
|
const fieldName = case_1.maybeSnakeToCamel(field.name, options);
|
|
@@ -1024,6 +1030,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1024
1030
|
(options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) {
|
|
1025
1031
|
return ts_poet_1.code `${from}`;
|
|
1026
1032
|
}
|
|
1033
|
+
else if (types_1.isValueType(ctx, valueType)) {
|
|
1034
|
+
return ts_poet_1.code `${from}`;
|
|
1035
|
+
}
|
|
1027
1036
|
else {
|
|
1028
1037
|
const type = types_1.basicTypeName(ctx, valueType);
|
|
1029
1038
|
return ts_poet_1.code `${type}.fromPartial(${from})`;
|
|
@@ -1040,6 +1049,9 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1040
1049
|
};
|
|
1041
1050
|
// and then use the snippet to handle repeated fields if necessary
|
|
1042
1051
|
if (types_1.isRepeated(field)) {
|
|
1052
|
+
const value = types_1.isMapType(ctx, messageDesc, field) ? '{}' : '[]';
|
|
1053
|
+
const name = case_1.maybeSnakeToCamel(field.name, options);
|
|
1054
|
+
chunks.push(ts_poet_1.code `message.${name} = ${value};`);
|
|
1043
1055
|
chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
|
|
1044
1056
|
if (types_1.isMapType(ctx, messageDesc, field)) {
|
|
1045
1057
|
const i = maybeCastToNumber(ctx, messageDesc, field, 'key');
|
|
@@ -1058,6 +1070,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1058
1070
|
}
|
|
1059
1071
|
`);
|
|
1060
1072
|
}
|
|
1073
|
+
chunks.push(ts_poet_1.code `}`);
|
|
1061
1074
|
}
|
|
1062
1075
|
else if (types_1.isWithinOneOfThatShouldBeUnion(options, field)) {
|
|
1063
1076
|
let oneofName = case_1.maybeSnakeToCamel(messageDesc.oneofDecl[field.oneofIndex].name, options);
|
|
@@ -1069,25 +1082,34 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1069
1082
|
&& object.${oneofName}?.${fieldName} !== null
|
|
1070
1083
|
) {
|
|
1071
1084
|
message.${oneofName} = { $case: '${fieldName}', ${fieldName}: ${v} };
|
|
1085
|
+
}
|
|
1072
1086
|
`);
|
|
1073
1087
|
}
|
|
1074
|
-
else {
|
|
1088
|
+
else if ((types_1.isLong(field) || types_1.isLongValueType(field)) && options.forceLong === options_1.LongOption.LONG) {
|
|
1089
|
+
const v = readSnippet(`object.${fieldName}`);
|
|
1090
|
+
const type = types_1.basicTypeName(ctx, field);
|
|
1075
1091
|
chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
}
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1092
|
+
chunks.push(ts_poet_1.code `message.${fieldName} = ${v} as ${type};`);
|
|
1093
|
+
chunks.push(ts_poet_1.code `} else {`);
|
|
1094
|
+
const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
|
|
1095
|
+
chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
|
|
1096
|
+
chunks.push(ts_poet_1.code `}`);
|
|
1097
|
+
}
|
|
1098
|
+
else if (types_1.isPrimitive(field) ||
|
|
1099
|
+
(types_1.isTimestamp(field) && (options.useDate === options_1.DateOption.DATE || options.useDate === options_1.DateOption.STRING)) ||
|
|
1100
|
+
types_1.isValueType(ctx, field)) {
|
|
1101
|
+
// An optimized case of the else below that works when `readSnippet` returns the plain input
|
|
1102
|
+
const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
|
|
1103
|
+
chunks.push(ts_poet_1.code `message.${fieldName} = object.${fieldName} ?? ${fallback};`);
|
|
1084
1104
|
}
|
|
1085
|
-
|
|
1105
|
+
else {
|
|
1106
|
+
chunks.push(ts_poet_1.code `if (object.${fieldName} !== undefined && object.${fieldName} !== null) {`);
|
|
1107
|
+
chunks.push(ts_poet_1.code `message.${fieldName} = ${readSnippet(`object.${fieldName}`)};`);
|
|
1086
1108
|
chunks.push(ts_poet_1.code `} else {`);
|
|
1087
|
-
const
|
|
1088
|
-
chunks.push(ts_poet_1.code `message.${fieldName} = ${
|
|
1109
|
+
const fallback = types_1.isWithinOneOf(field) ? 'undefined' : types_1.defaultValue(ctx, field);
|
|
1110
|
+
chunks.push(ts_poet_1.code `message.${fieldName} = ${fallback}`);
|
|
1111
|
+
chunks.push(ts_poet_1.code `}`);
|
|
1089
1112
|
}
|
|
1090
|
-
chunks.push(ts_poet_1.code `}`);
|
|
1091
1113
|
});
|
|
1092
1114
|
// and then wrap up the switch/while/return
|
|
1093
1115
|
chunks.push(ts_poet_1.code `return message;`);
|