@vtex-email/vtex 0.2.0 → 0.3.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,eA6JnC,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,
@@ -55,21 +56,37 @@ var messageCenterEmission = {
55
56
  arity: { min: 1, max: 1 },
56
57
  args: ["path"]
57
58
  },
59
+ {
60
+ name: "formatTime",
61
+ form: "inline",
62
+ evidence: "experimental",
63
+ note: "Local HH:mm from a Date-parseable path. Zero-padded 24-hour clock. Timezone follows the host Date. Not verified on Message Center.",
64
+ arity: { min: 1, max: 1 },
65
+ args: ["path"]
66
+ },
67
+ {
68
+ name: "formatDateTime",
69
+ form: "inline",
70
+ evidence: "experimental",
71
+ 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.",
72
+ arity: { min: 1, max: 1 },
73
+ args: ["path"]
74
+ },
58
75
  {
59
76
  name: "replace",
60
77
  form: "inline",
61
78
  evidence: "documented",
62
- note: "The official example replaces one occurrence of a literal in a path. The simulator follows that arity.",
79
+ note: "Replaces the first occurrence. Search and replacement are path or literal. The simulator expects three strings.",
63
80
  arity: { min: 3, max: 3 },
64
- args: ["path", "literal", "literal"]
81
+ args: ["path", "expression", "expression"]
65
82
  },
66
83
  {
67
84
  name: "eq",
68
85
  form: "block",
69
86
  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.",
87
+ note: "Strict equality. Path versus path or path versus literal. Both branches keep context. Not verified on Message Center.",
71
88
  arity: { min: 1, max: 1 },
72
- args: ["literal"],
89
+ args: ["expression"],
73
90
  context: "preserve",
74
91
  elseContext: "preserve"
75
92
  },
@@ -77,10 +94,10 @@ var messageCenterEmission = {
77
94
  name: "ifCond",
78
95
  form: "block",
79
96
  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.",
97
+ note: "Compares a path to a path or literal with ==, ===, !=, <, <=, >, or >=. Unknown operators fail at compile time. Both branches keep context. Project-local helper; not verified on Message Center.",
81
98
  arity: { min: 2, max: 2 },
82
- args: ["literal", "literal"],
83
- literals: [{ index: 0, values: ["==", "===", "!="] }],
99
+ args: ["literal", "expression"],
100
+ literals: [{ index: 0, values: comparisonOperators }],
84
101
  context: "preserve",
85
102
  elseContext: "preserve"
86
103
  },
@@ -88,9 +105,9 @@ var messageCenterEmission = {
88
105
  name: "hasSubStr",
89
106
  form: "block",
90
107
  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.",
108
+ 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
109
  arity: { min: 1, max: 1 },
93
- args: ["literal"],
110
+ args: ["expression"],
94
111
  context: "preserve",
95
112
  elseContext: "preserve"
96
113
  },
@@ -102,6 +119,42 @@ var messageCenterEmission = {
102
119
  hash: [{ name: "by", kind: "literal" }],
103
120
  context: "item",
104
121
  elseContext: "outer"
122
+ },
123
+ {
124
+ name: "with",
125
+ form: "block",
126
+ evidence: "experimental",
127
+ note: "Native Handlebars with. The object becomes the current context. The fallback uses the outer context. Not verified on Message Center.",
128
+ context: "item",
129
+ elseContext: "outer"
130
+ },
131
+ {
132
+ name: "math",
133
+ form: "inline",
134
+ evidence: "experimental",
135
+ note: "Arithmetic on two expressions with +, -, *, /, or %. Non-finite results fail the local simulator. Not verified on Message Center.",
136
+ arity: { min: 3, max: 3 },
137
+ args: ["expression", "literal", "expression"],
138
+ literals: [{ index: 1, values: ["+", "-", "*", "/", "%"] }]
139
+ },
140
+ {
141
+ name: "math",
142
+ form: "block",
143
+ evidence: "experimental",
144
+ 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.",
145
+ arity: { min: 2, max: 2 },
146
+ args: ["literal", "expression"],
147
+ literals: [{ index: 0, values: ["+", "-", "*", "/", "%"] }],
148
+ context: "preserve",
149
+ elseContext: "preserve"
150
+ },
151
+ {
152
+ name: "richShippingData",
153
+ form: "block",
154
+ evidence: "experimental",
155
+ 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. Not verified on Message Center.",
156
+ context: "item",
157
+ elseContext: "outer"
105
158
  }
106
159
  ]
107
160
  };
