Skip to content

Commit

Permalink
fix: restore Slice Zone Slices union in Content (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore authored Jul 26, 2023
1 parent f7b3c04 commit 6748a5e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function generateTypes(config: GenerateTypesConfig = {}): string {
allDocumentTypesTypeNames.push(customTypeType.name);

contentTypeNames.push(customTypeType.name);
contentTypeNames.push(customTypeType.dataName);
contentTypeNames.push(...customTypeType.contentTypeNames);
}

if (config.customTypeModels.length > 0) {
Expand All @@ -108,8 +108,7 @@ export function generateTypes(config: GenerateTypesConfig = {}): string {
code = addSection(sharedSliceType.code, code);

contentTypeNames.push(sharedSliceType.name);
contentTypeNames.push(sharedSliceType.variationUnionName);
contentTypeNames.push(...sharedSliceType.variationNames);
contentTypeNames.push(...sharedSliceType.contentTypeNames);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lib/buildCustomTypeDataType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ type BuildCustomTypeDataTypeReturnValue = {
name: string;
code: string;
auxiliaryTypes: AuxiliaryType[];
contentTypeNames: string[];
};

export function buildCustomTypeDataType(
args: BuildCustomTypeDataTypeArgs,
): BuildCustomTypeDataTypeReturnValue {
let code = "";
const auxiliaryTypes: AuxiliaryType[] = [];
const contentTypeNames: string[] = [];

const name = buildTypeName(args.model.id, "Document", "Data");
const humanReadableName = getHumanReadableModelName({
Expand Down Expand Up @@ -51,6 +53,7 @@ export function buildCustomTypeDataType(
fieldProperties += tabFieldProperties.code;

auxiliaryTypes.push(...tabFieldProperties.auxiliaryTypes);
contentTypeNames.push(...tabFieldProperties.contentTypeNames);
}

if (fieldProperties) {
Expand All @@ -70,5 +73,6 @@ export function buildCustomTypeDataType(
name,
code,
auxiliaryTypes,
contentTypeNames,
};
}
7 changes: 5 additions & 2 deletions src/lib/buildCustomTypeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ type BuildCustomTypeTypesArgs = {

type BuildCustomTypeTypeReturnValue = {
name: string;
dataName: string;
code: string;
auxiliaryTypes: AuxiliaryType[];
contentTypeNames: string[];
};

export function buildCustomTypeType(
Expand All @@ -43,6 +43,7 @@ export function buildCustomTypeType(
let code = "";

const auxiliaryTypes: AuxiliaryType[] = [];
const contentTypeNames: string[] = [];

const name = buildTypeName(args.model.id, "Document");
const langDefault =
Expand All @@ -63,6 +64,8 @@ export function buildCustomTypeType(
});

auxiliaryTypes.push(...dataType.auxiliaryTypes);
contentTypeNames.push(dataType.name);
contentTypeNames.push(...dataType.contentTypeNames);

code = addSection(dataType.code, code);

Expand All @@ -86,9 +89,9 @@ export function buildCustomTypeType(

const result = {
name,
dataName: dataType.name,
code,
auxiliaryTypes,
contentTypeNames,
};

if (args.cache) {
Expand Down
8 changes: 8 additions & 0 deletions src/lib/buildFieldProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type BuildFieldPropertyArgs = Pick<
type BuildFieldPropertyReturnType = {
code: string;
auxiliaryTypes: AuxiliaryType[];
contentTypeNames: string[];
};

function buildFieldProperty(
Expand All @@ -34,6 +35,7 @@ function buildFieldProperty(
});

const auxiliaryTypes: AuxiliaryType[] = [];
const contentTypeNames: string[] = [];

const name =
args.name.includes("-") ||
Expand Down Expand Up @@ -450,6 +452,7 @@ function buildFieldProperty(
name: choiceUnionName,
code: `type ${choiceUnionName} = ${choiceUnion}`,
});
contentTypeNames.push(choiceUnionName);

code = addLine(`${name}: prismic.SliceZone<${choiceUnionName}>;`, code);

Expand All @@ -464,6 +467,7 @@ function buildFieldProperty(
return {
code,
auxiliaryTypes,
contentTypeNames,
};
}

Expand All @@ -477,6 +481,7 @@ type BuildFieldPropertiesArgs = {
type BuildFieldPropertiesReturnType = {
code: string;
auxiliaryTypes: AuxiliaryType[];
contentTypeNames: string[];
};

export function buildFieldProperties(
Expand All @@ -485,6 +490,7 @@ export function buildFieldProperties(
let code = "";

const auxiliaryTypes: AuxiliaryType[] = [];
const contentTypeNames: string[] = [];

for (const name in args.fields) {
const field = args.fields[name];
Expand All @@ -500,10 +506,12 @@ export function buildFieldProperties(
code = addSection(fieldProperty.code, code);

auxiliaryTypes.push(...fieldProperty.auxiliaryTypes);
contentTypeNames.push(...fieldProperty.contentTypeNames);
}

return {
code,
auxiliaryTypes,
contentTypeNames,
};
}
12 changes: 10 additions & 2 deletions src/lib/buildSharedSliceType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type BuildSharedSliceTypeArgs = {

type BuildSharedSliceTypeReturnValue = {
name: string;
variationUnionName: string;
variationNames: string[];
code: string;
contentTypeNames: string[];
};

export function buildSharedSliceType(
Expand All @@ -40,6 +40,7 @@ export function buildSharedSliceType(
}

let code = "";
const contentTypeNames: string[] = [];

const name = buildTypeName(args.model.id, "Slice");
const humanReadableName = getHumanReadableModelName({
Expand Down Expand Up @@ -76,6 +77,8 @@ export function buildSharedSliceType(
path,
});

contentTypeNames.push(...primaryFieldProperties.contentTypeNames);

const docs = stripIndent`
/**
* Primary content in *${humanReadablePath}*
Expand Down Expand Up @@ -120,6 +123,8 @@ export function buildSharedSliceType(
path,
});

contentTypeNames.push(...itemFieldProperties.contentTypeNames);

const docs = stripIndent`
/**
* Primary content in *${humanReadablePath}*
Expand Down Expand Up @@ -168,6 +173,9 @@ export function buildSharedSliceType(
const variationUnionName = buildTypeName(name, "Variation");
const variationsUnion = buildUnion(variationNames);

contentTypeNames.push(variationUnionName);
contentTypeNames.push(...variationNames);

code = addSection(
source`
/**
Expand Down Expand Up @@ -198,9 +206,9 @@ export function buildSharedSliceType(

const result = {
name,
variationUnionName,
variationNames,
code,
contentTypeNames,
};

if (args.cache) {
Expand Down
5 changes: 5 additions & 0 deletions test/__snapshots__/generateTypes.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2400,14 +2400,19 @@ declare module \\"@prismicio/client\\" {
export type {
MorbiDocument,
MorbiDocumentData,
MorbiDocumentDataSliceZoneSlice,
NetusDocument,
NetusDocumentData,
NetusDocumentDataSliceZoneSlice,
AliquamDocument,
AliquamDocumentData,
AliquamDocumentDataSliceZoneSlice,
ADocument,
ADocumentData,
ADocumentDataSliceZoneSlice,
ConsequatDocument,
ConsequatDocumentData,
ConsequatDocumentDataSliceZoneSlice,
AllDocumentTypes,
InterdumSlice,
InterdumSliceVariation,
Expand Down

0 comments on commit 6748a5e

Please sign in to comment.