Skip to content

Commit b79eb96

Browse files
committed
typed action groups
1 parent d1e98eb commit b79eb96

61 files changed

Lines changed: 896 additions & 293 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

x-pack/plugins/alerts/common/alert_type.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@
55
*/
66

77
import { LicenseType } from '../../licensing/common/types';
8+
import { RecoveredActionGroupId, DefaultActionGroupId } from './builtin_action_groups';
89

9-
export interface AlertType {
10+
export interface AlertType<
11+
ActionGroupIds extends Exclude<string, RecoveredActionGroupId> = DefaultActionGroupId,
12+
RecoveryActionGroupId extends string = RecoveredActionGroupId
13+
> {
1014
id: string;
1115
name: string;
12-
actionGroups: ActionGroup[];
13-
recoveryActionGroup: ActionGroup;
16+
actionGroups: Array<ActionGroup<ActionGroupIds>>;
17+
recoveryActionGroup: ActionGroup<RecoveryActionGroupId>;
1418
actionVariables: string[];
15-
defaultActionGroupId: ActionGroup['id'];
19+
defaultActionGroupId: ActionGroupIds;
1620
producer: string;
1721
minimumLicenseRequired: LicenseType;
1822
}
1923

20-
export interface ActionGroup {
21-
id: string;
24+
export interface ActionGroup<ActionGroupIds extends string> {
25+
id: ActionGroupIds;
2226
name: string;
2327
}
28+
29+
export type ActionGroupIdsOf<T> = T extends ActionGroup<infer groups>
30+
? groups
31+
: T extends Readonly<ActionGroup<infer groups>>
32+
? groups
33+
: never;

x-pack/plugins/alerts/common/builtin_action_groups.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
import { i18n } from '@kbn/i18n';
77
import { ActionGroup } from './alert_type';
88

9-
export const RecoveredActionGroup: Readonly<ActionGroup> = {
9+
export type DefaultActionGroupId = 'default';
10+
11+
export type RecoveredActionGroupId = typeof RecoveredActionGroup['id'];
12+
export const RecoveredActionGroup: Readonly<ActionGroup<'recovered'>> = Object.freeze({
1013
id: 'recovered',
1114
name: i18n.translate('xpack.alerts.builtinActionGroups.recovered', {
1215
defaultMessage: 'Recovered',
1316
}),
14-
};
17+
});
1518

16-
export function getBuiltinActionGroups(customRecoveryGroup?: ActionGroup): ActionGroup[] {
17-
return [customRecoveryGroup ?? Object.freeze(RecoveredActionGroup)];
19+
export function getBuiltinActionGroups<RecoveryActionGroupId extends string>(
20+
customRecoveryGroup?: ActionGroup<RecoveryActionGroupId>
21+
): [ActionGroup<RecoveryActionGroupId | RecoveredActionGroupId>] {
22+
return [customRecoveryGroup ?? RecoveredActionGroup];
1823
}

0 commit comments

Comments
 (0)