Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: No migration op event for empty flag key. #287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ it('does not generate an event if an op is not set', () => {
},
);

tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});

Expand All @@ -22,8 +24,27 @@ it('does not generate an event with missing context keys', () => {
kind: 'FALLTHROUGH',
});

// Set the op otherwise that would prevent an event as well.
// Set the op otherwise/invoked that would prevent an event as well.
tracker.op('write');
tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});

it('does not generate an event with empty flag key', () => {
const tracker = new MigrationOpTracker(
'',
{ key: 'user-key' },
LDMigrationStage.Off,
LDMigrationStage.Off,
{
kind: 'FALLTHROUGH',
},
);

// Set the op/invoked otherwise that would prevent an event as well.
tracker.op('write');
tracker.invoked('old');

expect(tracker.createEvent()).toBeUndefined();
});
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/sdk-server/src/MigrationOpTracker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { internal, LDEvaluationReason, LDLogger } from '@launchdarkly/js-sdk-common';
import {
internal,
LDEvaluationReason,
LDLogger,
TypeValidators,
} from '@launchdarkly/js-sdk-common';

import { LDMigrationStage, LDMigrationTracker } from './api';
import {
Expand Down Expand Up @@ -82,6 +87,11 @@ export default class MigrationOpTracker implements LDMigrationTracker {
}

createEvent(): LDMigrationOpEvent | undefined {
if (!TypeValidators.String.is(this.flagKey) || this.flagKey === '') {
this.logger?.error('The flag key for a migration operation must be a non-empty string.');
return undefined;
}

if (!this.operation) {
this.logger?.error('The operation must be set using "op" before an event can be created.');
return undefined;
Expand Down
Loading