|
| 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 | +} |
0 commit comments