work version archive

This commit is contained in:
Andrey Kuvshinov
2025-12-14 23:03:30 +03:00
parent 80fb06e420
commit abe55943fc
11 changed files with 611 additions and 167 deletions

136
src/lib/api/archiveById.ts Normal file
View File

@@ -0,0 +1,136 @@
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';
interface ArchiveParams {
type: ArchiveType;
id?: number; // для tag/category/author
postType?: string; // для CPT
page?: number;
perPage?: number;
}
/**
* Универсальный helper архивов с cursor-based пагинацией по ID
*/
export async function getArchivePostsById(params: ArchiveParams) {
const page = params.page ?? 1;
const perPage = params.perPage ?? 12;
const after = await getCursorForPage(params, page, perPage);
const cacheKey = `archive:${params.type}:${params.id || params.postType || '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 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
}
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, variables);
const posts = data.profileArticles?.edges?.map((edge: any) => edge.node) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null },
currentPage: page,
perPage,
};
},
{ ttl: CACHE_TTL.POSTS }
);
}
/**
* Получение курсора для страницы N
*/
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 || params.postType || 'all'}:${page}:${perPage}`;
return await cache.wrap(cacheKey, async () => {
const first = perPage * (page - 1);
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 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 variables = { first, ...whereVars };
const result = await fetchGraphQL(query, variables);
// используем profileArticles, а не posts
const edges = result.profileArticles?.edges || [];
return edges.at(-1)?.cursor ?? null;
}, { ttl: CACHE_TTL.POSTS });
}

View File

