@numoteq/emdash-plugin-jetemail 0.1.0 → 0.2.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/README.md CHANGED
@@ -17,6 +17,34 @@ If the domain publishes JetEmail Domain Security (`_jm` TXT), add the API key's
17
17
 
18
18
  The key is stored in the plugin-scoped KV store and is sent only to `https://api.jetemail.com/email`. The plugin requests only the fixed-host network and email transport registration capabilities. Recipients, CC and reply-to are passed through; EmDash email has no attachments.
19
19
 
20
+ ## Native use (Cloudflare Workers sites)
21
+
22
+ Sites that configure EmDash in code can register the transport in `astro.config.mjs` instead; the API key then lives in a Worker secret and nothing is set in the admin:
23
+
24
+ ```js
25
+ import { jetEmail } from "@numoteq/emdash-plugin-jetemail/native";
26
+
27
+ emdash({
28
+ plugins: [jetEmail({ from: { email: "noreply@example.com", name: "Example" } })],
29
+ });
30
+ ```
31
+
32
+ ```sh
33
+ wrangler secret put JETEMAIL_API_KEY
34
+ ```
35
+
36
+ Activate it under **Admin > Extensions** and select it under **Settings > Email**. Site code (contact forms) can send through the same client:
37
+
38
+ ```ts
39
+ import { deliverWithJetEmail } from "@numoteq/emdash-plugin-jetemail/client";
40
+
41
+ await deliverWithJetEmail(
42
+ fetch,
43
+ { apiToken, fromName: "Example", fromEmail: "noreply@example.com" },
44
+ message,
45
+ );
46
+ ```
47
+
20
48
  ## Development
21
49
 
