posthog-node 5.60.1 → 5.61.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/dist/client.d.ts +34 -0
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +33 -4
  4. package/dist/client.mjs +33 -4
  5. package/dist/emails/emails.d.ts +45 -0
  6. package/dist/emails/emails.d.ts.map +1 -0
  7. package/dist/emails/emails.js +360 -0
  8. package/dist/emails/emails.mjs +316 -0
  9. package/dist/emails/errors.d.ts +37 -0
  10. package/dist/emails/errors.d.ts.map +1 -0
  11. package/dist/emails/errors.js +79 -0
  12. package/dist/emails/errors.mjs +41 -0
  13. package/dist/emails/types.d.ts +112 -0
  14. package/dist/emails/types.d.ts.map +1 -0
  15. package/dist/emails/types.js +18 -0
  16. package/dist/emails/types.mjs +1 -0
  17. package/dist/exports.d.ts +3 -0
  18. package/dist/exports.d.ts.map +1 -1
  19. package/dist/exports.js +19 -9
  20. package/dist/exports.mjs +1 -0
  21. package/dist/extensions/error-tracking/autocapture.d.ts +32 -2
  22. package/dist/extensions/error-tracking/autocapture.d.ts.map +1 -1
  23. package/dist/extensions/error-tracking/autocapture.js +337 -35
  24. package/dist/extensions/error-tracking/autocapture.mjs +324 -28
  25. package/dist/extensions/error-tracking/index.d.ts +16 -1
  26. package/dist/extensions/error-tracking/index.d.ts.map +1 -1
  27. package/dist/extensions/error-tracking/index.js +23 -8
  28. package/dist/extensions/error-tracking/index.mjs +24 -9
  29. package/dist/types.d.ts +33 -2
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/version.d.ts +1 -1
  32. package/dist/version.js +1 -1
  33. package/dist/version.mjs +1 -1
  34. package/package.json +2 -2
  35. package/src/client.ts +76 -3
  36. package/src/emails/emails.ts +459 -0
  37. package/src/emails/errors.ts +90 -0
  38. package/src/emails/types.ts +108 -0
  39. package/src/exports.ts +12 -0
  40. package/src/extensions/error-tracking/autocapture.ts +579 -57
  41. package/src/extensions/error-tracking/index.ts +48 -10
  42. package/src/types.ts +34 -2
  43. package/src/version.ts +1 -1
@@ -1,4 +1,5 @@
1
- import { addUncaughtExceptionListener, addUnhandledRejectionListener } from './autocapture'
1
+ import { capturePendingExceptions, claimExplicitCapture, startExceptionAutocapture } from './autocapture'
2
+ import type { AutocaptureClient } from './autocapture'
2
3
  import { PostHogBackendClient } from '@/client'
3
4
  import type { ContextData } from '../context/types'
4
5
  import { snapshotEventContext } from '../context/event-context'
@@ -15,6 +16,8 @@ export default class ErrorTracking {
15
16
  private _exceptionAutocaptureEnabled: boolean
16
17
  private _rateLimiter: BucketedRateLimiter<string>
17
18
  private _logger: Logger
19
+ private _autocaptureClient?: AutocaptureClient
20
+ private _stopAutocapture?: () => void
18
21
 
19
22
  constructor(client: PostHogBackendClient, options: PostHogOptions, _logger: Logger) {
20
23
  this.client = client
@@ -76,10 +79,44 @@ export default class ErrorTracking {
76
79
  })
77
80
  }
78
81
 
82
+ // Clients with the same project token and host send to the same project.
83
+ private get destinationKey(): string {
84
+ return `${this.client.apiKey}@${this.client.host}`
85
+ }
86
+
79
87
  private startAutocaptureIfEnabled(): void {
80
88
  if (this.isEnabled()) {
81
- addUncaughtExceptionListener(this.onException.bind(this), this.onFatalError.bind(this))
82
- addUnhandledRejectionListener(this.onException.bind(this))
89
+ this._autocaptureClient = {
90
+ key: this.destinationKey,
91
+ capture: (exception, hint) => this.onException(exception, hint),
92
+ getContext: () => this.client.getContext(),
93
+ flush: () => this.client.flush(),
94
+ shutdown: () => this.client.shutdown(SHUTDOWN_TIMEOUT),
95
+ }
96
+ this._stopAutocapture = startExceptionAutocapture(this._autocaptureClient)
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Whether captureException should capture this error. False when exception autocapture already sent the same error
102
+ * object to this project in this turn of the event loop, for example when a framework passes an error from its own
103
+ * process listener to an error hook that calls captureException.
104
+ */
105
+ claimExplicitCapture(error: unknown): boolean {
106
+ return claimExplicitCapture(error, this.destinationKey)
107
+ }
108
+
109
+ /**
110
+ * Captures at once the exceptions autocapture holds until the end of the turn, so that this client's flush or
111
+ * shutdown sends them: an app's crash handler often flushes or shuts the client down and exits before the turn ends.
112
+ * The capture can land in another client of the project, the one whose request context is active, so this client
113
+ * waits for that client's flush too.
114
+ */
115
+ capturePendingExceptions(): void {
116
+ for (const owner of capturePendingExceptions()) {
117
+ if (owner !== this._autocaptureClient) {
118
+ void this.client.addPendingPromise(owner.flush().catch(() => undefined))
119
+ }
83
120
  }
84
121
  }
85
122
 
@@ -106,21 +143,22 @@ export default class ErrorTracking {
106
143
  }
107
144
  return this.client._capturePreparedEvent(eventMessage, false)
108
145
  }
109
- })()
146
+ })().catch((error) => {
147
+ // shutdown() waits for every pending promise and stops before it flushes if one rejects, so an error that
148
+ // cannot be turned into an event would take the others down with it.
149
+ this._logger.error('Failed to capture an exception:', error)
150
+ })
110
151
  )
