1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

chore: drop event hook (#3565)

## About the changes
Ref:
https://docs.getunleash.io/reference/deploy/configuring-unleash#further-customization
> **eventHook** (`function(event, data)`) - (_deprecated in Unleash 4.3_
in favor of the [Webhook addon](../addons/webhook.md)) If provided, this
function will be invoked whenever a feature is mutated. The possible
values for `event` are `'feature-created'`, `'feature-archived'` and
`'feature-revived'`. The `data` argument contains information about the
mutation. Its fields are `type` (string) - the event type (same as
`event`); `createdBy` (string) - the user who performed the mutation;
`data` - the contents of the change. The contents in `data` differs
based on the event type; For `'feature-archived'` and
`'feature-revived'`, the only field will be `name` - the name of the
feature. For `'feature-created'` the data follows a schema defined in
the code
[here](7b7f0b84e8/src/lib/schema/feature-schema.ts (L77)).
See an [api here](/reference/api/legacy/unleash/admin/events).


Related to: https://github.com/Unleash/unleash/issues/1265
This commit is contained in:
Gastón Fournier 2023-04-21 09:44:18 +02:00 committed by GitHub
parent 96633f1a34
commit ca01a79f71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 25 additions and 95 deletions

View File

@ -61,7 +61,6 @@ exports[`should create default config 1`] = `
"_maxListeners": undefined,
Symbol(kCapture): false,
},
"eventHook": undefined,
"experimental": {
"externalResolver": {
"isEnabled": [Function],

View File

@ -45,13 +45,14 @@ import {
import FlagResolver from './util/flag-resolver';
import { validateOrigins } from './util/validateOrigin';
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
const safeToUpper = (s?: string) => (s ? s.toUpperCase() : s);
export function authTypeFromString(
s?: string,
defaultType: IAuthType = IAuthType.OPEN_SOURCE,
): IAuthType {
return IAuthType[safeToUpper(s)] || defaultType;
const upperS = safeToUpper(s);
return upperS && IAuthType[upperS] ? IAuthType[upperS] : defaultType;
}
function mergeAll<T>(objects: Partial<T>[]): T {
@ -93,7 +94,7 @@ function loadClientCachingOptions(
return mergeAll([
defaultClientCachingOptions,
options.clientFeatureCaching,
options.clientFeatureCaching || {},
envs,
]);
}
@ -249,7 +250,10 @@ const formatServerOptions = (
};
};
const loadTokensFromString = (tokenString: String, tokenType: ApiTokenType) => {
const loadTokensFromString = (
tokenString: String | undefined,
tokenType: ApiTokenType,
) => {
if (!tokenString) {
return [];
}
@ -297,7 +301,7 @@ const loadEnvironmentEnableOverrides = () => {
};
const parseCspConfig = (
cspConfig: ICspDomainOptions,
cspConfig?: ICspDomainOptions,
): ICspDomainConfig | undefined => {
if (!cspConfig) {
return undefined;
@ -366,12 +370,12 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
defaultDbOptions,
dbPort(extraDbOptions),
dbPort(fileDbOptions),
options.db,
options.db || {},
]);
const session: ISessionOption = mergeAll([
defaultSessionOption,
options.session,
options.session || {},
]);
const logLevel =
@ -381,12 +385,12 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
const server: IServerOption = mergeAll([
defaultServerOption,
formatServerOptions(options.server),
formatServerOptions(options.server) || {},
]);
const versionCheck: IVersionOption = mergeAll([
defaultVersionOption,
options.versionCheck,
options.versionCheck || {},
]);
const initApiTokens = loadInitApiTokens();
@ -403,7 +407,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
const importSetting: IImportOption = mergeAll([
defaultImport,
options.import,
options.import || {},
]);
const experimental = loadExperimental(options);
@ -411,7 +415,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
const ui = loadUI(options);
const email: IEmailOption = mergeAll([defaultEmail, options.email]);
const email: IEmailOption = mergeAll([defaultEmail, options.email || {}]);
let listen: IListeningPipe | IListeningHost;
if (server.pipe) {
@ -483,7 +487,6 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
disableLegacyFeaturesApi,
preHook: options.preHook,
preRouterHook: options.preRouterHook,
eventHook: options.eventHook,
enterpriseVersion: options.enterpriseVersion,
eventBus: new EventEmitter(),
environmentEnableOverrides,

View File

@ -1,29 +0,0 @@
import { EventEmitter } from 'events';
import { addEventHook } from './event-hook';
import {
FEATURE_CREATED,
FEATURE_UPDATED,
FEATURE_ARCHIVED,
FEATURE_REVIVED,
} from './types/events';
const eventStore = new EventEmitter();
const o = {};
function testHook(feature, data) {
o[feature] = data;
}
beforeAll(() => {
addEventHook(testHook, eventStore);
});
[FEATURE_CREATED, FEATURE_UPDATED, FEATURE_ARCHIVED, FEATURE_REVIVED].forEach(
(feature) => {
test(`should invoke hook on ${feature}`, () => {
const data = { dataKey: feature };
eventStore.emit(feature, data);
expect(o[feature] === data).toBe(true);
});
},
);

View File

@ -1,26 +0,0 @@
import EventEmitter from 'events';
import { EventHook } from './types/option';
import {
FEATURE_CREATED,
FEATURE_UPDATED,
FEATURE_ARCHIVED,
FEATURE_REVIVED,
} from './types/events';
export const addEventHook = (
eventHook: EventHook,
eventStore: Pick<EventEmitter, 'on'>,
): void => {
eventStore.on(FEATURE_CREATED, (data) => {
eventHook(FEATURE_CREATED, data);
});
eventStore.on(FEATURE_UPDATED, (data) => {
eventHook(FEATURE_UPDATED, data);
});
eventStore.on(FEATURE_ARCHIVED, (data) => {
eventHook(FEATURE_ARCHIVED, data);
});
eventStore.on(FEATURE_REVIVED, (data) => {
eventHook(FEATURE_REVIVED, data);
});
};

View File

@ -1,6 +1,5 @@
export * from './logger';
export * from './metrics';
export * from './event-hook';
export * from './metric-events';
export * from './default-custom-auth-deny-all';
export * from './addons';

View File

@ -93,20 +93,6 @@ test('should call preRouterHook', async () => {
await stop();
});
test('should call eventHook', async () => {
let called = 0;
const config = createTestConfig({
server: { port: 0 },
eventHook: () => {
called++;
},
});
const { stop } = await start(config);
eventStore.emit('feature-created', {});
expect(called === 1).toBe(true);
await stop();
});
test('should auto-create server on start()', async () => {
const { server, stop } = await start(
createTestConfig({ server: { port: 0 } }),

View File

@ -7,7 +7,6 @@ import { createMetricsMonitor } from './metrics';
import { createStores } from './db';
import { createServices, scheduleServices } from './services';
import { createConfig } from './create-config';
import { addEventHook } from './event-hook';
import registerGracefulShutdown from './util/graceful-shutdown';
import { createDb } from './db/db-pool';
import sessionDb from './middleware/session-db';
@ -70,9 +69,6 @@ async function createApp(
}
const app = await getApp(config, stores, services, unleashSession, db);
if (typeof config.eventHook === 'function') {
addEventHook(config.eventHook, stores.eventStore);
}
await metricsMonitor.startMonitoring(
config,
stores,

View File

@ -4,8 +4,6 @@ import { ILegacyApiTokenCreate } from './models/api-token';
import { IFlagResolver, IExperimentalOptions, IFlags } from './experimental';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
export type EventHook = (eventName: string, data: object) => void;
export interface ISSLOption {
rejectUnauthorized: boolean;
ca?: string;
@ -112,7 +110,6 @@ export interface IUnleashOptions {
enableOAS?: boolean;
preHook?: Function;
preRouterHook?: Function;
eventHook?: EventHook;
enterpriseVersion?: string;
disableLegacyFeaturesApi?: boolean;
inlineSegmentConstraints?: boolean;
@ -195,7 +192,6 @@ export interface IUnleashConfig {
enableOAS: boolean;
preHook?: Function;
preRouterHook?: Function;
eventHook?: EventHook;
enterpriseVersion?: string;
eventBus: EventEmitter;
disableLegacyFeaturesApi?: boolean;

View File

@ -1,14 +1,14 @@
export const formatBaseUri = (input: string): string => {
export const formatBaseUri = (input?: string): string => {
if (!input) return '';
const firstChar = input[0];
const lastChar = input[input.length - 1];
if (firstChar === '/' && lastChar === '/') {
return input.substr(0, input.length - 1);
return input.substring(0, input.length - 1);
}
if (firstChar !== '/' && lastChar === '/') {
return `/${input.substr(0, input.length - 1)}`;
return `/${input.substring(0, input.length - 1)}`;
}
if (firstChar !== '/') {

View File

@ -1,4 +1,10 @@
export function parseEnvVarNumber(envVar: string, defaultVal: number): number {
export function parseEnvVarNumber(
envVar: string | undefined,
defaultVal: number,
): number {
if (!envVar) {
return defaultVal;
}
const parsed = Number.parseInt(envVar, 10);
if (Number.isNaN(parsed)) {