@decocms/blocks 7.79.3 → 7.79.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/blocks",
3
- "version": "7.79.3",
3
+ "version": "7.79.5",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=24"
package/src/cms/schema.ts CHANGED
@@ -252,6 +252,43 @@ export interface MatcherConfig {
252
252
  propsSchema?: Record<string, any>;
253
253
  }
254
254
 
255
+ /** A hidden, const discriminator — deco-cx/deco's shape for `type: "X"` literals. */
256
+ function constType(type: string) {
257
+ return { type: "string", const: type, default: type, readonly: "*", hide: "true", title: "Type" };
258
+ }
259
+
260
+ /** One `case` branch of deco-cx/apps' pathname matcher. */
261
+ function pathnameCaseSchema(type: string) {
262
+ return {
263
+ title: type,
264
+ type: "object",
265
+ required: ["type"],
266
+ properties: {
267
+ pathname: { type: "string", title: "Pathname", description: 'Must start with "/"' },
268
+ negate: {
269
+ type: "boolean",
270
+ title: "Negate",
271
+ description: "Inverts the match result (NOT)",
272
+ default: false,
273
+ },
274
+ type: constType(type),
275
+ },
276
+ };
277
+ }
278
+
279
+ /** One `case` branch of deco-cx/apps' query string matcher condition. */
280
+ function queryCaseSchema(type: string, hasValue: boolean) {
281
+ return {
282
+ title: type,
283
+ type: "object",
284
+ required: hasValue ? ["value", "type"] : ["type"],
285
+ properties: {
286
+ ...(hasValue ? { value: { type: "string", title: "Value" } } : {}),
287
+ type: constType(type),
288
+ },
289
+ };
290
+ }
291
+
255
292
  const matcherRegistry: MatcherConfig[] = [];
256
293
 
257
294
  /** Register a single matcher schema for the admin. */
