ts-proto 2.0.4 → 2.1.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/README.markdown CHANGED
@@ -454,7 +454,11 @@ Generated code will be placed in the Gradle build directory.
454
454
 
455
455
  - With `--ts_proto_opt=annotateFilesWithVersion=false`, the generated files will not contain the versions of `protoc` and `ts-proto` used to generate the file. This option is normally set to `true`, such that files list the versions used.
456
456
 
457
- - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators. If outputSchema is instead specified to be `no-file-descriptor` then we do not include the file descriptor in the generated schema. This is useful if you are trying to minimize the size of the generated schema.
457
+ - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
458
+
459
+ - With `--ts_proto_opt=outputSchema=no-file-descriptor`, meta typings will be generated, but we do not include the file descriptor in the generated schema. This is useful if you are trying to minimize the size of the generated schema.
460
+
461
+ - With `--ts_proto_opt=outputSchema=const`, meta typings will be generated `as const`, allowing type-safe access to all its properties. (only works with TypeScript 4.9 and up, because it also uses the [`satisfies`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator) operator). Can be combined with the `no-file-descriptor` option (`outputSchema=const,outputSchema=no-file-descriptor`) to not include the file descriptor in the generated schema.
458
462
 
459
463
  - With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.
460
464
 
@@ -32,6 +32,11 @@ export declare enum ServiceOption {
32
32
  DEFAULT = "default",
33
33
  NONE = "none"
34
34
  }
35
+ export declare enum OutputSchemaOption {
36
+ TRUE = "true",
37
+ NO_FILE_DESCRIPTOR = "no-file-descriptor",
38
+ CONST = "const"
39
+ }
35
40
  export type Options = {
36
41
  context: boolean;
37
42
  snakeToCamel: Array<"json" | "keys">;
@@ -69,7 +74,7 @@ export type Options = {
69
74
  unrecognizedEnumName: string;
70
75
  unrecognizedEnumValue: number;
71
76
  exportCommonSymbols: boolean;
72
- outputSchema: boolean | "no-file-descriptor";
77
+ outputSchema: false | OutputSchemaOption[];
73
78
  onlyTypes: boolean;
74
79
  emitImportedFiles: boolean;
75
80
  useAbortSignal: boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.JsonTimestampOption = exports.DateOption = exports.LongOption = void 0;
3
+ exports.OutputSchemaOption = exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.JsonTimestampOption = exports.DateOption = exports.LongOption = void 0;
4
4
  exports.defaultOptions = defaultOptions;
5
5
  exports.optionsFromParameter = optionsFromParameter;
6
6
  exports.getTsPoetOpts = getTsPoetOpts;
@@ -44,6 +44,12 @@ var ServiceOption;
44
44
  ServiceOption["DEFAULT"] = "default";
45
45
  ServiceOption["NONE"] = "none";
46
46
  })(ServiceOption || (exports.ServiceOption = ServiceOption = {}));
