ts-proto 1.163.0 → 1.164.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 CHANGED
@@ -147,6 +147,8 @@ If you'd like an out-of-the-box RPC framework built on top of ts-proto, there ar
147
147
 
148
148
  (Note for potential contributors, if you develop other frameworks/mini-frameworks, or even blog posts/tutorials, on using `ts-proto`, we're happy to link to them.)
149
149
 
150
+ We also don't support clients for `google.api.http`-based [Google Cloud](https://cloud.google.com/endpoints/docs/grpc/transcoding) APIs, see [#948](https://github.com/stephenh/ts-proto/issues/948) if you'd like to submit a PR.
151
+
150
152
  # Example Types
151
153
 
152
154
  The generated types are "just data", i.e.:
@@ -446,6 +448,10 @@ Generated code will be placed in the Gradle build directory.
446
448
 
447
449
  - With `--ts_proto_opt=outputServices=false`, or `=none`, ts-proto will output NO service definitions.
448
450
 
451
+ - With `--ts_proto_opt=outputBeforeRequest=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `beforeRequest(request: <RequestType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `beforeRequest` before performing it's request.
452
+
453
+ - With `--ts_proto_opt=outputAfterResponse=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `afterResponse(response: <ResponseType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `afterResponse` before returning the response.
454
+
449
455
  - With `--ts_proto_opt=useAbortSignal=true`, the generated services will accept an `AbortSignal` to cancel RPC calls.
450
456
 
451
457
  - With `--ts_proto_opt=useAsyncIterable=true`, the generated services will use `AsyncIterable` instead of `Observable`.
@@ -885,11 +891,14 @@ ExampleMessage.encode({ anything: true });
885
891
  ## Timestamp
886
892
 
887
893
  The representation of `google.protobuf.Timestamp` is configurable by the `useDate` flag.
894
+ The `useJsonTimestamp` flag controls precision when `useDate` is `false`.
888
895
 
889
896
  | Protobuf well-known type | Default/`useDate=true` | `useDate=false` | `useDate=string` |
890
897
  | --------------------------- | ---------------------- | ------------------------------------ | ---------------- |
891
898
  | `google.protobuf.Timestamp` | `Date` | `{ seconds: number, nanos: number }` | `string` |
892
899
 
900
+ When using `useDate=false` and `useJsonTimestamp=raw` timestamp is represented as `{ seconds: number, nanos: number }`, but has nanosecond precision.
901
+
893
902
  # Number Types
894
903
 
895
904
  Numbers are by default assumed to be plain JavaScript `number`s.
@@ -88,7 +88,23 @@ function generateRegularRpcMethod(ctx, methodDesc) {
88
88
  const maybeCtx = options.context ? "ctx," : "";
89
89
  const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";
90
90
  let encode = (0, ts_poet_1.code) `${rawInputType}.encode(request).finish()`;
91
+ let beforeRequest;
92
+ if (options.outputBeforeRequest) {
93
+ beforeRequest = (0, ts_poet_1.code) `
94
+ if (this.rpc.beforeRequest) {
95
+ this.rpc.beforeRequest(request);
96
+ }`;
97
+ }
91
98
  let decode = (0, ts_poet_1.code) `data => ${rawOutputType}.decode(${Reader}.create(data))`;
99
+ if (options.outputAfterResponse) {
100
+ decode = (0, ts_poet_1.code) `data => {
101
+ const response = ${rawOutputType}.decode(${Reader}.create(data));
102
+ if (this.rpc.afterResponse) {
103
+ this.rpc.afterResponse(response);
104
+ }
105
+ return response;
106
+ }`;
107
+ }
92
108
  // if (options.useDate && rawOutputType.toString().includes("Timestamp")) {
93
109
  // decode = code`data => ${utils.fromTimestamp}(${rawOutputType}.decode(${Reader}.create(data)))`;
94
110
  // }
@@ -131,7 +147,7 @@ function generateRegularRpcMethod(ctx, methodDesc) {
131
147
  ${methodDesc.formattedName}(
132
148
  ${(0, ts_poet_1.joinCode)(params, { on: "," })}
133
149
  ): ${(0, types_1.responsePromiseOrObservable)(ctx, methodDesc)} {
134
- const data = ${encode};
150
+ const data = ${encode}; ${beforeRequest ? beforeRequest : ""}
135
151
  const ${returnVariable} = this.rpc.${rpcMethod}(
136
152
  ${maybeCtx}
137
153
  this.service,
@@ -285,6 +301,13 @@ function generateRpcType(ctx, hasStreamingMethods) {
285
301
  const maybeContextParam = options.context ? "ctx: Context," : "";
286
302
  const maybeAbortSignalParam = options.useAbortSignal ? "abortSignal?: AbortSignal," : "";
287
303
  const methods = [[(0, ts_poet_1.code) `request`, (0, ts_poet_1.code) `Uint8Array`, (0, ts_poet_1.code) `Promise<Uint8Array>`]];
304
+ const additionalMethods = [];
305
+ if (options.outputBeforeRequest) {
306
+ additionalMethods.push((0, ts_poet_1.code) `beforeRequest?<T extends { [k in keyof T]: unknown }>(request: T): void;`);
307
+ }
308
+ if (options.outputAfterResponse) {
309
+ additionalMethods.push((0, ts_poet_1.code) `afterResponse?<T extends { [k in keyof T]: unknown }>(response: T): void;`);
310
+ }
288
311
  if (hasStreamingMethods) {
289
312
  const observable = (0, types_1.observableType)(ctx, true);
290
313
  methods.push([(0, ts_poet_1.code) `clientStreamingRequest`, (0, ts_poet_1.code) `${observable}<Uint8Array>`, (0, ts_poet_1.code) `Promise<Uint8Array>`]);
@@ -307,6 +330,7 @@ function generateRpcType(ctx, hasStreamingMethods) {
307
330
  ${maybeAbortSignalParam}
308
331
  ): ${method[2]};`);
309
332
  });
333
+ additionalMethods.forEach((method) => chunks.push(method));
310
334
  chunks.push((0, ts_poet_1.code) ` }`);
311
335
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
312
336
  }
package/build/main.js CHANGED
@@ -2,24 +2,24 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.contextTypeVar = exports.makeUtils = exports.generateFile = void 0;
4
4
  const ts_poet_1 = require("ts-poet");
5
+ const ConditionalOutput_1 = require("ts-poet/build/ConditionalOutput");
5
6
  const ts_proto_descriptors_1 = require("ts-proto-descriptors");
6
- const types_1 = require("./types");
7
- const sourceInfo_1 = require("./sourceInfo");
8
- const utils_1 = require("./utils");
9
7
  const case_1 = require("./case");
10
- const generate_nestjs_1 = require("./generate-nestjs");
11
- const generate_services_1 = require("./generate-services");
12
- const generate_grpc_web_1 = require("./generate-grpc-web");
13
- const generate_async_iterable_1 = require("./generate-async-iterable");
14
8
  const enums_1 = require("./enums");
15
- const visit_1 = require("./visit");
16
- const options_1 = require("./options");
17
- const schema_1 = require("./schema");
18
- const ConditionalOutput_1 = require("ts-poet/build/ConditionalOutput");
19
- const generate_grpc_js_1 = require("./generate-grpc-js");
9
+ const generate_async_iterable_1 = require("./generate-async-iterable");
20
10
  const generate_generic_service_definition_1 = require("./generate-generic-service-definition");
11
+ const generate_grpc_js_1 = require("./generate-grpc-js");
12
+ const generate_grpc_web_1 = require("./generate-grpc-web");
13
+ const generate_nestjs_1 = require("./generate-nestjs");
21
14
  const generate_nice_grpc_1 = require("./generate-nice-grpc");
15
+ const generate_services_1 = require("./generate-services");
22
16
  const generate_struct_wrappers_1 = require("./generate-struct-wrappers");
17
+ const options_1 = require("./options");
18
+ const schema_1 = require("./schema");
19
+ const sourceInfo_1 = require("./sourceInfo");
20
+ const types_1 = require("./types");
21
+ const utils_1 = require("./utils");
22
+ const visit_1 = require("./visit");
23
23
  function generateFile(ctx, fileDesc) {
24
24
  var _a;
25
25
  const { options, utils } = ctx;
@@ -1733,7 +1733,10 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
1733
1733
  return (0, ts_poet_1.code) `${from}`;
1734
1734
  }
1735
1735
  else if ((0, types_1.isTimestamp)(field) && options.useDate === options_1.DateOption.TIMESTAMP) {
1736
- return (0, ts_poet_1.code) `${from}`;
1736
+ if (options.useJsonTimestamp === options_1.JsonTimestampOption.RAW) {
1737
+ return (0, ts_poet_1.code) `${from}`;
1738
+ }
1739
+ return (0, ts_poet_1.code) `${utils.fromTimestamp}(${from}).toISOString()`;
1737
1740
  }
1738
1741
  else if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
1739
1742
  // For map types, drill-in and then admittedly re-hard-code our per-value-type logic
package/build/options.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addTypeToMessages = exports.getTsPoetOpts = exports.optionsFromParameter = exports.defaultOptions = exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.DateOption = exports.LongOption = void 0;
3
+ exports.addTypeToMessages = exports.getTsPoetOpts = exports.optionsFromParameter = exports.defaultOptions = exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.JsonTimestampOption = exports.DateOption = exports.LongOption = void 0;
4
4
  var LongOption;
5
5
  (function (LongOption) {
6
6
  LongOption["NUMBER"] = "number";
@@ -14,6 +14,11 @@ var DateOption;
14
14
  DateOption["STRING"] = "string";
15
15
  DateOption["TIMESTAMP"] = "timestamp";
16
16
  })(DateOption || (exports.DateOption = DateOption = {}));
17
+ var JsonTimestampOption;
18
+ (function (JsonTimestampOption) {
19
+ JsonTimestampOption["RFC3339"] = "rfc3339";
20
+ JsonTimestampOption["RAW"] = "raw";
21
+ })(JsonTimestampOption || (exports.JsonTimestampOption = JsonTimestampOption = {}));
17
22
  var EnvOption;
18
23
  (function (EnvOption) {
19
24
  EnvOption["NODE"] = "node";
@@ -42,6 +47,7 @@ function defaultOptions() {
42
47
  forceLong: LongOption.NUMBER,
43
48
  useOptionals: "none",
44
49
  useDate: DateOption.DATE,
50
+ useJsonTimestamp: JsonTimestampOption.RFC3339,
45
51
  useMongoObjectId: false,
46
52
  oneof: OneofOption.PROPERTIES,
47
53
  esModuleInterop: false,
@@ -87,6 +93,8 @@ function defaultOptions() {
87
93
  outputExtensions: false,
88
94
  outputIndex: false,
89
95
  M: {},
96
+ outputBeforeRequest: false,
97
+ outputAfterResponse: false,
90
98
  };
91
99
  }
92
100
  exports.defaultOptions = defaultOptions;
@@ -175,6 +183,9 @@ function optionsFromParameter(parameter) {
175
183
  if (options.outputIndex) {
176
184
  options.exportCommonSymbols = false;
177
185
  }
186
+ if (options.outputBeforeRequest || options.outputAfterResponse) {
187
+ options.outputServices = [ServiceOption.DEFAULT];
188
+ }
178
189
  if (options.unrecognizedEnumValue) {
179
190
  // Make sure to cast number options to an actual number
180
191
  options.unrecognizedEnumValue = Number(options.unrecognizedEnumValue);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.163.0",
3
+ "version": "1.164.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",