const API_BASE = process.env.SKAYLE_API_BASE_URL || "https://api.skayle.ai";
const ORG_ID = process.env.SKAYLE_ORG_ID;
if (!ORG_ID) {
throw new Error("Missing SKAYLE_ORG_ID env var");
}
type JsonApiResource<T = Record<string, unknown>> = {
id: string;
type: string;
attributes: T;
relationships?: Record<string, unknown>;
};
type JsonApiListResponse<T> = {
data: Array<JsonApiResource<T>>;
included?: Array<JsonApiResource>;
meta?: { total?: number; page?: number; per_page?: number };
};
type JsonApiSingleResponse<T> = {
data: JsonApiResource<T> | null;
included?: Array<JsonApiResource>;
};
export type CmsPost = {
title: string;
slug: string;
excerpt: string | null;
content_html: string | null;
created_at: string;
updated_at: string;
};
async function fetchJson<T>(path: string): Promise<T> {
const url = `${API_BASE}/v1/${ORG_ID}${path}`;
const res = await fetch(url, {
next: { revalidate: 300 },
headers: { "Content-Type": "application/json" },
});
if (!res.ok) {
throw new Error(`Skayle CMS request failed: ${res.status}`);
}
return res.json() as Promise<T>;
}
export async function getPosts() {
const response = await fetchJson<JsonApiListResponse<CmsPost>>(
"/articles?status=published&orderby=date&order=desc&page=1&per_page=50",
);
return response.data;
}
export async function getPostBySlug(slug: string) {
const response = await fetchJson<JsonApiSingleResponse<CmsPost>>(
`/articles/${encodeURIComponent(slug)}?include=categories,tags,authors`,
);
return response.data;
}