ts-proto 1.121.4 → 1.122.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,25 @@
1
+ # [1.122.0](https://github.com/stephenh/ts-proto/compare/v1.121.6...v1.122.0) (2022-08-15)
2
+
3
+
4
+ ### Features
5
+
6
+ * **Grpc-Web:** Add & export GrpcWebError type ([#593](https://github.com/stephenh/ts-proto/issues/593)) ([645987d](https://github.com/stephenh/ts-proto/commit/645987d023e666290e87086f5a0770c34e2fe978))
7
+
8
+ ## [1.121.6](https://github.com/stephenh/ts-proto/compare/v1.121.5...v1.121.6) (2022-08-14)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * Use jsonName even with snakeToCamel=false. ([#653](https://github.com/stephenh/ts-proto/issues/653)) ([1144886](https://github.com/stephenh/ts-proto/commit/1144886ef43eaf97b780c7aafb2c123fd49b3fe5)), closes [#635](https://github.com/stephenh/ts-proto/issues/635)
14
+
15
+ ## [1.121.5](https://github.com/stephenh/ts-proto/compare/v1.121.4...v1.121.5) (2022-08-08)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * remove Record word conflict ([#638](https://github.com/stephenh/ts-proto/issues/638)) ([5664d09](https://github.com/stephenh/ts-proto/commit/5664d097ac33da423b0f4e79f962fd71912358a0))
21
+ * resolve import collisions for services ([#651](https://github.com/stephenh/ts-proto/issues/651)) ([ee0296f](https://github.com/stephenh/ts-proto/commit/ee0296ffe087665b54bac714e964e7243010fb22))
22
+
1
23
  ## [1.121.4](https://github.com/stephenh/ts-proto/compare/v1.121.3...v1.121.4) (2022-08-07)
2
24
 
3
25
 
package/build/case.js CHANGED
@@ -1,29 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.camelCase = exports.capitalize = exports.camelToSnake = exports.maybeSnakeToCamel = void 0;
4
- function maybeSnakeToCamel(s, options) {
5
- if (options.snakeToCamel.includes('keys') && s.includes('_')) {
6
- const hasLowerCase = !!s.match(/[a-z]/);
7
- return s
8
- .split('_')
9
- .map((word, i) => {
10
- // If the word is already mixed case, leave the exist case as-is
11
- word = hasLowerCase ? word : word.toLowerCase();
12
- return i === 0 ? word : capitalize(word);
13
- })
14
- .join('');
3
+ exports.camelCase = exports.capitalize = exports.camelToSnake = exports.snakeToCamel = exports.maybeSnakeToCamel = void 0;
4
+ /** Converts `key` to TS/JS camel-case idiom, unless overridden not to. */
5
+ function maybeSnakeToCamel(key, options) {
6
+ if (options.snakeToCamel.includes('keys') && key.includes('_')) {
7
+ return snakeToCamel(key);
15
8
  }
16
9
  else {
17
- return s;
10
+ return key;
18
11
  }
19
12
  }
20
13
  exports.maybeSnakeToCamel = maybeSnakeToCamel;
21
- function camelToSnake(s) {
14
+ function snakeToCamel(s) {
15
+ const hasLowerCase = !!s.match(/[a-z]/);
22
16
  return s
23
- .replace(/[\w]([A-Z])/g, function (m) {
24
- return m[0] + '_' + m[1];
17
+ .split('_')
18
+ .map((word, i) => {
19
+ // If the word is already mixed case, leave the existing case as-is
20
+ word = hasLowerCase ? word : word.toLowerCase();
21
+ return i === 0 ? word : capitalize(word);
25
22
  })
26
- .toUpperCase();
23
+ .join('');
24
+ }
25
+ exports.snakeToCamel = snakeToCamel;
26
+ function camelToSnake(s) {
27
+ return s.replace(/\w([A-Z])/g, (m) => m[0] + '_' + m[1]).toUpperCase();
27
28
  }
28
29
  exports.camelToSnake = camelToSnake;
29
30
  function capitalize(s) {
@@ -188,7 +188,7 @@ function generateGrpcWebImpl(ctx, returnObservable, hasStreamingMethods) {
188
188
  chunks.push(createObservableUnaryMethod(ctx));
189
189
  }
190
190
  else {
191
- chunks.push(createPromiseUnaryMethod());
191
+ chunks.push(createPromiseUnaryMethod(ctx));
192
192
  }
193
193
  if (hasStreamingMethods) {
194
194
  chunks.push(createInvokeMethod(ctx));
@@ -196,7 +196,7 @@ function generateGrpcWebImpl(ctx, returnObservable, hasStreamingMethods) {
196
196
  chunks.push((0, ts_poet_1.code) `}`);
197
197
  return (0, ts_poet_1.joinCode)(chunks, { trim: false });
198
198
  }
199
- function createPromiseUnaryMethod() {
199
+ function createPromiseUnaryMethod(ctx) {
200
200
  return (0, ts_poet_1.code) `
201
201
  unary<T extends UnaryMethodDefinitionish>(
202
202
  methodDesc: T,
@@ -219,9 +219,7 @@ function createPromiseUnaryMethod() {
219
219
  if (response.status === grpc.Code.OK) {
220
220
  resolve(response.message);
221
221
  } else {
222
- const err = new Error(response.statusMessage) as any;
223
- err.code = response.status;
224
- err.metadata = response.trailers;
222
+ const err = new ${ctx.utils.GrpcWebError}(response.statusMessage, response.status, response.trailers);
225
223
  reject(err);
226
224
  }
227
225
  },
@@ -251,7 +249,8 @@ function createObservableUnaryMethod(ctx) {
251
249
  debug: this.options.debug,
252
250
  onEnd: (next) => {
253
251
  if (next.status !== 0) {
254
- observer.error({ code: next.status, message: next.statusMessage });
252
+ const err = new ${ctx.utils.GrpcWebError}(next.statusMessage, next.status, next.trailers);
253
+ observer.error(err);
255
254
  } else {
256
255
  observer.next(next.message as any);
257
256
  observer.complete();
@@ -24,7 +24,7 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
24
24
  const chunks = [];
25
25
  (0, utils_1.maybeAddComment)(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
26
26
  const maybeTypeVar = options.context ? `<${main_1.contextTypeVar}>` : '';
27
- chunks.push((0, ts_poet_1.code) `export interface ${serviceDesc.name}${maybeTypeVar} {`);
27
+ chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(serviceDesc.name)}${maybeTypeVar} {`);
28
28
  serviceDesc.method.forEach((methodDesc, index) => {
29
29
  var _a;
30
30
  (0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
@@ -144,7 +144,7 @@ function generateServiceClientImpl(ctx, fileDesc, serviceDesc) {
144
144
  const { name } = serviceDesc;
145
145
  const i = options.context ? `${name}<Context>` : name;
146
146
  const t = options.context ? `<${main_1.contextTypeVar}>` : '';
147
- chunks.push((0, ts_poet_1.code) `export class ${name}ClientImpl${t} implements ${i} {`);
147
+ chunks.push((0, ts_poet_1.code) `export class ${name}ClientImpl${t} implements ${(0, ts_poet_1.def)(i)} {`);
148
148
  // Create the constructor(rpc: Rpc)
149
149
  const rpcType = options.context ? 'Rpc<Context>' : 'Rpc';
150
150
  chunks.push((0, ts_poet_1.code) `private readonly rpc: ${rpcType};`);
package/build/main.js CHANGED
@@ -221,6 +221,7 @@ function makeUtils(options) {
221
221
  ...longs,
222
222
  ...makeComparisonUtils(),
223
223
  ...makeNiceGrpcServerStreamingMethodResult(),
224
+ ...makeGrpcWebErrorClass(),
224
225
  };
225
226
  }
226
227
  exports.makeUtils = makeUtils;
@@ -337,7 +338,7 @@ function makeDeepPartial(options, longs) {
337
338
  ${maybeExport} type Exact<P, I extends P> = P extends ${Builtin}
338
339
  ? P
339
340
  : P &
340
- { [K in keyof P]: Exact<P[K], I[K]> } & Record<Exclude<keyof I, KeysOfUnion<P> ${maybeExcludeType}>, never>;
341
+ { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P> ${maybeExcludeType}>]: never };
341
342
  `);
342
343
  // Based on the type from ts-essentials
343
344
  const keys = options.outputTypeRegistry ? (0, ts_poet_1.code) `Exclude<keyof T, '$type'>` : (0, ts_poet_1.code) `keyof T`;
@@ -472,6 +473,16 @@ function makeNiceGrpcServerStreamingMethodResult() {
472
473
  `);
473
474
  return { NiceGrpcServerStreamingMethodResult };
474
475
  }
476
+ function makeGrpcWebErrorClass() {
477
+ const GrpcWebError = (0, ts_poet_1.conditionalOutput)('GrpcWebError', (0, ts_poet_1.code) `
478
+ export class GrpcWebError extends Error {
479
+ constructor(message: string, public code: grpc.Code, public metadata: grpc.Metadata) {
480
+ super(message);
481
+ }
482
+ }
483
+ `);
484
+ return { GrpcWebError };
485
+ }
475
486
  // Create the interface with properties
476
487
  function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fullTypeName) {
477
488
  var _a;
package/build/utils.js CHANGED
@@ -151,7 +151,11 @@ function getFieldJsonName(field, options) {
151
151
  return field.jsonName;
152
152
  }
153
153
  else {
154
- return field.name;
154
+ // The user wants to keep snake case in the JSON, but we still want to see if the jsonName
155
+ // attribute is set as an explicit override.
156
+ const probableJsonName = (0, case_1.snakeToCamel)(field.name);
157
+ const isJsonNameSet = probableJsonName !== field.jsonName;
158
+ return isJsonNameSet ? field.jsonName : field.name;
155
159
  }
156
160
  }
157
161
  exports.getFieldJsonName = getFieldJsonName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.121.4",
3
+ "version": "1.122.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",