|
| 1 | +import { logger } from "@deco/deco/o11y"; |
| 2 | +import { PageInfo } from "../../commerce/types.ts"; |
| 3 | +import { AppContext } from "../mod.ts"; |
| 4 | +import { BlogPost, BlogPostListingPage, SpirePagination } from "../types.ts"; |
| 5 | +import { spirePostSummaryToBlogPost } from "./BlogpostList.ts"; |
| 6 | + |
| 7 | +export interface Props { |
| 8 | + /** |
| 9 | + * @title Items per page |
| 10 | + * @description Number of posts per page to display. |
| 11 | + */ |
| 12 | + count?: number; |
| 13 | + /** |
| 14 | + * @title Page number |
| 15 | + * @description The current page number. Defaults to 1. |
| 16 | + */ |
| 17 | + page?: number; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @title BlogpostListing |
| 22 | + * @description Retrieves a paginated listing page of Spire blog posts. |
| 23 | + */ |
| 24 | +export const cache = { |
| 25 | + maxAge: 60 * 60 * 24, // 24 hours |
| 26 | +}; |
| 27 | + |
| 28 | +/** Parse an integer from a value that may be a number, string, or null. Falls back to `fallback` on NaN/null. */ |
| 29 | +function parseIntParam( |
| 30 | + value: number | string | null | undefined, |
| 31 | + fallback: number, |
| 32 | +): number { |
| 33 | + const n = parseInt(String(value ?? ""), 10); |
| 34 | + return Number.isFinite(n) && n > 0 ? n : fallback; |
| 35 | +} |
| 36 | + |
| 37 | +export const cacheKey = (props: Props, req: Request, ctx: AppContext) => { |
| 38 | + const url = new URL(req.url); |
| 39 | + const page = parseIntParam(props.page ?? url.searchParams.get("page"), 1); |
| 40 | + const count = parseIntParam(props.count ?? url.searchParams.get("count"), 12); |
| 41 | + return `spire-listing-${ctx.account}-page${page}-count${count}`; |
| 42 | +}; |
| 43 | + |
| 44 | +export default async function BlogpostListing( |
| 45 | + { page, count }: Props, |
| 46 | + req: Request, |
| 47 | + ctx: AppContext, |
| 48 | +): Promise<BlogPostListingPage | null> { |
| 49 | + const { account, api } = ctx; |
| 50 | + const url = new URL(req.url); |
| 51 | + const params = url.searchParams; |
| 52 | + const perPage = parseIntParam(count ?? params.get("count"), 12); |
| 53 | + const pageNumber = parseIntParam(page ?? params.get("page"), 1); |
| 54 | + |
| 55 | + try { |
| 56 | + const response = await api["GET /blog/:account"]( |
| 57 | + { account, page: pageNumber, perPage }, |
| 58 | + ); |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + const { posts: rawPosts, pagination, blog } = await response.json(); |
| 65 | + const posts: BlogPost[] = (rawPosts ?? []).map(spirePostSummaryToBlogPost); |
| 66 | + |
| 67 | + return { |
| 68 | + posts, |
| 69 | + pageInfo: toPageInfo(pagination, params), |
| 70 | + seo: { |
| 71 | + title: blog?.name || "Blog", |
| 72 | + canonical: new URL(url.pathname, url.origin).href, |
| 73 | + }, |
| 74 | + }; |
| 75 | + } catch (e) { |
| 76 | + logger.error(e); |
| 77 | + return null; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function toPageInfo( |
| 82 | + pagination: SpirePagination, |
| 83 | + params: URLSearchParams, |
| 84 | +): PageInfo { |
| 85 | + const { page, totalPages, total, perPage } = pagination; |
| 86 | + const hasNextPage = page < totalPages; |
| 87 | + const hasPrevPage = page > 1; |
| 88 | + |
| 89 | + const nextPageParams = new URLSearchParams(params); |
| 90 | + const prevPageParams = new URLSearchParams(params); |
| 91 | + |
| 92 | + if (hasNextPage) nextPageParams.set("page", String(page + 1)); |
| 93 | + if (hasPrevPage) prevPageParams.set("page", String(page - 1)); |
| 94 | + |
| 95 | + return { |
| 96 | + nextPage: hasNextPage ? `?${nextPageParams}` : undefined, |
| 97 | + previousPage: hasPrevPage ? `?${prevPageParams}` : undefined, |
| 98 | + currentPage: page, |
| 99 | + records: total, |
| 100 | + recordPerPage: perPage, |
| 101 | + }; |
| 102 | +} |
0 commit comments