22
50
  ```sh
@@ -0,0 +1,21 @@
1
+ //#region src/client.d.ts
2
+ interface JetEmailSender {
3
+ apiToken: string;
4
+ fromName: string;
5
+ fromEmail: string;
6
+ }
7
+ interface JetEmailMessage {
8
+ to: string | string[];
9
+ cc?: string[];
10
+ replyTo?: string;
11
+ subject: string;
12
+ text?: string;
13
+ html?: string;
14
+ }
15
+ type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;
16
+ declare function formatSender(name: string, email: string): string;
17
+ declare function deliverWithJetEmail(fetchFn: FetchFn, sender: JetEmailSender, message: JetEmailMessage): Promise<{
18
+ id?: string;
19
+ }>;
20
+ //#endregion
21
+ export { FetchFn, JetEmailMessage, JetEmailSender, deliverWithJetEmail, formatSender };
@@ -0,0 +1,33 @@
1
+ //#region src/client.ts
2
+ const API_URL = "https://api.jetemail.com/email";
3
+ function formatSender(name, email) {
4
+ return `${name.replace(/[<>"\r\n]/g, "").trim()} <${email}>`;
5
+ }
6
+ async function deliverWithJetEmail(fetchFn, sender, message) {
7
+ const body = {
8
+ from: formatSender(sender.fromName, sender.fromEmail),
9
+ to: message.to,
10
+ subject: message.subject
11
+ };
12
+ if (message.text) body.text = message.text;
13
+ if (message.html) body.html = message.html;
14
+ if (message.cc?.length) body.cc = message.cc;
15
+ if (message.replyTo) body.reply_to = message.replyTo;
16
+ const response = await fetchFn(API_URL, {
17
+ method: "POST",
18
+ headers: {
19
+ Authorization: `Bearer ${sender.apiToken}`,
20
+ "Content-Type": "application/json"
21
+ },
22
+ body: JSON.stringify(body)
23
+ });
24
+ if (!response.ok) {
25
+ const detail = (await response.text()).slice(0, 500);
26
+ throw new Error(`JetEmail rejected the message with HTTP ${response.status}${detail ? `: ${detail}` : ""}`);
27
+ }
28
+ const result = await response.json().catch(() => ({}));
29
+ return typeof result.id === "string" ? { id: result.id } : {};
30
+ }
31
+
32
+ //#endregion
33
+ export { deliverWithJetEmail, formatSender };
@@ -0,0 +1,9 @@
1
+ //#region src/native/config.ts
2
+ const PLUGIN_ID = "jetemail";
3
+ const PLUGIN_VERSION = "1.0.0";
4
+ function assertValidConfig(config) {
5
+ if (!config.from?.email?.includes("@") || !config.from.name?.trim()) throw new Error("[jetemail] config.from needs an email and a name, e.g. { from: { email: \"noreply@example.com\", name: \"Example\" } }");
6
+ }
7
+
8
+ //#endregion
9
+ export { PLUGIN_VERSION as n, assertValidConfig as r, PLUGIN_ID as t };
@@ -0,0 +1,14 @@
1
+ //#region src/native/config.d.ts
2
+ interface JetEmailConfig {
3
+ /** Sender. The domain must be verified in JetEmail and allowed for the API key. */
4
+ from: {
5
+ email: string;
6
+ name: string;
7
+ };
8
+ /** Reply-To used when the message sets none. */
9
+ replyTo?: string;
10
+ /** Name of the Worker secret that holds the transactional API key. @default "JETEMAIL_API_KEY" */
11
+ secret?: string;
12
+ }
13
+ //#endregion
14
+ export { JetEmailConfig as t };
@@ -0,0 +1,3 @@
1
+ import { env } from "cloudflare:workers";
2
+
3
+ export { env };
package/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@
7
7
  /** @type {import("emdash").PluginDescriptor} */
8
8
  const descriptor = Object.freeze({
9
9
  "id": "jetemail",
10
- "version": "0.1.0",
10
+ "version": "0.2.0",
11
11
  "format": "standard",
12
12
  "entrypoint": "@numoteq/emdash-plugin-jetemail/sandbox",
13
13
  "capabilities": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "jetemail",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "declaredAccess": {
5
5
  "network": {
6
6
  "request": {
@@ -0,0 +1,11 @@
1
+ import { t as JetEmailConfig } from "../config-DcMo9a49.mjs";
2
+ import { PluginDescriptor } from "emdash";
3
+
4
+ //#region src/native/index.d.ts
5
+ /**
6
+ * JetEmail transport for the emdash() integration's `plugins` array. The API key is read at
7
+ * delivery time from a Worker secret (default `JETEMAIL_API_KEY`), so nothing is configured in the admin.
8
+ */
9
+ declare function jetEmail(config: JetEmailConfig): PluginDescriptor<JetEmailConfig>;
10
+ //#endregion
11
+ export { type JetEmailConfig, jetEmail as default, jetEmail };
@@ -0,0 +1,21 @@
1
+ import { n as PLUGIN_VERSION, r as assertValidConfig, t as PLUGIN_ID } from "../config-Bdqx5Bs6.mjs";
2
+
3
+ //#region src/native/index.ts
4
+ /**
5
+ * JetEmail transport for the emdash() integration's `plugins` array. The API key is read at
6
+ * delivery time from a Worker secret (default `JETEMAIL_API_KEY`), so nothing is configured in the admin.
7
+ */
8
+ function jetEmail(config) {
9
+ assertValidConfig(config);
10
+ return {
11
+ id: PLUGIN_ID,
12
+ version: PLUGIN_VERSION,
13
+ entrypoint: "@numoteq/emdash-plugin-jetemail/native/plugin",
14
+ format: "native",
15
+ options: config,
16
+ capabilities: ["hooks.email-transport:register"]
17
+ };
18
+ }
19
+
20
+ //#endregion
21
+ export { jetEmail as default, jetEmail };
@@ -0,0 +1,12 @@
1
+ import { t as JetEmailConfig } from "../config-DcMo9a49.mjs";
2
+ import * as emdash from "emdash";
3
+ import { EmailDeliverEvent, PluginContext } from "emdash/plugin";
4
+
5
+ //#region src/native/plugin.d.ts
6
+ type LoadEnv = () => Promise<Record<string, unknown>>;
7
+ /** @internal Exported for tests. */
8
+ declare function createJetEmailDeliver(config: JetEmailConfig, loadEnv?: LoadEnv): (event: EmailDeliverEvent, ctx: PluginContext) => Promise<void>;
9
+ /** Called by EmDash with the options of the {@link jetEmail} descriptor. */
10
+ declare function createPlugin(config: JetEmailConfig): emdash.ResolvedPlugin<emdash.PluginStorageConfig>;
11
+ //#endregion
12
+ export { createJetEmailDeliver, createPlugin };
@@ -0,0 +1,51 @@
1
+ import { deliverWithJetEmail } from "../client.mjs";
2
+ import { n as PLUGIN_VERSION, r as assertValidConfig, t as PLUGIN_ID } from "../config-Bdqx5Bs6.mjs";
3
+ import { definePlugin } from "emdash";
4
+
5
+ //#region src/native/plugin.ts
6
+ async function loadWorkerEnv() {
7
+ try {
8
+ const { env } = await import("../env-DnAfk4Vp.mjs");
9
+ return env;
10
+ } catch {
11
+ throw new Error("[jetemail] cloudflare:workers is not available; this provider runs only on Cloudflare Workers.");
12
+ }
13
+ }
14
+ /** @internal Exported for tests. */
15
+ function createJetEmailDeliver(config, loadEnv = loadWorkerEnv) {
16
+ const secret = config.secret ?? "JETEMAIL_API_KEY";
17
+ return async (event, ctx) => {
18
+ const apiToken = (await loadEnv())[secret];
19
+ if (typeof apiToken !== "string" || !apiToken) throw new Error(`[jetemail] Worker secret "${secret}" is not set (wrangler secret put ${secret}).`);
20
+ const { message } = event;
21
+ const result = await deliverWithJetEmail(fetch, {
22
+ apiToken,
23
+ fromName: config.from.name,
24
+ fromEmail: config.from.email
25
+ }, {
26
+ ...message,
27
+ replyTo: message.replyTo ?? config.replyTo
28
+ });
29
+ ctx.log.info("Email delivered via JetEmail", {
30
+ to: message.to,
31
+ subject: message.subject,
32
+ id: result.id
33
+ });
34
+ };
35
+ }
36
+ /** Called by EmDash with the options of the {@link jetEmail} descriptor. */
37
+ function createPlugin(config) {
38
+ assertValidConfig(config);
39
+ return definePlugin({
40
+ id: PLUGIN_ID,
41
+ version: PLUGIN_VERSION,
42
+ capabilities: ["hooks.email-transport:register"],
43
+ hooks: { "email:deliver": {
44
+ exclusive: true,
45
+ handler: createJetEmailDeliver(config)
46
+ } }
47
+ });
48
+ }
49
+
50
+ //#endregion
51
+ export { createJetEmailDeliver, createPlugin };
package/dist/plugin.d.mts CHANGED
@@ -1,22 +1,26 @@
1
1
  import { SandboxedPlugin } from "emdash/plugin";
2
2
 
3
- //#region src/plugin.d.ts
4
- interface JetEmailSettings {
3
+ //#region src/client.d.ts
4
+ interface JetEmailSender {
5
5
  apiToken: string;
6
6
  fromName: string;
7
7
  fromEmail: string;
8
8
  }
9
9
  interface JetEmailMessage {
10
- to: string;
10
+ to: string | string[];
11
11
  cc?: string[];
12
12
  replyTo?: string;
13
13
  subject: string;
14
- text: string;
14
+ text?: string;
15
15
  html?: string;
16
16
  }
17
17
  type FetchFn = (url: string, init?: RequestInit) => Promise<Response>;
18
18
  declare function formatSender(name: string, email: string): string;
19
- declare function deliverWithJetEmail(fetchFn: FetchFn, settings: JetEmailSettings, message: JetEmailMessage): Promise<void>;
19
+ declare function deliverWithJetEmail(fetchFn: FetchFn, sender: JetEmailSender, message: JetEmailMessage): Promise<{
20
+ id?: string;
21
+ }>;
22
+ //#endregion
23
+ //#region src/plugin.d.ts
20
24
  declare const plugin: SandboxedPlugin;
21
25
  //#endregion
22
26
  export { plugin as default, deliverWithJetEmail, formatSender };
package/dist/plugin.mjs CHANGED
@@ -1 +1 @@
1
- function e(e,t){return`${e.replace(/[<>"\r\n]/g,``).trim()} <${t}>`}async function t(t,n,r){let i={from:e(n.fromName,n.fromEmail),to:r.to,subject:r.subject,text:r.text};r.html&&(i.html=r.html),r.cc?.length&&(i.cc=r.cc),r.replyTo&&(i.reply_to=r.replyTo);let a=await t(`https://api.jetemail.com/email`,{method:`POST`,headers:{Authorization:`Bearer ${n.apiToken}`,"Content-Type":`application/json`},body:JSON.stringify(i)});if(!a.ok){let e=(await a.text()).slice(0,500);throw Error(`JetEmail rejected the message with HTTP ${a.status}${e?`: ${e}`:``}`)}}async function n(e){let t=await e.kv.get(`settings:apiToken`),n=await e.kv.get(`settings:fromName`),r=await e.kv.get(`settings:fromEmail`);if(!t||!n||!r)throw Error(`JetEmail is not configured. Set the API key, sender name and sender email.`);return{apiToken:t,fromName:n,fromEmail:r}}async function r(e){let t=await e.kv.get(`settings:fromName`)??``,n=await e.kv.get(`settings:fromEmail`)??``,r=!!await e.kv.get(`settings:apiToken`);return{blocks:[{type:`header`,text:`JetEmail`},{type:`section`,text:`Deliver EmDash transactional email through the JetEmail API. The sender domain must be verified in JetEmail and allowed for the API key.`},{type:`form`,block_id:`jetemail-settings`,fields:[{type:`text_input`,action_id:`fromName`,label:`Sender name`,initial_value:t},{type:`text_input`,action_id:`fromEmail`,label:`Sender email`,initial_value:n},{type:`secret_input`,action_id:`apiToken`,label:r?`Transactional API key (leave blank to keep current)`:`Transactional API key`}],submit:{label:`Save settings`,action_id:`save_settings`}}]}}function i(e){return{blocks:[{type:`banner`,style:`error`,text:e}],toast:{message:e,type:`error`}}}async function a(e,t){let n=typeof t.fromName==`string`?t.fromName.trim():``,a=typeof t.fromEmail==`string`?t.fromEmail.trim():``,o=typeof t.apiToken==`string`?t.apiToken.trim():``;return n.replace(/[<>"\r\n]/g,``).trim()?/^[^\s@<>]+@[^\s@<>]+$/.test(a)?o&&!o.startsWith(`transactional_`)?i(`Use a JetEmail transactional API key (transactional_…).`):(await e.kv.set(`settings:fromName`,n),await e.kv.set(`settings:fromEmail`,a),o&&await e.kv.set(`settings:apiToken`,o),{...await r(e),toast:{message:`JetEmail settings saved`,type:`success`}}):i(`Enter a valid sender email.`):i(`Enter a sender name.`)}const o={hooks:{"email:deliver":{exclusive:!0,timeout:3e4,handler:async(e,r)=>{if(!r.http)throw Error(`JetEmail requires network access.`);let i=await n(r);await t(r.http.fetch.bind(r.http),i,e.message),r.log.info(`Email delivered via JetEmail`,{to:e.message.to,subject:e.message.subject})}}},routes:{admin:{handler:async(e,t)=>{let n=e.input;return n.type===`page_load`&&n.page===`/settings`?r(t):n.type===`form_submit`&&n.action_id===`save_settings`?a(t,n.values??{}):{blocks:[]}}}}};export{o as default,t as deliverWithJetEmail,e as formatSender};
1
+ function e(e,t){return`${e.replace(/[<>"\r\n]/g,``).trim()} <${t}>`}async function t(t,n,r){let i={from:e(n.fromName,n.fromEmail),to:r.to,subject:r.subject};r.text&&(i.text=r.text),r.html&&(i.html=r.html),r.cc?.length&&(i.cc=r.cc),r.replyTo&&(i.reply_to=r.replyTo);let a=await t(`https://api.jetemail.com/email`,{method:`POST`,headers:{Authorization:`Bearer ${n.apiToken}`,"Content-Type":`application/json`},body:JSON.stringify(i)});if(!a.ok){let e=(await a.text()).slice(0,500);throw Error(`JetEmail rejected the message with HTTP ${a.status}${e?`: ${e}`:``}`)}let o=await a.json().catch(()=>({}));return typeof o.id==`string`?{id:o.id}:{}}async function n(e){let t=await e.kv.get(`settings:apiToken`),n=await e.kv.get(`settings:fromName`),r=await e.kv.get(`settings:fromEmail`);if(!t||!n||!r)throw Error(`JetEmail is not configured. Set the API key, sender name and sender email.`);return{apiToken:t,fromName:n,fromEmail:r}}async function r(e){let t=await e.kv.get(`settings:fromName`)??``,n=await e.kv.get(`settings:fromEmail`)??``,r=!!await e.kv.get(`settings:apiToken`);return{blocks:[{type:`header`,text:`JetEmail`},{type:`section`,text:`Deliver EmDash transactional email through the JetEmail API. The sender domain must be verified in JetEmail and allowed for the API key.`},{type:`form`,block_id:`jetemail-settings`,fields:[{type:`text_input`,action_id:`fromName`,label:`Sender name`,initial_value:t},{type:`text_input`,action_id:`fromEmail`,label:`Sender email`,initial_value:n},{type:`secret_input`,action_id:`apiToken`,label:r?`Transactional API key (leave blank to keep current)`:`Transactional API key`}],submit:{label:`Save settings`,action_id:`save_settings`}}]}}function i(e){return{blocks:[{type:`banner`,style:`error`,text:e}],toast:{message:e,type:`error`}}}async function a(e,t){let n=typeof t.fromName==`string`?t.fromName.trim():``,a=typeof t.fromEmail==`string`?t.fromEmail.trim():``,o=typeof t.apiToken==`string`?t.apiToken.trim():``;return n.replace(/[<>"\r\n]/g,``).trim()?/^[^\s@<>]+@[^\s@<>]+$/.test(a)?o&&!o.startsWith(`transactional_`)?i(`Use a JetEmail transactional API key (transactional_…).`):(await e.kv.set(`settings:fromName`,n),await e.kv.set(`settings:fromEmail`,a),o&&await e.kv.set(`settings:apiToken`,o),{...await r(e),toast:{message:`JetEmail settings saved`,type:`success`}}):i(`Enter a valid sender email.`):i(`Enter a sender name.`)}const o={hooks:{"email:deliver":{exclusive:!0,timeout:3e4,handler:async(e,r)=>{if(!r.http)throw Error(`JetEmail requires network access.`);let i=await n(r);await t(r.http.fetch.bind(r.http),i,e.message),r.log.info(`Email delivered via JetEmail`,{to:e.message.to,subject:e.message.subject})}}},routes:{admin:{handler:async(e,t)=>{let n=e.input;return n.type===`page_load`&&n.page===`/settings`?r(t):n.type===`form_submit`&&n.action_id===`save_settings`?a(t,n.values??{}):{blocks:[]}}}}};export{o as default,t as deliverWithJetEmail,e as formatSender};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@numoteq/emdash-plugin-jetemail",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "JetEmail transport provider for EmDash",
5
5
  "keywords": [
6
6
  "email",
@@ -37,13 +37,25 @@
37
37
  "import": "./dist/index.mjs",
38
38
  "types": "./dist/index.d.mts"
39
39
  },
40
- "./sandbox": "./dist/plugin.mjs"
40
+ "./sandbox": "./dist/plugin.mjs",
41
+ "./native": {
42
+ "import": "./dist/native/index.mjs",
43
+ "types": "./dist/native/index.d.mts"
44
+ },
45
+ "./native/plugin": {
46
+ "import": "./dist/native/plugin.mjs",
47
+ "types": "./dist/native/plugin.d.mts"
48
+ },
49
+ "./client": {
50
+ "import": "./dist/client.mjs",
51
+ "types": "./dist/client.d.mts"
52
+ }
41
53
  },
42
54
  "publishConfig": {
43
55
  "access": "public"
44
56
  },
45
57
  "scripts": {
46
- "build": "emdash-plugin validate && emdash-plugin build",
58
+ "build": "emdash-plugin validate && emdash-plugin build && tsdown",
47
59
  "dev": "emdash-plugin dev",
48
60
  "test": "vitest run",
49
61
  "typecheck": "tsc --noEmit"
@@ -51,6 +63,7 @@
51
63
  "devDependencies": {
52
64
  "@emdash-cms/plugin-cli": "0.13.2",
53
65
  "emdash": "1.1.0",
66
+ "tsdown": "0.20.3",
54
67
  "typescript": "6.0.3",
55
68
  "vitest": "5.0.2"
56
69
  },