111
152
  }
112
153
 
113
- private async onFatalError(exception: Error): Promise<void> {
114
- console.error(exception)
115
- await this.client.shutdown(SHUTDOWN_TIMEOUT)
116
- globalThis.process.exit(1)
117
- }
118
-
119
154
  isEnabled(): boolean {
120
155
  return !this.client.isDisabled && this._exceptionAutocaptureEnabled
121
156
  }
122
157
 
123
158
  shutdown(): void {
124
159
  this._rateLimiter.stop()
160
+ this.capturePendingExceptions()
161
+ this._stopAutocapture?.()
162
+ this._stopAutocapture = undefined
125
163
  }
126
164
  }
package/src/types.ts CHANGED
@@ -15,6 +15,7 @@ import type {
15
15
  } from '@posthog/core'
16
16
  import { ContextData, ContextOptions } from './extensions/context/types'
17
17
 
18
+ import type { Emails } from './emails/types'
18
19
  import type { FeatureFlagEvaluations } from './feature-flag-evaluations'
19
20
  import type { FlagDefinitionCacheInput, FlagDefinitionCacheProvider } from './extensions/feature-flags/cache'
20
21
 
@@ -229,12 +230,15 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
229
230
  */
230
231
  traces?: TracesConfig
231
232
  /**
232
- * Credential for fetching local feature flag definitions and decrypting remote config.
233
+ * Credential for fetching local feature flag definitions, decrypting remote config and
234
+ * sending email.
233
235
  *
234
236
  * Accepts either a Personal API Key (`phx_...`) or a Project Secret API Key (`phs_...`).
235
237
  * Local evaluation can also use definitions from `flagDefinitionCacheProvider` without
236
238
  * this credential. Remote config payload decryption via `getRemoteConfigPayload`
237
- * still requires it. Prefer this over the deprecated
239
+ * still requires it. `emails.send` needs a Project Secret API Key with the
240
+ * `hog_flow:write` scope; set `enableLocalEvaluation: false` when the key is only for email.
241
+ * Prefer this over the deprecated
238
242
  * `personalApiKey` option; when both are set, `secretKey` takes precedence.
239
243
  *
240
244
  * @example
@@ -247,6 +251,28 @@ export type PostHogOptions = Omit<PostHogCoreOptions, 'before_send' | 'flushInte
247
251
  * @deprecated Use `secretKey` instead.
248
252
  */
249
253
  personalApiKey?: string
254
+ /**
255
+ * Your PostHog app host, such as `https://posthog.example.com`. `emails.send` sends there.
256
+ *
257
+ * Needed only when `host` is not PostHog Cloud, for example a reverse proxy, which must not
258
+ * receive the secret key. On PostHog Cloud, the client derives `https://us.posthog.com` or
259
+ * `https://eu.posthog.com` from `host`.
260
+ */
261
+ appHost?: string
262
+ /**
263
+ * Timeout in milliseconds for each `emails.send` request.
264
+ *
265
+ * @default 15000
266
+ */
267
+ emailRequestTimeoutMs?: number
268
+ /**
269
+ * Number of retries for an `emails.send` after a network error, a rate limit, or an
270
+ * unavailable service. Every retry sends the same idempotency key. Set to 0 when a job
271
+ * runner already retries the send.
272
+ *
273
+ * @default 2
274
+ */
275
+ emailRequestMaxRetries?: number
250
276
  /**
251
277
  * Redact prompts, outputs and error details from every event that the PostHog AI
252
278
  * integrations (`@posthog/ai`) capture with this client. Per-call privacy settings
@@ -922,6 +948,12 @@ export interface IPostHog {
922
948
  */
923
949
  readonly metrics: Metrics
924
950
 
951
+ /**
952
+ * @description The `posthog.emails` API: sends transactional email from PostHog email templates.
953
+ * Needs `secretKey`, a project secret API key with the `hog_flow:write` scope.
954
+ */
955
+ readonly emails: Emails
956
+
925
957
  /**
926
958
  * @description Starts a span without making it active, for work that can't wrap a callback.
927
959
  * Prefer `withSpan`. Always returns a handle — an inert one when tracing is off — so calling
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = "5.60.1"
1
+ export const version = "5.61.1"