47
+ var OutputSchemaOption;
48
+ (function (OutputSchemaOption) {
49
+ OutputSchemaOption["TRUE"] = "true";
50
+ OutputSchemaOption["NO_FILE_DESCRIPTOR"] = "no-file-descriptor";
51
+ OutputSchemaOption["CONST"] = "const";
52
+ })(OutputSchemaOption || (exports.OutputSchemaOption = OutputSchemaOption = {}));
47
53
  function defaultOptions() {
48
54
  return {
49
55
  context: false,
@@ -165,6 +171,13 @@ function optionsFromParameter(parameter) {
165
171
  if (options.nestJs && options.outputServices.length > 0) {
166
172
  options.outputEncodeMethods = true;
167
173
  }
174
+ // Handle outputSchema=true
175
+ if (options.outputSchema === true) {
176
+ options.outputSchema = [];
177
+ }
178
+ if (typeof options.outputSchema === "string") {
179
+ options.outputSchema = [options.outputSchema];
180
+ }
168
181
  if (options.useDate === true) {
169
182
  // Treat useDate=true as DATE
170
183
  options.useDate = DateOption.DATE;
@@ -7,6 +7,7 @@ const visit_1 = require("./visit");
7
7
  const utils_1 = require("./utils");
8
8
  const types_1 = require("./types");
9
9
  const wire_1 = require("@bufbuild/protobuf/wire");
10
+ const options_1 = require("./options");
10
11
  const fileDescriptorProto = (0, ts_poet_1.imp)("FileDescriptorProto@ts-proto-descriptors");
11
12
  const extensionCache = {};
12
13
  function generateSchema(ctx, fileDesc, sourceInfo) {
@@ -18,6 +19,9 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
18
19
  }
19
20
  extensionCache[extension.extendee][extension.number] = extension;
20
21
  });
22
+ const outputSchemaOptions = ctx.options.outputSchema ? ctx.options.outputSchema : [];
23
+ const outputFileDescriptor = !outputSchemaOptions.includes(options_1.OutputSchemaOption.NO_FILE_DESCRIPTOR);
24
+ const outputAsConst = outputSchemaOptions.includes(options_1.OutputSchemaOption.CONST);
21
25
  chunks.push((0, ts_poet_1.code) `
22
26
  type ProtoMetaMessageOptions = {
23
27
  options?: { [key: string]: any };
@@ -27,7 +31,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
27
31
  };
28
32
 
29
33
  export interface ProtoMetadata {
30
- ${ctx.options.outputSchema !== "no-file-descriptor" ? (0, ts_poet_1.code) `fileDescriptor: ${fileDescriptorProto};\n` : ""}references: { [key: string]: any };
34
+ ${outputFileDescriptor ? (0, ts_poet_1.code) `fileDescriptor: ${fileDescriptorProto};\n` : ""}references: { [key: string]: any };
31
35
  dependencies?: ProtoMetadata[];
32
36
  options?: {
33
37
  options?: { [key: string]: any };
@@ -106,7 +110,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
106
110
  if (methodsOptions.length > 0 || serviceOptions) {
107
111
  servicesOptions.push((0, ts_poet_1.code) `
108
112
  '${service.name}': {
109
- options: ${serviceOptions},
113
+ ${serviceOptions ? (0, ts_poet_1.code) `options: ${serviceOptions},` : ""}
110
114
  methods: {${(0, ts_poet_1.joinCode)(methodsOptions, { on: "," })}}
111
115
  }
112
116
  `);
@@ -132,17 +136,17 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
132
136
  if (valuesOptions.length > 0 || enumOptions) {
133
137
  enumsOptions.push((0, ts_poet_1.code) `
134
138
  '${Enum.name}': {
135
- options: ${enumOptions},
139
+ ${enumOptions ? (0, ts_poet_1.code) `options: ${enumOptions},` : ""}
136
140
  values: {${(0, ts_poet_1.joinCode)(valuesOptions, { on: "," })}}
137
141
  }
138
142
  `);
139
143
  }
140
144
  });
141
145
  chunks.push((0, ts_poet_1.code) `
142
- export const ${(0, ts_poet_1.def)("protoMetadata")}: ProtoMetadata = {
143
- ${ctx.options.outputSchema !== "no-file-descriptor"
144
- ? (0, ts_poet_1.code) `fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),\n`
145
- : ""}references: { ${(0, ts_poet_1.joinCode)(references, { on: "," })} },
146
+ export const ${(0, ts_poet_1.def)("protoMetadata")}${outputAsConst ? "" : ": ProtoMetadata"} = {
147
+ ${outputFileDescriptor ? (0, ts_poet_1.code) `fileDescriptor: ${descriptor},\n` : ""}references: { ${(0, ts_poet_1.joinCode)(references, {
148
+ on: ",",
149
+ })} },
146
150
  dependencies: [${(0, ts_poet_1.joinCode)(dependencies, { on: "," })}],
147
151
  ${fileOptions || messagesOptions.length > 0 || servicesOptions.length > 0 || enumsOptions.length > 0
148
152
  ? (0, ts_poet_1.code) `options: {
@@ -152,7 +156,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
152
156
  ${enumsOptions.length > 0 ? (0, ts_poet_1.code) `enums: {${(0, ts_poet_1.joinCode)(enumsOptions, { on: "," })}}` : ""}
153
157
  }`
154
158
  : ""}
155
- }
159
+ }${outputAsConst ? " as const satisfies ProtoMetadata" : ""}
156
160
  `);
157
161
  return chunks;
158
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.0.4",
3
+ "version": "2.1.0",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",