@nacelle/compatibility-connector
Version:
Connect @nacelle/client-js-sdk to Nacelle's v2 back end with minimal code changes
57 lines (49 loc) • 1.59 kB
text/typescript
import { toIetfLocale } from './toIetfLocale';
import type { ContentArticleList } from '@nacelle/types';
import type { Content } from 'storefrontSdkV1';
import { destructureNacelleEntryId } from '../../src/utils';
export type NacelleReference = Pick<
Content,
'type' | 'locale' | 'nacelleEntryId'
> & {
message?: string;
referenceType?: string;
};
export const createArticleReferenceErrorMessage = (
blogHandle: string,
problematicArticles: string[]
): string =>
`In blog '${blogHandle}', articles with sourceEntryId: ${problematicArticles.join(
', '
)} are not valid. Please check your CMS to ensure that these articles are correctly referenced by the '${blogHandle}' blog.`;
export function transformArticleLists(
articlesInput: Array<Content | NacelleReference> | null,
blogHandle: string,
locale: string
): ContentArticleList[] | null {
if (!articlesInput || !Array.isArray(articlesInput)) {
return null;
}
const problematicArticles: string[] = [];
const handles: string[] = articlesInput.map((article) => {
if ('fields' in article && typeof article.fields !== 'undefined') {
return article.fields.handle;
}
const { sourceEntryId } = destructureNacelleEntryId(article.nacelleEntryId);
problematicArticles.push(sourceEntryId);
return '';
});
if (problematicArticles.length) {
throw new Error(
createArticleReferenceErrorMessage(blogHandle, problematicArticles)
);
}
return [
{
handles,
locale: toIetfLocale(locale),
slug: 'default',
title: 'default'
}
];
}