-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for alternative runtimes like Egde (Axios not functioning in Vercel Edge runtime) #130
Comments
I've also encountered this issue today. Using axios in edge runtime is in fact not supported and causes runtime exception. The best approach would be to use the Until, the library supports Edge runtime, you can use one of these workarounds:
import { type Message } from "postmark";
if (!process.env.POSTMARK_TOKEN) {
throw new Error(`POSTMARK_TOKEN is not set!`);
}
// We are using this workaround because the internal postmark client uses axios and that prevents us from using the edge runtime.
export const postmark = {
/**
* Sends an email through the Postmark API.
* @param message The content of the email.
* @returns The pure response object from the server.
*/
sendEmail: async (message: Message) => {
const response = await fetch('https://api.postmarkapp.com/email', {
method: 'POST',
headers: {
"Accept": 'application/json',
"Content-Type": 'application/json',
"X-Postmark-Server-Token": process.env.POSTMARK_TOKEN,
} as any,
body: JSON.stringify(message),
});
if(!response.ok) {
throw new Error(await response.json());
}
return await response.json();
},
}; |
Hi Postmark.js is mainly a Node JS library and at it does not have support for now for alternative runtimes like Edge. The Postmark library is flexible though and http client used can be replaced, in case you would like to use something else. For node-fetch http client for example, implemented http client contract could look like this:
In order to use your custom http client you will need to handle requests and errors to provide library compatible errors and response in json format. One thing to keep in mind is that although this is possible now, we do not provide support for it, since it can completely alter the behaviour of the library and is out of the scope of our verifications. |
Hi @ibalosh Thank you for response.
That's really good to know! My problem has been solved, but I'm leaving this issue open because of the added |
Just so you know, I have replaced Axios for myself. I didn't run unit/int. tests, but it seems to work fine. I hope it helps your work! https://github.com/sundaycrafts/postmark.js/commits/main (npm) |
+1 for edge runtime support please |
try using https://www.npmjs.com/package/@sundaycrafts/postmark, it works well thank fork repo @sundaycrafts |
Similar issues with Bun. I think moving to fetch based implementation is the way to go. At least supporting both. |
+1 for adding edge runtime support. |
Why is postmark listed as the preferred Vercel email provider if the nodejs client doesn't even work on the edge? Sharing my workaround for templated emails: import { TemplatedMessage } from "postmark";
// TODO: replace postmark https://github.com/ActiveCampaign/postmark.js/issues/130
export const sendInviteEmail = async (fromEmail: string, toEmail: string, org: string) => {
const template: TemplatedMessage = {
"From": fromEmail,
"To": toEmail,
"TemplateAlias": "user-invitation",
"TemplateModel": {
"to_email": toEmail,
"from_email": fromEmail,
"org": org,
}
};
await fetch("https://api.postmarkapp.com/email/withTemplate", {
method: "POST",
headers: {
"Accept": 'application/json',
"Content-Type": 'application/json',
"X-Postmark-Server-Token": process.env.POSTMARK_SERVER_API_TOKEN,
} as any,
body: JSON.stringify(template),
})
} |
Hi @EvanBoyle , we did not list on Vercel postmark, but since the library allows you to implement any http client you want, its pretty straightforward to make the adjustment to any http client you want including fetch which I described at the top. Or you can use @sundaycrafts solution which does just that, implements alternative http client. |
Hi maintainers,
I've encountered an issue where Axios is not functioning within the Vercel Edge runtime.
My understanding is that Postmark.js depends on Axios for some kind of compatibility, but it is intended to run solely on the server side, so concerns such as browser backward compatibility are not relevant.
As a potential solution, replacing Axios with the Fetch Web standard API might be best. I would appreciate your thoughts on this.
Thanks in advance for your attention to this matter.
The text was updated successfully, but these errors were encountered: