@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
85 lines (83 loc) • 3.28 kB
JavaScript
import { softrApiUrl } from "../softr-api.js";
import { fetch } from "../fetch.js";
import { sessionConfig } from "../auth/config.js";
import { assertCanChange, readSessionToken } from "../auth/session.js";
//#region src/core/email.ts
const EMAIL_ERROR_CODES = [
"NOT_FOUND",
"INVALID_INPUT",
"CREDITS_EXHAUSTED",
"RATE_LIMITED",
"DAILY_LIMIT_REACHED",
"SENDER_UNAVAILABLE",
"BLOCKED",
"PROVIDER_REJECTED",
"UNAVAILABLE"
];
/** For an answer whose `code` is not an email code, such as Softr's generic error bodies. */
const CODE_OF_STATUS = {
400: "INVALID_INPUT",
404: "NOT_FOUND",
429: "RATE_LIMITED"
};
/**
* Thrown when Softr did not send the email. `code` says why:
*
* - `NOT_FOUND`: no such action, or the caller is not in its audience.
* - `INVALID_INPUT`: the call's inputs were refused; the message says which.
* - `CREDITS_EXHAUSTED`: the workspace has no workflow credits left.
* - `RATE_LIMITED`: the caller or the app sent too many emails lately; try again later.
* - `DAILY_LIMIT_REACHED`: Softr's own address sent the app's emails for today.
* - `SENDER_UNAVAILABLE`: the chosen sender is missing or not confirmed, or the action leaves `to` open and would send from Softr's address.
* - `BLOCKED`: Softr paused the app's emails for a day because one looked like phishing.
* - `PROVIDER_REJECTED`: the mail provider refused the email.
* - `UNAVAILABLE`: Softr could not send it right now, or could not be reached.
*/
var EmailError = class extends Error {
status;
code;
constructor(status, code, message) {
super(message);
this.name = "EmailError";
this.status = status;
this.code = code;
}
};
/** Sends one email through the action. Never retries by itself. */
async function sendEmail(action, init) {
assertCanChange(`Email "${action.id}"`);
const { to, cc, bcc, replyTo, subject, body } = init;
const sessionToken = readSessionToken();
let res;
try {
res = await fetch(`${softrApiUrl("SOFTR_PLATFORM_API_URL")}/v1/vibe/applications/${sessionConfig().applicationId}/actions/${action.id}/email`, {
method: "POST",
headers: {
...sessionToken ? { Authorization: `Bearer ${sessionToken}` } : {},
"Content-Type": "application/json"
},
body: JSON.stringify({
to,
cc,
bcc,
replyTo,
subject,
body
})
});
} catch (error) {
throw new EmailError(502, "UNAVAILABLE", `Email "${action.id}" could not reach Softr: ${error instanceof Error ? error.message : String(error)}`);
}
if (res.ok) {
const sent = await res.json().catch(() => null);
return { messageId: typeof sent === "object" && sent !== null && "messageId" in sent && typeof sent.messageId === "string" ? sent.messageId : null };
}
const failure = await res.json().catch(() => null);
const known = typeof failure === "object" && failure !== null ? failure : {};
const sentCode = "code" in known ? EMAIL_ERROR_CODES.find((errorCode) => errorCode === known.code) : void 0;
const message = "message" in known && typeof known.message === "string" && known.message !== "" ? known.message : `Email "${action.id}" failed with HTTP ${res.status}`;
throw new EmailError(res.status, sentCode ?? CODE_OF_STATUS[res.status] ?? "UNAVAILABLE", message);
}
const email = { send: sendEmail };
//#endregion
export { EmailError, email };