Skip to content

Commit

Permalink
Add possibility to extend arboretum page by selected fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacjan committed Aug 20, 2024
1 parent 201f679 commit 5ec83de
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 42 deletions.
2 changes: 2 additions & 0 deletions src/arboretum-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type ArboretumClientContentfulConfigOptionsT = {
titleFieldId?: string;
childPagesFieldId?: string;
parentPageFieldId?: string;
select?: Array<string>
};
};
redirectContentType?: {
Expand Down Expand Up @@ -132,6 +133,7 @@ export type ArboretumPageT = ArboretumPageBaseT & {
totalDirectChildrenCount: number;
children?: Array<ArboretumPageNodeT>;
ancestors?: Array<Omit<ArboretumPageT, "children" | "ancestors">>;
additionalFields?: {[key: string]: any}
};

export type ArboretumPageNodeT =
Expand Down
1 change: 1 addition & 0 deletions src/impl/arboretum-client.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type PageT = {
slug: string;
title?: string;
childPages: Array<{ sys: { id: string } }>;
additionalFields?: { [key: string]: any };
};
export type RedirectT = {
sys: {
Expand Down
34 changes: 29 additions & 5 deletions src/impl/data/helpers/get-all-entries-recursively.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,28 @@ export const getAllEntriesRecursively = async (
skip,
content_type: contentType,
include: 0,
select,
/* For some reason select param causes errors in CMA. I'm getting the following response:
{
"status": 400,
"statusText": "Bad Request",
"message": "The query you sent was invalid. Probably a filter or ordering specification is not applicable to the type of a field.",
"details": {
"errors": [
{
"name": "select",
"details": "Select is only applicable when querying a collection of entities."
}
]
},
"request": {
"url": "/spaces/8h4rcnu50txt/environments/dacjan-test/public/entries",
"method": "get",
...
},
}*/
select: contentfulClientType !== "cma-client" ? select : undefined,
locale:
contentfulClientType === "cda-client-with-all-locales"
? undefined
: "*",
contentfulClientType === "cda-client-with-all-locales" ? undefined : "*",
});

items.forEach((i) => {
Expand All @@ -54,6 +71,13 @@ export const getAllEntriesRecursively = async (

acc.push(...items);
return items.length >= limit
? getAllEntriesRecursively({getEntries}, contentfulClientType, contentType, skip + limit, acc, select)
? getAllEntriesRecursively(
{ getEntries },
contentfulClientType,
contentType,
skip + limit,
acc,
select
)
: acc;
};
51 changes: 17 additions & 34 deletions src/impl/data/page-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,13 @@ const getAllPageEntriesRecursively = async (
acc: Array<EntryT>,
select?: string
): Promise<Array<EntryT>> => {
const fieldsSelect = [
pageContentTypeOpt.slugFieldId,
pageContentTypeOpt.childPagesFieldId,
pageContentTypeOpt.titleFieldId,
pageContentTypeOpt.parentPageFieldId,
].flatMap((id) => (id ? [`fields.${id}`] : []));

return getAllEntriesRecursively(
{ getEntries },
contentfulClientType,
pageContentTypeOpt.id,
skip,
acc,
/* For some reason select param causes errors in CMA. I'm getting the following response:
{
"status": 400,
"statusText": "Bad Request",
"message": "The query you sent was invalid. Probably a filter or ordering specification is not applicable to the type of a field.",
"details": {
"errors": [
{
"name": "select",
"details": "Select is only applicable when querying a collection of entities."
}
]
},
"request": {
"url": "/spaces/8h4rcnu50txt/environments/dacjan-test/public/entries",
"method": "get",
...
},
}*/
select || contentfulClientType === "cma-client"
? undefined
: ["sys", "metadata", ...fieldsSelect].join(","),
select,
pageContentTypeOpt.childPagesFieldId
? [pageContentTypeOpt.childPagesFieldId]
: []
Expand Down Expand Up @@ -97,15 +69,26 @@ export const pageEntries = async (
const pageContentTypes = Object.entries(options.pageContentTypes);

const pageEntriesPromise = Promise.all(
pageContentTypes.map(([id, fieldIds]) =>
getAllPageEntriesRecursively(
pageContentTypes.map(([id, fieldIds]) => {
const fieldsSelect = [
fieldIds.slugFieldId,
fieldIds.childPagesFieldId,
fieldIds.titleFieldId,
fieldIds.parentPageFieldId,
...(fieldIds.select || []),
].flatMap((id) => (id ? [`fields.${id}`] : []));

const select = ["sys", "metadata", ...fieldsSelect].join(",");

return getAllPageEntriesRecursively(
apiClient,
ctx.contentfulClientType,
{ id, ...fieldIds },
0,
[]
)
)
[],
select
);
})
);
/*
This is woraround. Published entries from CMA (the same with CDA and CPA)
Expand Down
28 changes: 25 additions & 3 deletions src/impl/sitemap/adapters/page-entry-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PageT } from "../../arboretum-client.impl";
import { ArboretumClientCtx, PageT } from "../../arboretum-client.impl";
import {
ContentTypeT,
EntryT,
Expand All @@ -8,7 +8,11 @@ import { SitemapDataT } from "../../data/sitemap-data";
import { localizeField } from "../helpers/localize-contentful-field";

export const pageEntryAdapter = (
data: Pick<SitemapDataT, "locales" | "defaultLocaleCode" | "pages">,
data: Pick<
SitemapDataT,
"locales" | "defaultLocaleCode" | "pages" | "contentTypes"
>,
options: Pick<ArboretumClientCtx["options"], "pageContentTypes">,
slugField: ContentTypeT["fields"][number],
titleField: ContentTypeT["fields"][number] | undefined,
childrenRefs: PageT["childPages"],
Expand All @@ -27,6 +31,23 @@ export const pageEntryAdapter = (
? fieldValue<string>(titleField.localized, entry.fields[titleField.id])
: undefined;

const contentTypeId = entry.sys.contentType.sys.id;

const additionalFields = (
options.pageContentTypes[contentTypeId].select || []
).reduce((acc, fieldId) => {
const contentType = data.contentTypes.get(contentTypeId);
if (contentType) {
const field = contentType.fields.get(fieldId);
const value = field
? fieldValue(field.localized, entry.fields[field.id])
: undefined;
acc[fieldId] = value;
}

return acc;
}, {} as { [key: string]: any });

return slug
? {
type: "page",
Expand All @@ -37,13 +58,14 @@ export const pageEntryAdapter = (
sys: {
id: entry.sys.id,
cmaOnlyStatus: entry.sys.cmaOnlyStatus,
contentTypeId: entry.sys.contentType.sys.id,
contentTypeId,
},
metadata: entry.metadata,
childPages:
childrenRefs?.flatMap((childPage) =>
childPage?.sys?.id ? [{ sys: { id: childPage?.sys.id } }] : []
) || [],
additionalFields,
}
: undefined;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export const toArboretumPageWithMissingData =
: page.childPages.length,
children,
ancestors,
additionalFields: page.additionalFields,
});
1 change: 1 addition & 0 deletions src/impl/sitemap/helpers/build-localized-sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const buildLocalizedSitemapArrRecursively = (
const page = slugField
? pageEntryAdapter(
data,
options,
slugField,
titleField,
childrenRefs,
Expand Down

0 comments on commit 5ec83de

Please sign in to comment.