-
Notifications
You must be signed in to change notification settings - Fork 0
/
getNews.ts
43 lines (35 loc) · 1.09 KB
/
getNews.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
import type { Asset } from 'contentful';
import type { ContentTypeNews, NewsArticle } from './types';
import { ContentTypes } from './constants';
import { contentfulDeliveryClient } from './contentful';
import { flattenImageAssetFields } from './flattenAssetFields';
interface Options {
limit?: number,
}
/**
* Queries and returns news articles entries from Contentful.
* @param options Options.
* @param [options.limit] Number of entries to query.
* @returns News article entries.
*/
const getNews = async ({
limit = 1000,
}: Options): Promise<Array<NewsArticle>> => {
const { items } = await contentfulDeliveryClient.getEntries<ContentTypeNews>({
content_type: ContentTypes.NewsArticle,
order: ['-fields.date'],
limit,
});
const data = items.map(({ fields }) => ({
title: fields.title,
slug: fields.slug,
date: fields.date,
category: fields.category,
keywords: fields.keywords ?? [],
content: fields.content,
description: fields.description,
image: flattenImageAssetFields(fields.image as Asset),
}));
return data;
};
export default getNews;