File size: 1,811 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import GhostContentAPI from "@tryghost/content-api";
import { GHOST_API_URL_DEFAULT, GHOST_API_KEY_DEFAULT } from "./defaults.js";
const GHOST_API_URL = process.env.GHOST_API_URL || GHOST_API_URL_DEFAULT;
const GHOST_API_KEY = process.env.GHOST_API_KEY || GHOST_API_KEY_DEFAULT;
const api = new GhostContentAPI({
url: GHOST_API_URL,
key: GHOST_API_KEY,
version: "v3.0",
});
const is404 = (error) => /not found/i.test(error.message);
export async function getPreviewPostBySlug(slug) {
const params = {
slug,
fields: "slug",
limit: "all",
};
try {
const post = await api.posts.read(params);
return post;
} catch (error) {
// Don't throw if an slug doesn't exist
if (is404(error)) return;
throw error;
}
}
export async function getAllPostsWithSlug() {
const params = {
fields: "slug",
limit: "all",
};
const posts = await api.posts.browse(params);
return posts;
}
export async function getAllPostsForHome(preview) {
const params = {
limit: "all",
include: "authors",
order: "published_at DESC",
...(preview && { status: "all" }),
};
const posts = await api.posts.browse(params);
return posts;
}
export async function getPostAndMorePosts(slug, preview) {
const singleObjectParams = {
slug,
include: "authors",
...(preview && { status: "all" }),
};
const moreObjectParams = {
limit: 3,
include: "authors",
...(preview && { status: "all" }),
};
const post = await api.posts.read(singleObjectParams).catch((error) => {
// Don't throw if an slug doesn't exist
if (is404(error)) return;
throw error;
});
const morePosts = (await api.posts.browse(moreObjectParams))
?.filter(({ slug }) => post.slug !== slug)
.slice(0, 2);
return {
post,
morePosts,
};
}
|