Files
brb-store/lib/blog.ts
T
Alireza HassaniandCursor 0cb9aa57e2 Use Meshkee {id}/{fa-slug} detail URLs for products and blogs.
Resolve pages via by-id APIs, slugify Farsi titles for public paths, and redirect legacy slug-only URLs to the canonical form.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-22 23:52:45 +03:30

61 lines
1.6 KiB
TypeScript

import { meshkeeGet, tenantPath } from "@/lib/meshkee";
import { detailPath, slugifyForUrl } from "@/lib/seo-slug";
export type BlogPost = {
id: string;
title: string;
slug: string;
abstract?: string | null;
mainTextHtml?: string | null;
titleImageUrl?: string | null;
publishedAt?: string | null;
createdAt?: string | null;
commentCount?: number;
author?: { name?: string } | string | null;
};
export type PagedBlogs = {
items: BlogPost[];
total: number;
page: number;
pageSize: number;
};
export async function fetchBlogs(params: { page?: number; pageSize?: number } = {}) {
const query = new URLSearchParams();
query.set("page", String(params.page ?? 1));
query.set("pageSize", String(params.pageSize ?? 12));
return meshkeeGet<PagedBlogs>(tenantPath(`/blogs?${query.toString()}`), {
items: [],
total: 0,
page: 1,
pageSize: params.pageSize ?? 12,
});
}
export async function fetchBlogById(id: string) {
const data = await meshkeeGet<{ blog?: BlogPost }>(
tenantPath(`/blogs/by-id/${encodeURIComponent(id)}`),
{},
);
return data.blog ?? null;
}
/** @deprecated Prefer fetchBlogById for public detail pages. */
export async function fetchBlogBySlug(slug: string) {
const data = await meshkeeGet<{ blog?: BlogPost }>(tenantPath(`/blogs/${encodeURIComponent(slug)}`), {});
return data.blog ?? null;
}
export function blogImage(post: BlogPost) {
return post.titleImageUrl || "/placeholders/blog.svg";
}
export function blogHref(post: Pick<BlogPost, "id" | "title">) {
return detailPath("/blog", post.id, post.title);
}
export function blogTitleSlug(title: string) {
return slugifyForUrl(title);
}