ts-proto 1.167.2 → 1.167.3

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.
@@ -98,11 +98,11 @@ function generateRegularRpcMethod(ctx, methodDesc) {
98
98
  }
99
99
  let encode = (0, ts_poet_1.code) `${rawInputType}.encode(request).finish()`;
100
100
  let beforeRequest;
101
- if (options.rpcBeforeRequest) {
102
- beforeRequest = (0, ts_poet_1.code) `
103
- if (this.rpc.beforeRequest) {
104
- this.rpc.beforeRequest(this.service, "${methodDesc.name}", request);
105
- }`;
101
+ if (options.rpcBeforeRequest && !methodDesc.clientStreaming) {
102
+ beforeRequest = generateBeforeRequest(methodDesc.name);
103
+ }
104
+ else if (methodDesc.clientStreaming && options.rpcBeforeRequest) {
105
+ encode = (0, ts_poet_1.code) `{const encodedRequest = ${encode}; ${generateBeforeRequest(methodDesc.name, "encodedRequest")}; return encodedRequest}`;
106
106
  }
107
107
  let decode = (0, ts_poet_1.code) `${rawOutputType}.decode(${Reader}.create(data))`;
108
108
  if (options.rpcAfterResponse) {
@@ -162,28 +162,35 @@ function generateRegularRpcMethod(ctx, methodDesc) {
162
162
  }
163
163
  `;
164
164
  }
165
+ function generateBeforeRequest(methodName, requestVariableName = "request") {
166
+ return (0, ts_poet_1.code) `
167
+ if (this.rpc.beforeRequest) {
168
+ this.rpc.beforeRequest(this.service, "${methodName}", ${requestVariableName});
169
+ }`;
170
+ }
165
171
  function createDefaultServiceReturn(ctx, methodDesc, decode, errorHandler) {
166
172
  const { options } = ctx;
167
173
  const rawOutputType = (0, types_1.responseType)(ctx, methodDesc, { keepValueType: true });
174
+ const returnStatement = (0, utils_1.arrowFunction)("data", decode, !options.rpcAfterResponse);
168
175
  if (options.returnObservable || methodDesc.serverStreaming) {
169
176
  if (options.useAsyncIterable) {
170
177
  return (0, ts_poet_1.code) `${rawOutputType}.decodeTransform(result)`;
171
178
  }
172
179
  else {
173
- return (0, ts_poet_1.code) `result.pipe(${(0, ts_poet_1.imp)("map@rxjs/operators")}(data => ${decode}))`;
180
+ if (errorHandler) {
181
+ const tc = (0, utils_1.arrowFunction)("data", (0, utils_1.tryCatchBlock)(decode, (0, ts_poet_1.code) `throw error`), !options.rpcAfterResponse);
182
+ return (0, ts_poet_1.code) `result.pipe(${(0, ts_poet_1.imp)("map@rxjs/operators")}(${tc}))`;
183
+ }
184
+ return (0, ts_poet_1.code) `result.pipe(${(0, ts_poet_1.imp)("map@rxjs/operators")}(${returnStatement}))`;
174
185
  }
175
186
  }
176
187
  if (errorHandler) {
177
- let tryBlock = decode;
178
188
  if (!options.rpcAfterResponse) {
179
- tryBlock = (0, ts_poet_1.code) `return ${decode}`;
189
+ decode = (0, ts_poet_1.code) `return ${decode}`;
180
190
  }
181
- return (0, ts_poet_1.code) `promise.then(data => { ${(0, utils_1.tryCatchBlock)(tryBlock, (0, ts_poet_1.code) `return Promise.reject(error);`)}}).catch((error) => { ${errorHandler} })`;
182
- }
183
- else if (options.rpcAfterResponse) {
184
- return (0, ts_poet_1.code) `promise.then(data => { ${decode} } )`;
191
+ return (0, ts_poet_1.code) `promise.then(${(0, utils_1.arrowFunction)("data", (0, utils_1.tryCatchBlock)(decode, (0, ts_poet_1.code) `return Promise.reject(error);`), false)}).catch(${(0, utils_1.arrowFunction)("error", errorHandler, false)})`;
185
192
  }
186
- return (0, ts_poet_1.code) `promise.then(data => ${decode})`;
193
+ return (0, ts_poet_1.code) `promise.then(${returnStatement})`;
187
194
  }
188
195
  function generateServiceClientImpl(ctx, fileDesc, serviceDesc) {
189
196
  const { options } = ctx;
package/build/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tryCatchBlock = exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.safeAccessor = exports.getFieldName = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.generateIndexFiles = exports.protoFilesToGenerate = void 0;
3
+ exports.arrowFunction = exports.tryCatchBlock = exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.safeAccessor = exports.getFieldName = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.generateIndexFiles = exports.protoFilesToGenerate = void 0;
4
4
  const path = require("path");
5
5
  const ts_poet_1 = require("ts-poet");
6
6
  const options_1 = require("./options");
@@ -254,3 +254,10 @@ function tryCatchBlock(tryBlock, handleErrorBlock) {
254
254
  }`;
255
255
  }
256
256
  exports.tryCatchBlock = tryCatchBlock;
257
+ function arrowFunction(params, body, isOneLine = true) {
258
+ if (isOneLine) {
259
+ return (0, ts_poet_1.code) `(${params}) => ${body}`;
260
+ }
261
+ return (0, ts_poet_1.code) `(${params}) => { ${body} }`;
262
+ }
263
+ exports.arrowFunction = arrowFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.167.2",
3
+ "version": "1.167.3",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",