ts-proto 1.163.0 → 1.164.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/README.markdown +4 -0
- package/build/generate-services.js +25 -1
- package/build/options.js +5 -0
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -446,6 +446,10 @@ Generated code will be placed in the Gradle build directory.
|
|
|
446
446
|
|
|
447
447
|
- With `--ts_proto_opt=outputServices=false`, or `=none`, ts-proto will output NO service definitions.
|
|
448
448
|
|
|
449
|
+
- With `--ts_proto_opt=outputBeforeRequest=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `beforeRequest(request: <RequestType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `beforeRequest` before performing it's request.
|
|
450
|
+
|
|
451
|
+
- With `--ts_proto_opt=outputAfterResponse=true`, ts-proto will add a function definition to the Rpc interface definition with the signature: `afterResponse(response: <ResponseType>)`. It will will also automatically set `outputTypeRegistry=true` and `outputServices=true`. Each of the Service's methods will call `afterResponse` before returning the response.
|
|
452
|
+
|
|
449
453
|
- With `--ts_proto_opt=useAbortSignal=true`, the generated services will accept an `AbortSignal` to cancel RPC calls.
|
|
450
454
|
|
|
451
455
|
- With `--ts_proto_opt=useAsyncIterable=true`, the generated services will use `AsyncIterable` instead of `Observable`.
|
|
@@ -88,7 +88,23 @@ function generateRegularRpcMethod(ctx, methodDesc) {
|
|
|
88
88
|
const maybeCtx = options.context ? "ctx," : "";
|
|
89
89
|
const maybeAbortSignal = options.useAbortSignal ? "abortSignal || undefined," : "";
|
|
90
90
|
let encode = (0, ts_poet_1.code) `${rawInputType}.encode(request).finish()`;
|
|
91
|
+
let beforeRequest;
|
|
92
|
+
if (options.outputBeforeRequest) {
|
|
93
|
+
beforeRequest = (0, ts_poet_1.code) `
|
|
94
|
+
if (this.rpc.beforeRequest) {
|
|
95
|
+
this.rpc.beforeRequest(request);
|
|
96
|
+
}`;
|
|
97
|
+
}
|
|
91
98
|
let decode = (0, ts_poet_1.code) `data => ${rawOutputType}.decode(${Reader}.create(data))`;
|
|
99
|
+
if (options.outputAfterResponse) {
|
|
100
|
+
decode = (0, ts_poet_1.code) `data => {
|
|
101
|
+
const response = ${rawOutputType}.decode(${Reader}.create(data));
|
|
102
|
+
if (this.rpc.afterResponse) {
|
|
103
|
+
this.rpc.afterResponse(response);
|
|
104
|
+
}
|
|
105
|
+
return response;
|
|
106
|
+
}`;
|
|
107
|
+
}
|
|
92
108
|
// if (options.useDate && rawOutputType.toString().includes("Timestamp")) {
|
|
93
109
|
// decode = code`data => ${utils.fromTimestamp}(${rawOutputType}.decode(${Reader}.create(data)))`;
|
|
94
110
|
// }
|
|
@@ -131,7 +147,7 @@ function generateRegularRpcMethod(ctx, methodDesc) {
|
|
|
131
147
|
${methodDesc.formattedName}(
|
|
132
148
|
${(0, ts_poet_1.joinCode)(params, { on: "," })}
|
|
133
149
|
): ${(0, types_1.responsePromiseOrObservable)(ctx, methodDesc)} {
|
|
134
|
-
const data = ${encode};
|
|
150
|
+
const data = ${encode}; ${beforeRequest ? beforeRequest : ""}
|
|
135
151
|
const ${returnVariable} = this.rpc.${rpcMethod}(
|
|
136
152
|
${maybeCtx}
|
|
137
153
|
this.service,
|
|
@@ -285,6 +301,13 @@ function generateRpcType(ctx, hasStreamingMethods) {
|
|
|
285
301
|
const maybeContextParam = options.context ? "ctx: Context," : "";
|
|
286
302
|
const maybeAbortSignalParam = options.useAbortSignal ? "abortSignal?: AbortSignal," : "";
|
|
287
303
|
const methods = [[(0, ts_poet_1.code) `request`, (0, ts_poet_1.code) `Uint8Array`, (0, ts_poet_1.code) `Promise<Uint8Array>`]];
|
|
304
|
+
const additionalMethods = [];
|
|
305
|
+
if (options.outputBeforeRequest) {
|
|
306
|
+
additionalMethods.push((0, ts_poet_1.code) `beforeRequest?<T extends { [k in keyof T]: unknown }>(request: T): void;`);
|
|
307
|
+
}
|
|
308
|
+
if (options.outputAfterResponse) {
|
|
309
|
+
additionalMethods.push((0, ts_poet_1.code) `afterResponse?<T extends { [k in keyof T]: unknown }>(response: T): void;`);
|
|
310
|
+
}
|
|
288
311
|
if (hasStreamingMethods) {
|
|
289
312
|
const observable = (0, types_1.observableType)(ctx, true);
|
|
290
313
|
methods.push([(0, ts_poet_1.code) `clientStreamingRequest`, (0, ts_poet_1.code) `${observable}<Uint8Array>`, (0, ts_poet_1.code) `Promise<Uint8Array>`]);
|
|
@@ -307,6 +330,7 @@ function generateRpcType(ctx, hasStreamingMethods) {
|
|
|
307
330
|
${maybeAbortSignalParam}
|
|
308
331
|
): ${method[2]};`);
|
|
309
332
|
});
|
|
333
|
+
additionalMethods.forEach((method) => chunks.push(method));
|
|
310
334
|
chunks.push((0, ts_poet_1.code) ` }`);
|
|
311
335
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
312
336
|
}
|
package/build/options.js
CHANGED
|
@@ -87,6 +87,8 @@ function defaultOptions() {
|
|
|
87
87
|
outputExtensions: false,
|
|
88
88
|
outputIndex: false,
|
|
89
89
|
M: {},
|
|
90
|
+
outputBeforeRequest: false,
|
|
91
|
+
outputAfterResponse: false,
|
|
90
92
|
};
|
|
91
93
|
}
|
|
92
94
|
exports.defaultOptions = defaultOptions;
|
|
@@ -175,6 +177,9 @@ function optionsFromParameter(parameter) {
|
|
|
175
177
|
if (options.outputIndex) {
|
|
176
178
|
options.exportCommonSymbols = false;
|
|
177
179
|
}
|
|
180
|
+
if (options.outputBeforeRequest || options.outputAfterResponse) {
|
|
181
|
+
options.outputServices = [ServiceOption.DEFAULT];
|
|
182
|
+
}
|
|
178
183
|
if (options.unrecognizedEnumValue) {
|
|
179
184
|
// Make sure to cast number options to an actual number
|
|
180
185
|
options.unrecognizedEnumValue = Number(options.unrecognizedEnumValue);
|