Skip to content

Commit e87e577

Browse files
committed
feat(core-api): add IPluginGrpcService type & user-defined type guard
1. This will be used by the upcoming functionality of the API server that allows all plugins to register their own gRPC services as part of the API server's own gRPC service. 2. The above mechanism will largely be the same conceptually as the one we have for HTTP and SocketIO endpoints already. 3. It is optional for plugins to implement gRPC services and therefore the interface is a standalone one instead of being baked into the more generic IPluginWebService interface for example. Signed-off-by: Peter Somogyvari <[email protected]>
1 parent e42cac7 commit e87e577

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type {
2+
ServiceDefinition,
3+
UntypedServiceImplementation,
4+
} from "@grpc/grpc-js";
5+
6+
/**
7+
* Implementers of this interface are responsible for providing a gRPC service
8+
* that can be dynamically registered at runtime by the API server.
9+
*
10+
* It describes what methods a class (plugin) needs to implement
11+
* in order to be able to operate as a Cacti gRPC service (e.g. expose its
12+
* functionality through gRPC not just HTTP or SocketIO)
13+
*
14+
* @see {IPluginWebService}
15+
* @see {ApiServer}
16+
*/
17+
export interface IPluginGrpcService {
18+
/**
19+
* Used by the API server and/or automated test cases when hooking up this
20+
* plugin instance into a gRPC Server object.
21+
*
22+
* The returned pair of service
23+
* definition and implementation objects are passed in to the `.addService()`
24+
* method of the `Server` object of the `@grpc/grpc-js` library.
25+
*
26+
* @see {ServiceDefinition}
27+
* @see {IGrpcSvcDefAndImplPair}
28+
*/
29+
createGrpcSvcDefAndImplPairs(
30+
opts: unknown,
31+
): Promise<Array<IGrpcSvcDefAndImplPair>>;
32+
}
33+
34+
/**
35+
* A wrapper object that contains both the the definition and the implementation
36+
* objects of a gRPC service that a plugin can implement to expose it's functionality
37+
* over gRPC.
38+
*/
39+
export interface IGrpcSvcDefAndImplPair {
40+
readonly definition: ServiceDefinition;
41+
readonly implementation: UntypedServiceImplementation;
42+
}
43+
44+
/**
45+
* Custom (user-defined) Typescript type-guard that verifies at runtime whether
46+
* `x` is an implementation of {IPluginGrpcService} or not.
47+
*
48+
* @param x Literally any object or value that you'd want to verify for having
49+
* the right method(s) implemented in-order to qualify as an implementer of the
50+
* {IPluginGrpcService} interface.
51+
*
52+
* The {ApiServer} uses this to filter the total list of plugins down to the ones
53+
* that have gRPC services of their own and then hook those up at runtime.
54+
*
55+
* @returns `true` if `x` does implement {IPluginGrpcService} `false` otherwise.
56+
*/
57+
export function isIPluginGrpcService(x: unknown): x is IPluginGrpcService {
58+
return (
59+
!!x &&
60+
typeof (x as IPluginGrpcService).createGrpcSvcDefAndImplPairs === "function"
61+
);
62+
}

packages/cactus-core-api/src/main/typescript/public-api.ts

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ export {
4444
} from "./client/i-verifier";
4545

4646
export { ISendRequestResultV1 } from "./plugin/ledger-connector/i-send-request-response-v1";
47+
48+
export { IPluginGrpcService } from "./plugin/grpc-service/i-plugin-grpc-service";
49+
export { IGrpcSvcDefAndImplPair } from "./plugin/grpc-service/i-plugin-grpc-service";
50+
export { isIPluginGrpcService } from "./plugin/grpc-service/i-plugin-grpc-service";

0 commit comments

Comments
 (0)