@formepdf/resend 0.9.0 → 0.9.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.
@@ -1,10 +1,33 @@
1
1
  import { renderDocument } from '@formepdf/core';
2
2
  import { getTemplate } from '@formepdf/templates';
3
+ // User-supplied render callbacks are called outside React's render cycle.
4
+ // If the callback was compiled by React Compiler, it will inject useMemoCache
5
+ // and throw a cryptic "Invalid hook call" error. Catch and rethrow with guidance.
6
+ function callRenderFn(fn, entryPoint) {
7
+ try {
8
+ return fn();
9
+ }
10
+ catch (err) {
11
+ if (err instanceof Error && /hook|useMemoCache|Invalid hook call/i.test(err.message)) {
12
+ const name = fn.displayName || fn.name || 'render callback';
13
+ throw new Error(`The function passed to ${entryPoint}() ("${name}") appears to be compiled by ` +
14
+ `React Compiler, which injects hooks that cannot run outside of React's render cycle.\n\n` +
15
+ `Fix: Add 'use no memo' at the top of the function to opt it out:\n\n` +
16
+ ` function ${name}() {\n` +
17
+ ` 'use no memo';\n` +
18
+ ` return <Document>...</Document>;\n` +
19
+ ` }\n\n` +
20
+ `Alternatively, wrap it in an inline arrow:\n\n` +
21
+ ` ${entryPoint}({ render: () => <${name} /> })`);
22
+ }
23
+ throw err;
24
+ }
25
+ }
3
26
  export async function renderAndAttach(options) {
4
27
  const { template, data, render, filename } = options;
5
28
  let element;
6
29
  if (render) {
7
- element = render();
30
+ element = callRenderFn(render, 'renderAndAttach');
8
31
  }
9
32
  else if (template) {
10
33
  const templateFn = getTemplate(template);
package/dist/send-pdf.js CHANGED
@@ -2,6 +2,29 @@ import { Resend } from 'resend';
2
2
  import { renderDocument } from '@formepdf/core';
3
3
  import { getTemplate } from '@formepdf/templates';
4
4
  import { buildDefaultEmail } from './default-email.js';
5
+ // User-supplied render callbacks are called outside React's render cycle.
6
+ // If the callback was compiled by React Compiler, it will inject useMemoCache
7
+ // and throw a cryptic "Invalid hook call" error. Catch and rethrow with guidance.
8
+ function callRenderFn(fn, entryPoint) {
9
+ try {
10
+ return fn();
11
+ }
12
+ catch (err) {
13
+ if (err instanceof Error && /hook|useMemoCache|Invalid hook call/i.test(err.message)) {
14
+ const name = fn.displayName || fn.name || 'render callback';
15
+ throw new Error(`The function passed to ${entryPoint}() ("${name}") appears to be compiled by ` +
16
+ `React Compiler, which injects hooks that cannot run outside of React's render cycle.\n\n` +
17
+ `Fix: Add 'use no memo' at the top of the function to opt it out:\n\n` +
18
+ ` function ${name}() {\n` +
19
+ ` 'use no memo';\n` +
20
+ ` return <Document>...</Document>;\n` +
21
+ ` }\n\n` +
22
+ `Alternatively, wrap it in an inline arrow:\n\n` +
23
+ ` ${entryPoint}({ render: () => <${name} /> })`);
24
+ }
25
+ throw err;
26
+ }
27
+ }
5
28
  export async function sendPdf(options) {
6
29
  const { resendApiKey, from, to, subject, filename, html, text, react, cc, bcc, replyTo, tags, headers, } = options;
7
30
  let pdfBytes;
@@ -9,7 +32,7 @@ export async function sendPdf(options) {
9
32
  pdfBytes = options.pdf;
10
33
  }
11
34
  else if ('render' in options && options.render) {
12
- pdfBytes = await renderDocument(options.render());
35
+ pdfBytes = await renderDocument(callRenderFn(options.render, 'sendPdf'));
13
36
  }
14
37
  else if ('template' in options && options.template) {
15
38
  const templateFn = getTemplate(options.template);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formepdf/resend",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Render a PDF and email it in one call — Forme + Resend",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -13,9 +13,9 @@
13
13
  "dist"
14
14
  ],
15
15
  "dependencies": {
16
- "@formepdf/react": "0.9.0",
17
- "@formepdf/core": "0.9.0",
18
- "@formepdf/templates": "0.9.0"
16
+ "@formepdf/react": "0.9.1",
17
+ "@formepdf/core": "0.9.1",
18
+ "@formepdf/templates": "0.9.1"
19
19
  },
20
20
  "peerDependencies": {
21
21
  "resend": ">=3.0.0"