add templates

This commit is contained in:
Profile Profile
2025-12-15 17:01:51 +03:00
parent 11e2a99646
commit a72d47f6d9
13 changed files with 749 additions and 353 deletions

View File

@@ -4,133 +4,179 @@ import { fetchGraphQL } from '@lib/graphql-client.js';
import { cache } from '@lib/cache/manager.js';
import { CACHE_TTL } from '@lib/cache/cache-ttl';
type ArchiveType = 'tag' | 'category' | 'author' | 'postType';
/**
* Типы архивов
*/
export type ArchiveType = 'tag' | 'category' | 'author' | 'postType';
interface ArchiveParams {
export interface ArchiveParams {
type: ArchiveType;
id?: number; // для tag/category/author
postType?: string; // для CPT
page?: number;
perPage?: number;
id?: string; // ID ВСЕГДА строка
postType?: string; // для CPT
page: number;
perPage: number;
}
/**
* Универсальный helper архивов с cursor-based пагинацией по ID
* WHERE builder
* ⚠️ Все ID — строки
*/
export async function getArchivePostsById(params: ArchiveParams) {
const page = params.page ?? 1;
const perPage = params.perPage ?? 12;
const after = await getCursorForPage(params, page, perPage);
function buildWhere(params: ArchiveParams): string {
const { type, id } = params;
const cacheKey = `archive:${params.type}:${params.id || params.postType || 'all'}:${page}:${perPage}`;
switch (type) {
case 'tag':
return `tagId: "${id}"`;
case 'category':
return `categoryId: "${id}"`;
case 'author':
return `authorId: "${id}"`;
case 'postType':
// profileArticles — твой CPT
return '';
default:
return '';
}
}
/**
* Получаем курсор для страницы N
* page 1 → cursor = null
*/
async function getCursorForPage(
params: ArchiveParams,
page: number,
perPage: number
): Promise<string | null> {
if (page <= 1) return null;
const cacheKey = `archive-cursor:${params.type}:${params.id || 'all'}:${page}:${perPage}`;
return await cache.wrap(
cacheKey,
async () => {
// Формируем where через переменные
const whereVars: Record<string, string> = {};
let whereClause = '';
if (params.type === 'tag') {
whereClause = 'tagId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'category') {
whereClause = 'categoryId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'author') {
whereClause = 'authorId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'postType' && params.postType) {
whereClause = '__typename: $postType';
whereVars.postType = params.postType;
}
const first = perPage * (page - 1);
const where = buildWhere(params);
const query = `
query GetArchivePosts($first: Int!, $after: String${whereVars.id ? ', $id: ID!' : ''}${whereVars.postType ? ', $postType: String!' : ''}) {
profileArticles(first: $first, after: $after, where: { ${whereClause}, orderby: { field: DATE, order: DESC } }) {
pageInfo {
hasNextPage
endCursor
query GetCursor($first: Int!) {
profileArticles(
first: $first,
where: {
${where}
orderby: { field: DATE, order: DESC }
}
) {
edges {
cursor
node {
id
databaseId
title
uri
date
featuredImage { node { sourceUrl(size: LARGE) altText } }
author { node { id name firstName lastName avatar { url } uri } }
categories { nodes { id name slug uri databaseId } }
tags { nodes { id name slug uri } }
}
}
}
}
`;
const variables = {
first: perPage,
after,
...whereVars
};
const data = await fetchGraphQL(query, { first });
const data = await fetchGraphQL(query, variables);
const posts = data.profileArticles?.edges?.map((edge: any) => edge.node) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null },
currentPage: page,
perPage,
};
const edges = data.profileArticles?.edges || [];
return edges.length ? edges.at(-1).cursor : null;
},
{ ttl: CACHE_TTL.POSTS }
);
}
/**
* Получение курсора для страницы N
* Основной helper для архивов
*/
async function getCursorForPage(params: ArchiveParams, page: number, perPage: number): Promise<string | null> {
if (page <= 1) return null;
export async function getArchivePostsById(params: ArchiveParams) {
const { page, perPage } = params;
const cacheKey = `archive-cursor:${params.type}:${params.id || params.postType || 'all'}:${page}:${perPage}`;
const cursor = await getCursorForPage(params, page, perPage);
return await cache.wrap(cacheKey, async () => {
const first = perPage * (page - 1);
const cacheKey = `archive:${params.type}:${params.id || 'all'}:${page}:${perPage}`;
const whereVars: Record<string, string> = {};
let whereClause = '';
return await cache.wrap(
cacheKey,
async () => {
const where = buildWhere(params);
if (params.type === 'tag') {
whereClause = 'tagId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'category') {
whereClause = 'categoryId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'author') {
whereClause = 'authorId: $id';
whereVars.id = String(params.id);
} else if (params.type === 'postType' && params.postType) {
whereClause = '__typename: $postType';
whereVars.postType = params.postType;
}
const query = `
query GetCursor($first: Int!${whereVars.id ? ', $id: ID!' : ''}${whereVars.postType ? ', $postType: String!' : ''}) {
profileArticles(first: $first, where: { ${whereClause}, orderby: { field: DATE, order: DESC } }) {
edges { cursor }
const query = `
query GetArchive(
$first: Int!
$after: String
) {
profileArticles(
first: $first
after: $after
where: {
${where}
orderby: { field: DATE, order: DESC }
}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
databaseId
title
uri
date
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
author {
node {
id
name
uri
avatar {
url
}
}
}
categories {
nodes {
id
name
uri
}
}
tags {
nodes {
id
name
uri
}
}
}
}
}
}
}
`;
`;
const variables = { first, ...whereVars };
const result = await fetchGraphQL(query, variables);
const data = await fetchGraphQL(query, {
first: perPage,
after: cursor,
});
// используем profileArticles, а не posts
const edges = result.profileArticles?.edges || [];
return edges.at(-1)?.cursor ?? null;
}, { ttl: CACHE_TTL.POSTS });
const edges = data.profileArticles?.edges || [];
return {
posts: edges.map((e: any) => e.node),
pageInfo: data.profileArticles?.pageInfo ?? {
hasNextPage: false,
endCursor: null,
},
};
},
{ ttl: CACHE_TTL.POSTS }
);
}

44
src/lib/api/categories.ts Normal file
View File

@@ -0,0 +1,44 @@
import { fetchGraphQL } from '@lib/graphql-client.js';
//кэширование
import { cache } from '@lib/cache/manager.js';
import { CACHE_TTL } from '@lib/cache/cache-ttl';
export interface Category {
id: string;
databaseId: number;
name: string;
slug: string;
description: string;
count: number;
parentId?: number | null;
}
export async function getCategory(slug: string): Promise<Category | null> {
const cacheKey = `category:${slug}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetCategory($slug: ID!) {
category(id: $slug, idType: SLUG) {
id
databaseId
name
slug
description
count
parentId
}
}
`;
const data = await fetchGraphQL(query, { slug });
return data?.category || null;
},
{ ttl: CACHE_TTL.TAXONOMY }
);
}