-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathgenerate-generic-service-definition.ts
89 lines (74 loc) · 2.54 KB
/
generate-generic-service-definition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Code, code, def, joinCode } from 'ts-poet';
import {
FileDescriptorProto,
MethodDescriptorProto,
MethodOptions,
MethodOptions_IdempotencyLevel,
ServiceDescriptorProto,
} from 'ts-proto-descriptors';
import { camelCase } from './case';
import { Context } from './context';
import SourceInfo, { Fields } from './sourceInfo';
import { messageToTypeName } from './types';
import { maybeAddComment, maybePrefixPackage } from './utils';
/**
* Generates a framework-agnostic service descriptor.
*/
export function generateGenericServiceDefinition(
ctx: Context,
fileDesc: FileDescriptorProto,
sourceInfo: SourceInfo,
serviceDesc: ServiceDescriptorProto
) {
const chunks: Code[] = [];
maybeAddComment(sourceInfo, chunks, serviceDesc.options?.deprecated);
// Service definition
chunks.push(code`
export const ${def(`${serviceDesc.name}Definition`)} = {
`);
serviceDesc.options?.uninterpretedOption;
chunks.push(code`
name: '${serviceDesc.name}',
fullName: '${maybePrefixPackage(fileDesc, serviceDesc.name)}',
methods: {
`);
for (const [index, methodDesc] of serviceDesc.method.entries()) {
const info = sourceInfo.lookup(Fields.service.method, index);
maybeAddComment(info, chunks, methodDesc.options?.deprecated);
chunks.push(code`
${camelCase(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
`);
}
chunks.push(code`
},
} as const;
`);
return joinCode(chunks, { on: '\n' });
}
function generateMethodDefinition(ctx: Context, methodDesc: MethodDescriptorProto) {
const inputType = messageToTypeName(ctx, methodDesc.inputType, { keepValueType: true });
const outputType = messageToTypeName(ctx, methodDesc.outputType, { keepValueType: true });
return code`
{
name: '${methodDesc.name}',
requestType: ${inputType},
requestStream: ${methodDesc.clientStreaming},
responseType: ${outputType},
responseStream: ${methodDesc.serverStreaming},
options: ${generateMethodOptions(methodDesc.options)}
}
`;
}
function generateMethodOptions(options: MethodOptions | undefined) {
const chunks: Code[] = [];
chunks.push(code`{`);
if (options != null) {
if (options.idempotencyLevel === MethodOptions_IdempotencyLevel.IDEMPOTENT) {
chunks.push(code`idempotencyLevel: 'IDEMPOTENT',`);
} else if (options.idempotencyLevel === MethodOptions_IdempotencyLevel.NO_SIDE_EFFECTS) {
chunks.push(code`idempotencyLevel: 'NO_SIDE_EFFECTS',`);
}
}
chunks.push(code`}`);
return joinCode(chunks, { on: '\n' });
}