oxen-website/services/cms.ts

128 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-01-29 03:50:49 +01:00
import { ContentfulClientApi, createClient } from 'contentful';
import moment from 'moment';
2021-02-04 06:42:15 +01:00
import { SideMenuItem } from '../state/navigation';
import { IAuthor, IFigureImage, IPost, ISplitPage } from '../types/cms';
2021-01-29 03:50:49 +01:00
2021-02-04 06:42:15 +01:00
export class CmsApi {
2021-01-29 03:50:49 +01:00
client: ContentfulClientApi;
constructor() {
this.client = createClient({
space: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
});
}
2021-01-31 11:02:19 +01:00
public async fetchBlogEntries(): Promise<Array<IPost>> {
2021-01-29 03:50:49 +01:00
return this.client
.getEntries({
2021-02-02 06:28:00 +01:00
content_type: 'post', // only fetch blog post entry
2021-01-29 03:50:49 +01:00
})
.then(entries => {
if (entries && entries.items && entries.items.length > 0) {
const blogPosts = entries.items.map(entry => this.convertPost(entry));
return blogPosts;
}
return [];
});
}
2021-01-31 11:02:19 +01:00
public async fetchBlogById(id): Promise<IPost> {
2021-01-29 03:50:49 +01:00
return this.client.getEntry(id).then(entry => {
if (entry) {
2021-02-04 07:19:04 +01:00
return this.convertPost(entry);
2021-01-29 03:50:49 +01:00
}
return null;
});
}
2021-02-04 06:42:15 +01:00
public async fetchPageById(id: SideMenuItem): Promise<ISplitPage> {
2021-02-04 07:19:04 +01:00
return this.client
.getEntries({
content_type: 'splitPage',
'fields.id[in]': id,
})
.then(entries => {
console.log('blog ➡️ entries:', entries);
if (entries && entries.items && entries.items.length > 0) {
return this.convertPage(entries.items[0]);
}
return null;
});
2021-02-04 06:42:15 +01:00
}
2021-02-02 06:28:00 +01:00
public async fetchBlogBySlug(slug: string): Promise<IPost> {
return this.client
.getEntries({
content_type: 'post',
'fields.slug[in]': slug,
})
.then(entries => {
console.log('blog ➡️ entries:', entries);
if (entries && entries.items && entries.items.length > 0) {
const post = this.convertPost(entries.items[0]);
return post;
}
return null;
});
}
2021-01-31 11:02:19 +01:00
public convertImage = (rawImage): IFigureImage =>
rawImage
? {
imageUrl: rawImage.file.url.replace('//', 'http://'), // may need to put null check as well here
2021-02-02 06:28:00 +01:00
description: rawImage.description ?? null,
title: rawImage.title ?? null,
2021-01-31 11:02:19 +01:00
}
: null;
2021-01-29 03:50:49 +01:00
2021-01-31 11:02:19 +01:00
public convertAuthor = (rawAuthor): IAuthor =>
rawAuthor
? {
name: rawAuthor.name,
2021-02-02 06:28:00 +01:00
avatar: this.convertImage(rawAuthor.avatar.fields),
2021-01-31 11:02:19 +01:00
shortBio: rawAuthor.shortBio,
position: rawAuthor.position,
email: rawAuthor.email,
twitter: rawAuthor?.twitter ?? null,
2021-02-01 03:57:54 +01:00
facebook: rawAuthor.facebook ?? null,
github: rawAuthor.github ?? null,
2021-01-31 11:02:19 +01:00
}
: null;
2021-01-29 03:50:49 +01:00
2021-01-31 11:02:19 +01:00
public convertPost = (rawData): IPost => {
2021-01-29 03:50:49 +01:00
const rawPost = rawData.fields;
2021-02-02 06:28:00 +01:00
const rawFeatureImage = rawPost?.featureImage
? rawPost?.featureImage.fields
2021-01-29 03:50:49 +01:00
: null;
const rawAuthor = rawPost.author ? rawPost.author.fields : null;
2021-01-31 11:02:19 +01:00
2021-02-02 06:28:00 +01:00
console.log('blog ➡️ author', rawAuthor);
2021-01-29 03:50:49 +01:00
return {
id: rawData.sys.id,
body: rawPost.body,
2021-01-31 11:02:19 +01:00
subtitle: rawPost.subtitle,
2021-01-29 03:50:49 +01:00
publishedDate: moment(rawPost.publishedDate).format('DD MMM YYYY'),
slug: rawPost.slug,
2021-01-31 11:02:19 +01:00
tags: rawPost?.tags ?? [],
2021-01-29 03:50:49 +01:00
title: rawPost.title,
featureImage: this.convertImage(rawFeatureImage),
author: this.convertAuthor(rawAuthor),
};
};
2021-02-04 07:19:04 +01:00
public convertPage = (rawData): ISplitPage => {
const rawPage = rawData.fields;
const rawHero = rawPage?.hero ? rawPage?.hero?.fields : null;
return {
id: SideMenuItem[rawPage?.id],
label: rawPage?.label,
title: rawPage?.title,
body: rawPage?.body,
hero: this.convertImage(rawHero),
};
};
2021-01-29 03:50:49 +01:00
}