Skip to content

Commit 8ec4423

Browse files
rolodatoZaimwa9
andcommitted
Map OpenFeature context to Flagsmith traits
Signed-off-by: Rodrigo López Dato <[email protected]> Co-authored-by: Zaimwa9 <[email protected]>
1 parent dd18406 commit 8ec4423

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

libs/providers/flagsmith-client/src/lib/flagsmith-client-provider.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ export class FlagsmithClientProvider implements Provider {
5656
if (isLogout) {
5757
return this._client.logout();
5858
}
59-
if (identity) {
60-
const { targetingKey, ...traits } = context;
61-
return this._client.identify(identity, traits as any);
59+
if (context?.targetingKey) {
60+
const { targetingKey, ...contextTraits } = context;
61+
// OpenFeature context attributes can be Date objects, but Flagsmith traits can't
62+
const traits: Parameters<IFlagsmith['identify']>[1] = {};
63+
for (const [key, value] of Object.entries(contextTraits)) {
64+
if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
65+
traits[key] = value;
66+
} else if (value instanceof Date) {
67+
traits[key] = value.toISOString();
68+
}
69+
}
70+
return this._client.identify(targetingKey, traits);
6271
}
6372
return this._client.getFlags();
6473
}

0 commit comments

Comments
 (0)