ts-proto 1.95.1 → 1.96.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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.96.0](https://github.com/stephenh/ts-proto/compare/v1.95.1...v1.96.0) (2021-12-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * `enumsAsLiterals` option ([#450](https://github.com/stephenh/ts-proto/issues/450)) ([fcaade2](https://github.com/stephenh/ts-proto/commit/fcaade2855ae28ea3553a365556ccb92a9644d70))
7
+
1
8
  ## [1.95.1](https://github.com/stephenh/ts-proto/compare/v1.95.0...v1.95.1) (2021-12-23)
2
9
 
3
10
 
package/README.markdown CHANGED
@@ -348,6 +348,8 @@ Generated code will be placed in the Gradle build directory.
348
348
 
349
349
  - With `--ts_proto_opt=fileSuffix=<SUFFIX>`, ts-proto will emit generated files using the specified suffix. A `helloworld.proto` file with `fileSuffix=.pb` would be generated as `helloworld.pb.ts`. This is common behavior in other protoc plugins and provides a way to quickly glob all the generated files.
350
350
 
351
+ - With `--ts_proto_opt=enumsAsLiterals=true`, the generated enum types will be enum-ish object with `as const`.
352
+
351
353
  ### Only Types
352
354
 
353
355
  If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.:
package/build/enums.js CHANGED
@@ -13,17 +13,30 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
13
13
  const { options } = ctx;
14
14
  const chunks = [];
15
15
  utils_1.maybeAddComment(sourceInfo, chunks, (_a = enumDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
16
- chunks.push(ts_poet_1.code `export ${options.constEnums ? 'const ' : ''}enum ${ts_poet_1.def(fullName)} {`);
16
+ if (options.enumsAsLiterals) {
17
+ chunks.push(ts_poet_1.code `export const ${ts_poet_1.def(fullName)} = {`);
18
+ }
19
+ else {
20
+ chunks.push(ts_poet_1.code `export ${options.constEnums ? 'const ' : ''}enum ${ts_poet_1.def(fullName)} {`);
21
+ }
22
+ const delimiter = options.enumsAsLiterals ? ':' : '=';
17
23
  enumDesc.value.forEach((valueDesc, index) => {
18
24
  var _a;
19
25
  const info = sourceInfo.lookup(sourceInfo_1.Fields.enum.value, index);
20
26
  utils_1.maybeAddComment(info, chunks, (_a = valueDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated, `${valueDesc.name} - `);
21
- chunks.push(ts_poet_1.code `${valueDesc.name} = ${options.stringEnums ? `"${valueDesc.name}"` : valueDesc.number.toString()},`);
27
+ chunks.push(ts_poet_1.code `${valueDesc.name} ${delimiter} ${options.stringEnums ? `"${valueDesc.name}"` : valueDesc.number.toString()},`);
22
28
  });
23
29
  if (options.unrecognizedEnum)
24
30
  chunks.push(ts_poet_1.code `
25
- ${UNRECOGNIZED_ENUM_NAME} = ${options.stringEnums ? `"${UNRECOGNIZED_ENUM_NAME}"` : UNRECOGNIZED_ENUM_VALUE.toString()},`);
26
- chunks.push(ts_poet_1.code `}`);
31
+ ${UNRECOGNIZED_ENUM_NAME} ${delimiter} ${options.stringEnums ? `"${UNRECOGNIZED_ENUM_NAME}"` : UNRECOGNIZED_ENUM_VALUE.toString()},`);
32
+ if (options.enumsAsLiterals) {
33
+ chunks.push(ts_poet_1.code `} as const`);
34
+ chunks.push(ts_poet_1.code `\n`);
35
+ chunks.push(ts_poet_1.code `export type ${ts_poet_1.def(fullName)} = typeof ${ts_poet_1.def(fullName)}[keyof typeof ${ts_poet_1.def(fullName)}]`);
36
+ }
37
+ else {
38
+ chunks.push(ts_poet_1.code `}`);
39
+ }
27
40
  if (options.outputJsonMethods || (options.stringEnums && options.outputEncodeMethods)) {
28
41
  chunks.push(ts_poet_1.code `\n`);
29
42
  chunks.push(generateEnumFromJson(ctx, fullName, enumDesc));
package/build/options.js CHANGED
@@ -48,6 +48,7 @@ function defaultOptions() {
48
48
  outputTypeRegistry: false,
49
49
  stringEnums: false,
50
50
  constEnums: false,
51
+ enumsAsLiterals: false,
51
52
  outputClientImpl: true,
52
53
  outputServices: ServiceOption.DEFAULT,
53
54
  returnObservable: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.95.1",
3
+ "version": "1.96.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",