ts-proto 1.110.2 → 1.111.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,53 @@
1
+ # [1.111.0](https://github.com/stephenh/ts-proto/compare/v1.110.4...v1.111.0) (2022-05-01)
2
+
3
+
4
+ ### Features
5
+
6
+ * include service and definition types with implementations ([#552](https://github.com/stephenh/ts-proto/issues/552)) ([6b896f4](https://github.com/stephenh/ts-proto/commit/6b896f4b7f4ba0f0d97730767421786171646aea))
7
+
8
+ ## [next](https://github.com/stephenh/ts-proto/compare/v1.110.4...main) (????-??-??)
9
+
10
+ ### Features
11
+
12
+ * When outputing service and service definition implementations, include types. Eg, before:
13
+
14
+ ```ts
15
+ export const TestDefinition = {
16
+ name: 'Test',
17
+ fullName: 'simple.Test',
18
+ methods: {
19
+ …
20
+ },
21
+ } as const;
22
+ ```
23
+
24
+ Now:
25
+
26
+ ```ts
27
+ export type TestDefinition = typeof TestDefinition;
28
+ export const TestDefinition = {
29
+ name: 'Test',
30
+ fullName: 'simple.Test',
31
+ methods: {
32
+ …
33
+ },
34
+ } as const;
35
+ ```
36
+
37
+ ## [1.110.4](https://github.com/stephenh/ts-proto/compare/v1.110.3...v1.110.4) (2022-04-08)
38
+
39
+
40
+ ### Bug Fixes
41
+
42
+ * Use Uint8Array.forEach in base64FromBytes ([#544](https://github.com/stephenh/ts-proto/issues/544)) ([c7641ce](https://github.com/stephenh/ts-proto/commit/c7641ceb6a1c9da234245b9d808b2ded899dbbbc))
43
+
44
+ ## [1.110.3](https://github.com/stephenh/ts-proto/compare/v1.110.2...v1.110.3) (2022-04-08)
45
+
46
+
47
+ ### Bug Fixes
48
+
49
+ * regression in being able to return a Date as a GRPC return value ([#534](https://github.com/stephenh/ts-proto/issues/534)) ([22b76ec](https://github.com/stephenh/ts-proto/commit/22b76eccfc0d26309aab9e454de31ef020595be8))
50
+
1
51
  ## [1.110.2](https://github.com/stephenh/ts-proto/compare/v1.110.1...v1.110.2) (2022-03-27)
2
52
 
3
53
 
package/README.markdown CHANGED
@@ -473,7 +473,7 @@ If you need ts-proto customizations or priority support for your company, you ca
473
473
 
474
474
  **Setup**
475
475
 
476
- The commands below assume you have **Docker** installed. To use a **local** copy of `protoc` without docker, use commands suffixed with `:local`
476
+ The commands below assume you have **Docker** installed. To use a **local** copy of `protoc` without docker, use commands suffixed with `:local`. If you are using OS X, install **coreutils**, `brew install coreutils`.
477
477
 
478
478
  - Check out the [repository]() for the latest code.
479
479
  - Run `yarn install` to install the dependencies.
@@ -784,4 +784,4 @@ Where (\*) indicates they might throw an error at runtime.
784
784
  - Required primitives: use as-is, i.e. `string name = 1`.
785
785
  - Optional primitives: use wrapper types, i.e. `StringValue name = 1`.
786
786
  - Required messages: not available
787
- - Optional primitives: use as-is, i.e. `SubMessage message = 1`.
787
+ - Optional messages: use as-is, i.e. `SubMessage message = 1`.
@@ -14,9 +14,14 @@ function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc
14
14
  var _a, _b, _c;
15
15
  const chunks = [];
16
16
  utils_1.maybeAddComment(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
17
+ // Service definition type
18
+ const name = ts_poet_1.def(`${serviceDesc.name}Definition`);
19
+ chunks.push(ts_poet_1.code `
20
+ export type ${name} = typeof ${name};
21
+ `);
17
22
  // Service definition
18
23
  chunks.push(ts_poet_1.code `
19
- export const ${ts_poet_1.def(`${serviceDesc.name}Definition`)} = {
24
+ export const ${name} = {
20
25
  `);
21
26
  (_b = serviceDesc.options) === null || _b === void 0 ? void 0 : _b.uninterpretedOption;
22
27
  chunks.push(ts_poet_1.code `
@@ -42,9 +42,14 @@ function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
42
42
  var _a, _b;
43
43
  const chunks = [];
44
44
  utils_1.maybeAddComment(sourceInfo, chunks, (_a = serviceDesc.options) === null || _a === void 0 ? void 0 : _a.deprecated);
45
+ // Service definition type
46
+ const name = ts_poet_1.def(`${serviceDesc.name}Service`);
47
+ chunks.push(ts_poet_1.code `
48
+ export type ${name} = typeof ${name};
49
+ `);
45
50
  // Service definition
46
51
  chunks.push(ts_poet_1.code `
47
- export const ${ts_poet_1.def(`${serviceDesc.name}Service`)} = {
52
+ export const ${name} = {
48
53
  `);
49
54
  for (const [index, methodDesc] of serviceDesc.method.entries()) {
50
55
  utils_1.assertInstanceOf(methodDesc, utils_1.FormattedMethodDescriptor);
@@ -75,15 +75,19 @@ function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
75
75
  exports.generateService = generateService;
76
76
  function generateRegularRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
77
77
  utils_1.assertInstanceOf(methodDesc, utils_1.FormattedMethodDescriptor);
78
- const { options } = ctx;
78
+ const { options, utils } = ctx;
79
79
  const Reader = ts_poet_1.imp('Reader@protobufjs/minimal');
80
80
  const rawInputType = types_1.rawRequestType(ctx, methodDesc);
81
81
  const inputType = types_1.requestType(ctx, methodDesc);
82
82
  const outputType = types_1.responseType(ctx, methodDesc);
83
+ const rawOutputType = types_1.responseType(ctx, methodDesc, { keepValueType: true });
83
84
  const params = [...(options.context ? [ts_poet_1.code `ctx: Context`] : []), ts_poet_1.code `request: ${inputType}`];
84
85
  const maybeCtx = options.context ? 'ctx,' : '';
85
86
  let encode = ts_poet_1.code `${rawInputType}.encode(request).finish()`;
86
87
  let decode = ts_poet_1.code `data => ${outputType}.decode(new ${Reader}(data))`;
88
+ if (options.useDate && rawOutputType.toString().includes('Timestamp')) {
89
+ decode = ts_poet_1.code `data => ${utils.fromTimestamp}(${rawOutputType}.decode(new ${Reader}(data)))`;
90
+ }
87
91
  if (methodDesc.clientStreaming) {
88
92
  encode = ts_poet_1.code `request.pipe(${ts_poet_1.imp('map@rxjs/operators')}(request => ${encode}))`;
89
93
  }
package/build/main.js CHANGED
@@ -290,9 +290,9 @@ function makeByteUtils() {
290
290
  const btoa : (bin: string) => string = ${globalThis}.btoa || ((bin) => ${globalThis}.Buffer.from(bin, 'binary').toString('base64'));
291
291
  function base64FromBytes(arr: Uint8Array): string {
292
292
  const bin: string[] = [];
293
- for (const byte of arr) {
293
+ arr.forEach((byte) => {
294
294
  bin.push(String.fromCharCode(byte));
295
- }
295
+ });
296
296
  return btoa(bin.join(''));
297
297
  }
298
298
  `);
package/build/types.js CHANGED
@@ -612,8 +612,8 @@ function requestType(ctx, methodDesc, partial = false) {
612
612
  return typeName;
613
613
  }
614
614
  exports.requestType = requestType;
615
- function responseType(ctx, methodDesc) {
616
- return messageToTypeName(ctx, methodDesc.outputType, { keepValueType: true });
615
+ function responseType(ctx, methodDesc, typeOptions = {}) {
616
+ return messageToTypeName(ctx, methodDesc.outputType, typeOptions);
617
617
  }
618
618
  exports.responseType = responseType;
619
619
  function responsePromise(ctx, methodDesc) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.110.2",
3
+ "version": "1.111.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",