-
Notifications
You must be signed in to change notification settings - Fork 237
feat(node): add versioned defaults and strictCapture option #3261
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog-node': minor | ||
| --- | ||
|
|
||
| Add `defaults` versioned config and `strictCapture` option to throw on invalid capture() arguments |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -412,6 +412,77 @@ describe('PostHog Node.js', () => { | |
| ) | ||
| warnSpy.mockRestore() | ||
| }) | ||
|
|
||
| it('should throw if capture is called with a string when strictCapture is enabled', async () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| strictCapture: true, | ||
| }) | ||
| // @ts-expect-error - Testing the error when passing a string instead of an object | ||
| expect(() => ph.capture('test-event')).toThrow(TypeError) | ||
| await ph.shutdown() | ||
| }) | ||
|
|
||
| it('should throw if captureImmediate is called with a string when strictCapture is enabled', async () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| strictCapture: true, | ||
| }) | ||
| // @ts-expect-error - Testing the error when passing a string instead of an object | ||
| await expect(ph.captureImmediate('test-event')).rejects.toThrow(TypeError) | ||
| await ph.shutdown() | ||
| }) | ||
|
|
||
| it('should throw if capture is called with a string when defaults >= 2026-03-19', async () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| defaults: '2026-03-19', | ||
| }) | ||
| // @ts-expect-error - Testing the error when passing a string instead of an object | ||
| expect(() => ph.capture('test-event')).toThrow(TypeError) | ||
| await ph.shutdown() | ||
| }) | ||
|
|
||
| it('should throw if captureImmediate is called with a string when defaults >= 2026-03-19', async () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| defaults: '2026-03-19', | ||
| }) | ||
| // @ts-expect-error - Testing the error when passing a string instead of an object | ||
| await expect(ph.captureImmediate('test-event')).rejects.toThrow(TypeError) | ||
| await ph.shutdown() | ||
| }) | ||
|
|
||
| it('should warn if capture is called with a string when defaults is unset', () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| defaults: 'unset', | ||
| }) | ||
| ph.debug(true) | ||
| // @ts-expect-error - Testing the warning when passing a string instead of an object | ||
| ph.capture('test-event') | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| '[PostHog]', | ||
| 'Called capture() with a string as the first argument when an object was expected.' | ||
| ) | ||
| ph.shutdown() | ||
| }) | ||
|
|
||
| it('should allow explicit strictCapture: false to override defaults', async () => { | ||
| const ph = new PostHog('TEST_API_KEY', { | ||
| host: 'http://example.com', | ||
| defaults: '2026-03-19', | ||
| strictCapture: false, | ||
| }) | ||
| ph.debug(true) | ||
| // @ts-expect-error - Testing the warning when passing a string instead of an object | ||
| ph.capture('test-event') | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| '[PostHog]', | ||
| 'Called capture() with a string as the first argument when an object was expected.' | ||
| ) | ||
| await ph.shutdown() | ||
|
||
| }) | ||
| }) | ||
|
|
||
| describe('before_send', () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ import { | |
| GroupIdentifyMessage, | ||
| IdentifyMessage, | ||
| IPostHog, | ||
| NodeConfigDefaults, | ||
| OverrideFeatureFlagsOptions, | ||
| PostHogOptions, | ||
| SendFeatureFlagsOptions, | ||
|
|
@@ -49,6 +50,10 @@ const MAX_CACHE_SIZE = 50 * 1000 | |
| const WAITUNTIL_DEBOUNCE_MS = 50 | ||
| const WAITUNTIL_MAX_WAIT_MS = 500 | ||
|
|
||
| const defaultsThatVaryByConfig = (defaults?: NodeConfigDefaults): Pick<PostHogOptions, 'strictCapture'> => ({ | ||
| strictCapture: !!defaults && defaults !== 'unset' && defaults >= '2026-03-19', | ||
| }) | ||
|
|
||
| // The actual exported Nodejs API. | ||
| export abstract class PostHogBackendClient extends PostHogCoreStateless implements IPostHog { | ||
| private _memoryStorage = new PostHogMemoryStorage() | ||
|
|
@@ -104,7 +109,7 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen | |
| constructor(apiKey: string, options: PostHogOptions = {}) { | ||
| super(apiKey, options) | ||
|
|
||
| this.options = options | ||
| this.options = { ...defaultsThatVaryByConfig(options.defaults), ...options } | ||
lricoy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.context = this.initializeContext() | ||
|
|
||
| this.options.featureFlagsPollingInterval = | ||
|
|
@@ -431,7 +436,11 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen | |
| */ | ||
| capture(props: EventMessage): void { | ||
| if (typeof props === 'string') { | ||
| this._logger.warn('Called capture() with a string as the first argument when an object was expected.') | ||
| const msg = 'Called capture() with a string as the first argument when an object was expected.' | ||
| if (this.options.strictCapture) { | ||
| throw new TypeError(msg) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO SDKs should never throw, SDKs should degrade gracefully and warn/log out if something is wrong (which is whats happening here). |
||
| } | ||
| this._logger.warn(msg) | ||
| } | ||
| if (props.event === '$exception' && !props._originatedFromCaptureException) { | ||
| this._logger.warn( | ||
|
|
@@ -500,7 +509,11 @@ export abstract class PostHogBackendClient extends PostHogCoreStateless implemen | |
| */ | ||
| async captureImmediate(props: EventMessage): Promise<void> { | ||
| if (typeof props === 'string') { | ||
| this._logger.warn('Called captureImmediate() with a string as the first argument when an object was expected.') | ||
| const msg = 'Called captureImmediate() with a string as the first argument when an object was expected.' | ||
| if (this.options.strictCapture) { | ||
| throw new TypeError(msg) | ||
| } | ||
| this._logger.warn(msg) | ||
| } | ||
| if (props.event === '$exception' && !props._originatedFromCaptureException) { | ||
| this._logger.warn( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.