posthog-node 5.60.1 → 5.61.0
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.
- package/dist/client.d.ts +34 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +30 -2
- package/dist/client.mjs +30 -2
- package/dist/emails/emails.d.ts +45 -0
- package/dist/emails/emails.d.ts.map +1 -0
- package/dist/emails/emails.js +360 -0
- package/dist/emails/emails.mjs +316 -0
- package/dist/emails/errors.d.ts +37 -0
- package/dist/emails/errors.d.ts.map +1 -0
- package/dist/emails/errors.js +79 -0
- package/dist/emails/errors.mjs +41 -0
- package/dist/emails/types.d.ts +112 -0
- package/dist/emails/types.d.ts.map +1 -0
- package/dist/emails/types.js +18 -0
- package/dist/emails/types.mjs +1 -0
- package/dist/exports.d.ts +3 -0
- package/dist/exports.d.ts.map +1 -1
- package/dist/exports.js +19 -9
- package/dist/exports.mjs +1 -0
- package/dist/types.d.ts +33 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +2 -2
- package/src/client.ts +72 -1
- package/src/emails/emails.ts +459 -0
- package/src/emails/errors.ts +90 -0
- package/src/emails/types.ts +108 -0
- package/src/exports.ts +12 -0
- package/src/types.ts +34 -2
- package/src/version.ts +1 -1
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { parseRetryAfterMs, removeTrailingSlash, safeJsonStringify } from "@posthog/core";
|
|
2
|
+
import { EmailError } from "./errors.mjs";
|
|
3
|
+
const SEND_PATH = '/api/projects/@current/messaging/send/email/';
|
|
4
|
+
const STATUS_PATH = '/api/projects/@current/messaging/emails/{messageId}/';
|
|
5
|
+
const US_APP_HOST = 'https://us.posthog.com';
|
|
6
|
+
const EU_APP_HOST = 'https://eu.posthog.com';
|
|
7
|
+
const CLOUD_APP_HOSTS = {
|
|
8
|
+
'https://us.i.posthog.com': US_APP_HOST,
|
|
9
|
+
'https://us.posthog.com': US_APP_HOST,
|
|
10
|
+
'https://app.posthog.com': US_APP_HOST,
|
|
11
|
+
'https://eu.i.posthog.com': EU_APP_HOST,
|
|
12
|
+
'https://eu.posthog.com': EU_APP_HOST
|
|
13
|
+
};
|
|
14
|
+
const MAX_RETRY_DELAY_MS = 30000;
|
|
15
|
+
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
16
|
+
const IDEMPOTENCY_KEY_PATTERN = /^[\x21-\x7e]{1,255}$/;
|
|
17
|
+
const DEFAULT_EMAIL_REQUEST_TIMEOUT_MS = 15000;
|
|
18
|
+
const DEFAULT_EMAIL_REQUEST_MAX_RETRIES = 2;
|
|
19
|
+
class EmailSender {
|
|
20
|
+
constructor(config, hooks){
|
|
21
|
+
this.inFlight = new Set();
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.hooks = hooks;
|
|
24
|
+
}
|
|
25
|
+
send(options) {
|
|
26
|
+
return this.track(this.sendUntracked(options));
|
|
27
|
+
}
|
|
28
|
+
get(id) {
|
|
29
|
+
return this.track(this.getUntracked(id));
|
|
30
|
+
}
|
|
31
|
+
track(pending) {
|
|
32
|
+
this.inFlight.add(pending);
|
|
33
|
+
pending.finally(()=>this.inFlight.delete(pending));
|
|
34
|
+
return pending;
|
|
35
|
+
}
|
|
36
|
+
async drain() {
|
|
37
|
+
while(this.inFlight.size > 0)await Promise.all(this.inFlight);
|
|
38
|
+
}
|
|
39
|
+
async sendUntracked(options) {
|
|
40
|
+
try {
|
|
41
|
+
const prepared = prepareSend(options, this.config.maxRetries);
|
|
42
|
+
const { appHost, secretKey } = this.connect('no email was sent', 'Sending email needs secretKey, a project secret API key with the hog_flow:write scope.');
|
|
43
|
+
const headers = this.headers(secretKey, {
|
|
44
|
+
'Content-Type': 'application/json',
|
|
45
|
+
'Idempotency-Key': prepared.idempotencyKey
|
|
46
|
+
});
|
|
47
|
+
const result = await this.withRetries(prepared.maxRetries, async ()=>parseSendReply(await this.request(appHost + prepared.path, 'POST', headers, prepared.body)));
|
|
48
|
+
for (const warning of result.warnings)this.hooks.logger.warn(`PostHog email warning ${warning.code}: ${warning.message}`);
|
|
49
|
+
return {
|
|
50
|
+
data: result,
|
|
51
|
+
error: null
|
|
52
|
+
};
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return {
|
|
55
|
+
data: null,
|
|
56
|
+
error: asEmailError(error, 'Sending the email failed unexpectedly.')
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getUntracked(id) {
|
|
61
|
+
try {
|
|
62
|
+
if ('string' != typeof id || !UUID_PATTERN.test(id)) throw invalid('invalid_message_id', 'id must be the message ID that send() returned.', 'id');
|
|
63
|
+
const { appHost, secretKey } = this.connect('no email status was read', 'Reading an email status needs secretKey, a project secret API key with the hog_flow:read or hog_flow:write scope.');
|
|
64
|
+
const url = appHost + STATUS_PATH.replace('{messageId}', id.toLowerCase());
|
|
65
|
+
const headers = this.headers(secretKey, {});
|
|
66
|
+
const status = await this.withRetries(this.config.maxRetries, async ()=>parseStatusReply(await this.request(url, 'GET', headers)));
|
|
67
|
+
return {
|
|
68
|
+
data: status,
|
|
69
|
+
error: null
|
|
70
|
+
};
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
data: null,
|
|
74
|
+
error: asEmailError(error, 'Reading the email status failed unexpectedly.')
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
connect(nothingDone, missingSecretKey) {
|
|
79
|
+
if (this.hooks.isDisabled()) throw new EmailError({
|
|
80
|
+
code: 'client_disabled',
|
|
81
|
+
message: `The PostHog client is disabled, so ${nothingDone}.`
|
|
82
|
+
});
|
|
83
|
+
if (this.hooks.isClosed()) throw new EmailError({
|
|
84
|
+
code: 'client_closed',
|
|
85
|
+
message: `The PostHog client is shut down, so ${nothingDone}.`
|
|
86
|
+
});
|
|
87
|
+
const appHost = resolveAppHost(this.config.host, this.config.appHost);
|
|
88
|
+
if (!appHost) throw new EmailError({
|
|
89
|
+
code: 'missing_app_host',
|
|
90
|
+
message: "The client's host is not PostHog Cloud, so the client can't tell where to send email. Set appHost to your PostHog app host on the client."
|
|
91
|
+
});
|
|
92
|
+
const secretKey = this.config.secretKey?.trim();
|
|
93
|
+
if (!secretKey) throw new EmailError({
|
|
94
|
+
code: 'missing_secret_key',
|
|
95
|
+
message: missingSecretKey
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
appHost,
|
|
99
|
+
secretKey
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
headers(secretKey, extra) {
|
|
103
|
+
const headers = {
|
|
104
|
+
Authorization: `Bearer ${secretKey}`,
|
|
105
|
+
Accept: 'application/json',
|
|
106
|
+
...extra
|
|
107
|
+
};
|
|
108
|
+
if (this.hooks.userAgent) headers['User-Agent'] = this.hooks.userAgent;
|
|
109
|
+
return headers;
|
|
110
|
+
}
|
|
111
|
+
async withRetries(maxRetries, attemptOnce) {
|
|
112
|
+
for(let attempt = 0;; attempt++){
|
|
113
|
+
let error;
|
|
114
|
+
try {
|
|
115
|
+
return await attemptOnce();
|
|
116
|
+
} catch (caught) {
|
|
117
|
+
error = caught instanceof EmailError ? caught : new EmailError({
|
|
118
|
+
code: 'network_error',
|
|
119
|
+
message: 'The request to PostHog failed.',
|
|
120
|
+
cause: caught
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const delayMs = retryDelayMs(error, attempt, maxRetries);
|
|
124
|
+
if (void 0 === delayMs) throw error;
|
|
125
|
+
await new Promise((resolve)=>setTimeout(resolve, delayMs));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async request(url, method, headers, body) {
|
|
129
|
+
const controller = new AbortController();
|
|
130
|
+
const timeoutMs = this.config.requestTimeoutMs;
|
|
131
|
+
let timer;
|
|
132
|
+
const deadline = new Promise((_resolve, reject)=>{
|
|
133
|
+
timer = setTimeout(()=>{
|
|
134
|
+
const timeoutError = new Error(`Email request timed out after ${timeoutMs}ms`);
|
|
135
|
+
timeoutError.name = 'AbortError';
|
|
136
|
+
reject(timeoutError);
|
|
137
|
+
controller.abort(timeoutError);
|
|
138
|
+
}, timeoutMs);
|
|
139
|
+
});
|
|
140
|
+
const fetchPromise = Promise.resolve().then(()=>this.hooks.fetch(url, {
|
|
141
|
+
method,
|
|
142
|
+
headers,
|
|
143
|
+
...void 0 === body ? {} : {
|
|
144
|
+
body
|
|
145
|
+
},
|
|
146
|
+
signal: controller.signal
|
|
147
|
+
}));
|
|
148
|
+
fetchPromise.then((lateResponse)=>{
|
|
149
|
+
if (controller.signal.aborted) Promise.resolve(lateResponse.body?.cancel()).catch(()=>{});
|
|
150
|
+
}).catch(()=>{});
|
|
151
|
+
let response;
|
|
152
|
+
try {
|
|
153
|
+
response = await Promise.race([
|
|
154
|
+
fetchPromise,
|
|
155
|
+
deadline
|
|
156
|
+
]);
|
|
157
|
+
let text;
|
|
158
|
+
try {
|
|
159
|
+
text = await Promise.race([
|
|
160
|
+
response.text(),
|
|
161
|
+
deadline
|
|
162
|
+
]);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (response.status >= 200 && response.status < 300) throw error;
|
|
165
|
+
return {
|
|
166
|
+
status: response.status,
|
|
167
|
+
payload: null,
|
|
168
|
+
retryAfterMs: readRetryAfter(response)
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
status: response.status,
|
|
173
|
+
payload: parseJson(text),
|
|
174
|
+
retryAfterMs: readRetryAfter(response)
|
|
175
|
+
};
|
|
176
|
+
} finally{
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
if (controller.signal.aborted && response) Promise.resolve(response.body?.cancel()).catch(()=>{});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function prepareSend(options, defaultMaxRetries) {
|
|
183
|
+
const fallback = {};
|
|
184
|
+
const { to, templateId, templateData, distinctId, idempotencyKey, maxRetries } = options ?? fallback;
|
|
185
|
+
if ('string' != typeof to || !to.trim()) throw invalid('invalid_recipient', 'to must be one email address as a nonempty string.', 'to');
|
|
186
|
+
if ('string' != typeof templateId || !UUID_PATTERN.test(templateId)) throw invalid('invalid_template_id', "templateId must be the template's UUID.", 'templateId');
|
|
187
|
+
if (void 0 !== templateData && ('object' != typeof templateData || null === templateData || Array.isArray(templateData))) throw invalid('invalid_template_data', 'templateData must be an object.', 'templateData');
|
|
188
|
+
if (void 0 !== distinctId && ('string' != typeof distinctId || !distinctId)) throw invalid('invalid_distinct_id', 'distinctId must be a nonempty string.', 'distinctId');
|
|
189
|
+
if (void 0 !== idempotencyKey && ('string' != typeof idempotencyKey || !IDEMPOTENCY_KEY_PATTERN.test(idempotencyKey))) throw invalid('invalid_idempotency_key', 'idempotencyKey must be 1 to 255 visible ASCII characters without spaces.', 'idempotencyKey');
|
|
190
|
+
if (void 0 !== maxRetries && !(Number.isInteger(maxRetries) && maxRetries >= 0)) throw invalid('invalid_max_retries', 'maxRetries must be a nonnegative integer.', 'maxRetries');
|
|
191
|
+
const payload = {
|
|
192
|
+
template: {
|
|
193
|
+
id: templateId.toLowerCase()
|
|
194
|
+
},
|
|
195
|
+
to,
|
|
196
|
+
message_data: templateData ?? {}
|
|
197
|
+
};
|
|
198
|
+
if (void 0 !== distinctId) payload.identifiers = {
|
|
199
|
+
distinct_id: distinctId
|
|
200
|
+
};
|
|
201
|
+
return {
|
|
202
|
+
path: SEND_PATH,
|
|
203
|
+
body: safeJsonStringify(payload),
|
|
204
|
+
idempotencyKey: idempotencyKey ?? globalThis.crypto.randomUUID(),
|
|
205
|
+
maxRetries: maxRetries ?? defaultMaxRetries
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function invalid(code, message, field) {
|
|
209
|
+
return new EmailError({
|
|
210
|
+
code,
|
|
211
|
+
message,
|
|
212
|
+
field
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
function resolveAppHost(host, appHost) {
|
|
216
|
+
const explicit = 'string' == typeof appHost ? removeTrailingSlash(appHost.trim()) : '';
|
|
217
|
+
if (explicit) return CLOUD_APP_HOSTS[explicit] ?? explicit;
|
|
218
|
+
return CLOUD_APP_HOSTS[removeTrailingSlash(host)];
|
|
219
|
+
}
|
|
220
|
+
function parseJson(text) {
|
|
221
|
+
try {
|
|
222
|
+
return JSON.parse(text);
|
|
223
|
+
} catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
function readRetryAfter(response) {
|
|
228
|
+
try {
|
|
229
|
+
return parseRetryAfterMs(response.headers?.get('retry-after'), Date.now(), Number.MAX_SAFE_INTEGER);
|
|
230
|
+
} catch {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function emails_text(value) {
|
|
235
|
+
return 'string' == typeof value && value ? value : void 0;
|
|
236
|
+
}
|
|
237
|
+
function replyFields(payload) {
|
|
238
|
+
return null === payload || 'object' != typeof payload || Array.isArray(payload) ? {} : payload;
|
|
239
|
+
}
|
|
240
|
+
function parseSendReply({ status, payload, retryAfterMs }) {
|
|
241
|
+
const fields = replyFields(payload);
|
|
242
|
+
if (status >= 200 && status < 300) {
|
|
243
|
+
const id = emails_text(fields.message_id);
|
|
244
|
+
if (!id) throw new EmailError({
|
|
245
|
+
code: 'api_error',
|
|
246
|
+
message: 'PostHog answered without a message ID.',
|
|
247
|
+
statusCode: status
|
|
248
|
+
});
|
|
249
|
+
const warnings = [];
|
|
250
|
+
if (Array.isArray(fields.warnings)) {
|
|
251
|
+
for (const warning of fields.warnings)if (null !== warning && 'object' == typeof warning) warnings.push({
|
|
252
|
+
code: emails_text(warning.code) ?? 'warning',
|
|
253
|
+
message: emails_text(warning.detail) ?? emails_text(warning.message) ?? '',
|
|
254
|
+
fix: emails_text(warning.fix)
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
id,
|
|
259
|
+
replayed: true === fields.replayed,
|
|
260
|
+
warnings
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
throw replyError(fields, status, retryAfterMs);
|
|
264
|
+
}
|
|
265
|
+
function parseStatusReply({ status, payload, retryAfterMs }) {
|
|
266
|
+
const fields = replyFields(payload);
|
|
267
|
+
if (status < 200 || status >= 300) throw replyError(fields, status, retryAfterMs);
|
|
268
|
+
const id = emails_text(fields.id);
|
|
269
|
+
const deliveryStatus = emails_text(fields.status);
|
|
270
|
+
const createdAt = emails_text(fields.created_at);
|
|
271
|
+
const statusAt = emails_text(fields.status_at);
|
|
272
|
+
if (!id || !deliveryStatus || !createdAt || !statusAt) throw new EmailError({
|
|
273
|
+
code: 'api_error',
|
|
274
|
+
message: 'PostHog answered without an email status.',
|
|
275
|
+
statusCode: status
|
|
276
|
+
});
|
|
277
|
+
return {
|
|
278
|
+
id,
|
|
279
|
+
status: deliveryStatus,
|
|
280
|
+
createdAt,
|
|
281
|
+
statusAt,
|
|
282
|
+
reason: emails_text(fields.reason) ?? null
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function replyError(fields, status, retryAfterMs) {
|
|
286
|
+
return new EmailError({
|
|
287
|
+
code: emails_text(fields.code) ?? 'api_error',
|
|
288
|
+
message: emails_text(fields.detail) ?? `PostHog answered HTTP ${status}.`,
|
|
289
|
+
statusCode: status,
|
|
290
|
+
why: emails_text(fields.why),
|
|
291
|
+
fix: emails_text(fields.fix),
|
|
292
|
+
docsUrl: emails_text(fields.docs_url),
|
|
293
|
+
field: emails_text(fields.attr),
|
|
294
|
+
retryAfterMs
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
function asEmailError(error, message) {
|
|
298
|
+
return error instanceof EmailError ? error : new EmailError({
|
|
299
|
+
code: 'unexpected_error',
|
|
300
|
+
message,
|
|
301
|
+
cause: error
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
function isRetryable(error) {
|
|
305
|
+
if (void 0 === error.statusCode) return 'network_error' === error.code;
|
|
306
|
+
if (409 === error.statusCode) return 'idempotency_key_in_flight' === error.code;
|
|
307
|
+
if (429 === error.statusCode) return 'quota_limited' !== error.code;
|
|
308
|
+
return 503 === error.statusCode;
|
|
309
|
+
}
|
|
310
|
+
function retryDelayMs(error, attempt, maxRetries) {
|
|
311
|
+
if (attempt >= maxRetries || !isRetryable(error)) return;
|
|
312
|
+
if (void 0 !== error.retryAfterMs && error.retryAfterMs > MAX_RETRY_DELAY_MS) return;
|
|
313
|
+
const backoffMs = Math.min(MAX_RETRY_DELAY_MS, 1000 * 2 ** attempt * (0.8 + 0.4 * Math.random()));
|
|
314
|
+
return Math.max(backoffMs, error.retryAfterMs ?? 0);
|
|
315
|
+
}
|
|
316
|
+
export { DEFAULT_EMAIL_REQUEST_MAX_RETRIES, DEFAULT_EMAIL_REQUEST_TIMEOUT_MS, EmailSender };
|
|
@@ -0,0 +1,37 @@
|
|
|
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 = 'validation_error' | 'missing_api_key' | 'invalid_permission' | 'not_found' | 'concurrent_idempotent_requests' | 'invalid_idempotent_request' | 'rate_limit_exceeded' | 'monthly_quota_exceeded' | 'service_unavailable' | 'application_error';
|
|
5
|
+
export interface EmailErrorParams {
|
|
6
|
+
code: string;
|
|
7
|
+
message: string;
|
|
8
|
+
statusCode?: number;
|
|
9
|
+
why?: string;
|
|
10
|
+
fix?: string;
|
|
11
|
+
docsUrl?: string;
|
|
12
|
+
field?: string;
|
|
13
|
+
retryAfterMs?: number;
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* PostHog did not queue the email, or the client could not tell whether it did.
|
|
18
|
+
*
|
|
19
|
+
* Branch on `code`, PostHog's stable error code, or on `name`, a coarse category of the
|
|
20
|
+
* failure. The message never contains the secret key or the request body.
|
|
21
|
+
*/
|
|
22
|
+
export declare class EmailError extends Error {
|
|
23
|
+
name: EmailErrorName;
|
|
24
|
+
/** PostHog's error code, such as `quota_limited`, or an SDK code such as `network_error`. */
|
|
25
|
+
readonly code: string;
|
|
26
|
+
/** The HTTP status, when PostHog answered. */
|
|
27
|
+
readonly statusCode?: number;
|
|
28
|
+
readonly why?: string;
|
|
29
|
+
readonly fix?: string;
|
|
30
|
+
readonly docsUrl?: string;
|
|
31
|
+
/** The request field the error is about, when PostHog named one. */
|
|
32
|
+
readonly field?: string;
|
|
33
|
+
/** How long PostHog asked the client to wait before it sends again, in milliseconds. */
|
|
34
|
+
readonly retryAfterMs?: number;
|
|
35
|
+
constructor(params: EmailErrorParams);
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/emails/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,kBAAkB,GAClB,iBAAiB,GACjB,oBAAoB,GACpB,WAAW,GACX,gCAAgC,GAChC,4BAA4B,GAC5B,qBAAqB,GACrB,wBAAwB,GACxB,qBAAqB,GACrB,mBAAmB,CAAA;AAEvB,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;;;;GAKG;AACH,qBAAa,UAAW,SAAQ,KAAK;IAC1B,IAAI,EAAE,cAAc,CAAA;IAC7B,6FAA6F;IAC7F,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,8CAA8C;IAC9C,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAA;IACzB,oEAAoE;IACpE,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,wFAAwF;IACxF,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;IAE9B,YAAY,MAAM,EAAE,gBAAgB,EAUnC;CACF"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
class EmailError extends Error {
|
|
31
|
+
constructor(params){
|
|
32
|
+
super(params.message, void 0 === params.cause ? void 0 : {
|
|
33
|
+
cause: params.cause
|
|
34
|
+
});
|
|
35
|
+
this.name = emailErrorName(params.statusCode, params.code);
|
|
36
|
+
this.code = params.code;
|
|
37
|
+
this.statusCode = params.statusCode;
|
|
38
|
+
this.why = params.why;
|
|
39
|
+
this.fix = params.fix;
|
|
40
|
+
this.docsUrl = params.docsUrl;
|
|
41
|
+
this.field = params.field;
|
|
42
|
+
this.retryAfterMs = params.retryAfterMs;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function emailErrorName(status, code) {
|
|
46
|
+
if ('missing_secret_key' === code || 401 === status) return 'missing_api_key';
|
|
47
|
+
if (void 0 === status) {
|
|
48
|
+
if ('network_error' === code) return 'service_unavailable';
|
|
49
|
+
return code.startsWith('invalid_') ? 'validation_error' : 'application_error';
|
|
50
|
+
}
|
|
51
|
+
switch(status){
|
|
52
|
+
case 400:
|
|
53
|
+
return 'validation_error';
|
|
54
|
+
case 403:
|
|
55
|
+
return 'invalid_permission';
|
|
56
|
+
case 404:
|
|
57
|
+
return 'not_found';
|
|
58
|
+
case 409:
|
|
59
|
+
return 'template_outdated' === code ? 'validation_error' : 'concurrent_idempotent_requests';
|
|
60
|
+
case 422:
|
|
61
|
+
return 'idempotency_key_reused' === code ? 'invalid_idempotent_request' : 'validation_error';
|
|
62
|
+
case 429:
|
|
63
|
+
return 'quota_limited' === code ? 'monthly_quota_exceeded' : 'rate_limit_exceeded';
|
|
64
|
+
case 503:
|
|
65
|
+
return 'service_unavailable';
|
|
66
|
+
default:
|
|
67
|
+
return 'application_error';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
71
|
+
EmailError: ()=>EmailError
|
|
72
|
+
});
|
|
73
|
+
exports.EmailError = __webpack_exports__.EmailError;
|
|
74
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
75
|
+
"EmailError"
|
|
76
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
77
|
+
Object.defineProperty(exports, '__esModule', {
|
|
78
|
+
value: true
|
|
79
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
class EmailError extends Error {
|
|
2
|
+
constructor(params){
|
|
3
|
+
super(params.message, void 0 === params.cause ? void 0 : {
|
|
4
|
+
cause: params.cause
|
|
5
|
+
});
|
|
6
|
+
this.name = emailErrorName(params.statusCode, params.code);
|
|
7
|
+
this.code = params.code;
|
|
8
|
+
this.statusCode = params.statusCode;
|
|
9
|
+
this.why = params.why;
|
|
10
|
+
this.fix = params.fix;
|
|
11
|
+
this.docsUrl = params.docsUrl;
|
|
12
|
+
this.field = params.field;
|
|
13
|
+
this.retryAfterMs = params.retryAfterMs;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function emailErrorName(status, code) {
|
|
17
|
+
if ('missing_secret_key' === code || 401 === status) return 'missing_api_key';
|
|
18
|
+
if (void 0 === status) {
|
|
19
|
+
if ('network_error' === code) return 'service_unavailable';
|
|
20
|
+
return code.startsWith('invalid_') ? 'validation_error' : 'application_error';
|
|
21
|
+
}
|
|
22
|
+
switch(status){
|
|
23
|
+
case 400:
|
|
24
|
+
return 'validation_error';
|
|
25
|
+
case 403:
|
|
26
|
+
return 'invalid_permission';
|
|
27
|
+
case 404:
|
|
28
|
+
return 'not_found';
|
|
29
|
+
case 409:
|
|
30
|
+
return 'template_outdated' === code ? 'validation_error' : 'concurrent_idempotent_requests';
|
|
31
|
+
case 422:
|
|
32
|
+
return 'idempotency_key_reused' === code ? 'invalid_idempotent_request' : 'validation_error';
|
|
33
|
+
case 429:
|
|
34
|
+
return 'quota_limited' === code ? 'monthly_quota_exceeded' : 'rate_limit_exceeded';
|
|
35
|
+
case 503:
|
|
36
|
+
return 'service_unavailable';
|
|
37
|
+
default:
|
|
38
|
+
return 'application_error';
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export { EmailError };
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { EmailError } from './errors';
|
|
2
|
+
/**
|
|
3
|
+
* Options for `posthog.emails.send()`.
|
|
4
|
+
*/
|
|
5
|
+
export interface SendEmailOptions {
|
|
6
|
+
/** One recipient email address. */
|
|
7
|
+
to: string;
|
|
8
|
+
/** The UUID of an email template made transactional in PostHog. */
|
|
9
|
+
templateId: string;
|
|
10
|
+
/** Values for the template's `{{ variables.* }}` placeholders. Sent as `message_data`. */
|
|
11
|
+
templateData?: Record<string, unknown>;
|
|
12
|
+
/**
|
|
13
|
+
* The recipient's distinct ID in your product. The email's events go to this person.
|
|
14
|
+
* Without it, they go to a person whose distinct ID is the email address.
|
|
15
|
+
*/
|
|
16
|
+
distinctId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* 1 to 255 visible ASCII characters. A send with a key that PostHog has already accepted
|
|
19
|
+
* queues nothing and returns the first send's result. Without it, the client makes a
|
|
20
|
+
* UUID that covers its own retries. Pass a key derived from what the email is for, such
|
|
21
|
+
* as `welcome:user_123`, to cover retries outside this call too.
|
|
22
|
+
*/
|
|
23
|
+
idempotencyKey?: string;
|
|
24
|
+
/** Overrides the client's `emailRequestMaxRetries` for this send. `0` turns retries off. */
|
|
25
|
+
maxRetries?: number;
|
|
26
|
+
}
|
|
27
|
+
/** A warning PostHog returned with a queued email. */
|
|
28
|
+
export interface EmailWarning {
|
|
29
|
+
code: string;
|
|
30
|
+
message: string;
|
|
31
|
+
fix?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The queued email. A result means PostHog queued the email, not that it was delivered.
|
|
35
|
+
*/
|
|
36
|
+
export interface SendEmailResult {
|
|
37
|
+
/** The queued email's message ID. */
|
|
38
|
+
id: string;
|
|
39
|
+
/** True when PostHog had already accepted this idempotency key and returned the first send's result. */
|
|
40
|
+
replayed: boolean;
|
|
41
|
+
warnings: EmailWarning[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* What `posthog.emails.send()` resolves to. Exactly one of `data` and `error` is set.
|
|
45
|
+
* The promise does not reject.
|
|
46
|
+
*/
|
|
47
|
+
export type SendEmailResponse = {
|
|
48
|
+
data: SendEmailResult;
|
|
49
|
+
error: null;
|
|
50
|
+
} | {
|
|
51
|
+
data: null;
|
|
52
|
+
error: EmailError;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* What happened to an email. Later SDK versions can see values this list does not have yet.
|
|
56
|
+
*
|
|
57
|
+
* - `queued`: the email waits to go out.
|
|
58
|
+
* - `sent`: PostHog handed the email to the email provider.
|
|
59
|
+
* - `failed`: the email was not sent, and `reason` says why.
|
|
60
|
+
* - `delivered`: the recipient's mail server accepted the email.
|
|
61
|
+
* - `bounced` and `complained`: the recipient's mail provider reported them after delivery.
|
|
62
|
+
*/
|
|
63
|
+
export type EmailDeliveryStatus = 'queued' | 'sent' | 'failed' | 'delivered' | 'bounced' | 'complained';
|
|
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
|
+
* What `posthog.emails.get()` resolves to. Exactly one of `data` and `error` is set.
|
|
81
|
+
* The promise does not reject.
|
|
82
|
+
*/
|
|
83
|
+
export type GetEmailResponse = {
|
|
84
|
+
data: EmailStatus;
|
|
85
|
+
error: null;
|
|
86
|
+
} | {
|
|
87
|
+
data: null;
|
|
88
|
+
error: EmailError;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* The `posthog.emails` API: sends transactional email from PostHog email templates, and reads
|
|
92
|
+
* the status of each email it sent.
|
|
93
|
+
*/
|
|
94
|
+
export interface Emails {
|
|
95
|
+
/**
|
|
96
|
+
* Queue one email from a transactional email template.
|
|
97
|
+
*
|
|
98
|
+
* Resolves to `{ data, error }` and does not reject. `data` means PostHog queued the email,
|
|
99
|
+
* not that it was delivered. Network errors, rate limits, an in-flight idempotency key and
|
|
100
|
+
* an unavailable service are retried with the same idempotency key.
|
|
101
|
+
*/
|
|
102
|
+
send(options: SendEmailOptions): Promise<SendEmailResponse>;
|
|
103
|
+
/**
|
|
104
|
+
* Read the status of an email that `send()` queued, for 30 days after the send.
|
|
105
|
+
*
|
|
106
|
+
* Resolves to `{ data, error }` and does not reject. The error code is `not_found` when no
|
|
107
|
+
* email of this project has the ID, when the email is older than 30 days, or for a few
|
|
108
|
+
* seconds after the send. Network errors, rate limits and an unavailable service are retried.
|
|
109
|
+
*/
|
|
110
|
+
get(id: string): Promise<GetEmailResponse>;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/emails/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAE1C;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,mCAAmC;IACnC,EAAE,EAAE,MAAM,CAAA;IACV,mEAAmE;IACnE,UAAU,EAAE,MAAM,CAAA;IAClB,0FAA0F;IAC1F,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACtC;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;;OAKG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,4FAA4F;IAC5F,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,sDAAsD;AACtD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,qCAAqC;IACrC,EAAE,EAAE,MAAM,CAAA;IACV,wGAAwG;IACxG,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,EAAE,YAAY,EAAE,CAAA;CACzB;AAED;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,KAAK,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAA;AAE1G;;;;;;;;GAQG;AACH,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,MAAM,GAAG,QAAQ,GAAG,WAAW,GAAG,SAAS,GAAG,YAAY,CAAA;AAEvG,kEAAkE;AAClE,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,mBAAmB,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAA;IAC3C,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAA;IACjB,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;CACtB;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,KAAK,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,UAAU,CAAA;CAAE,CAAA;AAErG;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB;;;;;;OAMG;IACH,IAAI,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAA;IAE3D;;;;;;OAMG;IACH,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAAA;CAC3C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
package/dist/exports.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ export * from './extensions/express';
|
|
|
3
3
|
export * from './types';
|
|
4
4
|
export { enrichCurrentSpan } from './extensions/context/listeners';
|
|
5
5
|
export { FeatureFlagEvaluations } from './feature-flag-evaluations';
|
|
6
|
+
export { EmailError } from './emails/errors';
|
|
7
|
+
export type { EmailErrorName } from './emails/errors';
|
|
8
|
+
export type { EmailDeliveryStatus, Emails, EmailStatus, EmailWarning, GetEmailResponse, SendEmailOptions, SendEmailResponse, SendEmailResult, } from './emails/types';
|
|
6
9
|
export type { FlagDefinitionCacheData, FlagDefinitionCacheInput, FlagDefinitionCacheProvider, } from './extensions/feature-flags/cache';
|
|
7
10
|
export { FeatureFlagError } from '@posthog/core';
|
|
8
11
|
export type { FeatureFlagErrorType, UnresolvedFlagReason } from '@posthog/core';
|
package/dist/exports.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../src/exports.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAA;AAC/C,cAAc,sBAAsB,CAAA;AACpC,cAAc,SAAS,CAAA;AACvB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAElE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACnE,YAAY,EACV,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AAIzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAChD,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAA;AAI/E,YAAY,EAAE,oBAAoB,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAIjF,YAAY,EACV,IAAI,EACJ,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,gBAAgB,EAChB,YAAY,GACb,MAAM,eAAe,CAAA;AAKtB,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,MAAM,GACP,MAAM,eAAe,CAAA;AACtB,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA"}
|
|
1
|
+
{"version":3,"file":"exports.d.ts","sourceRoot":"","sources":["../src/exports.ts"],"names":[],"mappings":"AAAA,cAAc,iCAAiC,CAAA;AAC/C,cAAc,sBAAsB,CAAA;AACpC,cAAc,SAAS,CAAA;AACvB,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAElE,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAA;AACnE,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAC5C,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,YAAY,EACV,mBAAmB,EACnB,MAAM,EACN,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,GAChB,MAAM,gBAAgB,CAAA;AACvB,YAAY,EACV,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,kCAAkC,CAAA;AAIzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAChD,YAAY,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAA;AAI/E,YAAY,EAAE,oBAAoB,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAIjF,YAAY,EACV,IAAI,EACJ,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,gBAAgB,EAChB,YAAY,GACb,MAAM,eAAe,CAAA;AAKtB,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,EACtB,MAAM,GACP,MAAM,eAAe,CAAA;AACtB,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAA"}
|