-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPageContent.ts
48 lines (39 loc) · 1.33 KB
/
getPageContent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type { PageData, ContentTypePageContent } from "./types";
import { ContentTypes } from "./constants";
import { contentfulDeliveryClient, contentfulPreviewClient } from "./contentful";
import { flattenImageAssetFields } from "./flattenAssetFields";
interface Options {
references?: boolean;
preview?: boolean;
}
/**
* Queries and returns a page content entry from Contentful.
* @param path Path field to query.
* @returns Species page entries.
*/
const getPageContent = async (path: string, options?: Options): Promise<PageData> => {
let client = contentfulDeliveryClient;
if (options?.preview) {
client = contentfulPreviewClient;
}
const { items } = await client.getEntries<ContentTypePageContent>({
content_type: ContentTypes.PageContent,
"fields.path": path,
limit: 1,
include: 2,
});
const [{ sys, fields }] = items;
const pageData: PageData = {
id: sys.id,
description: fields.description ?? null,
content: fields.content ?? null,
data: fields.data ?? null,
image: fields.image ? flattenImageAssetFields(fields.image) : null,
background: fields.background ? flattenImageAssetFields(fields.background) : null,
};
if (options?.references) {
pageData.references = (fields.references as PageData["references"]) ?? null;
}
return pageData;
};
export default getPageContent;