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
@@ -0,0 +1,90 @@
1
+ /**
2
+ * A coarse category of the failure, for code that branches on the kind of failure rather than the exact `code`.
3
+ */
4
+ export type EmailErrorName =
5
+ | 'validation_error'
6
+ | 'missing_api_key'
7
+ | 'invalid_permission'
8
+ | 'not_found'
9
+ | 'concurrent_idempotent_requests'
10
+ | 'invalid_idempotent_request'
11
+ | 'rate_limit_exceeded'
12
+ | 'monthly_quota_exceeded'
13
+ | 'service_unavailable'
14
+ | 'application_error'
15
+
16
+ export interface EmailErrorParams {
17
+ code: string
18
+ message: string
19
+ statusCode?: number
20
+ why?: string
21
+ fix?: string
22
+ docsUrl?: string
23
+ field?: string
24
+ retryAfterMs?: number
25
+ cause?: unknown
26
+ }
27
+
28
+ /**
29
+ * PostHog did not queue the email, or the client could not tell whether it did.
30
+ *
31
+ * Branch on `code`, PostHog's stable error code, or on `name`, a coarse category of the
32
+ * failure. The message never contains the secret key or the request body.
33
+ */
34
+ export class EmailError extends Error {
35
+ override name: EmailErrorName
36
+ /** PostHog's error code, such as `quota_limited`, or an SDK code such as `network_error`. */
37
+ readonly code: string
38
+ /** The HTTP status, when PostHog answered. */
39
+ readonly statusCode?: number
40
+ readonly why?: string
41
+ readonly fix?: string
42
+ readonly docsUrl?: string
43
+ /** The request field the error is about, when PostHog named one. */
44
+ readonly field?: string
45
+ /** How long PostHog asked the client to wait before it sends again, in milliseconds. */
46
+ readonly retryAfterMs?: number
47
+
48
+ constructor(params: EmailErrorParams) {
49
+ super(params.message, params.cause === undefined ? undefined : { cause: params.cause })
50
+ this.name = emailErrorName(params.statusCode, params.code)
51
+ this.code = params.code
52
+ this.statusCode = params.statusCode
53
+ this.why = params.why
54
+ this.fix = params.fix
55
+ this.docsUrl = params.docsUrl
56
+ this.field = params.field
57
+ this.retryAfterMs = params.retryAfterMs
58
+ }
59
+ }
60
+
61
+ function emailErrorName(status: number | undefined, code: string): EmailErrorName {
62
+ if (code === 'missing_secret_key' || status === 401) {
63
+ return 'missing_api_key'
64
+ }
65
+ if (status === undefined) {
66
+ if (code === 'network_error') {
67
+ return 'service_unavailable'
68
+ }
69
+ return code.startsWith('invalid_') ? 'validation_error' : 'application_error'
70
+ }
71
+ switch (status) {
72
+ case 400:
73
+ return 'validation_error'
74
+ case 403:
75
+ return 'invalid_permission'
76
+ case 404:
77
+ return 'not_found'
78
+ case 409:
79
+ return code === 'template_outdated' ? 'validation_error' : 'concurrent_idempotent_requests'
80
+ case 422:
81
+ // The template send route also answers 422 for send checks such as recipient_suppressed.
82
+ return code === 'idempotency_key_reused' ? 'invalid_idempotent_request' : 'validation_error'
83
+ case 429:
84
+ return code === 'quota_limited' ? 'monthly_quota_exceeded' : 'rate_limit_exceeded'
85
+ case 503:
86
+ return 'service_unavailable'
87
+ default:
88
+ return 'application_error'
89
+ }
90
+ }
@@ -0,0 +1,108 @@
1
+ import type { EmailError } from './errors'
2
+
3
+ /**
4
+ * Options for `posthog.emails.send()`.
5
+ */
6
+ export interface SendEmailOptions {
7
+ /** One recipient email address. */
8
+ to: string
9
+ /** The UUID of an email template made transactional in PostHog. */
10
+ templateId: string
11
+ /** Values for the template's `{{ variables.* }}` placeholders. Sent as `message_data`. */
12
+ templateData?: Record<string, unknown>
13
+ /**
14
+ * The recipient's distinct ID in your product. The email's events go to this person.
15
+ * Without it, they go to a person whose distinct ID is the email address.
16
+ */
17
+ distinctId?: string
18
+ /**
19
+ * 1 to 255 visible ASCII characters. A send with a key that PostHog has already accepted
20
+ * queues nothing and returns the first send's result. Without it, the client makes a
21
+ * UUID that covers its own retries. Pass a key derived from what the email is for, such
22
+ * as `welcome:user_123`, to cover retries outside this call too.
23
+ */
24
+ idempotencyKey?: string
25
+ /** Overrides the client's `emailRequestMaxRetries` for this send. `0` turns retries off. */
26
+ maxRetries?: number
27
+ }
28
+
29
+ /** A warning PostHog returned with a queued email. */
30
+ export interface EmailWarning {
31
+ code: string
32
+ message: string
33
+ fix?: string
34
+ }
35
+
36
+ /**
37
+ * The queued email. A result means PostHog queued the email, not that it was delivered.
38
+ */
39
+ export interface SendEmailResult {
40
+ /** The queued email's message ID. */
41
+ id: string
42
+ /** True when PostHog had already accepted this idempotency key and returned the first send's result. */
43
+ replayed: boolean
44
+ warnings: EmailWarning[]
45
+ }
46
+
47
+ /**
48
+ * What `posthog.emails.send()` resolves to. Exactly one of `data` and `error` is set.
49
+ * The promise does not reject.
50
+ */
51
+ export type SendEmailResponse = { data: SendEmailResult; error: null } | { data: null; error: EmailError }
52
+
53
+ /**
54
+ * What happened to an email. Later SDK versions can see values this list does not have yet.
55
+ *
56
+ * - `queued`: the email waits to go out.
57
+ * - `sent`: PostHog handed the email to the email provider.
58
+ * - `failed`: the email was not sent, and `reason` says why.
59
+ * - `delivered`: the recipient's mail server accepted the email.
60
+ * - `bounced` and `complained`: the recipient's mail provider reported them after delivery.
61
+ */
62
+ export type EmailDeliveryStatus = 'queued' | 'sent' | 'failed' | 'delivered' | 'bounced' | 'complained'
63
+
64
+ /** The status of an email that `posthog.emails.send()` queued. */
65
+ export interface EmailStatus {
66
+ /** The message ID that `send()` returned. */
67
+ id: string
68
+ status: EmailDeliveryStatus | (string & {})
69
+ /** When PostHog queued the email, as an ISO 8601 timestamp. */
70
+ createdAt: string
71
+ /** When the email reached its current status, as an ISO 8601 timestamp. */
72
+ statusAt: string
73
+ /**
74
+ * Why a failed email was not sent, such as `provider_error` or `recipient_suppressed`, or the
75
+ * bounce type of a bounced email: `hard`, `soft` or `unknown`. Null otherwise.
76
+ */
77
+ reason: string | null
78
+ }
79
+
80
+ /**
81
+ * What `posthog.emails.get()` resolves to. Exactly one of `data` and `error` is set.
82
+ * The promise does not reject.
83
+ */
84
+ export type GetEmailResponse = { data: EmailStatus; error: null } | { data: null; error: EmailError }
85
+
86
+ /**
87
+ * The `posthog.emails` API: sends transactional email from PostHog email templates, and reads
88
+ * the status of each email it sent.
89
+ */
90
+ export interface Emails {
91
+ /**
92
+ * Queue one email from a transactional email template.
93
+ *
94
+ * Resolves to `{ data, error }` and does not reject. `data` means PostHog queued the email,
95
+ * not that it was delivered. Network errors, rate limits, an in-flight idempotency key and
96
+ * an unavailable service are retried with the same idempotency key.
97
+ */
98
+ send(options: SendEmailOptions): Promise<SendEmailResponse>
99
+
100
+ /**
101
+ * Read the status of an email that `send()` queued, for 30 days after the send.
102
+ *
103
+ * Resolves to `{ data, error }` and does not reject. The error code is `not_found` when no
104
+ * email of this project has the ID, when the email is older than 30 days, or for a few
105
+ * seconds after the send. Network errors, rate limits and an unavailable service are retried.
106
+ */
107
+ get(id: string): Promise<GetEmailResponse>
108
+ }
package/src/exports.ts CHANGED
@@ -4,6 +4,18 @@ export * from './types'
4
4
  export { enrichCurrentSpan } from './extensions/context/listeners'
5
5
 
6
6
  export { FeatureFlagEvaluations } from './feature-flag-evaluations'
7
+ export { EmailError } from './emails/errors'
8
+ export type { EmailErrorName } from './emails/errors'
9
+ export type {
10
+ EmailDeliveryStatus,
11
+ Emails,
12
+ EmailStatus,
13
+ EmailWarning,
14
+ GetEmailResponse,
15
+ SendEmailOptions,
16
+ SendEmailResponse,
17
+ SendEmailResult,
18
+ } from './emails/types'
7
19
  export type {
8
20
  FlagDefinitionCacheData,
9
21
  FlagDefinitionCacheInput,