Skip to content

Commit

Permalink
chore(prebinding): add contentType to patternPropertySchema (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
chasepoirier authored Feb 20, 2025
1 parent 7b55c28 commit 2f40399
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 57 deletions.
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type {
DesignValue,
UnboundValue,
BoundValue,
NoValue,
ComponentValue,
ComponentDefinitionPropertyType as ComponentDefinitionVariableType,
PatternProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ export const deserializeAssemblyNode = ({
componentInstanceVariables,
componentSettings,
patternProperties,
entityStore,
}: {
node: ComponentTreeNode;
componentInstanceVariables: ComponentTreeNode['variables'];
componentSettings: ExperienceComponentSettings;
patternProperties: Record<string, PatternProperty>;
entityStore: EntityStore;
}): ComponentTreeNode => {
const variables: Record<string, ComponentPropertyValue> = {};

Expand All @@ -44,7 +42,6 @@ export const deserializeAssemblyNode = ({
const path = resolvePrebindingPath({
componentSettings,
componentValueKey,
entityStore,
patternProperties,
});

Expand Down Expand Up @@ -95,7 +92,6 @@ export const deserializeAssemblyNode = ({
componentInstanceVariables,
componentSettings,
patternProperties,
entityStore,
}),
);

Expand Down Expand Up @@ -146,7 +142,6 @@ export const resolveAssembly = ({
componentInstanceVariables: node.variables,
componentSettings: componentFields.componentSettings!,
patternProperties: node.patternProperties || {},
entityStore,
});

entityStore.addAssemblyUnboundValues(componentFields.unboundValues);
Expand Down
49 changes: 11 additions & 38 deletions packages/experience-builder-sdk/src/utils/prebindingUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { shouldUsePrebinding, resolvePrebindingPath } from './prebindingUtils';
import { EntityStore } from '@contentful/experiences-core';

import {
ComponentPropertyValue,
ExperienceComponentSettings,
Expand Down Expand Up @@ -99,7 +99,11 @@ describe('shouldUsePrebinding', () => {
variableMappings: {},
} as unknown as ExperienceComponentSettings;
const patternProperties: Record<string, PatternProperty> = {
testPatternPropertyDefinitionId: { path: '/entries/testEntry', type: 'BoundValue' },
testPatternPropertyDefinitionId: {
path: '/entries/testEntry',
type: 'BoundValue',
contenType: 'testContentType',
},
};
const variable = {
type: 'NoValue',
Expand Down Expand Up @@ -133,22 +137,17 @@ describe('resolvePrebindingPath', () => {
},
} as unknown as ExperienceComponentSettings;
const patternProperties: Record<string, PatternProperty> = {
testPatternPropertyDefinitionId: { path: '/entries/testEntry', type: 'BoundValue' },
};
const entityStore = {
dataSource: {
testEntry: { sys: { type: 'Entry', contentType: { sys: { id: 'testContentType' } } } },
testPatternPropertyDefinitionId: {
path: '/entries/testEntry',
type: 'BoundValue',
contenType: 'testContentType',
},
getEntryOrAsset: jest.fn().mockReturnValue({
sys: { type: 'Entry', contentType: { sys: { id: 'testContentType' } } },
}),
} as unknown as EntityStore;
};

const result = resolvePrebindingPath({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
});

expect(result).toBe('/entries/testEntry/fields/testField');
Expand All @@ -160,16 +159,11 @@ describe('resolvePrebindingPath', () => {
variableMappings: {},
} as unknown as ExperienceComponentSettings;
const patternProperties: Record<string, PatternProperty> = {};
const entityStore: EntityStore = {
dataSource: {},
getEntryOrAsset: jest.fn(),
} as unknown as EntityStore;

const result = resolvePrebindingPath({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
});

expect(result).toBe('');
Expand All @@ -185,16 +179,11 @@ describe('resolvePrebindingPath', () => {
},
} as unknown as ExperienceComponentSettings;
const patternProperties: Record<string, PatternProperty> = {};
const entityStore: EntityStore = {
dataSource: {},
getEntryOrAsset: jest.fn(),
} as unknown as EntityStore;

const result = resolvePrebindingPath({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
});

expect(result).toBe('');
Expand All @@ -212,18 +201,11 @@ describe('resolvePrebindingPath', () => {
const patternProperties: Record<string, PatternProperty> = {
testPatternPropertyDefinitionId: { path: '/entries/testEntry' },
} as unknown as Record<string, PatternProperty>;
const entityStore: EntityStore = {
dataSource: {
testEntry: { sys: { type: 'Asset' } },
},
getEntryOrAsset: jest.fn().mockReturnValue({ sys: { type: 'Asset' } }),
} as unknown as EntityStore;

const result = resolvePrebindingPath({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
});

expect(result).toBe('');
Expand All @@ -242,20 +224,11 @@ describe('resolvePrebindingPath', () => {
const patternProperties: Record<string, PatternProperty> = {
testPatternPropertyDefinitionId: { path: '/entries/testEntry' },
} as unknown as Record<string, PatternProperty>;
const entityStore: EntityStore = {
dataSource: {
testEntry: { sys: { type: 'Entry', contentType: { sys: { id: 'testContentType' } } } },
},
getEntryOrAsset: jest.fn().mockReturnValue({
sys: { type: 'Entry', contentType: { sys: { id: 'testContentType' } } },
}),
} as unknown as EntityStore;

const result = resolvePrebindingPath({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
});

expect(result).toBe('');
Expand Down
16 changes: 2 additions & 14 deletions packages/experience-builder-sdk/src/utils/prebindingUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { EntityStore } from '@contentful/experiences-core';
import {
ComponentPropertyValue,
ExperienceComponentSettings,
PatternProperty,
} from '@contentful/experiences-validators';
import { UnresolvedLink } from 'contentful';

export const shouldUsePrebinding = ({
componentValueKey,
Expand Down Expand Up @@ -35,12 +33,10 @@ export const resolvePrebindingPath = ({
componentValueKey,
componentSettings,
patternProperties,
entityStore,
}: {
componentValueKey: string;
componentSettings: ExperienceComponentSettings;
patternProperties: Record<string, PatternProperty>;
entityStore: EntityStore;
}) => {
const variableMapping = componentSettings.variableMappings?.[componentValueKey];

Expand All @@ -50,17 +46,9 @@ export const resolvePrebindingPath = ({

if (!patternProperty) return '';

const [, uuid] = patternProperty.path.split('/');
const binding = entityStore.dataSource[uuid] as UnresolvedLink<'Entry' | 'Asset'>;
const entityOrAsset = entityStore.getEntryOrAsset(binding, patternProperty.path);
const contentType = patternProperty.contenType;

if (entityOrAsset?.sys?.type !== 'Entry') {
return '';
}

const contentType = entityOrAsset.sys.contentType.sys.id;

const fieldPath = variableMapping.pathsByContentType[contentType]?.path;
const fieldPath = variableMapping?.pathsByContentType?.[contentType]?.path;

if (!fieldPath) return '';

Expand Down
2 changes: 2 additions & 0 deletions packages/validators/src/schemas/v2023_09_28/experience.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ const PatternPropertyDefinitionsSchema = z.record(
const PatternPropertySchema = z.object({
type: z.literal('BoundValue'),
path: z.string(),
contenType: z.string(),
});

const PatternPropertysSchema = z.record(propertyKeySchema, PatternPropertySchema);
Expand Down Expand Up @@ -321,6 +322,7 @@ export type Breakpoint = z.infer<typeof BreakpointSchema>;
export type PrimitiveValue = z.infer<typeof PrimitiveValueSchema>;
export type DesignValue = z.infer<typeof DesignValueSchema>;
export type BoundValue = z.infer<typeof BoundValueSchema>;
export type NoValue = z.infer<typeof NoValueSchema>;
export type UnboundValue = z.infer<typeof UnboundValueSchema>;
export type HyperlinkValue = z.infer<typeof HyperlinkValueSchema>;
export type ComponentValue = z.infer<typeof ComponentValueSchema>;
Expand Down
1 change: 1 addition & 0 deletions packages/validators/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type {
ComponentPropertyValue,
ComponentTreeNode,
PrimitiveValue,
NoValue,
DesignValue,
UnboundValue,
BoundValue,
Expand Down

0 comments on commit 2f40399

Please sign in to comment.