@@ -300,6 +337,7 @@ registerMatcherSchemas([
300
337
  type: "object",
301
338
  properties: {
302
339
  mobile: { type: "boolean", title: "Mobile" },
340
+ tablet: { type: "boolean", title: "Tablet" },
303
341
  desktop: { type: "boolean", title: "Desktop" },
304
342
  },
305
343
  },
@@ -355,7 +393,10 @@ registerMatcherSchemas([
355
393
  propsSchema: {
356
394
  type: "object",
357
395
  properties: {
358
- host: { type: "string", title: "Hostname" },
396
+ includes: { type: "string", title: "Includes" },
397
+ match: { type: "string", title: "Match" },
398
+ // Pre-parity format, still evaluated; kept so stored values validate.
399
+ host: { type: "string", title: "Hostname", hide: "true" },
359
400
  },
360
401
  },
361
402
  },
@@ -367,18 +408,16 @@ registerMatcherSchemas([
367
408
  namespace: "website",
368
409
  propsSchema: {
369
410
  type: "object",
411
+ required: ["case"],
370
412
  properties: {
371
- pattern: { type: "string", title: "Regex Pattern" },
372
- includes: {
373
- type: "array",
374
- title: "Includes",
375
- items: { type: "string" },
376
- },
377
- excludes: {
378
- type: "array",
379
- title: "Excludes",
380
- items: { type: "string" },
413
+ case: {
414
+ title: "Operation",
415
+ anyOf: ["Equals", "Includes", "Template"].map(pathnameCaseSchema),
381
416
  },
417
+ // Pre-parity format, still evaluated; kept so stored values validate.
418
+ pattern: { type: "string", title: "Regex Pattern", hide: "true" },
419
+ includes: { type: "array", title: "Includes", items: { type: "string" }, hide: "true" },
420
+ excludes: { type: "array", title: "Excludes", items: { type: "string" }, hide: "true" },
382
421
  },
383
422
  },
384
423
  },
@@ -390,9 +429,37 @@ registerMatcherSchemas([
390
429
  namespace: "website",
391
430
  propsSchema: {
392
431
  type: "object",
432
+ required: ["conditions"],
393
433
  properties: {
394
- key: { type: "string", title: "Parameter Name" },
395
- value: { type: "string", title: "Parameter Value" },
434
+ conditions: {
435
+ type: "array",
436
+ title: "Conditions",
437
+ items: {
438
+ type: "object",
439
+ title: "{{{param}}} {{{case.type}}} {{{case.value}}}",
440
+ required: ["param", "case"],
441
+ properties: {
442
+ param: { type: "string", title: "Param" },
443
+ case: {
444
+ title: "Case",
445
+ anyOf: [
446
+ ...[
447
+ "Equals",
448
+ "Greater",
449
+ "Lesser",
450
+ "GreaterOrEquals",
451
+ "LesserOrEquals",
452
+ "Includes",
453
+ ].map((type) => queryCaseSchema(type, true)),
454
+ queryCaseSchema("Exists", false),
455
+ ],
456
+ },
457
+ },
458
+ },
459
+ },
460
+ // Pre-parity format, still evaluated; kept so stored values validate.
461
+ key: { type: "string", title: "Parameter Name", hide: "true" },
462
+ value: { type: "string", title: "Parameter Value", hide: "true" },
396
463
  },
397
464
  },
398
465
  },
@@ -316,3 +316,132 @@ describe("dateMatcher — parity with deco-cx/apps website/matchers/date.ts", ()
316
316
  expect(matchDate({ end: "not-a-date" })).toBe(false);
317
317
  });
318
318
  });
319
+
320
+ // ---------------------------------------------------------------------------
321
+ // deco-cx/apps parity: pathname `case` (Template, negate), queryString
322
+ // `conditions`, host `includes`/`match`. Migrated content stores these shapes;
323
+ // the pre-parity readers ignored them, so e.g. a "brand segment" rule
324
+ // (`/:slug/p` AND `?brand=x`) never matched.
325
+ // ---------------------------------------------------------------------------
326
+
327
+ function urlCtx(url: string, headers: Record<string, string> = {}): MatcherContext {
328
+ const u = new URL(url);
329
+ return { url, path: u.pathname, request: new Request(url, { headers }) };
330
+ }
331
+
332
+ const PATHNAME_KEY = "website/matchers/pathname.ts";
333
+ const QUERY_KEY = "website/matchers/queryString.ts";
334
+ const HOST_KEY = "website/matchers/host.ts";
335
+
336
+ describe("pathnameMatcher — deco-cx/apps `case`", () => {
337
+ const pathname = (c: Record<string, unknown>, url: string) =>
338
+ evaluateMatcher({ __resolveType: PATHNAME_KEY, case: c }, urlCtx(url));
339
+
340
+ it("Template matches one path segment per `:param`, whole path", () => {
341
+ const c = { type: "Template", pathname: "/:slug/p" };
342
+ expect(pathname(c, "https://x.com/vestido-123-456/p")).toBe(true);
343
+ expect(pathname(c, "https://x.com/a/b/p")).toBe(false);
344
+ expect(pathname(c, "https://x.com/vestido-123-456/p/extra")).toBe(false);
345
+ expect(pathname(c, "https://x.com/farm-etc")).toBe(false);
346
+ });
347
+
348
+ it("negate inverts the result", () => {
349
+ const c = { type: "Includes", pathname: "/farm-etc", negate: true };
350
+ expect(pathname(c, "https://x.com/farm-etc/novidades")).toBe(false);
351
+ expect(pathname(c, "https://x.com/novidades")).toBe(true);
352
+ });
353
+
354
+ it("keeps Equals / Includes", () => {
355
+ expect(pathname({ type: "Equals", pathname: "/tapume" }, "https://x.com/tapume")).toBe(true);
356
+ expect(pathname({ type: "Equals", pathname: "/tapume" }, "https://x.com/tapume/a")).toBe(false);
357
+ expect(pathname({ type: "Includes", pathname: "/farm-etc" }, "https://x.com/farm-etc/a")).toBe(
358
+ true,
359
+ );
360
+ });
361
+
362
+ it("does not match a case without a pathname (as upstream)", () => {
363
+ expect(pathname({ type: "Includes" }, "https://x.com/anything")).toBe(false);
364
+ });
365
+ });
366
+
367
+ describe("queryStringMatcher — deco-cx/apps `conditions`", () => {
368
+ const query = (conditions: unknown[], url: string) =>
369
+ evaluateMatcher({ __resolveType: QUERY_KEY, conditions }, urlCtx(url));
370
+
371
+ it("Equals on a param", () => {
372
+ const conditions = [{ param: "brand", case: { type: "Equals", value: "farmetc" } }];
373
+ expect(query(conditions, "https://x.com/v-1-2/p?brand=farmetc")).toBe(true);
374
+ expect(query(conditions, "https://x.com/v-1-2/p?brand=farm")).toBe(false);
375
+ expect(query(conditions, "https://x.com/v-1-2/p")).toBe(false);
376
+ });
377
+
378
+ it("every condition must hold; Exists only needs the param", () => {
379
+ const conditions = [
380
+ { param: "a", case: { type: "Exists" } },
381
+ { param: "b", case: { type: "Includes", value: "x" } },
382
+ ];
383
+ expect(query(conditions, "https://x.com/?a=&b=1x2")).toBe(true);
384
+ expect(query(conditions, "https://x.com/?b=1x2")).toBe(false);
385
+ expect(query(conditions, "https://x.com/?a=1&b=2")).toBe(false);
386
+ });
387
+
388
+ it("matches when any of a repeated param's values does", () => {
389
+ const conditions = [{ param: "c", case: { type: "Equals", value: "2" } }];
390
+ expect(query(conditions, "https://x.com/?c=1&c=2")).toBe(true);
391
+ });
392
+
393
+ it("keeps the key/value format", () => {
394
+ expect(
395
+ evaluateMatcher(
396
+ { __resolveType: QUERY_KEY, key: "utm_source", value: "x" },
397
+ urlCtx("https://x.com/?utm_source=x"),
398
+ ),
399
+ ).toBe(true);
400
+ });
401
+ });
402
+
403
+ describe("a saved brand-segment rule (multi of pathname + queryString)", () => {
404
+ const segment = {
405
+ __resolveType: "website/matchers/multi.ts",
406
+ op: "or",
407
+ matchers: [
408
+ { __resolveType: PATHNAME_KEY, case: { type: "Includes", pathname: "/farm-etc" } },
409
+ {
410
+ __resolveType: "website/matchers/multi.ts",
411
+ op: "and",
412
+ matchers: [
413
+ { __resolveType: PATHNAME_KEY, case: { type: "Template", pathname: "/:slug/p" } },
414
+ {
415
+ __resolveType: QUERY_KEY,
416
+ conditions: [{ param: "brand", case: { type: "Equals", value: "farmetc" } }],
417
+ },
418
+ ],
419
+ },
420
+ ],
421
+ };
422
+
423
+ it("matches a brand listing and a brand PDP, not the other brand's PDP", () => {
424
+ expect(evaluateMatcher(segment, urlCtx("https://x.com/farm-etc/novidades"))).toBe(true);
425
+ expect(evaluateMatcher(segment, urlCtx("https://x.com/caneca-1-2/p?brand=farmetc"))).toBe(true);
426
+ expect(evaluateMatcher(segment, urlCtx("https://x.com/vestido-1-2/p?brand=farm"))).toBe(false);
427
+ });
428
+ });
429
+
430
+ describe("hostMatcher — deco-cx/apps `includes` / `match`", () => {
431
+ const host = (rule: Record<string, unknown>, url: string) =>
432
+ evaluateMatcher({ __resolveType: HOST_KEY, ...rule }, urlCtx(url));
433
+
434
+ it("includes is a substring of the host", () => {
435
+ expect(host({ includes: "www.example.com" }, "https://www.example.com/a")).toBe(true);
436
+ expect(host({ includes: "www.example.com" }, "https://preview.example.com/a")).toBe(false);
437
+ });
438
+
439
+ it("match is a regex; both must hold when both are set", () => {
440
+ expect(host({ match: "^www\\." }, "https://www.example.com/")).toBe(true);
441
+ expect(host({ match: "^www\\.", includes: "other" }, "https://www.example.com/")).toBe(false);
442
+ });
443
+
444
+ it("keeps the host format", () => {
445
+ expect(host({ host: "www.example.com" }, "https://www.example.com/")).toBe(true);
446
+ });
447
+ });
@@ -88,6 +88,30 @@ function dateMatcher(rule: Record<string, unknown>, _ctx: MatcherContext): boole
88
88
  // -------------------------------------------------------------------------
89
89
 
90
90
  function hostMatcher(rule: Record<string, unknown>, ctx: MatcherContext): boolean {
91
+ // deco-cx/apps format: `includes` (substring) and/or `match` (regex), both
92
+ // optional and AND'ed, against the request host.
93
+ const includes = rule.includes as string | undefined;
94
+ const match = rule.match as string | undefined;
95
+ if (includes || match) {
96
+ let host = ctx.request?.headers.get("host") ?? ctx.headers?.host ?? "";
97
+ if (!host && ctx.url) {
98
+ try {
99
+ host = new URL(ctx.url).host;
100
+ } catch {
101
+ host = "";
102
+ }
103
+ }
104
+ if (match) {
105
+ if (!isSafePattern(match)) return false;
106
+ try {
107
+ if (!new RegExp(match).test(host)) return false;
108
+ } catch {
109
+ return false;
110
+ }
111
+ }
112
+ return includes ? host.includes(includes) : true;
113
+ }
114
+
91
115
  const hostToMatch = rule.host as string | undefined;
92
116
  if (!hostToMatch) return false;
93
117
 
@@ -118,20 +142,13 @@ function isSafePattern(pattern: string): boolean {
118
142
  function pathnameMatcher(rule: Record<string, unknown>, ctx: MatcherContext): boolean {
119
143
  const path = ctx.path ?? "";
120
144
 
121
- // CMS "case" format: { type: "Includes" | "Equals" | "Not Includes" | "Starts With", pathname: "/..." }
122
- const caseObj = rule.case as { type?: string; pathname?: string } | undefined;
123
- if (caseObj?.pathname) {
124
- switch (caseObj.type) {
125
- case "Equals":
126
- return path === caseObj.pathname;
127
- case "Not Includes":
128
- return !path.includes(caseObj.pathname);
129
- case "Starts With":
130
- return path.startsWith(caseObj.pathname);
131
- case "Includes":
132
- default:
133
- return path.includes(caseObj.pathname);
134
- }
145
+ // CMS "case" format (deco-cx/apps): { type: "Equals" | "Includes" | "Template", pathname, negate? }
146
+ // plus "Not Includes" / "Starts With". `negate` inverts any of them.
147
+ const caseObj = rule.case as { type?: string; pathname?: string; negate?: boolean } | undefined;
148
+ if (caseObj) {
149
+ if (!caseObj.pathname) return false;
150
+ const result = pathnameCase(caseObj.type, path, caseObj.pathname);
151
+ return caseObj.negate ? !result : result;
135
152
  }
136
153
 
137
154
  // Standard format: pattern (regex), includes (exact/glob), excludes (exact/glob)
@@ -182,11 +199,73 @@ function pathnameMatcher(rule: Record<string, unknown>, ctx: MatcherContext): bo
182
199
  return true;
183
200
  }
184
201
 
202
+ function pathnameCase(type: string | undefined, path: string, pathname: string): boolean {
203
+ switch (type) {
204
+ case "Equals":
205
+ return path === pathname;
206
+ case "Not Includes":
207
+ return !path.includes(pathname);
208
+ case "Starts With":
209
+ return path.startsWith(pathname);
210
+ case "Template": {
211
+ // "/:slug/p" → each `:segment` matches one path segment, whole path.
212
+ const escaped = pathname
213
+ .split(/(:[^/]+)/)
214
+ .map((part) => (part.startsWith(":") ? "[^/]+" : part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")))
215
+ .join("");
216
+ return new RegExp(`^${escaped}$`).test(path);
217
+ }
218
+ default: // "Includes"
219
+ return path.includes(pathname);
220
+ }
221
+ }
222
+
185
223
  // -------------------------------------------------------------------------
186
224
  // Query string matcher
187
225
  // -------------------------------------------------------------------------
188
226
 
227
+ type QueryCase = { type?: string; value?: string };
228
+ const QUERY_OPERATIONS: Record<string, (param: string, value: string) => boolean> = {
229
+ Equals: (a, b) => a === b,
230
+ Greater: (a, b) => a > b,
231
+ GreaterOrEquals: (a, b) => a >= b,
232
+ Lesser: (a, b) => a < b,
233
+ LesserOrEquals: (a, b) => a <= b,
234
+ Includes: (a, b) => a.includes(b),
235
+ };
236
+
237
+ /**
238
+ * deco-cx/apps format: every condition must hold. A condition fails when its
239
+ * param is absent; `Exists` holds when it is present; the other operations
240
+ * hold when at least one of the param's values compares true (string compare,
241
+ * as upstream).
242
+ */
243
+ function queryStringConditions(
244
+ conditions: Array<{ param?: string; case?: QueryCase }>,
245
+ url: URL,
246
+ ): boolean {
247
+ return conditions.every((condition) => {
248
+ const values = condition.param ? url.searchParams.getAll(condition.param) : [];
249
+ if (values.length === 0) return false;
250
+ const type = condition.case?.type;
251
+ if (type === "Exists") return true;
252
+ const compare = type ? QUERY_OPERATIONS[type] : undefined;
253
+ const expected = condition.case?.value;
254
+ if (!compare || expected === undefined) return false;
255
+ return values.some((value) => compare(value, expected));
256
+ });
257
+ }
258
+
189
259
  function queryStringMatcher(rule: Record<string, unknown>, ctx: MatcherContext): boolean {
260
+ if (Array.isArray(rule.conditions)) {
261
+ if (!ctx.url) return false;
262
+ try {
263
+ return queryStringConditions(rule.conditions, new URL(ctx.url));
264
+ } catch {
265
+ return false;
266
+ }
267
+ }
268
+
190
269
  const key = (rule.key ?? rule.param) as string | undefined;
191
270
  const value = rule.value as string | undefined;
192
271