ts-proto 2.1.0 → 2.2.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/build/src/main.d.ts +10 -1
- package/build/src/main.js +152 -4
- package/package.json +1 -1
package/build/src/main.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { FileDescriptorProto } from "ts-proto-descriptors";
|
|
|
4
4
|
import { Context } from "./context";
|
|
5
5
|
import { Options } from "./options";
|
|
6
6
|
export declare function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [string, Code];
|
|
7
|
-
export type Utils = ReturnType<typeof makeDeepPartial> & ReturnType<typeof makeObjectIdMethods> & ReturnType<typeof makeTimestampMethods> & ReturnType<typeof makeByteUtils> & ReturnType<typeof makeLongUtils> & ReturnType<typeof makeComparisonUtils> & ReturnType<typeof makeNiceGrpcServerStreamingMethodResult> & ReturnType<typeof makeGrpcWebErrorClass> & ReturnType<typeof makeExtensionClass> & ReturnType<typeof makeAssertionUtils>;
|
|
7
|
+
export type Utils = ReturnType<typeof makeDeepPartial> & ReturnType<typeof makeObjectIdMethods> & ReturnType<typeof makeTimestampMethods> & ReturnType<typeof makeByteUtils> & ReturnType<typeof makeLongUtils> & ReturnType<typeof makeComparisonUtils> & ReturnType<typeof makeNiceGrpcServerStreamingMethodResult> & ReturnType<typeof makeGrpcWebErrorClass> & ReturnType<typeof makeExtensionClass> & ReturnType<typeof makeAssertionUtils> & ReturnType<typeof makeMessageFns>;
|
|
8
8
|
/** These are runtime utility methods used by the generated code. */
|
|
9
9
|
export declare function makeUtils(options: Options): Utils;
|
|
10
10
|
declare function makeLongUtils(options: Options, bytes: ReturnType<typeof makeByteUtils>): {
|
|
@@ -22,6 +22,15 @@ declare function makeDeepPartial(options: Options, longs: ReturnType<typeof make
|
|
|
22
22
|
DeepPartial: ConditionalOutput;
|
|
23
23
|
Exact: ConditionalOutput;
|
|
24
24
|
};
|
|
25
|
+
declare function makeMessageFns(options: Options, deepPartial: ReturnType<typeof makeDeepPartial>, extension: ReturnType<typeof makeExtensionClass>): {
|
|
26
|
+
MessageFns: ConditionalOutput;
|
|
27
|
+
ExtensionFns: ConditionalOutput;
|
|
28
|
+
ExtensionHolder: ConditionalOutput;
|
|
29
|
+
StructWrapperFns: ConditionalOutput;
|
|
30
|
+
AnyValueWrapperFns: ConditionalOutput;
|
|
31
|
+
ListValueWrapperFns: ConditionalOutput;
|
|
32
|
+
FieldMaskWrapperFns: ConditionalOutput;
|
|
33
|
+
};
|
|
25
34
|
declare function makeObjectIdMethods(): {
|
|
26
35
|
fromJsonObjectId: ConditionalOutput;
|
|
27
36
|
fromProtoObjectId: ConditionalOutput;
|
package/build/src/main.js
CHANGED
|
@@ -96,7 +96,8 @@ function generateFile(ctx, fileDesc) {
|
|
|
96
96
|
chunks.push(generateBaseInstanceFactory(ctx, fullName, message, fullTypeName));
|
|
97
97
|
}
|
|
98
98
|
const staticMembers = [];
|
|
99
|
-
|
|
99
|
+
const hasTypeMember = options.outputTypeAnnotations || options.outputTypeRegistry;
|
|
100
|
+
if (hasTypeMember) {
|
|
100
101
|
staticMembers.push((0, ts_poet_1.code) `$type: '${fullTypeName}' as const`);
|
|
101
102
|
}
|
|
102
103
|
if (options.outputExtensions) {
|
|
@@ -153,8 +154,37 @@ function generateFile(ctx, fileDesc) {
|
|
|
153
154
|
staticMembers.push(...(0, generate_struct_wrappers_1.generateUnwrapShallow)(ctx, fullTypeName, structFieldNames));
|
|
154
155
|
}
|
|
155
156
|
if (staticMembers.length > 0) {
|
|
157
|
+
const messageFnsTypeParameters = [fullName, hasTypeMember && `'${fullTypeName}'`]
|
|
158
|
+
.filter((p) => !!p)
|
|
159
|
+
.join(", ");
|
|
160
|
+
const interfaces = [(0, ts_poet_1.code) `${utils.MessageFns}<${messageFnsTypeParameters}>`];
|
|
161
|
+
if (options.outputEncodeMethods &&
|
|
162
|
+
options.outputExtensions &&
|
|
163
|
+
options.unknownFields &&
|
|
164
|
+
message.extensionRange.length) {
|
|
165
|
+
interfaces.push((0, ts_poet_1.code) `${utils.ExtensionFns}<${fullName}>`);
|
|
166
|
+
}
|
|
167
|
+
if ((0, types_1.isStructTypeName)(fullTypeName)) {
|
|
168
|
+
interfaces.push((0, ts_poet_1.code) `${utils.StructWrapperFns}`);
|
|
169
|
+
}
|
|
170
|
+
else if ((0, types_1.isAnyValueTypeName)(fullTypeName)) {
|
|
171
|
+
interfaces.push((0, ts_poet_1.code) `${utils.AnyValueWrapperFns}`);
|
|
172
|
+
}
|
|
173
|
+
else if ((0, types_1.isListValueTypeName)(fullTypeName)) {
|
|
174
|
+
interfaces.push((0, ts_poet_1.code) `${utils.ListValueWrapperFns}`);
|
|
175
|
+
}
|
|
176
|
+
else if ((0, types_1.isFieldMaskTypeName)(fullTypeName)) {
|
|
177
|
+
interfaces.push((0, ts_poet_1.code) `${utils.FieldMaskWrapperFns}`);
|
|
178
|
+
}
|
|
179
|
+
if (options.outputExtensions) {
|
|
180
|
+
for (const extension of message.extension) {
|
|
181
|
+
const name = (0, case_1.maybeSnakeToCamel)(extension.name, ctx.options);
|
|
182
|
+
const type = (0, types_1.toTypeName)(ctx, message, extension);
|
|
183
|
+
interfaces.push((0, ts_poet_1.code) `${utils.ExtensionHolder}<"${name}", ${type}>`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
156
186
|
chunks.push((0, ts_poet_1.code) `
|
|
157
|
-
export const ${(0, ts_poet_1.def)(fullName)} = {
|
|
187
|
+
export const ${(0, ts_poet_1.def)(fullName)}: ${(0, ts_poet_1.joinCode)(interfaces, { on: " & " })} = {
|
|
158
188
|
${(0, ts_poet_1.joinCode)(staticMembers, { on: ",\n\n" })}
|
|
159
189
|
};
|
|
160
190
|
`);
|
|
@@ -281,17 +311,20 @@ function generateFile(ctx, fileDesc) {
|
|
|
281
311
|
function makeUtils(options) {
|
|
282
312
|
const bytes = makeByteUtils(options);
|
|
283
313
|
const longs = makeLongUtils(options, bytes);
|
|
314
|
+
const deepPartial = makeDeepPartial(options, longs);
|
|
315
|
+
const extension = makeExtensionClass(options);
|
|
284
316
|
return {
|
|
285
317
|
...bytes,
|
|
286
|
-
...
|
|
318
|
+
...deepPartial,
|
|
287
319
|
...makeObjectIdMethods(),
|
|
288
320
|
...makeTimestampMethods(options, longs, bytes),
|
|
289
321
|
...longs,
|
|
290
322
|
...makeComparisonUtils(),
|
|
291
323
|
...makeNiceGrpcServerStreamingMethodResult(options),
|
|
292
324
|
...makeGrpcWebErrorClass(bytes),
|
|
293
|
-
...
|
|
325
|
+
...extension,
|
|
294
326
|
...makeAssertionUtils(bytes),
|
|
327
|
+
...makeMessageFns(options, deepPartial, extension),
|
|
295
328
|
};
|
|
296
329
|
}
|
|
297
330
|
function makeProtobufTimestampWrapper() {
|
|
@@ -461,6 +494,121 @@ function makeDeepPartial(options, longs) {
|
|
|
461
494
|
`);
|
|
462
495
|
return { Builtin, DeepPartial, Exact };
|
|
463
496
|
}
|
|
497
|
+
function makeMessageFns(options, deepPartial, extension) {
|
|
498
|
+
const BinaryWriter = (0, ts_poet_1.imp)("BinaryWriter@@bufbuild/protobuf/wire");
|
|
499
|
+
const BinaryReader = (0, ts_poet_1.imp)("BinaryReader@@bufbuild/protobuf/wire");
|
|
500
|
+
const { Exact, DeepPartial } = deepPartial;
|
|
501
|
+
const { Extension } = extension;
|
|
502
|
+
const commonStaticMembers = [];
|
|
503
|
+
const extensionStaticMembers = [];
|
|
504
|
+
const hasTypeMember = options.outputTypeAnnotations || options.outputTypeRegistry;
|
|
505
|
+
if (hasTypeMember) {
|
|
506
|
+
commonStaticMembers.push((0, ts_poet_1.code) `readonly $type: V;`);
|
|
507
|
+
}
|
|
508
|
+
if (options.outputEncodeMethods) {
|
|
509
|
+
if (options.outputEncodeMethods === true ||
|
|
510
|
+
options.outputEncodeMethods === "encode-only" ||
|
|
511
|
+
options.outputEncodeMethods === "encode-no-creation") {
|
|
512
|
+
commonStaticMembers.push((0, ts_poet_1.code) `
|
|
513
|
+
encode(message: T, writer?: ${BinaryWriter}): ${BinaryWriter};
|
|
514
|
+
`);
|
|
515
|
+
if (options.outputExtensions && options.unknownFields) {
|
|
516
|
+
extensionStaticMembers.push((0, ts_poet_1.code) `
|
|
517
|
+
setExtension<E>(message: T, extension: ${Extension}<E>, value: E): void;
|
|
518
|
+
`);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
if (options.outputEncodeMethods === true || options.outputEncodeMethods === "decode-only") {
|
|
522
|
+
commonStaticMembers.push((0, ts_poet_1.code) `
|
|
523
|
+
decode(input: ${BinaryReader} | Uint8Array, length?: number): T;
|
|
524
|
+
`);
|
|
525
|
+
if (options.outputExtensions && options.unknownFields) {
|
|
526
|
+
extensionStaticMembers.push((0, ts_poet_1.code) `
|
|
527
|
+
getExtension<E>(message: T, extension: ${Extension}<E>): E | undefined;
|
|
528
|
+
`);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
if (options.useAsyncIterable) {
|
|
533
|
+
commonStaticMembers.push((0, ts_poet_1.code) `
|
|
534
|
+
encodeTransform(
|
|
535
|
+
source: AsyncIterable<T | T[]> | Iterable<T | T[]>
|
|
536
|
+
): AsyncIterable<Uint8Array>;
|
|
537
|
+
`);
|
|
538
|
+
commonStaticMembers.push((0, ts_poet_1.code) `
|
|
539
|
+
decodeTransform(
|
|
540
|
+
source: AsyncIterable<Uint8Array | Uint8Array[]> | Iterable<Uint8Array | Uint8Array[]>
|
|
541
|
+
): AsyncIterable<T>;
|
|
542
|
+
`);
|
|
543
|
+
}
|
|
544
|
+
if (options.outputJsonMethods) {
|
|
545
|
+
if (options.outputJsonMethods === true || options.outputJsonMethods === "from-only") {
|
|
546
|
+
commonStaticMembers.push((0, ts_poet_1.code) `fromJSON(object: any): T;`);
|
|
547
|
+
}
|
|
548
|
+
if (options.outputJsonMethods === true || options.outputJsonMethods === "to-only") {
|
|
549
|
+
commonStaticMembers.push((0, ts_poet_1.code) `toJSON(message: T): unknown;`);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (options.outputPartialMethods) {
|
|
553
|
+
if (options.useExactTypes) {
|
|
554
|
+
commonStaticMembers.push((0, ts_poet_1.code) `create<I extends ${Exact}<${DeepPartial}<T>, I>>(base?: I): T;`);
|
|
555
|
+
commonStaticMembers.push((0, ts_poet_1.code) `fromPartial<I extends ${Exact}<${DeepPartial}<T>, I>>(object: I): T;`);
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
commonStaticMembers.push((0, ts_poet_1.code) `create(base?: DeepPartial<T>): T;`);
|
|
559
|
+
commonStaticMembers.push((0, ts_poet_1.code) `fromPartial(object: DeepPartial<T>): T;`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
const maybeExport = options.exportCommonSymbols ? "export" : "";
|
|
563
|
+
const MessageFns = (0, ts_poet_1.conditionalOutput)("MessageFns", (0, ts_poet_1.code) `
|
|
564
|
+
${maybeExport} interface MessageFns<T${hasTypeMember ? ", V extends string" : ""}> {
|
|
565
|
+
${(0, ts_poet_1.joinCode)(commonStaticMembers, { on: "\n" })}
|
|
566
|
+
}
|
|
567
|
+
`);
|
|
568
|
+
const ExtensionFns = (0, ts_poet_1.conditionalOutput)("ExtensionFns", (0, ts_poet_1.code) `
|
|
569
|
+
${maybeExport} interface ExtensionFns<T> {
|
|
570
|
+
${(0, ts_poet_1.joinCode)(extensionStaticMembers, { on: "\n" })}
|
|
571
|
+
}
|
|
572
|
+
`);
|
|
573
|
+
const ExtensionHolder = (0, ts_poet_1.conditionalOutput)("ExtensionHolder", (0, ts_poet_1.code) `
|
|
574
|
+
${maybeExport} type ExtensionHolder<T extends string, V> = {
|
|
575
|
+
[key in T]: Extension<V>;
|
|
576
|
+
}
|
|
577
|
+
`);
|
|
578
|
+
const StructWrapperFns = (0, ts_poet_1.conditionalOutput)("StructWrapperFns", (0, ts_poet_1.code) `
|
|
579
|
+
${maybeExport} interface StructWrapperFns {
|
|
580
|
+
wrap(object: {[key: string]: any} | undefined): Struct;
|
|
581
|
+
unwrap(message: Struct): {[key: string]: any};
|
|
582
|
+
}
|
|
583
|
+
`);
|
|
584
|
+
const AnyValueWrapperFns = (0, ts_poet_1.conditionalOutput)("AnyValueWrapperFns", (0, ts_poet_1.code) `
|
|
585
|
+
${maybeExport} interface AnyValueWrapperFns {
|
|
586
|
+
wrap(value: any): Value;
|
|
587
|
+
unwrap(message: any): string | number | boolean | Object | null | Array<any> | undefined;
|
|
588
|
+
}
|
|
589
|
+
`);
|
|
590
|
+
const ListValueWrapperFns = (0, ts_poet_1.conditionalOutput)("ListValueWrapperFns", (0, ts_poet_1.code) `
|
|
591
|
+
${maybeExport} interface ListValueWrapperFns {
|
|
592
|
+
wrap(array: ${options.useReadonlyTypes ? "Readonly" : ""}Array<any> | undefined): ListValue;
|
|
593
|
+
unwrap(message: ${options.useReadonlyTypes ? "any" : "ListValue"}): Array<any>;
|
|
594
|
+
}
|
|
595
|
+
`);
|
|
596
|
+
const FieldMaskWrapperFns = (0, ts_poet_1.conditionalOutput)("FieldMaskWrapperFns", (0, ts_poet_1.code) `
|
|
597
|
+
${maybeExport} interface FieldMaskWrapperFns {
|
|
598
|
+
wrap(paths: ${options.useReadonlyTypes ? "readonly" : ""} string[]): FieldMask;
|
|
599
|
+
unwrap(message: ${options.useReadonlyTypes ? "any" : "FieldMask"}): string[] ${options.useOptionals === "all" ? "| undefined" : ""};
|
|
600
|
+
}
|
|
601
|
+
`);
|
|
602
|
+
return {
|
|
603
|
+
MessageFns,
|
|
604
|
+
ExtensionFns,
|
|
605
|
+
ExtensionHolder,
|
|
606
|
+
StructWrapperFns,
|
|
607
|
+
AnyValueWrapperFns,
|
|
608
|
+
ListValueWrapperFns,
|
|
609
|
+
FieldMaskWrapperFns,
|
|
610
|
+
};
|
|
611
|
+
}
|
|
464
612
|
function makeObjectIdMethods() {
|
|
465
613
|
const mongodb = (0, ts_poet_1.imp)("mongodb*mongodb");
|
|
466
614
|
const fromProtoObjectId = (0, ts_poet_1.conditionalOutput)("fromProtoObjectId", (0, ts_poet_1.code) `
|