Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 9s
Build & Deploy / 🧪 QA (push) Failing after 3m20s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🏗️ Build (push) Successful in 14m43s
Build & Deploy / 🧪 Smoke Test (push) Has been skipped
Build & Deploy / ⚡ Lighthouse (push) Has been skipped
Build & Deploy / ♿ WCAG (push) Has been skipped
Build & Deploy / 🛡️ Quality Gates (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 13s
128 lines
3.2 KiB
TypeScript
128 lines
3.2 KiB
TypeScript
import { getPayload } from 'payload';
|
|
import configPromise from '@payload-config';
|
|
|
|
export interface PageFrontmatter {
|
|
title: string;
|
|
excerpt: string;
|
|
featuredImage: string | null;
|
|
locale: string;
|
|
public?: boolean;
|
|
}
|
|
|
|
export interface PageMdx {
|
|
slug: string;
|
|
frontmatter: PageFrontmatter;
|
|
content: any; // Lexical AST Document
|
|
}
|
|
|
|
export async function getPageBySlug(slug: string, locale: string): Promise<PageMdx | null> {
|
|
try {
|
|
const payload = await getPayload({ config: configPromise });
|
|
|
|
const result = await payload.find({
|
|
collection: 'pages' as any,
|
|
where: {
|
|
slug: { equals: slug },
|
|
locale: { equals: locale },
|
|
},
|
|
limit: 1,
|
|
});
|
|
|
|
const docs = result.docs as any[];
|
|
|
|
if (!docs || docs.length === 0) return null;
|
|
|
|
const doc = docs[0];
|
|
|
|
return {
|
|
slug: doc.slug,
|
|
frontmatter: {
|
|
title: doc.title,
|
|
excerpt: doc.excerpt || '',
|
|
locale: doc.locale,
|
|
featuredImage:
|
|
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
|
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
|
: null,
|
|
} as PageFrontmatter,
|
|
content: doc.content as any, // Native Lexical Editor State
|
|
};
|
|
} catch (error) {
|
|
console.error(`[Payload] getPageBySlug failed for ${slug}:`, error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getAllPages(locale: string): Promise<PageMdx[]> {
|
|
try {
|
|
const payload = await getPayload({ config: configPromise });
|
|
|
|
const result = await payload.find({
|
|
collection: 'pages' as any,
|
|
where: {
|
|
locale: {
|
|
equals: locale,
|
|
},
|
|
},
|
|
limit: 100,
|
|
});
|
|
|
|
const docs = result.docs as any[];
|
|
|
|
return docs.map((doc: any) => {
|
|
return {
|
|
slug: doc.slug,
|
|
frontmatter: {
|
|
title: doc.title,
|
|
excerpt: doc.excerpt || '',
|
|
locale: doc.locale,
|
|
featuredImage:
|
|
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
|
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
|
: null,
|
|
} as PageFrontmatter,
|
|
content: doc.content as any,
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.error(`[Payload] getAllPages failed for ${locale}:`, error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export async function getAllPagesMetadata(locale: string): Promise<Partial<PageMdx>[]> {
|
|
try {
|
|
const payload = await getPayload({ config: configPromise });
|
|
|
|
const result = await payload.find({
|
|
collection: 'pages' as any,
|
|
where: {
|
|
locale: {
|
|
equals: locale,
|
|
},
|
|
},
|
|
limit: 100,
|
|
});
|
|
|
|
const docs = result.docs as any[];
|
|
|
|
return docs.map((doc: any) => {
|
|
return {
|
|
slug: doc.slug,
|
|
frontmatter: {
|
|
title: doc.title,
|
|
excerpt: doc.excerpt || '',
|
|
locale: doc.locale,
|
|
featuredImage:
|
|
typeof doc.featuredImage === 'object' && doc.featuredImage !== null
|
|
? doc.featuredImage.sizes?.card?.url || doc.featuredImage.url
|
|
: null,
|
|
} as PageFrontmatter,
|
|
};
|
|
});
|
|
} catch (error) {
|
|
console.error(`[Payload] getAllPagesMetadata failed for ${locale}:`, error);
|
|
return [];
|
|
}
|
|
}
|