posthog-node 5.68.1 → 5.69.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/capture-v1/sender.js +2 -0
- package/dist/capture-v1/sender.mjs +2 -0
- package/dist/emails/emails.d.ts.map +1 -1
- package/dist/emails/emails.js +71 -6
- package/dist/emails/emails.mjs +71 -6
- package/dist/emails/types.d.ts +24 -3
- package/dist/emails/types.d.ts.map +1 -1
- package/dist/exports.d.ts +1 -1
- package/dist/exports.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 +11 -3
- package/src/capture-v1/sender.ts +2 -2
- package/src/emails/emails.ts +90 -7
- package/src/emails/types.ts +29 -5
- package/src/exports.ts +1 -0
- package/src/version.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"emails.d.ts","sourceRoot":"","sources":["../../src/emails/emails.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EACd,KAAK,MAAM,EAKZ,MAAM,eAAe,CAAA;AAGtB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"emails.d.ts","sourceRoot":"","sources":["../../src/emails/emails.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,SAAS,EACd,KAAK,MAAM,EAKZ,MAAM,eAAe,CAAA;AAGtB,OAAO,KAAK,EAKV,MAAM,EACN,gBAAgB,EAEhB,gBAAgB,EAChB,iBAAiB,EAIlB,MAAM,SAAS,CAAA;AA8DhB,eAAO,MAAM,gCAAgC,QAAS,CAAA;AACtD,eAAO,MAAM,iCAAiC,IAAI,CAAA;AAElD,MAAM,WAAW,iBAAiB;IAChC,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,+DAA+D;IAC/D,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,gBAAgB,EAAE,MAAM,CAAA;IACxB,UAAU,EAAE,MAAM,CAAA;IAClB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,SAAS,CAAA;IAChB,UAAU,EAAE,MAAM,OAAO,CAAA;IACzB,QAAQ,EAAE,MAAM,OAAO,CAAA;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAeD;;;;GAIG;AACH,qBAAa,WAAY,YAAW,MAAM;IACxC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAC1C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkB;IACxC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA8B;IAEvD,YAAY,MAAM,EAAE,iBAAiB,EAAE,KAAK,EAAE,gBAAgB,EAG7D;IAED,IAAI,CAAC,OAAO,EAAE,gBAAgB,EAAE,GAAG,UAAU,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAEpF;IAED,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAEzC;IAED,OAAO,CAAC,KAAK;IAMb;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAI3B;YAEa,aAAa;YA4Bb,YAAY;IAoB1B,OAAO,CAAC,OAAO;IA6Bf,OAAO,CAAC,OAAO;YAeD,WAAW;YAwBX,OAAO;CA2DtB"}
|
package/dist/emails/emails.js
CHANGED
|
@@ -66,7 +66,8 @@ const CODE_FIELDS = {
|
|
|
66
66
|
replyTo: true,
|
|
67
67
|
captureEngagement: true,
|
|
68
68
|
html: true,
|
|
69
|
-
text: true
|
|
69
|
+
text: true,
|
|
70
|
+
react: true
|
|
70
71
|
};
|
|
71
72
|
const TEMPLATE_FORM = {
|
|
72
73
|
name: 'templateId',
|
|
@@ -107,11 +108,12 @@ class EmailSender {
|
|
|
107
108
|
refuseSecondArgument(unexpected);
|
|
108
109
|
const prepared = prepareSend(options, this.config.maxRetries);
|
|
109
110
|
const { appHost, secretKey } = this.connect('no email was sent', 'Sending email needs secretKey, a project secret API key with the transactional_email:write scope.');
|
|
111
|
+
const body = void 0 === prepared.react ? prepared.body : await withRenderedReact(prepared.body, prepared.react, this.config.requestTimeoutMs);
|
|
110
112
|
const headers = this.headers(secretKey, {
|
|
111
113
|
'Content-Type': 'application/json',
|
|
112
114
|
'Idempotency-Key': prepared.idempotencyKey
|
|
113
115
|
});
|
|
114
|
-
const result = await this.withRetries(prepared.maxRetries, async ()=>parseSendReply(await this.request(appHost +
|
|
116
|
+
const result = await this.withRetries(prepared.maxRetries, async ()=>parseSendReply(await this.request(appHost + SEND_PATH, 'POST', headers, body)));
|
|
115
117
|
for (const warning of result.warnings)this.hooks.logger.warn(`PostHog email warning ${warning.code}: ${warning.message}`);
|
|
116
118
|
return {
|
|
117
119
|
data: result,
|
|
@@ -262,11 +264,11 @@ function prepareSend(options, defaultMaxRetries) {
|
|
|
262
264
|
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');
|
|
263
265
|
if (void 0 !== maxRetries && !isRetryCount(maxRetries)) throw invalid('invalid_max_retries', 'maxRetries must be a nonnegative integer.', 'maxRetries');
|
|
264
266
|
return {
|
|
265
|
-
path: SEND_PATH,
|
|
266
267
|
body: (0, core_namespaceObject.safeJsonStringify)({
|
|
267
268
|
...content,
|
|
268
269
|
to
|
|
269
270
|
}),
|
|
271
|
+
react: isReactElement(send.react) ? send.react : void 0,
|
|
270
272
|
idempotencyKey: idempotencyKey ?? globalThis.crypto.randomUUID(),
|
|
271
273
|
maxRetries: isRetryCount(maxRetries) ? maxRetries : defaultMaxRetries
|
|
272
274
|
};
|
|
@@ -277,7 +279,7 @@ function isRetryCount(value) {
|
|
|
277
279
|
function contentOf(send, distinctId) {
|
|
278
280
|
if (void 0 === send.templateId === (void 0 === send.templateKey)) throw new external_errors_js_namespaceObject.EmailError({
|
|
279
281
|
code: 'invalid_form',
|
|
280
|
-
message: 'Send either templateId, or templateKey with subject and html or
|
|
282
|
+
message: 'Send either templateId, or templateKey with subject and html, text or react.'
|
|
281
283
|
});
|
|
282
284
|
return void 0 === send.templateKey ? templateContent(send, distinctId) : codeContent(send, distinctId);
|
|
283
285
|
}
|
|
@@ -295,13 +297,14 @@ function templateContent(send, distinctId) {
|
|
|
295
297
|
};
|
|
296
298
|
}
|
|
297
299
|
function codeContent(send, distinctId) {
|
|
298
|
-
const { templateKey, subject, html, text, from, replyTo, captureEngagement } = send;
|
|
300
|
+
const { templateKey, subject, html, text, react, from, replyTo, captureEngagement } = send;
|
|
299
301
|
if ('string' != typeof templateKey || !TEMPLATE_KEY_PATTERN.test(templateKey)) throw invalid('invalid_template_key', 'templateKey can only have letters, digits, hyphens (-) and underscores (_), at most 400.', 'templateKey');
|
|
300
302
|
refuseFieldsOutside(send, CODE_FORM, TEMPLATE_FORM);
|
|
301
303
|
if ('string' != typeof subject) throw invalid('invalid_form', 'A send with templateKey needs a subject.', 'subject');
|
|
302
304
|
if (void 0 !== html && 'string' != typeof html) throw invalid('invalid_form', 'html must be a string.', 'html');
|
|
303
305
|
if (void 0 !== text && 'string' != typeof text) throw invalid('invalid_form', 'text must be a string.', 'text');
|
|
304
|
-
if (void 0
|
|
306
|
+
if (void 0 !== react) checkReactContent(react, html);
|
|
307
|
+
else if (void 0 === html && void 0 === text) throw invalid('invalid_form', 'A send with templateKey needs html, text or react.', 'html');
|
|
305
308
|
const sendsReplyTo = void 0 !== replyTo && !(Array.isArray(replyTo) && 0 === replyTo.length);
|
|
306
309
|
return {
|
|
307
310
|
template_key: templateKey,
|
|
@@ -326,6 +329,68 @@ function codeContent(send, distinctId) {
|
|
|
326
329
|
}
|
|
327
330
|
};
|
|
328
331
|
}
|
|
332
|
+
function checkReactContent(react, html) {
|
|
333
|
+
if (!isReactElement(react)) throw invalid('invalid_form', 'react must be a React element, such as <WelcomeEmail />.', 'react');
|
|
334
|
+
if (void 0 !== html) throw new external_errors_js_namespaceObject.EmailError({
|
|
335
|
+
code: 'invalid_form',
|
|
336
|
+
message: 'A send with react renders its own html, so it takes no html. No email was sent.',
|
|
337
|
+
field: 'html',
|
|
338
|
+
fix: 'Remove html to send the rendered react element, or remove react to send your html.'
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
const REACT_18_AND_19_ELEMENT_MARKERS = [
|
|
342
|
+
Symbol.for('react.element'),
|
|
343
|
+
Symbol.for('react.transitional.element')
|
|
344
|
+
];
|
|
345
|
+
function isReactElement(value) {
|
|
346
|
+
return 'object' == typeof value && null !== value && '$$typeof' in value && REACT_18_AND_19_ELEMENT_MARKERS.includes(value.$$typeof) && 'type' in value && 'props' in value;
|
|
347
|
+
}
|
|
348
|
+
async function withRenderedReact(body, react, timeoutMs) {
|
|
349
|
+
const { render, toPlainText } = await loadReactRenderer();
|
|
350
|
+
const fields = JSON.parse(body);
|
|
351
|
+
try {
|
|
352
|
+
const html = await settleWithin(render(react), timeoutMs, 'Rendering the react element');
|
|
353
|
+
return (0, core_namespaceObject.safeJsonStringify)({
|
|
354
|
+
...fields,
|
|
355
|
+
html,
|
|
356
|
+
...void 0 === fields.text && {
|
|
357
|
+
text: toPlainText(html)
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
} catch (cause) {
|
|
361
|
+
throw new external_errors_js_namespaceObject.EmailError({
|
|
362
|
+
code: 'react_render_failed',
|
|
363
|
+
message: 'Rendering the react element failed, so no email was sent.',
|
|
364
|
+
cause
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
async function settleWithin(work, timeoutMs, what) {
|
|
369
|
+
let timer;
|
|
370
|
+
const deadline = new Promise((_resolve, reject)=>{
|
|
371
|
+
timer = setTimeout(()=>reject(new Error(`${what} took longer than ${timeoutMs}ms.`)), timeoutMs);
|
|
372
|
+
});
|
|
373
|
+
try {
|
|
374
|
+
return await Promise.race([
|
|
375
|
+
work,
|
|
376
|
+
deadline
|
|
377
|
+
]);
|
|
378
|
+
} finally{
|
|
379
|
+
clearTimeout(timer);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
async function loadReactRenderer() {
|
|
383
|
+
try {
|
|
384
|
+
return await import("@react-email/render");
|
|
385
|
+
} catch (cause) {
|
|
386
|
+
throw new external_errors_js_namespaceObject.EmailError({
|
|
387
|
+
code: 'missing_react_renderer',
|
|
388
|
+
message: "react needs the @react-email/render package, which isn't installed or failed to load, so no email was sent.",
|
|
389
|
+
fix: 'Install @react-email/render with react and react-dom next to posthog-node, or render the email yourself and send html.',
|
|
390
|
+
cause
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
329
394
|
function refuseFieldsOutside(send, form, otherForm) {
|
|
330
395
|
const field = Object.entries(send).find(([name, value])=>void 0 !== value && !form.fields.has(name))?.[0];
|
|
331
396
|
if (void 0 === field) return;
|
package/dist/emails/emails.mjs
CHANGED
|
@@ -32,7 +32,8 @@ const CODE_FIELDS = {
|
|
|
32
32
|
replyTo: true,
|
|
33
33
|
captureEngagement: true,
|
|
34
34
|
html: true,
|
|
35
|
-
text: true
|
|
35
|
+
text: true,
|
|
36
|
+
react: true
|
|
36
37
|
};
|
|
37
38
|
const TEMPLATE_FORM = {
|
|
38
39
|
name: 'templateId',
|
|
@@ -73,11 +74,12 @@ class EmailSender {
|
|
|
73
74
|
refuseSecondArgument(unexpected);
|
|
74
75
|
const prepared = prepareSend(options, this.config.maxRetries);
|
|
75
76
|
const { appHost, secretKey } = this.connect('no email was sent', 'Sending email needs secretKey, a project secret API key with the transactional_email:write scope.');
|
|
77
|
+
const body = void 0 === prepared.react ? prepared.body : await withRenderedReact(prepared.body, prepared.react, this.config.requestTimeoutMs);
|
|
76
78
|
const headers = this.headers(secretKey, {
|
|
77
79
|
'Content-Type': 'application/json',
|
|
78
80
|
'Idempotency-Key': prepared.idempotencyKey
|
|
79
81
|
});
|
|
80
|
-
const result = await this.withRetries(prepared.maxRetries, async ()=>parseSendReply(await this.request(appHost +
|
|
82
|
+
const result = await this.withRetries(prepared.maxRetries, async ()=>parseSendReply(await this.request(appHost + SEND_PATH, 'POST', headers, body)));
|
|
81
83
|
for (const warning of result.warnings)this.hooks.logger.warn(`PostHog email warning ${warning.code}: ${warning.message}`);
|
|
82
84
|
return {
|
|
83
85
|
data: result,
|
|
@@ -228,11 +230,11 @@ function prepareSend(options, defaultMaxRetries) {
|
|
|
228
230
|
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');
|
|
229
231
|
if (void 0 !== maxRetries && !isRetryCount(maxRetries)) throw invalid('invalid_max_retries', 'maxRetries must be a nonnegative integer.', 'maxRetries');
|
|
230
232
|
return {
|
|
231
|
-
path: SEND_PATH,
|
|
232
233
|
body: safeJsonStringify({
|
|
233
234
|
...content,
|
|
234
235
|
to
|
|
235
236
|
}),
|
|
237
|
+
react: isReactElement(send.react) ? send.react : void 0,
|
|
236
238
|
idempotencyKey: idempotencyKey ?? globalThis.crypto.randomUUID(),
|
|
237
239
|
maxRetries: isRetryCount(maxRetries) ? maxRetries : defaultMaxRetries
|
|
238
240
|
};
|
|
@@ -243,7 +245,7 @@ function isRetryCount(value) {
|
|
|
243
245
|
function contentOf(send, distinctId) {
|
|
244
246
|
if (void 0 === send.templateId === (void 0 === send.templateKey)) throw new EmailError({
|
|
245
247
|
code: 'invalid_form',
|
|
246
|
-
message: 'Send either templateId, or templateKey with subject and html or
|
|
248
|
+
message: 'Send either templateId, or templateKey with subject and html, text or react.'
|
|
247
249
|
});
|
|
248
250
|
return void 0 === send.templateKey ? templateContent(send, distinctId) : codeContent(send, distinctId);
|
|
249
251
|
}
|
|
@@ -261,13 +263,14 @@ function templateContent(send, distinctId) {
|
|
|
261
263
|
};
|
|
262
264
|
}
|
|
263
265
|
function codeContent(send, distinctId) {
|
|
264
|
-
const { templateKey, subject, html, text, from, replyTo, captureEngagement } = send;
|
|
266
|
+
const { templateKey, subject, html, text, react, from, replyTo, captureEngagement } = send;
|
|
265
267
|
if ('string' != typeof templateKey || !TEMPLATE_KEY_PATTERN.test(templateKey)) throw invalid('invalid_template_key', 'templateKey can only have letters, digits, hyphens (-) and underscores (_), at most 400.', 'templateKey');
|
|
266
268
|
refuseFieldsOutside(send, CODE_FORM, TEMPLATE_FORM);
|
|
267
269
|
if ('string' != typeof subject) throw invalid('invalid_form', 'A send with templateKey needs a subject.', 'subject');
|
|
268
270
|
if (void 0 !== html && 'string' != typeof html) throw invalid('invalid_form', 'html must be a string.', 'html');
|
|
269
271
|
if (void 0 !== text && 'string' != typeof text) throw invalid('invalid_form', 'text must be a string.', 'text');
|
|
270
|
-
if (void 0
|
|
272
|
+
if (void 0 !== react) checkReactContent(react, html);
|
|
273
|
+
else if (void 0 === html && void 0 === text) throw invalid('invalid_form', 'A send with templateKey needs html, text or react.', 'html');
|
|
271
274
|
const sendsReplyTo = void 0 !== replyTo && !(Array.isArray(replyTo) && 0 === replyTo.length);
|
|
272
275
|
return {
|
|
273
276
|
template_key: templateKey,
|
|
@@ -292,6 +295,68 @@ function codeContent(send, distinctId) {
|
|
|
292
295
|
}
|
|
293
296
|
};
|
|
294
297
|
}
|
|
298
|
+
function checkReactContent(react, html) {
|
|
299
|
+
if (!isReactElement(react)) throw invalid('invalid_form', 'react must be a React element, such as <WelcomeEmail />.', 'react');
|
|
300
|
+
if (void 0 !== html) throw new EmailError({
|
|
301
|
+
code: 'invalid_form',
|
|
302
|
+
message: 'A send with react renders its own html, so it takes no html. No email was sent.',
|
|
303
|
+
field: 'html',
|
|
304
|
+
fix: 'Remove html to send the rendered react element, or remove react to send your html.'
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
const REACT_18_AND_19_ELEMENT_MARKERS = [
|
|
308
|
+
Symbol.for('react.element'),
|
|
309
|
+
Symbol.for('react.transitional.element')
|
|
310
|
+
];
|
|
311
|
+
function isReactElement(value) {
|
|
312
|
+
return 'object' == typeof value && null !== value && '$$typeof' in value && REACT_18_AND_19_ELEMENT_MARKERS.includes(value.$$typeof) && 'type' in value && 'props' in value;
|
|
313
|
+
}
|
|
314
|
+
async function withRenderedReact(body, react, timeoutMs) {
|
|
315
|
+
const { render, toPlainText } = await loadReactRenderer();
|
|
316
|
+
const fields = JSON.parse(body);
|
|
317
|
+
try {
|
|
318
|
+
const html = await settleWithin(render(react), timeoutMs, 'Rendering the react element');
|
|
319
|
+
return safeJsonStringify({
|
|
320
|
+
...fields,
|
|
321
|
+
html,
|
|
322
|
+
...void 0 === fields.text && {
|
|
323
|
+
text: toPlainText(html)
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} catch (cause) {
|
|
327
|
+
throw new EmailError({
|
|
328
|
+
code: 'react_render_failed',
|
|
329
|
+
message: 'Rendering the react element failed, so no email was sent.',
|
|
330
|
+
cause
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async function settleWithin(work, timeoutMs, what) {
|
|
335
|
+
let timer;
|
|
336
|
+
const deadline = new Promise((_resolve, reject)=>{
|
|
337
|
+
timer = setTimeout(()=>reject(new Error(`${what} took longer than ${timeoutMs}ms.`)), timeoutMs);
|
|
338
|
+
});
|
|
339
|
+
try {
|
|
340
|
+
return await Promise.race([
|
|
341
|
+
work,
|
|
342
|
+
deadline
|
|
343
|
+
]);
|
|
344
|
+
} finally{
|
|
345
|
+
clearTimeout(timer);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
async function loadReactRenderer() {
|
|
349
|
+
try {
|
|
350
|
+
return await import("@react-email/render");
|
|
351
|
+
} catch (cause) {
|
|
352
|
+
throw new EmailError({
|
|
353
|
+
code: 'missing_react_renderer',
|
|
354
|
+
message: "react needs the @react-email/render package, which isn't installed or failed to load, so no email was sent.",
|
|
355
|
+
fix: 'Install @react-email/render with react and react-dom next to posthog-node, or render the email yourself and send html.',
|
|
356
|
+
cause
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
295
360
|
function refuseFieldsOutside(send, form, otherForm) {
|
|
296
361
|
const field = Object.entries(send).find(([name, value])=>void 0 !== value && !form.fields.has(name))?.[0];
|
|
297
362
|
if (void 0 === field) return;
|
package/dist/emails/types.d.ts
CHANGED
|
@@ -45,16 +45,36 @@ interface CodeEmailFields {
|
|
|
45
45
|
captureEngagement?: boolean;
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
|
|
48
|
+
* A React element, such as `<WelcomeEmail firstName="Ada" />`. Typed by its shape, so
|
|
49
|
+
* posthog-node needs no React types.
|
|
50
|
+
*/
|
|
51
|
+
export interface EmailReactElement {
|
|
52
|
+
type: unknown;
|
|
53
|
+
props: unknown;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Email content from your code, named by a key: `html`, `text` or both, or a `react` element
|
|
57
|
+
* the client renders. PostHog sends it as is and never stores it. PostHog derives a missing
|
|
58
|
+
* `text` from the `html`.
|
|
51
59
|
*/
|
|
52
60
|
export type CodeEmailForm = CodeEmailFields & ({
|
|
53
61
|
html: string;
|
|
54
62
|
text?: string;
|
|
63
|
+
react?: never;
|
|
55
64
|
} | {
|
|
56
65
|
html?: string;
|
|
57
66
|
text: string;
|
|
67
|
+
react?: never;
|
|
68
|
+
} | {
|
|
69
|
+
/**
|
|
70
|
+
* A React Email element. The client renders it into `html`, and into `text` when the
|
|
71
|
+
* send has none. Needs the `@react-email/render` package installed next to posthog-node.
|
|
72
|
+
* The client renders the element after `send()` returns, so give each send its own
|
|
73
|
+
* element and props, and don't change them while the send is pending.
|
|
74
|
+
*/
|
|
75
|
+
react: EmailReactElement;
|
|
76
|
+
text?: string;
|
|
77
|
+
html?: never;
|
|
58
78
|
});
|
|
59
79
|
type Without<T> = {
|
|
60
80
|
[K in keyof T]?: never;
|
|
@@ -62,6 +82,7 @@ type Without<T> = {
|
|
|
62
82
|
export type SendTemplateEmailOptions = SendEmailBase & TemplateEmailForm & Without<CodeEmailFields & {
|
|
63
83
|
html: string;
|
|
64
84
|
text: string;
|
|
85
|
+
react: EmailReactElement;
|
|
65
86
|
}>;
|
|
66
87
|
export type SendCodeEmailOptions = SendEmailBase & CodeEmailForm & Without<TemplateEmailForm>;
|
|
67
88
|
/** Options for `posthog.emails.send()`: a template made in PostHog, or content from your code. */
|
|
@@ -1 +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,gDAAgD;AAChD,MAAM,WAAW,aAAa;IAC5B,mCAAmC;IACnC,EAAE,EAAE,MAAM,CAAA;IACV;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,4FAA4F;IAC5F,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,gEAAgE;AAChE,MAAM,WAAW,iBAAiB;IAChC,mEAAmE;IACnE,UAAU,EAAE,MAAM,CAAA;IAClB,0FAA0F;IAC1F,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACvC;AAED,UAAU,eAAe;IACvB;;;OAGG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,4GAA4G;IAC5G,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IAC3B;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;CAC5B;AAED;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG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AAW;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"}
|
package/dist/exports.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export { enrichCurrentSpan } from './extensions/context/listeners';
|
|
|
6
6
|
export { FeatureFlagEvaluations } from './feature-flag-evaluations';
|
|
7
7
|
export { EmailError } from './emails/errors';
|
|
8
8
|
export type { EmailErrorName } from './emails/errors';
|
|
9
|
-
export type { CodeEmailForm, EmailDeliveryStatus, Emails, EmailStatus, EmailWarning, GetEmailResponse, SendCodeEmailOptions, SendEmailBase, SendEmailOptions, SendEmailResponse, SendEmailResult, SendTemplateEmailOptions, TemplateEmailForm, } from './emails/types';
|
|
9
|
+
export type { CodeEmailForm, EmailDeliveryStatus, EmailReactElement, Emails, EmailStatus, EmailWarning, GetEmailResponse, SendCodeEmailOptions, SendEmailBase, SendEmailOptions, SendEmailResponse, SendEmailResult, SendTemplateEmailOptions, TemplateEmailForm, } from './emails/types';
|
|
10
10
|
export type { FlagDefinitionCacheData, FlagDefinitionCacheInput, FlagDefinitionCacheProvider, } from './extensions/feature-flags/cache';
|
|
11
11
|
export { FeatureFlagError } from '@posthog/core';
|
|
12
12
|
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,4BAA4B,CAAA;AAC1C,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,aAAa,EACb,mBAAmB,EACnB,MAAM,EACN,WAAW,EACX,YAAY,EACZ,gBAAgB,EAChB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,eAAe,EACf,wBAAwB,EACxB,iBAAiB,GAClB,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,gBAAgB,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAInG,YAAY,EACV,IAAI,EACJ,cAAc,EACd,kBAAkB,EAClB,QAAQ,EACR,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,UAAU,EACV,gBAAg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B,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"}
|
package/dist/version.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const version = "5.
|
|
1
|
+
export declare const version = "5.69.0";
|
|
2
2
|
//# sourceMappingURL=version.d.ts.map
|
package/dist/version.js
CHANGED
package/dist/version.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "5.
|
|
1
|
+
const version = "5.69.0";
|
|
2
2
|
export { version };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.69.0",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/PostHog/posthog-js/issues"
|
|
6
6
|
},
|
|
@@ -36,25 +36,33 @@
|
|
|
36
36
|
"@opentelemetry/api": "^1.9.0",
|
|
37
37
|
"@opentelemetry/context-async-hooks": "^2.2.0",
|
|
38
38
|
"@opentelemetry/sdk-trace-base": "^2.10.0",
|
|
39
|
+
"@react-email/render": "^2.1.1",
|
|
39
40
|
"@rslib/core": "0.23.2",
|
|
40
41
|
"@types/express": "^5.0.6",
|
|
41
42
|
"@types/node": "^20.0.0",
|
|
43
|
+
"@types/react": "^19.2.7",
|
|
42
44
|
"esbuild": "^0.25.0",
|
|
43
45
|
"@vitest/coverage-v8": "^1.6.1",
|
|
46
|
+
"react": "^19.2.1",
|
|
47
|
+
"react-dom": "^19.2.1",
|
|
44
48
|
"rxjs": "^7.8.0",
|
|
45
49
|
"typescript": "7.0.2",
|
|
46
50
|
"vitest": "^1.6.1",
|
|
47
|
-
"@posthog
|
|
48
|
-
"@posthog/
|
|
51
|
+
"@posthog/types": "^1.424.0",
|
|
52
|
+
"@posthog-tooling/tsconfig-base": "1.1.1"
|
|
49
53
|
},
|
|
50
54
|
"peerDependencies": {
|
|
51
55
|
"@opentelemetry/api": "^1.2.0",
|
|
56
|
+
"@react-email/render": "^2.1.0",
|
|
52
57
|
"rxjs": "^7.0.0"
|
|
53
58
|
},
|
|
54
59
|
"peerDependenciesMeta": {
|
|
55
60
|
"@opentelemetry/api": {
|
|
56
61
|
"optional": true
|
|
57
62
|
},
|
|
63
|
+
"@react-email/render": {
|
|
64
|
+
"optional": true
|
|
65
|
+
},
|
|
58
66
|
"rxjs": {
|
|
59
67
|
"optional": true
|
|
60
68
|
}
|
package/src/capture-v1/sender.ts
CHANGED
|
@@ -17,8 +17,8 @@ import type { V1BatchResponse, V1Event } from './types'
|
|
|
17
17
|
const V1_ANALYTICS_PATH = '/i/v1/analytics/events'
|
|
18
18
|
const DEFAULT_MAX_BACKOFF_MS = 30_000
|
|
19
19
|
|
|
20
|
-
/** HTTP statuses
|
|
21
|
-
const RETRYABLE_STATUSES = new Set([408, 500, 502, 503, 504])
|
|
20
|
+
/** HTTP statuses a capture request is retried on. 404 and 429 can come from a proxy in front of capture. */
|
|
21
|
+
const RETRYABLE_STATUSES = new Set([404, 408, 429, 500, 502, 503, 504])
|
|
22
22
|
|
|
23
23
|
export interface V1CaptureSenderConfig {
|
|
24
24
|
host: string
|
package/src/emails/emails.ts
CHANGED
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
import { EmailError } from './errors'
|
|
11
11
|
import type {
|
|
12
12
|
CodeEmailForm,
|
|
13
|
+
EmailReactElement,
|
|
13
14
|
EmailStatus,
|
|
14
15
|
EmailWarning,
|
|
15
16
|
Emails,
|
|
@@ -39,6 +40,10 @@ const IDEMPOTENCY_KEY_PATTERN = /^[\x21-\x7e]{1,255}$/
|
|
|
39
40
|
const TEMPLATE_KEY_PATTERN = /^[a-zA-Z0-9_-]{1,400}$/
|
|
40
41
|
|
|
41
42
|
type EveryField<Form> = Record<keyof Form, true>
|
|
43
|
+
interface ReactRenderer {
|
|
44
|
+
render(element: EmailReactElement): Promise<string>
|
|
45
|
+
toPlainText(html: string): string
|
|
46
|
+
}
|
|
42
47
|
type UncheckedSend = { [Field in keyof SendTemplateEmailOptions]?: unknown }
|
|
43
48
|
|
|
44
49
|
interface SendForm {
|
|
@@ -63,6 +68,7 @@ const CODE_FIELDS: EveryField<CodeEmailForm> = {
|
|
|
63
68
|
captureEngagement: true,
|
|
64
69
|
html: true,
|
|
65
70
|
text: true,
|
|
71
|
+
react: true,
|
|
66
72
|
}
|
|
67
73
|
const TEMPLATE_FORM: SendForm = {
|
|
68
74
|
name: 'templateId',
|
|
@@ -99,8 +105,8 @@ export interface EmailSenderHooks {
|
|
|
99
105
|
}
|
|
100
106
|
|
|
101
107
|
interface PreparedSend {
|
|
102
|
-
path: string
|
|
103
108
|
body: string
|
|
109
|
+
react?: EmailReactElement
|
|
104
110
|
idempotencyKey: string
|
|
105
111
|
maxRetries: number
|
|
106
112
|
}
|
|
@@ -158,12 +164,16 @@ export class EmailSender implements Emails {
|
|
|
158
164
|
'no email was sent',
|
|
159
165
|
'Sending email needs secretKey, a project secret API key with the transactional_email:write scope.'
|
|
160
166
|
)
|
|
167
|
+
const body =
|
|
168
|
+
prepared.react === undefined
|
|
169
|
+
? prepared.body
|
|
170
|
+
: await withRenderedReact(prepared.body, prepared.react, this.config.requestTimeoutMs)
|
|
161
171
|
const headers = this.headers(secretKey, {
|
|
162
172
|
'Content-Type': 'application/json',
|
|
163
173
|
'Idempotency-Key': prepared.idempotencyKey,
|
|
164
174
|
})
|
|
165
175
|
const result = await this.withRetries(prepared.maxRetries, async () =>
|
|
166
|
-
parseSendReply(await this.request(appHost +
|
|
176
|
+
parseSendReply(await this.request(appHost + SEND_PATH, 'POST', headers, body))
|
|
167
177
|
)
|
|
168
178
|
for (const warning of result.warnings) {
|
|
169
179
|
this.hooks.logger.warn(`PostHog email warning ${warning.code}: ${warning.message}`)
|
|
@@ -357,8 +367,8 @@ function prepareSend(options: SendEmailOptions, defaultMaxRetries: number): Prep
|
|
|
357
367
|
}
|
|
358
368
|
|
|
359
369
|
return {
|
|
360
|
-
path: SEND_PATH,
|
|
361
370
|
body: safeJsonStringify({ ...content, to }),
|
|
371
|
+
react: isReactElement(send.react) ? send.react : undefined,
|
|
362
372
|
idempotencyKey: idempotencyKey ?? globalThis.crypto.randomUUID(),
|
|
363
373
|
maxRetries: isRetryCount(maxRetries) ? maxRetries : defaultMaxRetries,
|
|
364
374
|
}
|
|
@@ -372,7 +382,7 @@ function contentOf(send: UncheckedSend, distinctId: string | undefined): Record<
|
|
|
372
382
|
if ((send.templateId === undefined) === (send.templateKey === undefined)) {
|
|
373
383
|
throw new EmailError({
|
|
374
384
|
code: 'invalid_form',
|
|
375
|
-
message: 'Send either templateId, or templateKey with subject and html or
|
|
385
|
+
message: 'Send either templateId, or templateKey with subject and html, text or react.',
|
|
376
386
|
})
|
|
377
387
|
}
|
|
378
388
|
return send.templateKey === undefined ? templateContent(send, distinctId) : codeContent(send, distinctId)
|
|
@@ -398,7 +408,7 @@ function templateContent(send: UncheckedSend, distinctId: string | undefined): R
|
|
|
398
408
|
}
|
|
399
409
|
|
|
400
410
|
function codeContent(send: UncheckedSend, distinctId: string | undefined): Record<string, unknown> {
|
|
401
|
-
const { templateKey, subject, html, text, from, replyTo, captureEngagement } = send
|
|
411
|
+
const { templateKey, subject, html, text, react, from, replyTo, captureEngagement } = send
|
|
402
412
|
if (typeof templateKey !== 'string' || !TEMPLATE_KEY_PATTERN.test(templateKey)) {
|
|
403
413
|
throw invalid(
|
|
404
414
|
'invalid_template_key',
|
|
@@ -416,8 +426,10 @@ function codeContent(send: UncheckedSend, distinctId: string | undefined): Recor
|
|
|
416
426
|
if (text !== undefined && typeof text !== 'string') {
|
|
417
427
|
throw invalid('invalid_form', 'text must be a string.', 'text')
|
|
418
428
|
}
|
|
419
|
-
if (
|
|
420
|
-
|
|
429
|
+
if (react !== undefined) {
|
|
430
|
+
checkReactContent(react, html)
|
|
431
|
+
} else if (html === undefined && text === undefined) {
|
|
432
|
+
throw invalid('invalid_form', 'A send with templateKey needs html, text or react.', 'html')
|
|
421
433
|
}
|
|
422
434
|
const sendsReplyTo = replyTo !== undefined && !(Array.isArray(replyTo) && replyTo.length === 0)
|
|
423
435
|
return {
|
|
@@ -432,6 +444,77 @@ function codeContent(send: UncheckedSend, distinctId: string | undefined): Recor
|
|
|
432
444
|
}
|
|
433
445
|
}
|
|
434
446
|
|
|
447
|
+
function checkReactContent(react: unknown, html: unknown): void {
|
|
448
|
+
if (!isReactElement(react)) {
|
|
449
|
+
throw invalid('invalid_form', 'react must be a React element, such as <WelcomeEmail />.', 'react')
|
|
450
|
+
}
|
|
451
|
+
if (html !== undefined) {
|
|
452
|
+
throw new EmailError({
|
|
453
|
+
code: 'invalid_form',
|
|
454
|
+
message: 'A send with react renders its own html, so it takes no html. No email was sent.',
|
|
455
|
+
field: 'html',
|
|
456
|
+
fix: 'Remove html to send the rendered react element, or remove react to send your html.',
|
|
457
|
+
})
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const REACT_18_AND_19_ELEMENT_MARKERS: unknown[] = [
|
|
462
|
+
Symbol.for('react.element'),
|
|
463
|
+
Symbol.for('react.transitional.element'),
|
|
464
|
+
]
|
|
465
|
+
|
|
466
|
+
function isReactElement(value: unknown): value is EmailReactElement {
|
|
467
|
+
return (
|
|
468
|
+
typeof value === 'object' &&
|
|
469
|
+
value !== null &&
|
|
470
|
+
'$$typeof' in value &&
|
|
471
|
+
REACT_18_AND_19_ELEMENT_MARKERS.includes(value.$$typeof) &&
|
|
472
|
+
'type' in value &&
|
|
473
|
+
'props' in value
|
|
474
|
+
)
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async function withRenderedReact(body: string, react: EmailReactElement, timeoutMs: number): Promise<string> {
|
|
478
|
+
const { render, toPlainText } = await loadReactRenderer()
|
|
479
|
+
const fields: Record<string, unknown> = JSON.parse(body)
|
|
480
|
+
try {
|
|
481
|
+
const html = await settleWithin(render(react), timeoutMs, 'Rendering the react element')
|
|
482
|
+
return safeJsonStringify({ ...fields, html, ...(fields.text === undefined && { text: toPlainText(html) }) })
|
|
483
|
+
} catch (cause) {
|
|
484
|
+
throw new EmailError({
|
|
485
|
+
code: 'react_render_failed',
|
|
486
|
+
message: 'Rendering the react element failed, so no email was sent.',
|
|
487
|
+
cause,
|
|
488
|
+
})
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async function settleWithin<T>(work: Promise<T>, timeoutMs: number, what: string): Promise<T> {
|
|
493
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
494
|
+
const deadline = new Promise<never>((_resolve, reject) => {
|
|
495
|
+
timer = setTimeout(() => reject(new Error(`${what} took longer than ${timeoutMs}ms.`)), timeoutMs)
|
|
496
|
+
})
|
|
497
|
+
try {
|
|
498
|
+
return await Promise.race([work, deadline])
|
|
499
|
+
} finally {
|
|
500
|
+
clearTimeout(timer)
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
async function loadReactRenderer(): Promise<ReactRenderer> {
|
|
505
|
+
try {
|
|
506
|
+
return await import('@react-email/render')
|
|
507
|
+
} catch (cause) {
|
|
508
|
+
throw new EmailError({
|
|
509
|
+
code: 'missing_react_renderer',
|
|
510
|
+
message:
|
|
511
|
+
"react needs the @react-email/render package, which isn't installed or failed to load, so no email was sent.",
|
|
512
|
+
fix: 'Install @react-email/render with react and react-dom next to posthog-node, or render the email yourself and send html.',
|
|
513
|
+
cause,
|
|
514
|
+
})
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
435
518
|
function refuseFieldsOutside(send: UncheckedSend, form: SendForm, otherForm: SendForm): void {
|
|
436
519
|
const field = Object.entries(send).find(([name, value]) => value !== undefined && !form.fields.has(name))?.[0]
|
|
437
520
|
if (field === undefined) {
|
package/src/emails/types.ts
CHANGED
|
@@ -49,17 +49,41 @@ interface CodeEmailFields {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* the `html`.
|
|
52
|
+
* A React element, such as `<WelcomeEmail firstName="Ada" />`. Typed by its shape, so
|
|
53
|
+
* posthog-node needs no React types.
|
|
55
54
|
*/
|
|
56
|
-
export
|
|
55
|
+
export interface EmailReactElement {
|
|
56
|
+
type: unknown
|
|
57
|
+
props: unknown
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Email content from your code, named by a key: `html`, `text` or both, or a `react` element
|
|
62
|
+
* the client renders. PostHog sends it as is and never stores it. PostHog derives a missing
|
|
63
|
+
* `text` from the `html`.
|
|
64
|
+
*/
|
|
65
|
+
export type CodeEmailForm = CodeEmailFields &
|
|
66
|
+
(
|
|
67
|
+
| { html: string; text?: string; react?: never }
|
|
68
|
+
| { html?: string; text: string; react?: never }
|
|
69
|
+
| {
|
|
70
|
+
/**
|
|
71
|
+
* A React Email element. The client renders it into `html`, and into `text` when the
|
|
72
|
+
* send has none. Needs the `@react-email/render` package installed next to posthog-node.
|
|
73
|
+
* The client renders the element after `send()` returns, so give each send its own
|
|
74
|
+
* element and props, and don't change them while the send is pending.
|
|
75
|
+
*/
|
|
76
|
+
react: EmailReactElement
|
|
77
|
+
text?: string
|
|
78
|
+
html?: never
|
|
79
|
+
}
|
|
80
|
+
)
|
|
57
81
|
|
|
58
82
|
type Without<T> = { [K in keyof T]?: never }
|
|
59
83
|
|
|
60
84
|
export type SendTemplateEmailOptions = SendEmailBase &
|
|
61
85
|
TemplateEmailForm &
|
|
62
|
-
Without<CodeEmailFields & { html: string; text: string }>
|
|
86
|
+
Without<CodeEmailFields & { html: string; text: string; react: EmailReactElement }>
|
|
63
87
|
|
|
64
88
|
export type SendCodeEmailOptions = SendEmailBase & CodeEmailForm & Without<TemplateEmailForm>
|
|
65
89
|
|
package/src/exports.ts
CHANGED
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "5.
|
|
1
|
+
export const version = "5.69.0"
|