@decocms/blocks 8.2.2 → 8.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.
Files changed (42) hide show
  1. package/dist/v8/builtins/lazy.d.ts +1 -1
  2. package/dist/v8/builtins/lazy.d.ts.map +1 -1
  3. package/dist/v8/builtins/lazy.js +1 -1
  4. package/dist/v8/builtins/lazy.js.map +1 -1
  5. package/dist/v8/builtins/multivariate.d.ts +12 -1
  6. package/dist/v8/builtins/multivariate.d.ts.map +1 -1
  7. package/dist/v8/builtins/multivariate.js +14 -2
  8. package/dist/v8/builtins/multivariate.js.map +1 -1
  9. package/dist/v8/cli/run.d.ts.map +1 -1
  10. package/dist/v8/cli/run.js +7 -3
  11. package/dist/v8/cli/run.js.map +1 -1
  12. package/dist/v8/cli/schema/index.d.ts +15 -1
  13. package/dist/v8/cli/schema/index.d.ts.map +1 -1
  14. package/dist/v8/cli/schema/index.js +96 -2
  15. package/dist/v8/cli/schema/index.js.map +1 -1
  16. package/dist/v8/cms.js +9 -1
  17. package/dist/v8/cms.js.map +1 -1
  18. package/dist/v8/draft.d.ts +3 -1
  19. package/dist/v8/draft.d.ts.map +1 -1
  20. package/dist/v8/draft.js +5 -3
  21. package/dist/v8/draft.js.map +1 -1
  22. package/dist/v8/image.d.ts +140 -0
  23. package/dist/v8/image.d.ts.map +1 -0
  24. package/dist/v8/image.js +226 -0
  25. package/dist/v8/image.js.map +1 -0
  26. package/dist/v8/remoteLoader.js +3 -3
  27. package/dist/v8/resolver.d.ts.map +1 -1
  28. package/dist/v8/resolver.js +36 -3
  29. package/dist/v8/resolver.js.map +1 -1
  30. package/dist/v8/types.d.ts +7 -5
  31. package/dist/v8/types.d.ts.map +1 -1
  32. package/package.json +6 -1
  33. package/src/v8/builtins/lazy.ts +1 -1
  34. package/src/v8/builtins/multivariate.ts +28 -11
  35. package/src/v8/cli/run.ts +7 -3
  36. package/src/v8/cli/schema/index.ts +114 -3
  37. package/src/v8/cms.ts +12 -1
  38. package/src/v8/draft.ts +5 -3
  39. package/src/v8/image.ts +325 -0
  40. package/src/v8/remoteLoader.ts +3 -3
  41. package/src/v8/resolver.ts +40 -3
  42. package/src/v8/types.ts +7 -5
@@ -7,8 +7,9 @@
7
7
  * in ./builtins/legacy) is a second key for a type, so like a function it wins
8
8
  * over a saved block of the same name; a saved block is replaced by
9
9
  * `{ ...saved, ...arguments }` and looked up again; anything else is
10
- * `UNKNOWN_BLOCK`. The built-in `lazy` is
11
- * the one special case: its `value` is not resolved first.
10
+ * `UNKNOWN_BLOCK`. Two built-ins are special cases: `lazy`, whose `value` is
11
+ * not resolved first, and `multivariate`, whose variants' rules resolve in
12
+ * order and stop at the first `true` one (later rules never run, as in v7).
12
13
  *
13
14
  * A `Resolver` belongs to one client, so its memo (results per block function
14
15
  * and canonical inputs) never outlives a request. Within the request the memo
@@ -19,6 +20,7 @@
19
20
  import { RESERVED_NAMES } from "./builtins/index.ts";
20
21
  import { isLazyBuiltin } from "./builtins/lazy.ts";
21
22
  import { LEGACY_ALIASES, wrapLegacyVariants } from "./builtins/legacy.ts";
23
+ import { isMultivariateBuiltin } from "./builtins/multivariate.ts";
22
24
  import { errors, isResolutionError } from "./errors.ts";
23
25
  import { canonicalKey, isPlainObject, type JsonObject, own } from "./json.ts";
