feat: allow defining signal type

this makes using especially the frontend API much usable as the signal
can be typed instead having plain json object to play around with.

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-02-02 08:31:39 +02:00
parent 216fce4b2c
commit 1ab22c40e0
10 changed files with 65 additions and 41 deletions
@@ -16,6 +16,7 @@
import { EventBroker } from '@backstage/plugin-events-node';
import { SignalPayload, SignalServiceOptions } from './types';
import { SignalService } from './SignalService';
import { JsonObject } from '@backstage/types';
/** @public */
export class DefaultSignalService implements SignalService {
@@ -31,12 +32,12 @@ export class DefaultSignalService implements SignalService {
}
/**
* Publishes a message to user refs to specific topic
* @param recipients - string or array of user ref strings to publish message to
* @param topic - message topic
* @param message - message to publish
* Publishes a signal to user refs to specific topic
* @param signal - Signal to publish
*/
async publish(signal: SignalPayload) {
async publish<SignalType extends JsonObject = JsonObject>(
signal: SignalPayload<SignalType>,
) {
await this.eventBroker?.publish({
topic: 'signals',
eventPayload: signal,
+6 -2
View File
@@ -14,11 +14,15 @@
* limitations under the License.
*/
import { SignalPayload } from './types';
import { JsonObject } from '@backstage/types';
/** @public */
export interface SignalService {
/**
* Publishes a message to user refs to specific topic
* Publishes a signal to user refs to specific topic
* @param signal - Signal to publish
*/
publish(signal: SignalPayload): Promise<void>;
publish<SignalType extends JsonObject = JsonObject>(
signal: SignalPayload<SignalType>,
): Promise<void>;
}
+2 -2
View File
@@ -24,8 +24,8 @@ export type SignalServiceOptions = {
};
/** @public */
export type SignalPayload = {
export type SignalPayload<SignalType extends JsonObject = JsonObject> = {
recipients: string[] | string | null;
channel: string;
message: JsonObject;
message: SignalType;
};