anote-server-libs 0.11.4 → 0.11.6
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/package.json +1 -1
- package/services/utils.js +19 -14
- package/services/utils.ts +33 -14
package/package.json
CHANGED
package/services/utils.js
CHANGED
|
@@ -15,7 +15,6 @@ exports.readEmailTemplates = readEmailTemplates;
|
|
|
15
15
|
exports.replacePlaceholders = replacePlaceholders;
|
|
16
16
|
exports.getHtmlReplaced = getHtmlReplaced;
|
|
17
17
|
exports.getEmailTitle = getEmailTitle;
|
|
18
|
-
exports.getEmailTranslation = getEmailTranslation;
|
|
19
18
|
const fs = require("fs");
|
|
20
19
|
function atob(str) {
|
|
21
20
|
return Buffer.from(str, 'base64').toString('binary');
|
|
@@ -234,8 +233,8 @@ function replacePlaceholders(data, keys, depth) {
|
|
|
234
233
|
function getHtml(template, translationKeyValue) {
|
|
235
234
|
if (!template)
|
|
236
235
|
return undefined;
|
|
237
|
-
const pattern = new RegExp('{{\\s*([
|
|
238
|
-
const replacer = (_, key) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ??
|
|
236
|
+
const pattern = new RegExp('{{\\s*([^{}]+?)\\s*}}', 'g');
|
|
237
|
+
const replacer = (_, key) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? key;
|
|
239
238
|
template = template.replace(pattern, replacer);
|
|
240
239
|
template = template.replace(pattern, replacer);
|
|
241
240
|
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
@@ -251,14 +250,21 @@ function getHtml(template, translationKeyValue) {
|
|
|
251
250
|
});
|
|
252
251
|
return template;
|
|
253
252
|
}
|
|
254
|
-
function getHtmlReplaced(config, templates, key, lang, defaultTemplateTranslations = {}, envSpecificTranslations, extraData, newPage) {
|
|
255
|
-
const
|
|
253
|
+
function getHtmlReplaced(config, templates, key, lang, defaultTemplateTranslations = {}, projectSpecificTranslations = {}, envSpecificTranslations, extraData, newPage) {
|
|
254
|
+
const asTranslationMap = (value) => value && typeof value === 'object' ? value : {};
|
|
256
255
|
const defaultTranslations = defaultTemplateTranslations?.[lang] || defaultTemplateTranslations?.[0] || {};
|
|
257
|
-
const
|
|
258
|
-
||
|
|
259
|
-
||
|
|
256
|
+
const projectSpecificTrans = {
|
|
257
|
+
...asTranslationMap(projectSpecificTranslations?.[config.app.product]?.[lang] || projectSpecificTranslations?.[config.app.name]?.[0]),
|
|
258
|
+
...asTranslationMap(projectSpecificTranslations?.[config.app.product]?.[key]?.[lang] || projectSpecificTranslations?.[config.app.name]?.[key]?.[0])
|
|
259
|
+
};
|
|
260
|
+
const envSpecificTrans = {
|
|
261
|
+
...asTranslationMap(envSpecificTranslations?.[config.app.name]?.[lang] || envSpecificTranslations?.[config.app.name]?.[0]),
|
|
262
|
+
...asTranslationMap(envSpecificTranslations?.[config.app.name]?.[key]?.[lang] || envSpecificTranslations?.[config.app.name]?.[key]?.[0])
|
|
263
|
+
};
|
|
264
|
+
extraData = Object.fromEntries(Object.entries(extraData || {}).map(([k, v]) => [k, `{{${v}}}`]));
|
|
260
265
|
const translationKeyValue = {
|
|
261
266
|
...defaultTranslations,
|
|
267
|
+
...projectSpecificTrans,
|
|
262
268
|
...envSpecificTrans,
|
|
263
269
|
...extraData,
|
|
264
270
|
domain: config.frontend,
|
|
@@ -279,11 +285,10 @@ function getHtmlReplaced(config, templates, key, lang, defaultTemplateTranslatio
|
|
|
279
285
|
}
|
|
280
286
|
return html;
|
|
281
287
|
}
|
|
282
|
-
function getEmailTitle(key, lang,
|
|
288
|
+
function getEmailTitle(config, key, lang, defaultTitles = {}, projectSpecificTitles = {}, envSpecificTitles = {}, extraData = {}) {
|
|
283
289
|
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
284
|
-
const title =
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return translationKeyValue?.[lang]?.[key] ?? translationKeyValue?.[defaultLang]?.[key];
|
|
290
|
+
const title = envSpecificTitles?.[config.app.name]?.[lang]?.[key]
|
|
291
|
+
|| projectSpecificTitles?.[config.app.product]?.[lang]?.[key]
|
|
292
|
+
|| defaultTitles?.[lang]?.[key];
|
|
293
|
+
return title?.replace(pattern, (_, placeholderKey) => extraData?.[placeholderKey] || config.app.emailConfig?.[placeholderKey] || placeholderKey);
|
|
289
294
|
}
|
package/services/utils.ts
CHANGED
|
@@ -211,8 +211,8 @@ export function replacePlaceholders(data: Record<string, any>, keys: string[], d
|
|
|
211
211
|
|
|
212
212
|
function getHtml(template: string, translationKeyValue: Record<string, any>): string | undefined {
|
|
213
213
|
if(!template) return undefined;
|
|
214
|
-
const pattern = new RegExp('{{\\s*([
|
|
215
|
-
const replacer = (_: string, key: string) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ??
|
|
214
|
+
const pattern = new RegExp('{{\\s*([^{}]+?)\\s*}}', 'g');
|
|
215
|
+
const replacer = (_: string, key: string) => replacePlaceholders(translationKeyValue, key.split('.'), 0) ?? key;
|
|
216
216
|
template = template.replace(pattern, replacer);
|
|
217
217
|
template = template.replace(pattern, replacer);
|
|
218
218
|
template = template.replace(new RegExp('\\[\\[\\s*([a-zA-Z._0-9:]+)\\s*\\]\\]', 'g'), (_, key) => {
|
|
@@ -229,14 +229,30 @@ function getHtml(template: string, translationKeyValue: Record<string, any>): st
|
|
|
229
229
|
return template;
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
-
export function getHtmlReplaced(config:
|
|
233
|
-
|
|
232
|
+
export function getHtmlReplaced(config: {frontend: string, app: {product: string, name: string, emailConfig: Record<string, string>}},
|
|
233
|
+
templates: Record<string, string>,
|
|
234
|
+
key: string, lang: number,
|
|
235
|
+
defaultTemplateTranslations: Record<number, Record<string | number, string>> = {},
|
|
236
|
+
projectSpecificTranslations: Record<string, Record<string | number, Record<string | number, string>>> = {},
|
|
237
|
+
envSpecificTranslations?: Record<string, Record<string | number, Record<string | number, string>>>,
|
|
238
|
+
extraData?: Record<string, any>,
|
|
239
|
+
newPage?: boolean): string {
|
|
240
|
+
const asTranslationMap = (value: unknown): Record<string | number, string> =>
|
|
241
|
+
value && typeof value === 'object' ? value as Record<string | number, string> : {};
|
|
234
242
|
const defaultTranslations = defaultTemplateTranslations?.[lang] || defaultTemplateTranslations?.[0] || {};
|
|
235
|
-
const
|
|
236
|
-
||
|
|
237
|
-
||
|
|
243
|
+
const projectSpecificTrans = {
|
|
244
|
+
...asTranslationMap(projectSpecificTranslations?.[config.app.product]?.[lang] || projectSpecificTranslations?.[config.app.name]?.[0]),
|
|
245
|
+
...asTranslationMap(projectSpecificTranslations?.[config.app.product]?.[key]?.[lang] || projectSpecificTranslations?.[config.app.name]?.[key]?.[0])
|
|
246
|
+
};
|
|
247
|
+
const envSpecificTrans = {
|
|
248
|
+
...asTranslationMap(envSpecificTranslations?.[config.app.name]?.[lang] || envSpecificTranslations?.[config.app.name]?.[0]),
|
|
249
|
+
...asTranslationMap(envSpecificTranslations?.[config.app.name]?.[key]?.[lang] || envSpecificTranslations?.[config.app.name]?.[key]?.[0])
|
|
250
|
+
};
|
|
251
|
+
// Add {{}} around all extraData values so that they will be auto translated in the template if needed
|
|
252
|
+
extraData = Object.fromEntries(Object.entries(extraData || {}).map(([k, v]) => [k, `{{${v}}}`]));
|
|
238
253
|
const translationKeyValue = {
|
|
239
254
|
...defaultTranslations,
|
|
255
|
+
...projectSpecificTrans,
|
|
240
256
|
...envSpecificTrans,
|
|
241
257
|
...extraData,
|
|
242
258
|
domain: config.frontend,
|
|
@@ -258,12 +274,15 @@ export function getHtmlReplaced(config: Record<string, any>, templates: Record<s
|
|
|
258
274
|
return html;
|
|
259
275
|
}
|
|
260
276
|
|
|
261
|
-
export function getEmailTitle(
|
|
277
|
+
export function getEmailTitle(config: {frontend: string, app: {product: string, name: string, emailConfig: Record<string, any>}},
|
|
278
|
+
key: string, lang: number,
|
|
279
|
+
defaultTitles: Record<number, Record<string | number, string>> = {},
|
|
280
|
+
projectSpecificTitles: Record<string, Record<number, Record<string | number, string>>> = {},
|
|
281
|
+
envSpecificTitles: Record<string, Record<number, Record<string | number, string>>> = {},
|
|
282
|
+
extraData: Record<string, string> = {}): string | undefined {
|
|
262
283
|
const pattern = new RegExp('{{\\s*([a-zA-Z._0-9]+)\\s*}}', 'g');
|
|
263
|
-
const title =
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
export function getEmailTranslation(key: string, lang: number, translationKeyValue: Record<number, any>, defaultLang: number = 0): string | undefined {
|
|
268
|
-
return translationKeyValue?.[lang]?.[key] ?? translationKeyValue?.[defaultLang]?.[key];
|
|
284
|
+
const title = envSpecificTitles?.[config.app.name]?.[lang]?.[key]
|
|
285
|
+
|| projectSpecificTitles?.[config.app.product]?.[lang]?.[key]
|
|
286
|
+
|| defaultTitles?.[lang]?.[key];
|
|
287
|
+
return title?.replace(pattern, (_: string, placeholderKey: string) => extraData?.[placeholderKey] || config.app.emailConfig?.[placeholderKey] || placeholderKey);
|
|
269
288
|
}
|