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

fix: PostHogProvider initialization logic #206

Merged
merged 2 commits into from
Mar 13, 2024

Conversation

AdamDorwart
Copy link
Contributor

Problem

A regression introduced by #189 requires a client and apiKey to initialize a PostHogProvider even though the error message says only one is required.

Changes

The error condition logic is fixed and a non null assertion is made.

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Libraries affected

  • All of them
  • posthog-web
  • posthog-node
  • posthog-react-native

Changelog notes

  • Fix: PostHogProvider initialization

A regression introduced by PostHog#189 requires a client and apiKey to
intialize a PostHogProvider even though the error message says only one
is required.

The error condition logic is fixed and a non null assertion is made.
Copy link
Collaborator

@benjackwhite benjackwhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oooh crikey that is definitely an oversight. Thanks for finding that!

@AdamDorwart
Copy link
Contributor Author

AdamDorwart commented Mar 13, 2024

nbd, it happens 😄

I'm pretty fresh to Typescript. How would you prefer I resolve:

Forbidden non-null assertion  @typescript-eslint/no-non-null-assertion
     return client ?? new PostHog(apiKey!, options)

I could just

    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion

This also works but I'm not sure if there's a more idiomatic way to go about it without the throw at the end.

    if (client) {
      return client
    } else if (apiKey) {
      return new PostHog(apiKey, options) 
    }

    throw new Error('Type logic error: This should never happen')

@marandaneto
Copy link
Member

@AdamDorwart I fixed the linting issue by just adding apiKey ?? '' since this code path won't be triggered anyway, less code and easier to read, also added the changelog.
Thanks!

@marandaneto marandaneto merged commit f04239e into PostHog:main Mar 13, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants