-
Notifications
You must be signed in to change notification settings - Fork 31
refactor: update LDClient to use LDContextPartial for identify methods #1045
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
base: main
Are you sure you want to change the base?
Conversation
|
@launchdarkly/browser size report |
|
@launchdarkly/js-sdk-common size report |
|
@launchdarkly/js-client-sdk-common size report |
|
@launchdarkly/js-client-sdk size report |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this typing looks good for a multi-kind context, but not for a single kind context. In that this typing for a single kind would make key appear to be not an available option.
I wonder if we should just be using basically a copy of the single/multi context kinds with key correct specified as being optional. Then converting that for internal use?
Though, I suppose, it could be an Omit and then & { key?: string }. I want to make sure we have somewhat intelligible code completion though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it could be type singleKindOptionalKey = Omit<LDSingleKindContextBase, 'key'> | LDSingleKindContextBase
Though again I am not sure how good the intellisense for that would be. Maybe fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there is something fundamentally wrong here... so the ensureKey function will ONLY federate the context with a key if the context is anonymous:
| if (anonymous && !key) { |
So given that perhaps the correct thing to do is create an anonymous context interface, eg:
interface anonymousSingleKindContext extends Omit<LDSingleKindContextBase, 'key'> {
key?: string;
anonymous: boolean;
}
And do the same for multi of course
Then ultimately the LDContext that could be used in a client side identify will be:
type LDContextWithAnonymous =
| LDSingleKindContext // original single kind context
| anonymousSingleKindContext // anon context that could be missing key
| multiKindContextWithAnonymous;
@kinyoklion FYI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like the right path.
We do need to also consider what the impact of this will be consumption side. My reflex is that it should be exported as LDContext, but if not, then we need to make sure that LDContext doesn't upset the structural typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ended up going the route of introducing a new type since I think there is still some value in having a context type that can match a context that is "ensured". Typescript is a bit finicky.
c423aaf to
a4097a4
Compare
- Changed the `identify` and `identifyResult` methods in `LDClient` to accept `LDContextPartial` instead of `LDContext`, allowing for more flexible context handling. - Introduced `LDContextPartial` type in a new `LDContext.ts` file to define contexts that may not have a key. - Updated documentation around the partial context to explain that client side sdks will assign a key to a context that does not have one.
a4097a4 to
7cfcaad
Compare
7cfcaad to
10d285d
Compare
Requirements
Related issues
sdk-1709
Describe the solution you've provided
identifyandidentifyResultmethods inLDClientto acceptLDContextWithAnonymousinstead ofLDContext, allowing for more flexible context handling.LDContextWithAnonymoustype in a newLDContext.tsfile to define contexts that may not have a key.Provide a clear and concise description of what you expect to happen.
Additional context
This change is prompted by seeing the type inconsistencies between v3 and v4 browser sdk. Since we have an
ensureKeyfunction that will fedrate a context id to the context if it does not have one, then we should also be able to support passing in contexts that do not have keys.Note
Aligns identify APIs to accept contexts without pre-generated keys and centralizes typing for anonymous contexts.
LDContextWithAnonymoustype inshared/sdk-client/src/api/LDContextand re-exports from package entry pointsidentify/identifyResultsignatures inLDClient,LDClientImpl,BrowserClient, andcreateClientto useLDContextWithAnonymousensureKeyto acceptLDContextWithAnonymousand generate keys for anonymous single/multi-kind contextskeyand updates related importscommon.ts,index.ts, and API surfacesWritten by Cursor Bugbot for commit 10d285d. This will update automatically on new commits. Configure here.