add templates
This commit is contained in:
44
src/lib/api/categories.ts
Normal file
44
src/lib/api/categories.ts
Normal 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 }
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user