@@ -117,15 +170,27 @@ function formatCurrency(value) {
117
170
  const cents = String(absolute % 100).padStart(2, "0");
118
171
  return `${sign}${units},${cents}`;
119
172
  }
120
- function formatDate(value) {
173
+ function parseDate(value, helper) {
121
174
  const date = value instanceof Date ? value : new Date(String(value ?? ""));
122
175
  if (Number.isNaN(date.getTime())) {
123
- throw new Error("formatDate: the local simulator expects a Date-parseable value.");
176
+ throw new Error(`${helper}: the local simulator expects a Date-parseable value.`);
124
177
  }
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}`;
178
+ return date;
179
+ }
180
+ function pad2(value) {
181
+ return String(value).padStart(2, "0");
182
+ }
183
+ function formatDate(value) {
184
+ const date = parseDate(value, "formatDate");
185
+ return `${pad2(date.getDate())}/${pad2(date.getMonth() + 1)}/${date.getFullYear()}`;
186
+ }
187
+ function formatTime(value) {
188
+ const date = parseDate(value, "formatTime");
189
+ return `${pad2(date.getHours())}:${pad2(date.getMinutes())}`;
190
+ }
191
+ function formatDateTime(value) {
192
+ const date = parseDate(value, "formatDateTime");
193
+ return `${formatDate(date)} ${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`;
129
194
  }
130
195
  function replaceOnce(value, search, next) {
131
196
  if (typeof value !== "string" || typeof search !== "string" || typeof next !== "string") {
@@ -144,6 +209,81 @@ function readProperty(obj, prop) {
144
209
  }
145
210
  return current;
146
211
  }
212
+ function compareValues(left, operator, right) {
213
+ switch (operator) {
214
+ case "==":
215
+ return left == right;
216
+ case "===":
217
+ return left === right;
218
+ case "!=":
219
+ return left != right;
220
+ case "<":
221
+ return left < right;
222
+ case "<=":
223
+ return left <= right;
224
+ case ">":
225
+ return left > right;
226
+ case ">=":
227
+ return left >= right;
228
+ default:
229
+ return null;
230
+ }
231
+ }
232
+ function applyMath(left, operator, right) {
233
+ const lvalue = Number.parseFloat(String(left));
234
+ const rvalue = Number.parseFloat(String(right));
235
+ const result = {
236
+ "+": lvalue + rvalue,
237
+ "-": lvalue - rvalue,
238
+ "*": lvalue * rvalue,
239
+ "/": lvalue / rvalue,
240
+ "%": lvalue % rvalue
241
+ }[String(operator)];
242
+ if (result === void 0 || !Number.isFinite(result)) {
243
+ throw new Error("math: the local simulator expects a finite numeric result.");
244
+ }
245
+ return String(result);
246
+ }
247
+ var estimatePattern = /^(\d+)(m|h|d|bd)$/;
248
+ function enrichLogisticsItem(item) {
249
+ const next = { ...item };
250
+ const selected = item.selectedSla;
251
+ const slas = Array.isArray(item.slas) ? item.slas : [];
252
+ for (const sla of slas) {
253
+ if (!sla || typeof sla !== "object") continue;
254
+ const row = sla;
255
+ if (item.selectedSla !== row.id) continue;
256
+ const estimate = typeof row.shippingEstimate === "string" ? row.shippingEstimate : "";
257
+ const match = estimatePattern.exec(estimate);
258
+ next.packageId = `${String(row.id ?? "")}${String(row.shippingEstimateDate ?? "")}${estimate}`;
259
+ next.shippingEstimateDays = match ? match[1] : estimate;
260
+ next.shippingEstimateDaysType = match ? match[2] : null;
261
+ next.shippingEstimate = row.shippingEstimate;
262
+ next.shippingEstimateDate = row.shippingEstimateDate;
263
+ next.deliveryWindow = row.deliveryWindow;
264
+ next.availableDeliveryWindows = row.availableDeliveryWindows;
265
+ void selected;
266
+ break;
267
+ }
268
+ return next;
269
+ }
270
+ function richShippingData(_context, values, options) {
271
+ const shipping = values[0];
272
+ if (shipping == null || typeof shipping !== "object") return options.inverse(_context);
273
+ const source = shipping;
274
+ const logistics = Array.isArray(source.logisticsInfo) ? source.logisticsInfo : [];
275
+ const enriched = logistics.map(
276
+ (item) => item && typeof item === "object" ? enrichLogisticsItem(item) : item
277
+ );
278
+ const sorted = [...enriched].sort((left, right) => {
279
+ const a = left && typeof left === "object" ? Number(left.shippingEstimateDays) : Number.NaN;
280
+ const b = right && typeof right === "object" ? Number(right.shippingEstimateDays) : Number.NaN;
281
+ const leftValue = Number.isFinite(a) ? a : Number.POSITIVE_INFINITY;
282
+ const rightValue = Number.isFinite(b) ? b : Number.POSITIVE_INFINITY;
283
+ return leftValue - rightValue;
284
+ });
285
+ return options.fn({ ...source, logisticsInfo: sorted });
286
+ }
147
287
  var messageCenterSimulator = {
148
288
  helpers: [
149
289
  {
@@ -160,18 +300,39 @@ var messageCenterSimulator = {
160
300
  note: "Local dd/MM/yyyy. Not verified on VTEX.",
161
301
  apply: formatDate
162
302
  },
303
+ {
304
+ name: "formatTime",
305
+ kind: "inline",
306
+ evidence: "experimental",
307
+ note: "Local HH:mm with zero padding. Not verified on VTEX.",
308
+ apply: formatTime
309
+ },
310
+ {
311
+ name: "formatDateTime",
312
+ kind: "inline",
313
+ evidence: "experimental",
314
+ note: "Local dd/MM/yyyy HH:mm:ss with zero padding. Not verified on VTEX.",
315
+ apply: formatDateTime
316
+ },
163
317
  {
164
318
  name: "replace",
165
319
  kind: "inline",
166
320
  evidence: "documented",
167
- note: "One occurrence. A path and two literals.",
321
+ note: "One occurrence. A path and two expressions that resolve to strings.",
168
322
  apply: replaceOnce
169
323
  },
324
+ {
325
+ name: "math",
326
+ kind: "inline",
327
+ evidence: "experimental",
328
+ note: "Local arithmetic. Not verified on VTEX.",
329
+ apply: applyMath
330
+ },
170
331
  {
171
332
  name: "eq",
172
333
  kind: "block",
173
334
  evidence: "experimental",
174
- note: "Local strict equality. Not verified on VTEX.",
335
+ note: "Local strict equality. Path versus path or literal. Not verified on VTEX.",
175
336
  apply: (context, values, options) => {
176
337
  const [left, right] = values;
177
338
  return left === right ? options.fn(context) : options.inverse(context);
@@ -181,14 +342,11 @@ var messageCenterSimulator = {
181
342
  name: "ifCond",
182
343
  kind: "block",
183
344
  evidence: "experimental",
184
- note: "Local ==, ===, and !=. Not verified on VTEX.",
345
+ note: "Local ==, ===, !=, <, <=, >, and >=. Not verified on VTEX.",
185
346
  apply: (context, values, options) => {
186
347
  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);
348
+ const pass = compareValues(left, operator, right);
349
+ if (pass === null) return options.inverse(context);
192
350
  return pass ? options.fn(context) : options.inverse(context);
193
351
  }
194
352
  },
@@ -196,7 +354,7 @@ var messageCenterSimulator = {
196
354
  name: "hasSubStr",
197
355
  kind: "block",
198
356
  evidence: "experimental",
199
- note: "Local substring check. Not verified on VTEX.",
357
+ note: "Local substring check. Search may be a path or literal. Not verified on VTEX.",
200
358
  apply: (context, values, options) => {
201
359
  const [value, search] = values;
202
360
  if (value != null && String(value).includes(String(search))) return options.fn(context);
@@ -232,6 +390,24 @@ var messageCenterSimulator = {
232
390
  }
233
391
  return buffer;
234
392
  }
393
+ },
394
+ {
395
+ name: "with",
396
+ kind: "block",
397
+ evidence: "experimental",
398
+ note: "Local Handlebars with. Not verified on VTEX.",
399
+ apply: (context, values, options) => {
400
+ const target = values[0];
401
+ if (!target) return options.inverse(context);
402
+ return options.fn(target);
403
+ }
404
+ },
405
+ {
406
+ name: "richShippingData",
407
+ kind: "block",
408
+ evidence: "experimental",
409
+ note: "Clones shippingData before deriving SLA fields. Not verified on VTEX.",
410
+ apply: richShippingData
235
411
  }
236
412
  ]
237
413
  };
@@ -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;AAmJtD,eAAO,MAAM,sBAAsB,EAAE,cA8HpC,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.3.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.3.0"
22
22
  }
23
23
  }