Skip to content

fix: this.identity reverse compatibility #302

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

Merged
merged 5 commits into from
May 13, 2025
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
9 changes: 8 additions & 1 deletion flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const Flagsmith = class {
};
if (identifier) {
this.evaluationContext.identity.identifier = identifier;
this.identity = identifier;
}
}
this.flags = flags;
Expand Down Expand Up @@ -277,6 +278,7 @@ const Flagsmith = class {
flags:IFlags|null= null
getFlagInterval: NodeJS.Timer|null= null
headers?: object | null= null
identity:string|null|undefined = null
initialised= false
oldFlags:IFlags|null= null
onChange:IInitConfig['onChange']|null= null
Expand Down Expand Up @@ -439,7 +441,7 @@ const Flagsmith = class {
const onRetrievedStorage = async (error: Error | null, res: string | null) => {
if (res) {
let flagsChanged = null
let traitsChanged = null
const traitsChanged = null
try {
const json = JSON.parse(res) as IState;
let cachePopulated = false;
Expand Down Expand Up @@ -563,6 +565,7 @@ const Flagsmith = class {
}

identify(userId?: string | null, traits?: ITraits, transient?: boolean) {
this.identity = userId
this.evaluationContext.identity = {
identifier: userId,
transient: transient,
Expand Down Expand Up @@ -591,6 +594,7 @@ const Flagsmith = class {
flags: this.flags,
ts: this.ts,
evaluationContext: this.evaluationContext,
identity: this.identity,
evaluationEvent: this.evaluationEvent,
} as IState
}
Expand All @@ -602,11 +606,13 @@ const Flagsmith = class {
this.flags = state.flags || this.flags;
this.evaluationContext = state.evaluationContext || this.evaluationContext,
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
this.identity = this.getContext()?.identity?.identifier
this.log("setState called", this)
}
}

logout() {
this.identity = null
this.evaluationContext.identity = null;
if (this.initialised) {
return this.getFlags();
Expand Down Expand Up @@ -676,6 +682,7 @@ const Flagsmith = class {
...evaluationContext,
environment: evaluationContext.environment || this.evaluationContext.environment,
};
this.identity = this.getContext()?.identity?.identifier

if (this.initialised) {
return this.getFlags();
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith",
"version": "9.2.1",
"version": "9.2.2",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"module": "./index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion lib/react-native-flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-flagsmith",
"version": "9.2.1",
"version": "9.2.2",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
3 changes: 3 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('Flagsmith.init', () => {
);
expect(getStateToCheck(flagsmith.getState())).toEqual({
...identityState,
identity: testIdentityWithTraits,
evaluationContext: {
...identityState.evaluationContext,
identity: {
Expand Down Expand Up @@ -135,7 +136,9 @@ describe('Flagsmith.init', () => {
status: 200,
text: () => fs.readFile(`./test/data/identities_${identityB}.json`, 'utf8'),
});
expect(flagsmith.identity).toEqual(identityA);
await flagsmith.identify(identityB);
expect(flagsmith.identity).toEqual(identityB);
expect(flagsmith.getTrait('a')).toEqual(undefined);
mockFetch.mockResolvedValueOnce({
status: 200,
Expand Down
2 changes: 2 additions & 0 deletions test/test-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const defaultState = {
export const testIdentity = 'test_identity'
export const identityState = {
api: 'https://edge.api.flagsmith.com/api/v1/',
identity: testIdentity,
evaluationContext: {
environment: {apiKey: environmentID},
identity: {
Expand Down Expand Up @@ -63,6 +64,7 @@ export const defaultStateAlt = {
export function getStateToCheck(_state: IState) {
const state = {
..._state,
identity: _state.evaluationContext?.identity?.identifier,
};
delete state.evaluationEvent;
// @ts-ignore internal property
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface IState<F extends string = string> {
evaluationContext?: EvaluationContext;
evaluationEvent?: Record<string, Record<string, number>> | null;
ts?: number;
identity?: string;
}

declare type ICacheOptions = {
Expand Down