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

Validating the posthog connection #372

Open
1 of 4 tasks
aqeelat opened this issue Feb 4, 2025 · 2 comments
Open
1 of 4 tasks

Validating the posthog connection #372

aqeelat opened this issue Feb 4, 2025 · 2 comments
Labels
enhancement New feature or request nodejs

Comments

@aqeelat
Copy link

aqeelat commented Feb 4, 2025

Is your feature request related to a problem?

In Node, in order to make sure we don't have an incorrect api key, it would be great if we can have an async function that checks that our sdk was initialized correctly.

Describe the solution you'd like

maybe await (new PostHog(apiKey)).validate(), or await PostHog.build(apiKey, { throwOnError: true })

Describe alternatives you've considered

I tried calling getAllFlags() and reloadFeatureFlags() but they don't throw, and I don't have flags that I can use to check.

Related sub-libraries

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

Additional context

Thank you for your feature request – we love each and every one!

@aqeelat aqeelat added the enhancement New feature or request label Feb 4, 2025
@samueldurantes
Copy link

@aqeelat I think a good solution would be to create a new function that checks if apiKey is valid by making a request for PostHog server, and then calling it into constructor block, like this:

type OOptions = {
  throwOnError: boolean;
};

class O {
  readonly apiKey: string;
  readonly throwOnError: boolean;

  constructor(apiKey: string, options: OOptions) {
    this.apiKey = apiKey;
    this.throwOnError = options.throwOnError;

    this.init();
  }

  private async init(): Promise<void> {
    const profile = await this.getProfile();

    if (profile) {
      return;
    }

    if (!this.throwOnError) {
      console.log('ERROR: Instance not initialized correctly');

      return;
    }

    throw new Error('ERROR: Instance not initialized correctly');
  }

  private async getProfile(): Promise<boolean> {
    return Promise.resolve(false);
  }
}

new O('api-1234', {
  throwOnError: true,
});

This way, it wouldn't affect the end user because they wouldn't need to call a new function, just instantiate the class normally.

@marandaneto
Copy link
Member

I'd not call in the constructor but rather have as a method you could call yourself if needed.
await posthog.validate() or something.
I think there's an API to validate the apiKey added by @zlwaterfield

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request nodejs
Projects
None yet
Development

No branches or pull requests

3 participants