-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBlogpostListing.ts
More file actions
99 lines (86 loc) · 2.67 KB
/
Copy pathBlogpostListing.ts
File metadata and controls
99 lines (86 loc) · 2.67 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { logger } from "@deco/deco/o11y";
import { PageInfo } from "../../commerce/types.ts";
import { STALE } from "../../utils/fetch.ts";
import { AppContext } from "../mod.ts";
import { BlogPost, BlogPostListingPage, SpirePagination } from "../types.ts";
import { spirePostSummaryToBlogPost } from "./BlogpostList.ts";
export interface Props {
/**
* @title Items per page
* @description Number of posts per page to display.
*/
count?: number;
/**
* @title Page number
* @description The current page number. Defaults to 1.
*/
page?: number;
}
/**
* @title BlogpostListing
* @description Retrieves a paginated listing page of Spire blog posts.
*/
export const cache = {
maxAge: 60 * 60 * 24, // 24 hours
};
export const cacheKey = (props: Props, req: Request, ctx: AppContext) => {
const url = new URL(req.url);
const page = props.page ?? url.searchParams.get("page") ?? 1;
const count = props.count ?? url.searchParams.get("count") ?? 12;
return `spire-listing-${ctx.account}-page${page}-count${count}`;
};
export default async function BlogpostListing(
{ page, count }: Props,
req: Request,
ctx: AppContext,
): Promise<BlogPostListingPage | null> {
const { account, api } = ctx;
const url = new URL(req.url);
const params = url.searchParams;
const perPage = Number(count ?? params.get("count") ?? 12);
const pageNumber = Number(page ?? params.get("page") ?? 1);
try {
const response = await api["GET /blog/:account"](
{ account, page: pageNumber, perPage },
STALE,
);
if (!response.ok) {
return null;
}
const { posts: rawPosts, pagination } = await response.json();
const posts: BlogPost[] = (rawPosts ?? []).map(spirePostSummaryToBlogPost);
if (posts.length === 0) {
return null;
}
return {
posts,
pageInfo: toPageInfo(pagination, params),
seo: {
title: "",
canonical: new URL(url.pathname, url.origin).href,
},
};
} catch (e) {
logger.error(e);
return null;
}
}
function toPageInfo(
pagination: SpirePagination,
params: URLSearchParams,
): PageInfo {
const { page, totalPages, total, perPage } = pagination;
const hasNextPage = page < totalPages;
const hasPrevPage = page > 1;
const nextPageParams = new URLSearchParams(params);
const prevPageParams = new URLSearchParams(params);
if (hasNextPage) nextPageParams.set("page", String(page + 1));
if (hasPrevPage) prevPageParams.set("page", String(page - 1));
return {
nextPage: hasNextPage ? `?${nextPageParams}` : undefined,
previousPage: hasPrevPage ? `?${prevPageParams}` : undefined,
currentPage: page,
records: total,
recordPerPage: perPage,
};
}