24
26
  import type { BlockFunction, Blocks, Lazy, Snapshot } from "./types.ts";
@@ -240,7 +242,10 @@ export class Resolver {
240
242
  ): Promise<unknown> {
241
243
  // Inputs are always fresh objects: a function may mutate its props freely.
242
244
  const { __resolveType: _type, ...inputs } = node;
243
- const resolved = await this.#resolveFields(inputs, path, chain, true);
245
+ const resolved =
246
+ isMultivariateBuiltin(fn) && Array.isArray(inputs.variants)
247
+ ? await this.#resolveFirstMatch(inputs, inputs.variants, path, chain)
248
+ : await this.#resolveFields(inputs, path, chain, true);
244
249
  const start = Date.now();
245
250
  try {
246
251
  const value = await fn(resolved);
@@ -254,6 +259,38 @@ export class Resolver {
254
259
  }
255
260
  }
256
261
 
262
+ /**
263
+ * The built-in `multivariate`'s inputs: its other fields as usual, then the
264
+ * variants one at a time, each rule before the rest of its variant, up to
265
+ * the first rule that is `true`. Later variants are left out, so their
266
+ * matchers never run.
267
+ */
268
+ async #resolveFirstMatch(
269
+ inputs: JsonObject,
270
+ variants: unknown[],
271
+ path: Path,
272
+ chain: string[],
273
+ ): Promise<JsonObject> {
274
+ const { variants: _variants, ...others } = inputs;
275
+ const resolved = await this.#resolveFields(others, path, chain, true);
276
+ const kept: unknown[] = [];
277
+ for (const [i, variant] of variants.entries()) {
278
+ const at = [...path, "variants", i];
279
+ if (!isPlainObject(variant) || typeof variant.__resolveType === "string") {
280
+ const value = await this.resolve(variant, at, chain, true);
281
+ if (value === undefined) continue;
282
+ kept.push(value);
283
+ if (isPlainObject(value) && value.rule === true) break;
284
+ continue;
285
+ }
286
+ const { rule, ...rest } = variant;
287
+ const matched = await this.resolve(rule, [...at, "rule"], chain, true);
288
+ kept.push({ rule: matched, ...(await this.#resolveFields(rest, at, chain, true)) });
289
+ if (matched === true) break;
290
+ }
291
+ return { ...resolved, variants: kept };
292
+ }
293
+
257
294
  #lazy(value: unknown, path: Path, chain: string[], fromContent: boolean): Lazy<unknown> {
258
295
  let pending: Promise<unknown> | undefined;
259
296
  return () => {
package/src/v8/types.ts CHANGED
@@ -256,9 +256,10 @@ export interface CMSConfig {
256
256
  /** ms between `update()` checks of a content source that has one; minimum 60 000. */
257
257
  interval?: number;
258
258
  /**
259
- * Local development (`import.meta.env.DEV`, `process.env.NODE_ENV === "development"`):
260
- * hosted releases are never swapped in, so local files win. Drafts still load.
261
- * The SDK reads no environment variable for it; default `false`.
259
+ * Local development: hosted releases are never swapped in, so local files win.
260
+ * Drafts still load. Default: `process.env.NODE_ENV === "development"` (read
261
+ * guarded, so `false` where there is no `process`: Workers, browsers). Pass it
262
+ * to override that, e.g. `dev: import.meta.env.DEV` or `dev: false`.
262
263
  */
263
264
  dev?: boolean;
264
265
  /** Where telemetry goes; see /next/telemetry. */
@@ -342,8 +343,9 @@ export interface CMS {
342
343
  settings(): Promise<EffectiveSettings>;
343
344
  /**
344
345
  * The request's draft pointer: `?__draft=` first, then the `__deco_draft`
345
- * cookie. `null` when neither is present, for `?__draft=off`, and on a host
346
- * outside `settings().preview.hosts`, where the request gets the release.
346
+ * cookie. `null` when neither is present, for `?__draft=off`, on a host
347
+ * outside `settings().preview.hosts`, and for a value that doesn't parse
348
+ * (ignored, as if absent): in each case the request gets the release.
347
349
  */
348
350
  draftPointer(request: RequestLike): Promise<string | null>;
349
351
  /**