@yimingliao/cms 0.0.46 → 0.0.48

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.
@@ -213,8 +213,8 @@ declare function createSendEmail({ transporter, config, }: {
213
213
  };
214
214
  }): (options: SendEmailOptions) => Promise<any>;
215
215
 
216
- declare function createRenderEmailTemplate({ storageUrl, logger, }: {
217
- storageUrl: string;
216
+ declare function createRenderEmailTemplate({ logoUrl, logger, }: {
217
+ logoUrl: string;
218
218
  logger: Logger;
219
219
  }): (templateKey: string, replacements?: Record<string, string>) => Promise<string>;
220
220
 
@@ -244,7 +244,7 @@ var normalizeCacheKey = (key, delimiter = CACHE_KEY_DELIMITER) => {
244
244
 
245
245
  // src/server/infrastructure/cache/create-cache-result.ts
246
246
  var DAY = 1e3 * 60 * 60 * 24;
247
- function createCacheResult(cache, logger) {
247
+ function createCacheResult(cache2, logger) {
248
248
  return async function cacheResult({
249
249
  key: rawKey,
250
250
  ttl = DAY,
@@ -259,13 +259,13 @@ function createCacheResult(cache, logger) {
259
259
  return load();
260
260
  }
261
261
  try {
262
- const cached = await cache.get(key);
262
+ const cached = await cache2.get(key);
263
263
  if (cached !== void 0) {
264
264
  return cached;
265
265
  }
266
266
  const data = await load();
267
267
  if (data !== void 0) {
268
- await cache.set(key, data, ttl);
268
+ await cache2.set(key, data, ttl);
269
269
  }
270
270
  return data;
271
271
  } catch (error) {
@@ -287,13 +287,13 @@ var lua = `
287
287
  end
288
288
  return current
289
289
  `;
290
- function createIpRateLimiter(cache, appName) {
290
+ function createIpRateLimiter(cache2, appName) {
291
291
  return async function ipRateLimiter({
292
292
  key: rawKey,
293
293
  maxAttempts = DEFAULT_MAX_ATTEMPTS,
294
294
  timeWindow = DEFAULT_TIME_WINDOW
295
295
  }) {
296
- const secondaryStore = cache.store;
296
+ const secondaryStore = cache2.store;
297
297
  const redis = await secondaryStore.getClient();
298
298
  if (!redis) return true;
299
299
  const headersStore = await headers();
@@ -643,26 +643,32 @@ function createSendEmail({
643
643
  }
644
644
  var TEMPLATE_DIR = path.resolve(process.cwd(), "emails");
645
645
  var LAYOUT_PATH = path.join(TEMPLATE_DIR, "layout.html");
646
- var templateCache = /* @__PURE__ */ new Map();
646
+ var isDev = process.env.NODE_ENV !== "production";
647
+ var cache = /* @__PURE__ */ new Map();
647
648
  async function readTemplate(filePath) {
648
- if (templateCache.has(filePath)) return templateCache.get(filePath);
649
+ if (!isDev && cache.has(filePath)) {
650
+ return cache.get(filePath);
651
+ }
649
652
  try {
650
653
  const html = await readFile(filePath, "utf8");
651
- templateCache.set(filePath, html);
654
+ if (!isDev) cache.set(filePath, html);
652
655
  return html;
653
656
  } catch (error) {
654
657
  throw new Error(`Email template file not found: ${filePath}`);
655
658
  }
656
659
  }
657
- function applyReplacements(html, replacements) {
658
- let result2 = html;
659
- for (const [key, value] of Object.entries(replacements)) {
660
- result2 = result2.replaceAll(`{{{${key}}}}`, value);
661
- }
662
- return result2;
660
+ function applyReplacements(html, replacements, logger) {
661
+ return html.replace(/\{\{\{(.*?)\}\}\}/g, (_, key) => {
662
+ const value = replacements[key];
663
+ if (value == null) {
664
+ logger?.warn({ msg: "Email template variable missing", variable: key });
665
+ return "";
666
+ }
667
+ return value;
668
+ });
663
669
  }
664
670
  function createRenderEmailTemplate({
665
- storageUrl,
671
+ logoUrl,
666
672
  logger
667
673
  }) {
668
674
  return async function renderEmailTemplate(templateKey, replacements = {}) {
@@ -670,10 +676,11 @@ function createRenderEmailTemplate({
670
676
  const contentPath = path.join(TEMPLATE_DIR, `${templateKey}.html`);
671
677
  const layoutHtml = await readTemplate(LAYOUT_PATH);
672
678
  let contentHtml = await readTemplate(contentPath);
673
- contentHtml = applyReplacements(contentHtml, {
674
- STORAGE_URL: storageUrl,
675
- ...replacements
676
- });
679
+ contentHtml = applyReplacements(
680
+ contentHtml,
681
+ { LOGO_URL: logoUrl, ...replacements },
682
+ logger
683
+ );
677
684
  return layoutHtml.replaceAll("{{{content}}}", contentHtml);
678
685
  } catch (error) {
679
686
  logger.error({
@@ -1853,7 +1860,7 @@ var normalizeError = (error, translator) => {
1853
1860
  function createExecuteAction({
1854
1861
  initI18n,
1855
1862
  cacheResult,
1856
- cache,
1863
+ cache: cache2,
1857
1864
  logger
1858
1865
  }) {
1859
1866
  return async function executeAction(fn, options = {}) {
@@ -1865,7 +1872,7 @@ function createExecuteAction({
1865
1872
  ...options.ttl ? { ttl: options.ttl } : {},
1866
1873
  load: async () => fn(translator)
1867
1874
  }) : await fn(translator);
1868
- if (options.type === "command") cache.clear();
1875
+ if (options.type === "command") cache2.clear();
1869
1876
  const finalMessage = i18nKey ? translator.t(i18nKey) : message;
1870
1877
  return result.success({
1871
1878
  ...finalMessage ? { message: finalMessage } : {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yimingliao/cms",
3
- "version": "0.0.46",
3
+ "version": "0.0.48",
4
4
  "author": "Yiming Liao",
5
5
  "license": "MIT",
6
6
  "type": "module",