@@ -1,102 +1,122 @@
import { fetchGraphQL } from './graphql-client.js';
import type { ProfileArticle } from '../types/graphql.js';
//кэширование
import { cache } from '@lib/cache/manager.js';
import { CACHE_TTL } from '@lib/cache/cache-ttl';
export interface AnewsPost {
title: string;
uri: string;
date: string;
}
//последние статьи
export async function getLatestPosts(first = 12, after = null) {
const query = `
query GetLatestProfileArticles($first: Int!, $after: String) {
profileArticles(
first: $first,
after: $after,
where: {orderby: { field: DATE, order: DESC }}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
databaseId
title
uri
date
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
// Создаем уникальный ключ для кэша
const cacheKey = `latest-posts:${first}:${after || 'first-page'}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetLatestProfileArticles($first: Int!, $after: String) {
profileArticles(
first: $first,
after: $after,
where: {orderby: { field: DATE, order: DESC }}
) {
pageInfo {
hasNextPage
endCursor
}
author {
edges {
cursor
node {
id
name
firstName
lastName
avatar {
url
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
}
}
uri
}
}
categories {
nodes {
id
name
slug
uri
databaseId
}
}
tags {
nodes {
id
name
slug
uri
}
}
}
}
}
}
`;
`;
const data = await fetchGraphQL(query, { first, after });
// Преобразуем edges в nodes
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
};
const data = await fetchGraphQL(query, { first, after });
// Преобразуем edges в nodes
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
};
},
{ ttl: CACHE_TTL.POSTS } // из конфигурации
);
}
//последние новости
//последние новости (кэшированная версия)
export async function getLatestAnews(count = 12): Promise<AnewsPost[]> {
const query = `
query GetAnews($count: Int!) {
aNews(first: $count, where: {orderby: {field: DATE, order: DESC}}) {
nodes {
title
uri
date
const cacheKey = `latest-anews:${count}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetAnews($count: Int!) {
aNews(first: $count, where: {orderby: {field: DATE, order: DESC}}) {
nodes {
title
uri
date
}
}
}
}
}
`;
`;
const data = await fetchGraphQL(query, { count });
return data.aNews?.nodes || []; // Исправлено: aNews вместо anews
const data = await fetchGraphQL(query, { count });
return data.aNews?.nodes || [];
},
{ ttl: CACHE_TTL.POSTS }
);
}
// Получить ProfileArticle по databaseId
@@ -182,16 +202,6 @@ export async function getTagBySlug(slug) {
slug
description
count
seo {
title
metaDesc
canonical
opengraphTitle
opengraphDescription
opengraphImage {
sourceUrl
}
}
}
}
`;
@@ -208,13 +218,17 @@ export async function getTagBySlug(slug) {
/**
* Получить посты тега с пагинацией (offset-based)
* Получить тег с постами по slug с пагинацией
*/
export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
export async function getTagWithPostsPaginated(
tagSlug: string,
perPage: number = 12,
page: number = 1
) {
const offset = (page - 1) * perPage;
const query = `
query GetTagPostsPaginated($slug: ID!, $first: Int!, $offset: Int!) {
query GetTagWithPostsPaginated($slug: ID!, $first: Int!, $offset: Int!) {
tag(id: $slug, idType: SLUG) {
id
databaseId
@@ -226,12 +240,20 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
title
metaDesc
canonical
opengraphTitle
opengraphDescription
opengraphImage {
sourceUrl
}
}
posts(
first: $first
offset: $offset
where: {
orderby: { field: DATE, order: DESC }
orderby: {
field: DATE,
order: DESC
}
}
) {
nodes {
@@ -332,7 +354,7 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
};
} catch (error) {
console.error('Error fetching tag posts:', error);
console.error('Error fetching tag with posts:', error);
return {
tag: null,
posts: [],
@@ -345,6 +367,9 @@ export async function getTagPostsPaginated(tagSlug, perPage = 12, page = 1) {
}
}
/**
* Получить общее количество постов для всех тегов
*/

132
src/lib/api/tags.ts Normal file
View File

@@ -0,0 +1,132 @@
import { fetchGraphQL } from '@lib/graphql-client.js';
//кэширование
import { cache } from '@lib/cache/manager.js';
import { CACHE_TTL } from '@lib/cache/cache-ttl';
interface Tag {
id: string;
databaseId: number;
name: string;
slug: string;
description: string;
count: number;
}
//информация о теге
export async function getTag(slug: string): Promise<Tag | null> {
const cacheKey = `tag:${slug}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetTag($slug: ID!) {
tag(id: $slug, idType: SLUG) {
id
databaseId
name
slug
description
count
}
}
`;
const data = await fetchGraphQL(query, { slug });
return data?.tag || null;
},
{ ttl: CACHE_TTL.TAXONOMY }
);
}
export async function getTagWithPostsById(tagId, page = 1, perPage = 10) {
const offset = (page - 1) * perPage;
const query = `
query GetTagWithPostsById($id: ID!, $size: Int!, $offset: Int!) {
tag(id: $id, idType: DATABASE_ID) {
id
databaseId
name
slug
description
posts(
where: {
offsetPagination: {
size: $size
offset: $offset
}
}
sort: { field: DATE, order: DESC }
) {
nodes {
id
databaseId
title
excerpt
uri
date
featuredImage {
node {
sourceUrl(size: MEDIUM)
altText
}
}
categories {
nodes {
name
slug
}
}
}
pageInfo {
offsetPagination {
total
hasMore
hasPrevious
}
}
}
}
}
`;
const data = await fetchGraphQL(query, {
id: tagId,
size: perPage,
offset: offset
});
if (!data?.tag) {
console.warn(`Tag with ID ${tagId} not found`);
return null;
}
const tag = data.tag;
const posts = tag.posts?.nodes || [];
const pagination = tag.posts?.pageInfo?.offsetPagination || {};
return {
tag: {
id: tag.id,
databaseId: tag.databaseId,
name: tag.name,
slug: tag.slug,
description: tag.description
},
posts,
pagination: {
currentPage: page,
totalPages: Math.ceil((pagination.total || 0) / perPage),
totalPosts: pagination.total || 0,
hasNextPage: pagination.hasMore || false,
hasPrevPage: pagination.hasPrevious || false,
postsPerPage: perPage
}
};
}