@vtex-email/vtex 0.2.0 → 0.4.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.
@@ -1 +1 @@
1
- {"version":3,"file":"capabilities.d.ts","sourceRoot":"","sources":["../src/capabilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAEvD,eAAO,MAAM,qBAAqB,EAAE,eAyGnC,CAAA"}
1
+ {"version":3,"file":"capabilities.d.ts","sourceRoot":"","sources":["../src/capabilities.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAIvD,eAAO,MAAM,qBAAqB,EAAE,eA+KnC,CAAA"}
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/capabilities.ts
2
+ var comparisonOperators = ["==", "===", "!=", "<", "<=", ">", ">="];
2
3
  var messageCenterEmission = {
3
4
  id: "p0-message-center-experimental",
4
5
  allowParentSegments: true,
@@ -6,52 +7,86 @@ var messageCenterEmission = {
6
7
  {
7
8
  name: "each",
8
9
  form: "block",
9
- evidence: "documented",
10
- note: "Native Handlebars helper. The item context replaces the current context. The fallback uses the outer context.",
10
+ evidence: "verified",
11
+ note: "Native Handlebars helper. The item context replaces the current context. The fallback uses the outer context. Verified on Message Center.",
11
12
  context: "item",
12
13
  elseContext: "outer"
13
14
  },
14
15
  {
15
16
  name: "if",
16
17
  form: "block",
17
- evidence: "documented",
18
- note: "Handlebars truthiness. 0, an empty string, and an empty array are false. The toolchain does not use Boolean(). Both branches keep the current context.",
18
+ evidence: "verified",
19
+ note: "Handlebars truthiness. 0, an empty string, and an empty array are false. The toolchain does not use Boolean(). Both branches keep the current context. Verified on Message Center.",
19
20
  context: "preserve",
20
21
  elseContext: "preserve"
21
22
  },
22
23
  {
23
24
  name: "unless",
24
25
  form: "block",
25
- evidence: "documented",
26
- note: "Negation of if. Both branches keep the current context.",
26
+ evidence: "verified",
27
+ note: "Negation of if. Both branches keep the current context. Verified on Message Center for a falsy path showing the positive branch.",
27
28
  context: "preserve",
28
29
  elseContext: "preserve"
29
30
  },
30
31
  {
31
32
  name: "../",
32
33
  form: "path",
33
- evidence: "documented",
34
- note: "Parent segment documented by Handlebars. A path cannot climb above the template root."
34
+ evidence: "verified",
35
+ note: "Parent segment documented by Handlebars. A path cannot climb above the template root. Verified on Message Center in nested item and estimate paths."
35
36
  },
36
37
  {
37
38
  name: "@index",
38
39
  form: "path",
39
- evidence: "experimental",
40
- note: "Zero-based index inside each. Not available in group. Not verified on Message Center."
40
+ evidence: "verified",
41
+ note: "Zero-based index inside each. Not available in group. Verified on Message Center with eq and ifCond against @index."
41
42
  },
42
43
  {
43
44
  name: "formatCurrency",
44
45
  form: "inline",
45
- evidence: "documented",
46
- note: "VTEX documents 20000 as 200,00, without a symbol. Other integers follow the same local simulator, without verified parity.",
46
+ evidence: "verified",
47
+ note: "Cents to decimal without a symbol. Message Center verified pairs include 24000, 24500, 500, 196500, and 197000.",
48
+ arity: { min: 1, max: 1 },
49
+ args: ["path"]
50
+ },
51
+ {
52
+ name: "formatDiscount",
53
+ form: "inline",
54
+ evidence: "experimental",
55
+ note: "Cents to decimal without a symbol and without the sign (-1500 prints 15,00). Project-local helper; not verified on Message Center.",
47
56
  arity: { min: 1, max: 1 },
48
57
  args: ["path"]
49
58
  },
59
+ {
60
+ name: "eval",
61
+ form: "inline",
62
+ emit: "empty-block",
63
+ evidence: "experimental",
64
+ note: 'Emitted as {{#eval "code" name=path}}{{/eval}}. The destination replaces each ${name} in the code with the named argument and prints the result of evaluating it, unescaped. The local simulator runs the code in an isolated VM context. Not verified on Message Center.',
65
+ arity: { min: 1, max: 1 },
66
+ args: ["literal"],
67
+ freeHash: "expression"
68
+ },
50
69
  {
51
70
  name: "formatDate",
52
71
  form: "inline",
72
+ evidence: "verified",
73
+ note: "dd/MM/yyyy from a Date-parseable path. Verified on Message Center for shippingEstimateDate when deliveryWindow is absent. Timezone follows the host Date.",
74
+ arity: { min: 1, max: 1 },
75
+ args: ["path"]
76
+ },
77
+ {
78
+ name: "formatTime",
79
+ form: "inline",
53
80
  evidence: "experimental",
54
- note: "Local dd/MM/yyyy from a Date-parseable path. Timezone follows the host Date. Not verified on Message Center.",
81
+ note: "Local HH:mm from a Date-parseable path. Zero-padded 24-hour clock. Timezone follows the host Date. Not verified on Message Center.",
82
+ arity: { min: 1, max: 1 },
83
+ args: ["path"]
84
+ },
85
+ {
86
+ name: "formatDateTime",
87
+ form: "inline",
88
+ evidence: "experimental",
89
+ note: "Local dd/MM/yyyy HH:mm:ss from a Date-parseable path. Zero-padded 24-hour clock. Timezone follows the host Date. Not verified on Message Center.",
55
90
  arity: { min: 1, max: 1 },
56
91
  args: ["path"]
57
92
  },
@@ -59,28 +94,28 @@ var messageCenterEmission = {
59
94
  name: "replace",
60
95
  form: "inline",
61
96
  evidence: "documented",
62
- note: "The official example replaces one occurrence of a literal in a path. The simulator follows that arity.",
97
+ note: "Replaces the first occurrence. Search and replacement are path or literal. The simulator expects three strings.",
63
98
  arity: { min: 3, max: 3 },
64
- args: ["path", "literal", "literal"]
99
+ args: ["path", "expression", "expression"]
65
100
  },
66
101
  {
67
102
  name: "eq",
68
103
  form: "block",
69
- evidence: "experimental",
70
- note: "Strict equality. The merge uses path plus locale literal. The DSL emits path plus a literal. Path versus path is out of scope. Not verified on Message Center.",
104
+ evidence: "verified",
105
+ note: "Strict equality. Path versus path or path versus literal. Both branches keep context. Verified on Message Center for locale merge (pt-BR), installments 1, totals Items/Shipping, and address or item match.",
71
106
  arity: { min: 1, max: 1 },
72
- args: ["literal"],
107
+ args: ["expression"],
73
108
  context: "preserve",
74
109
  elseContext: "preserve"
75
110
  },
76
111
  {
77
112
  name: "ifCond",
78
113
  form: "block",
79
- evidence: "experimental",
80
- note: "Compares a path to a literal with ==, ===, or !=. Unknown operators fail at compile time. Both branches keep context. Project-local helper; not verified on Message Center.",
114
+ evidence: "verified",
115
+ note: "Compares a path to a path or literal with ==, ===, !=, <, <=, >, or >=. Unknown operators fail at compile time. Both branches keep context. Verified on Message Center for == and !=. Other operators remain unexercised in those runs.",
81
116
  arity: { min: 2, max: 2 },
82
- args: ["literal", "literal"],
83
- literals: [{ index: 0, values: ["==", "===", "!="] }],
117
+ args: ["literal", "expression"],
118
+ literals: [{ index: 0, values: comparisonOperators }],
84
119
  context: "preserve",
85
120
  elseContext: "preserve"
86
121
  },
@@ -88,25 +123,62 @@ var messageCenterEmission = {
88
123
  name: "hasSubStr",
89
124
  form: "block",
90
125
  evidence: "experimental",
91
- note: "True when the path value is not null and String(value) contains the literal. Both branches keep context. Project-local helper; not verified on Message Center.",
126
+ note: "True when the path value is not null and String(value) contains the search path or literal. Both branches keep context. Project-local helper; not verified on Message Center.",
92
127
  arity: { min: 1, max: 1 },
93
- args: ["literal"],
128
+ args: ["expression"],
94
129
  context: "preserve",
95
130
  elseContext: "preserve"
96
131
  },
97
132
  {
98
133
  name: "group",
99
134
  form: "block",
100
- evidence: "experimental",
101
- note: "Groups an array by a hash by identifier. The item context is { index, value, items }. Empty or missing lists use the outer fallback. Does not invent keys. Project-local helper; not verified on Message Center.",
135
+ evidence: "verified",
136
+ note: "Groups an array by a hash by identifier. The item context is { index, value, items }. Empty or missing lists use the outer fallback. Does not invent keys. Verified on Message Center with by=packageId and by=addressId.",
102
137
  hash: [{ name: "by", kind: "literal" }],
103
138
  context: "item",
104
139
  elseContext: "outer"
140
+ },
141
+ {
142
+ name: "with",
143
+ form: "block",
144
+ evidence: "experimental",
145
+ note: "Native Handlebars with. The object becomes the current context. The fallback uses the outer context. Not verified on Message Center.",
146
+ context: "item",
147
+ elseContext: "outer"
148
+ },
149
+ {
150
+ name: "math",
151
+ form: "inline",
152
+ evidence: "experimental",
153
+ note: "Arithmetic on two expressions with +, -, *, /, or %. Non-finite results fail the local simulator. Not verified on Message Center.",
154
+ arity: { min: 3, max: 3 },
155
+ args: ["expression", "literal", "expression"],
156
+ literals: [{ index: 1, values: ["+", "-", "*", "/", "%"] }]
157
+ },
158
+ {
159
+ name: "math",
160
+ form: "block",
161
+ evidence: "experimental",
162
+ note: "Block form of math used by official templates. Path is the left operand; args are the operator and the right expression. Does not push context. Not verified on Message Center.",
163
+ arity: { min: 2, max: 2 },
164
+ args: ["literal", "expression"],
165
+ literals: [{ index: 0, values: ["+", "-", "*", "/", "%"] }],
166
+ context: "preserve",
167
+ elseContext: "preserve"
168
+ },
169
+ {
170
+ name: "richShippingData",
171
+ form: "block",
172
+ evidence: "verified",
173
+ note: "Clones shippingData, selects the SLA matching selectedSla, derives packageId and estimate fields on logisticsInfo, then renders with shippingData as the current context. Does not mutate the fixture. Verified on Message Center.",
174
+ context: "item",
175
+ elseContext: "outer"
105
176
  }
106
177
  ]
107
178
  };
108
179
 
109
180
  // src/simulator.ts
181
+ import { runInNewContext } from "node:vm";
110
182
  function formatCurrency(value) {
111
183
  if (typeof value !== "number" || !Number.isInteger(value)) {
112
184
  throw new Error("formatCurrency: the local simulator expects integer cents.");
@@ -117,15 +189,39 @@ function formatCurrency(value) {
117
189
  const cents = String(absolute % 100).padStart(2, "0");
118
190
  return `${sign}${units},${cents}`;
119
191
  }
120
- function formatDate(value) {
192
+ function formatDiscount(value) {
193
+ const number = typeof value === "number" ? value : Number.NaN;
194
+ const cents = Number.isNaN(number) ? 0 : Math.abs(number);
195
+ return (cents / 100).toFixed(2).replace(".", ",");
196
+ }
197
+ var evalVariable = /\$\{(\S+?)\}/g;
198
+ function evaluateCode(code, hash) {
199
+ if (typeof code !== "string") throw new Error("eval: the local simulator expects a code string.");
200
+ const compiled = code.replace(evalVariable, (_match, name) => String(hash[name] ?? ""));
201
+ const result = runInNewContext(compiled, /* @__PURE__ */ Object.create(null), { timeout: 100 });
202
+ return result == null ? "" : String(result);
203
+ }
204
+ function parseDate(value, helper) {
121
205
  const date = value instanceof Date ? value : new Date(String(value ?? ""));
122
206
  if (Number.isNaN(date.getTime())) {
123
- throw new Error("formatDate: the local simulator expects a Date-parseable value.");
207
+ throw new Error(`${helper}: the local simulator expects a Date-parseable value.`);
124
208
  }
125
- const day = String(date.getDate()).padStart(2, "0");
126
- const month = String(date.getMonth() + 1).padStart(2, "0");
127
- const year = String(date.getFullYear());
128
- return `${day}/${month}/${year}`;
209
+ return date;
210
+ }
211
+ function pad2(value) {
212
+ return String(value).padStart(2, "0");
213
+ }
214
+ function formatDate(value) {
215
+ const date = parseDate(value, "formatDate");
216
+ return `${pad2(date.getDate())}/${pad2(date.getMonth() + 1)}/${date.getFullYear()}`;
217
+ }
218
+ function formatTime(value) {
219
+ const date = parseDate(value, "formatTime");
220
+ return `${pad2(date.getHours())}:${pad2(date.getMinutes())}`;
221
+ }
222
+ function formatDateTime(value) {
223
+ const date = parseDate(value, "formatDateTime");
224
+ return `${formatDate(date)} ${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;
129
225
  }
130
226
  function replaceOnce(value, search, next) {
131
227
  if (typeof value !== "string" || typeof search !== "string" || typeof next !== "string") {
@@ -144,34 +240,144 @@ function readProperty(obj, prop) {
144
240
  }
145
241
  return current;
146
242
  }
243
+ function compareValues(left, operator, right) {
244
+ switch (operator) {
245
+ case "==":
246
+ return left == right;
247
+ case "===":
248
+ return left === right;
249
+ case "!=":
250
+ return left != right;
251
+ case "<":
252
+ return left < right;
253
+ case "<=":
254
+ return left <= right;
255
+ case ">":
256
+ return left > right;
257
+ case ">=":
258
+ return left >= right;
259
+ default:
260
+ return null;
261
+ }
262
+ }
263
+ function applyMath(left, operator, right) {
264
+ const lvalue = Number.parseFloat(String(left));
265
+ const rvalue = Number.parseFloat(String(right));
266
+ const result = {
267
+ "+": lvalue + rvalue,
268
+ "-": lvalue - rvalue,
269
+ "*": lvalue * rvalue,
270
+ "/": lvalue / rvalue,
271
+ "%": lvalue % rvalue
272
+ }[String(operator)];
273
+ if (result === void 0 || !Number.isFinite(result)) {
274
+ throw new Error("math: the local simulator expects a finite numeric result.");
275
+ }
276
+ return String(result);
277
+ }
278
+ var estimatePattern = /^(\d+)(m|h|d|bd)$/;
279
+ function enrichLogisticsItem(item) {
280
+ const next = { ...item };
281
+ const selected = item.selectedSla;
282
+ const slas = Array.isArray(item.slas) ? item.slas : [];
283
+ for (const sla of slas) {
284
+ if (!sla || typeof sla !== "object") continue;
285
+ const row = sla;
286
+ if (item.selectedSla !== row.id) continue;
287
+ const estimate = typeof row.shippingEstimate === "string" ? row.shippingEstimate : "";
288
+ const match = estimatePattern.exec(estimate);
289
+ next.packageId = `${String(row.id ?? "")}${String(row.shippingEstimateDate ?? "")}${estimate}`;
290
+ next.shippingEstimateDays = match ? match[1] : estimate;
291
+ next.shippingEstimateDaysType = match ? match[2] : null;
292
+ next.shippingEstimate = row.shippingEstimate;
293
+ next.shippingEstimateDate = row.shippingEstimateDate;
294
+ next.deliveryWindow = row.deliveryWindow;
295
+ next.availableDeliveryWindows = row.availableDeliveryWindows;
296
+ void selected;
297
+ break;
298
+ }
299
+ return next;
300
+ }
301
+ function richShippingData(_context, values, options) {
302
+ const shipping = values[0];
303
+ if (shipping == null || typeof shipping !== "object") return options.inverse(_context);
304
+ const source = shipping;
305
+ const logistics = Array.isArray(source.logisticsInfo) ? source.logisticsInfo : [];
306
+ const enriched = logistics.map(
307
+ (item) => item && typeof item === "object" ? enrichLogisticsItem(item) : item
308
+ );
309
+ const sorted = [...enriched].sort((left, right) => {
310
+ const a = left && typeof left === "object" ? Number(left.shippingEstimateDays) : Number.NaN;
311
+ const b = right && typeof right === "object" ? Number(right.shippingEstimateDays) : Number.NaN;
312
+ const leftValue = Number.isFinite(a) ? a : Number.POSITIVE_INFINITY;
313
+ const rightValue = Number.isFinite(b) ? b : Number.POSITIVE_INFINITY;
314
+ return leftValue - rightValue;
315
+ });
316
+ return options.fn({ ...source, logisticsInfo: sorted });
317
+ }
147
318
  var messageCenterSimulator = {
148
319
  helpers: [
149
320
  {
150
321
  name: "formatCurrency",
151
322
  kind: "inline",
152
- evidence: "documented",
153
- note: "20000 -> 200,00 is documented. Other integers are a local contract.",
323
+ evidence: "verified",
324
+ note: "Cents to decimal without a symbol. Message Center verified pairs include 24000, 24500, 500, 196500, and 197000.",
154
325
  apply: formatCurrency
155
326
  },
156
327
  {
157
- name: "formatDate",
328
+ name: "formatDiscount",
158
329
  kind: "inline",
159
330
  evidence: "experimental",
160
- note: "Local dd/MM/yyyy. Not verified on VTEX.",
331
+ note: "Local cents to decimal without sign. Not verified on VTEX.",
332
+ apply: formatDiscount
333
+ },
334
+ {
335
+ name: "eval",
336
+ kind: "block",
337
+ evidence: "experimental",
338
+ note: "Local evaluation in an isolated VM context after ${name} replacement. Not verified on VTEX.",
339
+ apply: (_context, values, options) => evaluateCode(values[0], options.hash)
340
+ },
341
+ {
342
+ name: "formatDate",
343
+ kind: "inline",
344
+ evidence: "verified",
345
+ note: "dd/MM/yyyy. Verified on Message Center for shippingEstimateDate when deliveryWindow is absent.",
161
346
  apply: formatDate
162
347
  },
348
+ {
349
+ name: "formatTime",
350
+ kind: "inline",
351
+ evidence: "experimental",
352
+ note: "Local HH:mm with zero padding. Not verified on VTEX.",
353
+ apply: formatTime
354
+ },
355
+ {
356
+ name: "formatDateTime",
357
+ kind: "inline",
358
+ evidence: "experimental",
359
+ note: "Local dd/MM/yyyy HH:mm:ss with zero padding. Not verified on VTEX.",
360
+ apply: formatDateTime
361
+ },
163
362
  {
164
363
  name: "replace",
165
364
  kind: "inline",
166
365
  evidence: "documented",
167
- note: "One occurrence. A path and two literals.",
366
+ note: "One occurrence. A path and two expressions that resolve to strings.",
168
367
  apply: replaceOnce
169
368
  },
369
+ {
370
+ name: "math",
371
+ kind: "inline",
372
+ evidence: "experimental",
373
+ note: "Local arithmetic. Not verified on VTEX.",
374
+ apply: applyMath
375
+ },
170
376
  {
171
377
  name: "eq",
172
378
  kind: "block",
173
- evidence: "experimental",
174
- note: "Local strict equality. Not verified on VTEX.",
379
+ evidence: "verified",
380
+ note: "Strict equality. Verified on Message Center for locale merge, installments, totals, and item match.",
175
381
  apply: (context, values, options) => {
176
382
  const [left, right] = values;
177
383
  return left === right ? options.fn(context) : options.inverse(context);
@@ -180,15 +386,12 @@ var messageCenterSimulator = {
180
386
  {
181
387
  name: "ifCond",
182
388
  kind: "block",
183
- evidence: "experimental",
184
- note: "Local ==, ===, and !=. Not verified on VTEX.",
389
+ evidence: "verified",
390
+ note: "Verified on Message Center for == and !=. Other operators remain unexercised in those runs.",
185
391
  apply: (context, values, options) => {
186
392
  const [left, operator, right] = values;
187
- let pass = false;
188
- if (operator === "==") pass = left == right;
189
- else if (operator === "===") pass = left === right;
190
- else if (operator === "!=") pass = left != right;
191
- else return options.inverse(context);
393
+ const pass = compareValues(left, operator, right);
394
+ if (pass === null) return options.inverse(context);
192
395
  return pass ? options.fn(context) : options.inverse(context);
193
396
  }
194
397
  },
@@ -196,7 +399,7 @@ var messageCenterSimulator = {
196
399
  name: "hasSubStr",
197
400
  kind: "block",
198
401
  evidence: "experimental",
199
- note: "Local substring check. Not verified on VTEX.",
402
+ note: "Local substring check. Search may be a path or literal. Not verified on VTEX.",
200
403
  apply: (context, values, options) => {
201
404
  const [value, search] = values;
202
405
  if (value != null && String(value).includes(String(search))) return options.fn(context);
@@ -206,8 +409,8 @@ var messageCenterSimulator = {
206
409
  {
207
410
  name: "group",
208
411
  kind: "block",
209
- evidence: "experimental",
210
- note: "Local array grouping by hash.by. Not verified on VTEX.",
412
+ evidence: "verified",
413
+ note: "Array grouping by hash.by. Verified on Message Center with packageId and addressId.",
211
414
  apply: (context, values, options) => {
212
415
  const list = values[0];
213
416
  const prop = options.hash.by;
@@ -232,6 +435,24 @@ var messageCenterSimulator = {
232
435
  }
233
436
  return buffer;
234
437
  }
438
+ },
439
+ {
440
+ name: "with",
441
+ kind: "block",
442
+ evidence: "experimental",
443
+ note: "Local Handlebars with. Not verified on VTEX.",
444
+ apply: (context, values, options) => {
445
+ const target = values[0];
446
+ if (!target) return options.inverse(context);
447
+ return options.fn(target);
448
+ }
449
+ },
450
+ {
451
+ name: "richShippingData",
452
+ kind: "block",
453
+ evidence: "verified",
454
+ note: "Clones shippingData before deriving SLA fields. Verified on Message Center.",
455
+ apply: richShippingData
235
456
  }
236
457
  ]
237
458
  };
@@ -1 +1 @@
1
- {"version":3,"file":"simulator.d.ts","sourceRoot":"","sources":["../src/simulator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AA2CtD,eAAO,MAAM,sBAAsB,EAAE,cA0FpC,CAAA"}
1
+ {"version":3,"file":"simulator.d.ts","sourceRoot":"","sources":["../src/simulator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAwKtD,eAAO,MAAM,sBAAsB,EAAE,cA4IpC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtex-email/vtex",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/marcelotk15/vtex-react-email.git"
@@ -18,6 +18,6 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@vtex-email/core": "0.2.0"
21
+ "@vtex-email/core": "0.4.0"
22
22
  }
23
23
  }