Skip to content

Instantly share code, notes, and snippets.

@reaktivo
Created November 26, 2019 15:22
Show Gist options
  • Save reaktivo/1aeb5322cdbe676320e14381706cc644 to your computer and use it in GitHub Desktop.
Save reaktivo/1aeb5322cdbe676320e14381706cc644 to your computer and use it in GitHub Desktop.
Typescript Contentful type
import { createClient, EntryCollection, ContentTypeLink, Entry, Sys } from 'contentful';
type FlatEntry<T> = {
id: Sys['id'];
contentType: ContentTypeLink['id'];
} & T;
type ValueOf<T> = T[keyof T];
function flatten<T>(contentfulValue: Entry<T> | ValueOf<T>) {
if (Array.isArray(contentfulValue)) {
return contentfulValue.map(item => flatten(item));
}
const entry = contentfulValue as Entry<T>;
if (entry?.fields) {
return flattenEntry(entry);
}
return contentfulValue;
}
function flattenEntryCollection<T>(entryCollection: EntryCollection<T>): FlatEntry<T>[] {
return entryCollection.items.map(item => flattenEntry(item));
}
function flattenEntry<T>(entry: Entry<T>): FlatEntry<T> {
return {
id: entry.sys.id,
contentType: entry.sys.contentType.sys.id,
// ...Object.entries(entry.fields).reduce(
// (acc, [key, value]) => ({
// ...acc,
// [key]: Array.isArray(value) ? value.map()
// }),
...Object.keys(entry.fields).reduce((acc, key) => {
return Object.assign(acc, { [key]: flatten(entry.fields[key]) });
}, {} as T) as T
};
}
const client = createClient({
space: 'utaji99zkvj6',
accessToken: 'c9e06be482d24784f886738260047ac9c19536dd84e5fb82b52c70b4747b798a',
host: 'contentful.aceandtate.show'
});
// TODO: add static list of country strings
type Country = string;
type Query = {
country?: Country;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
function queryCountry(country?: Country) {
if (typeof country !== 'string') {
return {};
}
return {
'fields.countryConfiguration.sys.contentType.sys.id': 'CountryConfiguration',
'fields.countryConfiguration.fields.includeInCountries': country.toLowerCase()
};
}
export async function getEntries<T>(query: Query) {
const { country, ...rest } = query;
const result = await client.getEntries<T>({
...rest,
...queryCountry(country)
});
return flattenEntryCollection(result);
}
export async function getEntry<T>(query: Query) {
const result = await getEntries(query);
return result[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment