@contentgrowth/content-emailing 0.7.6 → 0.7.8

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.
@@ -3,6 +3,7 @@ export { EmailingCacheDO, createDOCacheProvider } from './EmailingCacheDO.js';
3
3
  export { createEmailRoutes, createTemplateRoutes, createTrackingRoutes } from './routes/index.js';
4
4
  export { encodeTrackingLinks, extractVariables, getWebsiteUrl, markdownToPlainText, resetWebsiteUrlCache, wrapInEmailTemplate } from '../common/index.js';
5
5
  import 'hono';
6
+ import 'hono/types';
6
7
 
7
8
  /**
8
9
  * Create a simple logger callback for EmailService that logs to D1.
@@ -766,6 +766,9 @@ var EmailService = class {
766
766
  // Updater function to save settings to backend
767
767
  // Signature: async (profile, tenantId, settings) => void
768
768
  settingsUpdater: config.settingsUpdater || null,
769
+ // Settings configuration
770
+ settingsTableName: config.settingsTableName || "system_settings",
771
+ settingsKeyPrefix: config.settingsKeyPrefix || "system_email.",
769
772
  // Branding configuration for email templates
770
773
  branding: {
771
774
  brandName: config.branding?.brandName || "Your App",
@@ -814,6 +817,22 @@ var EmailService = class {
814
817
  console.warn("[EmailService] settingsLoader failed:", e);
815
818
  }
816
819
  }
820
+ if (!settings && profile === "system" && this.db) {
821
+ try {
822
+ const tableName = this.config.settingsTableName;
823
+ const prefix = this.config.settingsKeyPrefix;
824
+ const { results } = await this.db.prepare(`SELECT * FROM ${tableName} WHERE key LIKE ?`).bind(`${prefix}%`).all();
825
+ if (results && results.length > 0) {
826
+ settings = {};
827
+ for (const row of results) {
828
+ const cleanKey = row.key.slice(prefix.length);
829
+ settings[cleanKey] = row.value;
830
+ }
831
+ }
832
+ } catch (e) {
833
+ console.warn("[EmailService] Failed to load settings from DB:", e.message);
834
+ }
835
+ }
817
836
  if (settings) {
818
837
  if (this.cache && this.cache.putSettings) {
819
838
  try {
@@ -1344,12 +1363,68 @@ var EmailService = class {
1344
1363
  };
1345
1364
 
1346
1365
  // src/backend/routes/index.js
1347
- import { Hono as Hono4 } from "hono";
1366
+ import { Hono as Hono5 } from "hono";
1348
1367
 
1349
- // src/backend/routes/templates.js
1368
+ // src/backend/routes/settings.js
1350
1369
  import { Hono } from "hono";
1351
- function createTemplateRoutes(config = {}) {
1370
+ function createSettingsRoutes(config = {}) {
1352
1371
  const app = new Hono();
1372
+ const tableName = config.tableName || "system_settings";
1373
+ const keyPrefix = config.keyPrefix || "system_email.";
1374
+ const getSettings = async (db) => {
1375
+ try {
1376
+ const { results } = await db.prepare(`SELECT * FROM ${tableName} WHERE key LIKE ?`).bind(`${keyPrefix}%`).all();
1377
+ const settings = {};
1378
+ for (const row of results) {
1379
+ const cleanKey = row.key.slice(keyPrefix.length);
1380
+ settings[cleanKey] = row.value;
1381
+ }
1382
+ return settings;
1383
+ } catch (e) {
1384
+ console.warn(`[Settings] Failed to fetch from ${tableName}:`, e.message);
1385
+ return {};
1386
+ }
1387
+ };
1388
+ app.get("/", async (c) => {
1389
+ try {
1390
+ const settings = await getSettings(c.env.DB);
1391
+ return c.json(settings);
1392
+ } catch (error) {
1393
+ console.error("Failed to fetch settings:", error);
1394
+ return c.json({ error: error.message }, 500);
1395
+ }
1396
+ });
1397
+ app.post("/", async (c) => {
1398
+ try {
1399
+ const body = await c.req.json();
1400
+ const db = c.env.DB;
1401
+ const updates = Object.entries(body).map(([k, v]) => ({
1402
+ key: `${keyPrefix}${k}`,
1403
+ value: v
1404
+ }));
1405
+ if (updates.length === 0) return c.json({ success: true });
1406
+ const stmt = db.prepare(`
1407
+ INSERT INTO ${tableName} (key, value, updated_at)
1408
+ VALUES (?, ?, strftime('%s', 'now'))
1409
+ ON CONFLICT(key) DO UPDATE SET
1410
+ value = excluded.value,
1411
+ updated_at = excluded.updated_at
1412
+ `);
1413
+ const batch = updates.map((u) => stmt.bind(u.key, u.value === void 0 || u.value === null ? "" : String(u.value)));
1414
+ await db.batch(batch);
1415
+ return c.json({ success: true });
1416
+ } catch (error) {
1417
+ console.error("Failed to save settings:", error);
1418
+ return c.json({ error: error.message }, 500);
1419
+ }
1420
+ });
1421
+ return app;
1422
+ }
1423
+
1424
+ // src/backend/routes/templates.js
1425
+ import { Hono as Hono2 } from "hono";
1426
+ function createTemplateRoutes(config = {}) {
1427
+ const app = new Hono2();
1353
1428
  app.get("/", async (c) => {
1354
1429
  const emailService = new EmailService(c.env, config);
1355
1430
  try {
@@ -1429,7 +1504,7 @@ function createTemplateRoutes(config = {}) {
1429
1504
  }
1430
1505
 
1431
1506
  // src/backend/routes/tracking.js
1432
- import { Hono as Hono2 } from "hono";
1507
+ import { Hono as Hono3 } from "hono";
1433
1508
  var TRACKING_PIXEL = new Uint8Array([
1434
1509
  71,
1435
1510
  73,
@@ -1475,7 +1550,7 @@ var TRACKING_PIXEL = new Uint8Array([
1475
1550
  59
1476
1551
  ]);
1477
1552
  function createTrackingRoutes(env, config = {}) {
1478
- const app = new Hono2();
1553
+ const app = new Hono3();
1479
1554
  const db = env.DB;
1480
1555
  const tablePrefix = config.emailTablePrefix || config.tableNamePrefix || "system_email_";
1481
1556
  app.get("/track/open/:token", async (c) => {
@@ -1604,9 +1679,9 @@ function createTrackingRoutes(env, config = {}) {
1604
1679
  }
1605
1680
 
1606
1681
  // src/backend/routes/logs.js
1607
- import { Hono as Hono3 } from "hono";
1682
+ import { Hono as Hono4 } from "hono";
1608
1683
  function createLogRoutes(config = {}) {
1609
- const app = new Hono3();
1684
+ const app = new Hono4();
1610
1685
  app.get("/", async (c) => {
1611
1686
  const limit = Math.min(parseInt(c.req.query("limit") || "50"), 100);
1612
1687
  const offset = parseInt(c.req.query("offset") || "0");
@@ -1638,9 +1713,14 @@ function createLogRoutes(config = {}) {
1638
1713
 
1639
1714
  // src/backend/routes/index.js
1640
1715
  function createEmailRoutes(config = {}, cacheProvider = null) {
1641
- const app = new Hono4();
1716
+ const app = new Hono5();
1642
1717
  app.route("/templates", createTemplateRoutes(config, cacheProvider));
1643
1718
  app.route("/logs", createLogRoutes(config));
1719
+ app.route("/settings", createSettingsRoutes({
1720
+ ...config,
1721
+ tableName: config.settingsTableName || "system_settings",
1722
+ keyPrefix: config.settingsKeyPrefix || "system_email."
1723
+ }));
1644
1724
  return app;
1645
1725
  }
1646
1726