platform-types 1.10.781 → 1.10.782
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.
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.STOREFRONT_PREVIEW_PROTOCOL_ERRORS = exports.STOREFRONT_PREVIEW_READ_OPERATIONS = exports.STOREFRONT_DESIGN_AUDIENCES = exports.STOREFRONT_BENEFIT_ICONS = exports.STOREFRONT_RESERVED_SLUGS = exports.STOREFRONT_CONTACT_FIELDS = exports.STOREFRONT_PROTECTED_BUILTIN_DESTINATIONS = exports.STOREFRONT_BUILTIN_DESTINATIONS = exports.STOREFRONT_REPEATABLE_SECTION_TYPES = exports.STOREFRONT_SECTION_TYPES = exports.isStorefrontPageSlug = exports.STOREFRONT_PAGE_SLUG_RE = exports.STOREFRONT_DESIGN_LIMITS = exports.STOREFRONT_SOCIAL_HOSTS = void 0;
|
|
4
4
|
exports.createStorefrontDesignV1 = createStorefrontDesignV1;
|
|
5
5
|
exports.parseStorefrontPreviewDocument = parseStorefrontPreviewDocument;
|
|
6
|
+
exports.matchesStorefrontVisibility = matchesStorefrontVisibility;
|
|
7
|
+
exports.projectStorefrontDocumentForAudience = projectStorefrontDocumentForAudience;
|
|
6
8
|
exports.parseStorefrontPreviewMessage = parseStorefrontPreviewMessage;
|
|
7
9
|
exports.acceptStorefrontPreviewMessage = acceptStorefrontPreviewMessage;
|
|
8
10
|
/** The social kinds a contact may link, each to its own hosts; a contact links at most one per kind. */
|
|
@@ -253,7 +255,7 @@ function previewWithinBytes(value, maximum) {
|
|
|
253
255
|
/**
|
|
254
256
|
* A `visibility` rule kept on the transport only when well-formed: known audiences, and integer
|
|
255
257
|
* price lists within the store cap. Anything else is left off rather than failing the frame — the
|
|
256
|
-
*
|
|
258
|
+
* parser never filters by it, it only carries the rule the editor authored.
|
|
257
259
|
*/
|
|
258
260
|
function previewVisibility(input) {
|
|
259
261
|
if (!previewRecord(input) || !Array.isArray(input.audiences))
|
|
@@ -362,6 +364,102 @@ function previewAnnouncement(input) {
|
|
|
362
364
|
const { visibility, ...rest } = input;
|
|
363
365
|
return { ...rest, ...previewVisibility(visibility) };
|
|
364
366
|
}
|
|
367
|
+
function audienceNamed(audiences, audience) {
|
|
368
|
+
return audiences.some((entry) => entry === audience);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* The one audience rule: whether `caller` sees an item with this `visibility`. Absent means
|
|
372
|
+
* everyone. Otherwise any listed audience that matches, or the caller's list in `priceLists`:
|
|
373
|
+
* `guest` matches only a guest; `customer` any customer; `retail` / `wholesale` a customer of that
|
|
374
|
+
* class. A rule naming a list deleted since it was authored matches no one through it.
|
|
375
|
+
*/
|
|
376
|
+
function matchesStorefrontVisibility(visibility, caller) {
|
|
377
|
+
if (!visibility)
|
|
378
|
+
return true;
|
|
379
|
+
const audiences = Array.isArray(visibility.audiences) ? visibility.audiences : [];
|
|
380
|
+
if (caller.kind === 'guest')
|
|
381
|
+
return audienceNamed(audiences, 'guest');
|
|
382
|
+
if (audienceNamed(audiences, 'customer') || audienceNamed(audiences, caller.audienceClass))
|
|
383
|
+
return true;
|
|
384
|
+
return Array.isArray(visibility.priceLists) && visibility.priceLists.some((id) => id === caller.priceList);
|
|
385
|
+
}
|
|
386
|
+
const isAudienceNode = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
387
|
+
const audienceNonEmpty = (value) => Array.isArray(value) && value.length > 0;
|
|
388
|
+
function copyAudienceValue(value) {
|
|
389
|
+
if (Array.isArray(value))
|
|
390
|
+
return value.map(copyAudienceValue);
|
|
391
|
+
if (!isAudienceNode(value))
|
|
392
|
+
return value;
|
|
393
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, copyAudienceValue(entry)]));
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Filters `value` for `caller` into a NEW tree, or `undefined` when it goes. Every `visibility` key
|
|
397
|
+
* is dropped; an item kept because its rule matched gains `audienceScoped: true`. As a published
|
|
398
|
+
* reader drops a link whose target vanished, a link (or a section's action) to a page this caller
|
|
399
|
+
* does not see goes whole. A menu entry left with neither children nor a target, and a footer group
|
|
400
|
+
* left with no links, go too: they would render as dead labels.
|
|
401
|
+
*/
|
|
402
|
+
function scopeAudienceValue(value, caller, pages) {
|
|
403
|
+
if (Array.isArray(value))
|
|
404
|
+
return scopeAudienceArray(value, caller, pages);
|
|
405
|
+
if (!isAudienceNode(value))
|
|
406
|
+
return value;
|
|
407
|
+
if (value.kind === 'page')
|
|
408
|
+
return pages.has(String(value.id)) ? copyAudienceValue(value) : undefined;
|
|
409
|
+
const visibility = value.visibility;
|
|
410
|
+
if (!matchesStorefrontVisibility(visibility, caller))
|
|
411
|
+
return undefined;
|
|
412
|
+
const result = Object.fromEntries(scopedAudienceEntries(value, visibility, caller, pages));
|
|
413
|
+
return keepsAfterAudienceFilter(value, result) ? result : undefined;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* A kept node's entries, filtered. Built as entries for `Object.fromEntries`, never by assignment:
|
|
417
|
+
* an own `__proto__` key stays a data key and never becomes the result's prototype. A rule-matched
|
|
418
|
+
* item's flag keeps its place, or comes last.
|
|
419
|
+
*/
|
|
420
|
+
function scopedAudienceEntries(value, visibility, caller, pages) {
|
|
421
|
+
const entries = [];
|
|
422
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
423
|
+
if (key === 'visibility')
|
|
424
|
+
continue;
|
|
425
|
+
const scoped = key === 'audienceScoped' && visibility ? true : scopeAudienceValue(entry, caller, pages);
|
|
426
|
+
if (scoped !== undefined)
|
|
427
|
+
entries.push([key, scoped]);
|
|
428
|
+
}
|
|
429
|
+
if (visibility && !Object.hasOwn(value, 'audienceScoped'))
|
|
430
|
+
entries.push(['audienceScoped', true]);
|
|
431
|
+
return entries;
|
|
432
|
+
}
|
|
433
|
+
function scopeAudienceArray(items, caller, pages) {
|
|
434
|
+
return items.flatMap((item) => {
|
|
435
|
+
const scoped = scopeAudienceValue(item, caller, pages);
|
|
436
|
+
return scoped === undefined ? [] : [scoped];
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
/** Whether a node survives once its own parts are filtered, judged against what it had before. */
|
|
440
|
+
function keepsAfterAudienceFilter(had, result) {
|
|
441
|
+
if (had.target !== undefined && result.target === undefined)
|
|
442
|
+
return false;
|
|
443
|
+
if (audienceNonEmpty(had.children) && !audienceNonEmpty(result.children) && result.target === undefined)
|
|
444
|
+
return false;
|
|
445
|
+
return !(audienceNonEmpty(had.links) && !audienceNonEmpty(result.links));
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* A storefront design document as `caller` may see it, built as a NEW tree (the input is never
|
|
449
|
+
* changed or shared): drops every section, page, navigation entry (top level or child), footer link
|
|
450
|
+
* and announcement bar `caller` does not match, and every link to a page it does not see; strips
|
|
451
|
+
* every `visibility`; marks an item kept through a rule `audienceScoped: true`. Web `GET /store`
|
|
452
|
+
* applies it to a publication, the preview to its draft for a grant fixture's caller, and a preview
|
|
453
|
+
* frame may apply it to a posted document with the caller its design read echoed.
|
|
454
|
+
*/
|
|
455
|
+
function projectStorefrontDocumentForAudience(document, caller) {
|
|
456
|
+
const root = document;
|
|
457
|
+
const pages = new Set((Array.isArray(root.pages) ? root.pages : [])
|
|
458
|
+
.filter(isAudienceNode)
|
|
459
|
+
.filter((page) => matchesStorefrontVisibility(page.visibility, caller))
|
|
460
|
+
.map((page) => String(page.id)));
|
|
461
|
+
return (scopeAudienceValue(root, caller, pages) ?? {});
|
|
462
|
+
}
|
|
365
463
|
/** The codes a preview frame may report in an `error` message; the parser accepts exactly these. */
|
|
366
464
|
exports.STOREFRONT_PREVIEW_PROTOCOL_ERRORS = [
|
|
367
465
|
'STOREFRONT_PREVIEW_EXPIRED',
|
|
@@ -268,12 +268,25 @@ declare global {
|
|
|
268
268
|
* so the reference and identity walkers pass over it.
|
|
269
269
|
*
|
|
270
270
|
* Web `GET /store` filters by it and strips it, marking each item visible through a rule
|
|
271
|
-
* `audienceScoped: true`. The private preview
|
|
271
|
+
* `audienceScoped: true`. The private preview carries it as authored, unless its grant names a
|
|
272
|
+
* fixture: then it is filtered exactly as web `GET /store` filters it for that fixture's caller.
|
|
272
273
|
*/
|
|
273
274
|
interface StorefrontDesignVisibility {
|
|
274
275
|
audiences: StorefrontDesignAudience[];
|
|
275
276
|
priceLists?: number[];
|
|
276
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Who reads a storefront design, as {@link matchesStorefrontVisibility} judges a `visibility`
|
|
280
|
+
* rule: no approved customer (a guest, a pending or refused registrant included), or one approved
|
|
281
|
+
* customer with its effective list and that list's `audienceClass` (absent meaning `retail`).
|
|
282
|
+
*/
|
|
283
|
+
type StorefrontAudienceCaller = {
|
|
284
|
+
kind: 'guest';
|
|
285
|
+
} | {
|
|
286
|
+
kind: 'customer';
|
|
287
|
+
priceList: number;
|
|
288
|
+
audienceClass: 'retail' | 'wholesale';
|
|
289
|
+
};
|
|
277
290
|
/**
|
|
278
291
|
* The one announcement bar a design carries above every page, never a second. `text` and
|
|
279
292
|
* `subtitle` are plain text (no control characters; renderers escape markup); `text` is required
|
|
@@ -288,7 +301,7 @@ declare global {
|
|
|
288
301
|
*
|
|
289
302
|
* Every projection drops a disabled bar, as it drops a disabled section. Web `GET /store` also
|
|
290
303
|
* drops one whose `visibility` the caller does not match; the private preview carries the rule
|
|
291
|
-
* as authored.
|
|
304
|
+
* as authored, unless its grant names a fixture, when it drops the bar as web `GET /store` would.
|
|
292
305
|
*/
|
|
293
306
|
interface StorefrontDesignAnnouncement {
|
|
294
307
|
id: string;
|
|
@@ -414,7 +427,8 @@ declare global {
|
|
|
414
427
|
* An item that can carry `visibility` (section, page, navigation entry, footer link, the
|
|
415
428
|
* announcement bar) may also
|
|
416
429
|
* carry `audienceScoped: true`: web `GET /store` sets it on an item it returned because the
|
|
417
|
-
* caller matched that item's rule, and strips every `visibility`. The preview
|
|
430
|
+
* caller matched that item's rule, and strips every `visibility`. The preview sets it only when
|
|
431
|
+
* its grant names a fixture, the same way.
|
|
418
432
|
*/
|
|
419
433
|
type PublicStorefrontDesignValue<T> = T extends StorefrontDesignImagePlacement ? T & {
|
|
420
434
|
url: string;
|
|
@@ -528,7 +542,27 @@ declare global {
|
|
|
528
542
|
type StorefrontDesignDiscardInput = StorefrontDesignRevisionInput;
|
|
529
543
|
interface StorefrontDesignPreviewInput {
|
|
530
544
|
draftRevision: string;
|
|
545
|
+
/** Absent: the unfiltered preview at the default list. */
|
|
546
|
+
fixture?: StorefrontPreviewFixture;
|
|
531
547
|
}
|
|
548
|
+
/**
|
|
549
|
+
* The audience a preview grant renders for — synthetic, never a real customer: no customer row,
|
|
550
|
+
* cart, order or balance is read. `guest` is a visitor without an account. `priceList` is an
|
|
551
|
+
* approved customer on that list of the store (its current `audienceClass` decides `retail` /
|
|
552
|
+
* `wholesale` rules), and every catalogue read prices at it; a list the store does not have is
|
|
553
|
+
* refused on issue, 400 `STOREFRONT_DESIGN_INVALID` with `UNKNOWN_PRICE_LIST` at
|
|
554
|
+
* `fixture.priceList`, and a list deleted after issue reads as a customer on it would (PUBLICO,
|
|
555
|
+
* list 1, with its class). `pending` is a registrant awaiting approval: the api answers it exactly as `guest`
|
|
556
|
+
* (content, methods and prices); the storefront may still show its in-review notices.
|
|
557
|
+
*/
|
|
558
|
+
type StorefrontPreviewFixture = {
|
|
559
|
+
kind: 'guest';
|
|
560
|
+
} | {
|
|
561
|
+
kind: 'priceList';
|
|
562
|
+
priceList: number;
|
|
563
|
+
} | {
|
|
564
|
+
kind: 'pending';
|
|
565
|
+
};
|
|
532
566
|
/**
|
|
533
567
|
* GET draft answers a baseline built without copying the legacy logo, while POST init copies it,
|
|
534
568
|
* so `document.branding.logo` can differ between them: clients rebase onto init's document.
|
|
@@ -649,11 +683,20 @@ declare global {
|
|
|
649
683
|
grantId: string;
|
|
650
684
|
iat: number;
|
|
651
685
|
exp: number;
|
|
686
|
+
/** The issue body's fixture, signed in, so only the ADMIN-gated issue route can change it. */
|
|
687
|
+
fixture?: StorefrontPreviewFixture;
|
|
652
688
|
}
|
|
653
689
|
type StorefrontPreviewOperation = (typeof STOREFRONT_PREVIEW_READ_OPERATIONS)[number];
|
|
654
690
|
/** GET query fields use their serialized string representation. Store scope comes only from the grant. */
|
|
655
691
|
type StorefrontPreviewReadQuery = {
|
|
656
|
-
operation: 'design'
|
|
692
|
+
operation: 'design';
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* The public method rows. With a grant fixture, only the rows that caller may use, as web
|
|
696
|
+
* `GET /store` and checkout judge a row's `availability`; without one, every row.
|
|
697
|
+
*/
|
|
698
|
+
| {
|
|
699
|
+
operation: 'paymentMethods' | 'deliveryMethods';
|
|
657
700
|
}
|
|
658
701
|
/**
|
|
659
702
|
* The shop's public web `GET /store` frame — the same allow-listed STORE
|
|
@@ -679,7 +722,8 @@ declare global {
|
|
|
679
722
|
}
|
|
680
723
|
/**
|
|
681
724
|
* `bestSellers` is web `GET /products?mode=best-sellers`'s read and answer, priced as a
|
|
682
|
-
* guest
|
|
725
|
+
* guest, or at a `priceList` fixture's list as every catalogue operation is.
|
|
726
|
+
* `recentlyBought` answers `{ data: [] }`: a preview has no buyer to read for.
|
|
683
727
|
*/
|
|
684
728
|
| {
|
|
685
729
|
operation: 'bestSellers' | 'recentlyBought';
|
|
@@ -769,6 +813,11 @@ declare global {
|
|
|
769
813
|
interface StorefrontPreviewDesignResult {
|
|
770
814
|
mode: 'preview';
|
|
771
815
|
draftRevision: string;
|
|
816
|
+
/**
|
|
817
|
+
* Without a grant fixture, the draft with every `visibility` as authored. With one, the
|
|
818
|
+
* draft filtered for {@link StorefrontPreviewDesignResult.audience} by
|
|
819
|
+
* {@link projectStorefrontDocumentForAudience}, as web `GET /store` filters a publication.
|
|
820
|
+
*/
|
|
772
821
|
document: PublicStorefrontDesign;
|
|
773
822
|
literalLayers: {
|
|
774
823
|
scope: LiteralScope;
|
|
@@ -776,12 +825,19 @@ declare global {
|
|
|
776
825
|
}[];
|
|
777
826
|
effectiveSettings: StorefrontDesignSettings;
|
|
778
827
|
defaultsVersion: string;
|
|
828
|
+
/**
|
|
829
|
+
* The caller the grant's fixture resolved to, present only when the grant names one: filter a
|
|
830
|
+
* posted `StorefrontPreviewDocument` with this same caller and the same walker.
|
|
831
|
+
*/
|
|
832
|
+
audience?: StorefrontAudienceCaller;
|
|
779
833
|
}
|
|
780
834
|
/**
|
|
781
835
|
* `operation=store`'s `data`: the fields web `GET /store` builds from the STORE
|
|
782
836
|
* row, built by the same projector, and `storefrontDesign` from the pinned
|
|
783
837
|
* draft. The contact is the draft's (`publicContact`, plus `email`/`phone`
|
|
784
|
-
* derived from it), as an adopted shop's comes from its publication.
|
|
838
|
+
* derived from it), as an adopted shop's comes from its publication. With a grant
|
|
839
|
+
* fixture, its method rows are only those that caller may use and its design is
|
|
840
|
+
* filtered for that caller, as web `GET /store` answers that caller.
|
|
785
841
|
*
|
|
786
842
|
* Never carries what only an open shop's public route knows — `publicStorefront`,
|
|
787
843
|
* `maintenance` and `globals` — and never answers `STOREFRONT_UNAVAILABLE`: a
|
|
@@ -796,7 +852,11 @@ declare global {
|
|
|
796
852
|
type: StorefrontDesignSectionType;
|
|
797
853
|
enabled: boolean;
|
|
798
854
|
content: Record<string, unknown>;
|
|
799
|
-
/**
|
|
855
|
+
/**
|
|
856
|
+
* Carried as authored when well-formed, as navigation and footer entries carry theirs; never
|
|
857
|
+
* applied by the parser. A frame whose design read echoed an `audience` should apply it
|
|
858
|
+
* with {@link projectStorefrontDocumentForAudience}.
|
|
859
|
+
*/
|
|
800
860
|
visibility?: StorefrontDesignVisibility;
|
|
801
861
|
}
|
|
802
862
|
interface StorefrontPreviewDocument {
|
|
@@ -824,7 +884,7 @@ declare global {
|
|
|
824
884
|
contact: Record<string, unknown>;
|
|
825
885
|
seo: object;
|
|
826
886
|
literalOverrides: Record<string, unknown>;
|
|
827
|
-
/** Carried as sent when it is an object, its `visibility` kept only when well-formed; never applied. */
|
|
887
|
+
/** Carried as sent when it is an object, its `visibility` kept only when well-formed; never applied by the parser. */
|
|
828
888
|
announcement?: object;
|
|
829
889
|
}
|
|
830
890
|
type StorefrontPreviewProtocolError = (typeof STOREFRONT_PREVIEW_PROTOCOL_ERRORS)[number];
|
|
@@ -931,6 +991,22 @@ declare global {
|
|
|
931
991
|
export declare const STOREFRONT_PREVIEW_READ_OPERATIONS: readonly ["design", "store", "product", "productBySlug", "productsByIds", "bestSellers", "recentlyBought", "products", "incomes", "group", "related", "categories", "brands", "tags", "collection", "paymentMethods", "deliveryMethods", "asset"];
|
|
932
992
|
/** Checks transport structure and UTF-8 size; unfamiliar section types are skipped, never rendered. */
|
|
933
993
|
export declare function parseStorefrontPreviewDocument(input: unknown): StorefrontPreviewDocument | undefined;
|
|
994
|
+
/**
|
|
995
|
+
* The one audience rule: whether `caller` sees an item with this `visibility`. Absent means
|
|
996
|
+
* everyone. Otherwise any listed audience that matches, or the caller's list in `priceLists`:
|
|
997
|
+
* `guest` matches only a guest; `customer` any customer; `retail` / `wholesale` a customer of that
|
|
998
|
+
* class. A rule naming a list deleted since it was authored matches no one through it.
|
|
999
|
+
*/
|
|
1000
|
+
export declare function matchesStorefrontVisibility(visibility: StorefrontDesignVisibility | undefined, caller: StorefrontAudienceCaller): boolean;
|
|
1001
|
+
/**
|
|
1002
|
+
* A storefront design document as `caller` may see it, built as a NEW tree (the input is never
|
|
1003
|
+
* changed or shared): drops every section, page, navigation entry (top level or child), footer link
|
|
1004
|
+
* and announcement bar `caller` does not match, and every link to a page it does not see; strips
|
|
1005
|
+
* every `visibility`; marks an item kept through a rule `audienceScoped: true`. Web `GET /store`
|
|
1006
|
+
* applies it to a publication, the preview to its draft for a grant fixture's caller, and a preview
|
|
1007
|
+
* frame may apply it to a posted document with the caller its design read echoed.
|
|
1008
|
+
*/
|
|
1009
|
+
export declare function projectStorefrontDocumentForAudience<T extends object>(document: T, caller: StorefrontAudienceCaller): T;
|
|
934
1010
|
/** The codes a preview frame may report in an `error` message; the parser accepts exactly these. */
|
|
935
1011
|
export declare const STOREFRONT_PREVIEW_PROTOCOL_ERRORS: readonly ["STOREFRONT_PREVIEW_EXPIRED", "STOREFRONT_PREVIEW_REVOKED", "STOREFRONT_PREVIEW_FORBIDDEN", "STOREFRONT_PREVIEW_UNAVAILABLE", "STOREFRONT_DESIGN_INVALID", "STOREFRONT_SCHEMA_UNSUPPORTED"];
|
|
936
1012
|
/** Does not authenticate the grant. Keep it in memory and post only to the exact configured target origin. */
|
package/dist/storefrontDesign.js
CHANGED
|
@@ -245,7 +245,7 @@ function previewWithinBytes(value, maximum) {
|
|
|
245
245
|
/**
|
|
246
246
|
* A `visibility` rule kept on the transport only when well-formed: known audiences, and integer
|
|
247
247
|
* price lists within the store cap. Anything else is left off rather than failing the frame — the
|
|
248
|
-
*
|
|
248
|
+
* parser never filters by it, it only carries the rule the editor authored.
|
|
249
249
|
*/
|
|
250
250
|
function previewVisibility(input) {
|
|
251
251
|
if (!previewRecord(input) || !Array.isArray(input.audiences))
|
|
@@ -354,6 +354,102 @@ function previewAnnouncement(input) {
|
|
|
354
354
|
const { visibility, ...rest } = input;
|
|
355
355
|
return { ...rest, ...previewVisibility(visibility) };
|
|
356
356
|
}
|
|
357
|
+
function audienceNamed(audiences, audience) {
|
|
358
|
+
return audiences.some((entry) => entry === audience);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* The one audience rule: whether `caller` sees an item with this `visibility`. Absent means
|
|
362
|
+
* everyone. Otherwise any listed audience that matches, or the caller's list in `priceLists`:
|
|
363
|
+
* `guest` matches only a guest; `customer` any customer; `retail` / `wholesale` a customer of that
|
|
364
|
+
* class. A rule naming a list deleted since it was authored matches no one through it.
|
|
365
|
+
*/
|
|
366
|
+
export function matchesStorefrontVisibility(visibility, caller) {
|
|
367
|
+
if (!visibility)
|
|
368
|
+
return true;
|
|
369
|
+
const audiences = Array.isArray(visibility.audiences) ? visibility.audiences : [];
|
|
370
|
+
if (caller.kind === 'guest')
|
|
371
|
+
return audienceNamed(audiences, 'guest');
|
|
372
|
+
if (audienceNamed(audiences, 'customer') || audienceNamed(audiences, caller.audienceClass))
|
|
373
|
+
return true;
|
|
374
|
+
return Array.isArray(visibility.priceLists) && visibility.priceLists.some((id) => id === caller.priceList);
|
|
375
|
+
}
|
|
376
|
+
const isAudienceNode = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
377
|
+
const audienceNonEmpty = (value) => Array.isArray(value) && value.length > 0;
|
|
378
|
+
function copyAudienceValue(value) {
|
|
379
|
+
if (Array.isArray(value))
|
|
380
|
+
return value.map(copyAudienceValue);
|
|
381
|
+
if (!isAudienceNode(value))
|
|
382
|
+
return value;
|
|
383
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, copyAudienceValue(entry)]));
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Filters `value` for `caller` into a NEW tree, or `undefined` when it goes. Every `visibility` key
|
|
387
|
+
* is dropped; an item kept because its rule matched gains `audienceScoped: true`. As a published
|
|
388
|
+
* reader drops a link whose target vanished, a link (or a section's action) to a page this caller
|
|
389
|
+
* does not see goes whole. A menu entry left with neither children nor a target, and a footer group
|
|
390
|
+
* left with no links, go too: they would render as dead labels.
|
|
391
|
+
*/
|
|
392
|
+
function scopeAudienceValue(value, caller, pages) {
|
|
393
|
+
if (Array.isArray(value))
|
|
394
|
+
return scopeAudienceArray(value, caller, pages);
|
|
395
|
+
if (!isAudienceNode(value))
|
|
396
|
+
return value;
|
|
397
|
+
if (value.kind === 'page')
|
|
398
|
+
return pages.has(String(value.id)) ? copyAudienceValue(value) : undefined;
|
|
399
|
+
const visibility = value.visibility;
|
|
400
|
+
if (!matchesStorefrontVisibility(visibility, caller))
|
|
401
|
+
return undefined;
|
|
402
|
+
const result = Object.fromEntries(scopedAudienceEntries(value, visibility, caller, pages));
|
|
403
|
+
return keepsAfterAudienceFilter(value, result) ? result : undefined;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* A kept node's entries, filtered. Built as entries for `Object.fromEntries`, never by assignment:
|
|
407
|
+
* an own `__proto__` key stays a data key and never becomes the result's prototype. A rule-matched
|
|
408
|
+
* item's flag keeps its place, or comes last.
|
|
409
|
+
*/
|
|
410
|
+
function scopedAudienceEntries(value, visibility, caller, pages) {
|
|
411
|
+
const entries = [];
|
|
412
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
413
|
+
if (key === 'visibility')
|
|
414
|
+
continue;
|
|
415
|
+
const scoped = key === 'audienceScoped' && visibility ? true : scopeAudienceValue(entry, caller, pages);
|
|
416
|
+
if (scoped !== undefined)
|
|
417
|
+
entries.push([key, scoped]);
|
|
418
|
+
}
|
|
419
|
+
if (visibility && !Object.hasOwn(value, 'audienceScoped'))
|
|
420
|
+
entries.push(['audienceScoped', true]);
|
|
421
|
+
return entries;
|
|
422
|
+
}
|
|
423
|
+
function scopeAudienceArray(items, caller, pages) {
|
|
424
|
+
return items.flatMap((item) => {
|
|
425
|
+
const scoped = scopeAudienceValue(item, caller, pages);
|
|
426
|
+
return scoped === undefined ? [] : [scoped];
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/** Whether a node survives once its own parts are filtered, judged against what it had before. */
|
|
430
|
+
function keepsAfterAudienceFilter(had, result) {
|
|
431
|
+
if (had.target !== undefined && result.target === undefined)
|
|
432
|
+
return false;
|
|
433
|
+
if (audienceNonEmpty(had.children) && !audienceNonEmpty(result.children) && result.target === undefined)
|
|
434
|
+
return false;
|
|
435
|
+
return !(audienceNonEmpty(had.links) && !audienceNonEmpty(result.links));
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* A storefront design document as `caller` may see it, built as a NEW tree (the input is never
|
|
439
|
+
* changed or shared): drops every section, page, navigation entry (top level or child), footer link
|
|
440
|
+
* and announcement bar `caller` does not match, and every link to a page it does not see; strips
|
|
441
|
+
* every `visibility`; marks an item kept through a rule `audienceScoped: true`. Web `GET /store`
|
|
442
|
+
* applies it to a publication, the preview to its draft for a grant fixture's caller, and a preview
|
|
443
|
+
* frame may apply it to a posted document with the caller its design read echoed.
|
|
444
|
+
*/
|
|
445
|
+
export function projectStorefrontDocumentForAudience(document, caller) {
|
|
446
|
+
const root = document;
|
|
447
|
+
const pages = new Set((Array.isArray(root.pages) ? root.pages : [])
|
|
448
|
+
.filter(isAudienceNode)
|
|
449
|
+
.filter((page) => matchesStorefrontVisibility(page.visibility, caller))
|
|
450
|
+
.map((page) => String(page.id)));
|
|
451
|
+
return (scopeAudienceValue(root, caller, pages) ?? {});
|
|
452
|
+
}
|
|
357
453
|
/** The codes a preview frame may report in an `error` message; the parser accepts exactly these. */
|
|
358
454
|
export const STOREFRONT_PREVIEW_PROTOCOL_ERRORS = [
|
|
359
455
|
'STOREFRONT_PREVIEW_EXPIRED',
|