add all api
This commit is contained in:
393
src/lib/api/all.ts
Normal file
393
src/lib/api/all.ts
Normal file
@@ -0,0 +1,393 @@
|
||||
import { fetchGraphQL } from './graphql-client';
|
||||
import { cache } from '@lib/cache/manager';
|
||||
import { CACHE_TTL } from '@lib/cache/cache-ttl';
|
||||
|
||||
export interface NodeByUriResponse {
|
||||
nodeByUri: {
|
||||
__typename: string;
|
||||
id?: string;
|
||||
databaseId?: number;
|
||||
title?: string;
|
||||
uri?: string;
|
||||
date?: string;
|
||||
content?: string;
|
||||
excerpt?: string;
|
||||
slug?: string;
|
||||
name?: string;
|
||||
description?: string;
|
||||
count?: number;
|
||||
featuredImage?: {
|
||||
node: {
|
||||
sourceUrl: string;
|
||||
altText: string;
|
||||
};
|
||||
};
|
||||
author?: {
|
||||
node: {
|
||||
id: string;
|
||||
name: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
avatar: {
|
||||
url: string;
|
||||
};
|
||||
uri: string;
|
||||
};
|
||||
};
|
||||
categories?: {
|
||||
nodes: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
uri: string;
|
||||
databaseId: number;
|
||||
}>;
|
||||
};
|
||||
tags?: {
|
||||
nodes: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
uri: string;
|
||||
}>;
|
||||
};
|
||||
ancestors?: {
|
||||
nodes: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
uri: string;
|
||||
}>;
|
||||
};
|
||||
children?: {
|
||||
nodes: Array<{
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
uri: string;
|
||||
count: number;
|
||||
}>;
|
||||
};
|
||||
} | null;
|
||||
}
|
||||
|
||||
export async function getNodeByURI(uri: string): Promise<NodeByUriResponse> {
|
||||
const normalizedUri = uri.startsWith('/') ? uri : `/${uri}`;
|
||||
const cacheKey = `node-by-uri:${normalizedUri}`;
|
||||
|
||||
return await cache.wrap(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const query = `
|
||||
query GetNodeByURI($uri: String!) {
|
||||
nodeByUri(uri: $uri) {
|
||||
__typename
|
||||
uri
|
||||
|
||||
... on Category {
|
||||
id
|
||||
databaseId
|
||||
name
|
||||
slug
|
||||
description
|
||||
uri
|
||||
count
|
||||
ancestors {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
}
|
||||
}
|
||||
children {
|
||||
nodes {
|
||||
id
|
||||
name
|
||||
slug
|
||||
uri
|
||||
count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
... on Tag {
|
||||
id
|
||||
databaseId
|
||||
name
|
||||
slug
|
||||
description
|
||||
uri
|
||||
count
|
||||
}
|
||||
|
||||
... on ProfileArticle {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
content
|
||||
excerpt
|
||||
date
|
||||
slug
|
||||
uri
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
... on ANew {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
content
|
||||
excerpt
|
||||
date
|
||||
slug
|
||||
uri
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
... on Post {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
content
|
||||
excerpt
|
||||
date
|
||||
slug
|
||||
uri
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
... on Page {
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
content
|
||||
date
|
||||
slug
|
||||
uri
|
||||
featuredImage {
|
||||
node {
|
||||
sourceUrl(size: LARGE)
|
||||
altText
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchGraphQL(query, { uri: normalizedUri });
|
||||
return data;
|
||||
},
|
||||
{
|
||||
ttl: CACHE_TTL.POSTS
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
export async function getCategoryPosts(
|
||||
categoryId: number,
|
||||
page = 1,
|
||||
postsPerPage = 12
|
||||
) {
|
||||
const offset = (page - 1) * postsPerPage;
|
||||
const cacheKey = `category-posts:${categoryId}:${page}:${postsPerPage}`;
|
||||
|
||||
return await cache.wrap(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const query = `
|
||||
query GetCategoryPosts($id: Int!, $size: Int!, $offset: Int!) {
|
||||
category(id: $id, idType: DATABASE_ID) {
|
||||
posts(
|
||||
first: $size,
|
||||
offset: $offset,
|
||||
where: { orderby: { field: DATE, order: DESC } }
|
||||
) {
|
||||
pageInfo {
|
||||
offsetPagination {
|
||||
total
|
||||
hasMore
|
||||
hasPrevious
|
||||
}
|
||||
}
|
||||
nodes {
|
||||
__typename
|
||||
id
|
||||
databaseId
|
||||
title
|
||||
excerpt
|
||||
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 data = await fetchGraphQL(query, {
|
||||
id: categoryId,
|
||||
size: postsPerPage,
|
||||
offset
|
||||
});
|
||||
|
||||
return {
|
||||
posts: data?.category?.posts?.nodes || [],
|
||||
pageInfo: data?.category?.posts?.pageInfo?.offsetPagination || {
|
||||
total: 0,
|
||||
hasMore: false,
|
||||
hasPrevious: false
|
||||
}
|
||||
};
|
||||
},
|
||||
{ ttl: CACHE_TTL.POSTS }
|
||||
);
|
||||
}
|
||||
|
||||
export async function invalidateNodeCache(uri: string): Promise<void> {
|
||||
const normalizedUri = uri.startsWith('/') ? uri : `/${uri}`;
|
||||
const cacheKey = `node-by-uri:${normalizedUri}`;
|
||||
await cache.del(cacheKey);
|
||||
}
|
||||
|
||||
export async function invalidateCategoryCache(categoryId: number): Promise<void> {
|
||||
const pattern = `category-posts:${categoryId}:*`;
|
||||
await cache.delPattern(pattern);
|
||||
}
|
||||
Reference in New Issue
Block a user