Skip to content

Commit ad4fb31

Browse files
committed
move utility functions to seperate util file
1 parent fa7bab4 commit ad4fb31

File tree

4 files changed

+83
-100
lines changed

4 files changed

+83
-100
lines changed

app/sitemap/[entityPage]/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { type NextRequest } from 'next/server';
33

44
import { ENTITY_CONFIG, getSiteMapConfig, isSitemapEnabled } from '@/lib/utils';
5-
import { getGraphqlEntityCount, getSearchEntityCount } from '../main.xml/route';
5+
import { getGraphqlEntityCount, getSearchEntityCount } from '@/lib/sitemap-utils';
66

77
interface EntityItem {
88
id: string;

app/sitemap/main.xml/route.ts

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,9 @@
11
// app/sitemap.xml/route.ts
2-
import { type NextRequest } from 'next/server';
3-
42
import {
5-
ENTITY_CONFIG,
6-
ENTITY_CONFIG_TYPE,
73
getSiteMapConfig,
84
isSitemapEnabled,
95
} from '@/lib/utils';
10-
11-
const getAllEntityCounts = async (): Promise<Record<string, number>> => {
12-
const counts: Record<string, number> = {};
13-
14-
const countPromises: Promise<{ entityName: string; count: number }>[] = [];
15-
16-
Object.entries(ENTITY_CONFIG).forEach(([entityName, config]) => {
17-
if (config.source === 'graphql' && config.graphqlQuery) {
18-
countPromises.push(getGraphqlEntityCount(entityName, config));
19-
}
20-
if (config.source === 'search' && config.endpoint) {
21-
countPromises.push(getSearchEntityCount(entityName, 5, 1));
22-
}
23-
});
24-
25-
const results = await Promise.all(countPromises);
26-
27-
results.forEach(({ entityName, count }) => {
28-
counts[entityName] = count;
29-
});
30-
31-
return counts;
32-
};
33-
34-
export async function getGraphqlEntityCount(
35-
entity: string,
36-
config: ENTITY_CONFIG_TYPE[string]
37-
): Promise<{ entityName: string; count: number; list: any }> {
38-
try {
39-
const response = await fetch(
40-
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}/graphql`,
41-
{
42-
method: 'POST',
43-
headers: {
44-
'Content-Type': 'application/json',
45-
},
46-
body: JSON.stringify({
47-
query: config.graphqlQuery,
48-
variables: {},
49-
}),
50-
}
51-
);
52-
const data = await response.json();
53-
54-
return {
55-
entityName: entity,
56-
count: data?.data?.[config.queryResKey as string]?.length || 0,
57-
list: data?.data?.[config.queryResKey as string] || [],
58-
};
59-
} catch (error) {
60-
console.error(`Error fetching count for ${entity}:`, error);
61-
return { entityName: entity, count: 0, list: [] };
62-
}
63-
}
64-
65-
export async function getSearchEntityCount(
66-
entity: string,
67-
size: number,
68-
page: number
69-
): Promise<{ entityName: string; count: number; list: any }> {
70-
try {
71-
const config = ENTITY_CONFIG[entity];
72-
const response = await fetch(
73-
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}${config.endpoint}?sort=recent&size=${size}&page=${page}`,
74-
{
75-
method: 'GET',
76-
headers: {
77-
'Content-Type': 'application/json',
78-
},
79-
next: { revalidate: 3600 },
80-
}
81-
);
82-
const data = await response.json();
83-
return { entityName: entity, count: data.total, list: data.results };
84-
} catch (error) {
85-
console.error(`Error fetching count for ${entity}:`, error);
86-
return { entityName: entity, count: 0, list: [] };
87-
}
88-
}
6+
import { getAllEntityCounts } from '@/lib/sitemap-utils';
897

908
function generateStaticUrls(): string {
919
const baseUrl = process.env.NEXTAUTH_URL;
@@ -129,7 +47,7 @@ function generateSitemapIndex(
12947
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${staticUrls}\n${sitemapEntries}\n</urlset>`;
13048
}
13149

132-
export async function GET(request: NextRequest) {
50+
export async function GET() {
13351
// Check if sitemaps are enabled via feature flag
13452
if (!isSitemapEnabled()) {
13553
return new Response('Sitemaps are not enabled', { status: 404 });

lib/sitemap-utils.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { ENTITY_CONFIG, ENTITY_CONFIG_TYPE } from '@/lib/utils';
2+
3+
export async function getGraphqlEntityCount(
4+
entity: string,
5+
config: ENTITY_CONFIG_TYPE[string]
6+
): Promise<{ entityName: string; count: number; list: any }> {
7+
try {
8+
const response = await fetch(
9+
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}/graphql`,
10+
{
11+
method: 'POST',
12+
headers: {
13+
'Content-Type': 'application/json',
14+
},
15+
body: JSON.stringify({
16+
query: config.graphqlQuery,
17+
variables: {},
18+
}),
19+
}
20+
);
21+
const data = await response.json();
22+
23+
return {
24+
entityName: entity,
25+
count: data?.data?.[config.queryResKey as string]?.length || 0,
26+
list: data?.data?.[config.queryResKey as string] || [],
27+
};
28+
} catch (error) {
29+
console.error(`Error fetching count for ${entity}:`, error);
30+
return { entityName: entity, count: 0, list: [] };
31+
}
32+
}
33+
34+
export async function getSearchEntityCount(
35+
entity: string,
36+
size: number,
37+
page: number
38+
): Promise<{ entityName: string; count: number; list: any }> {
39+
try {
40+
const config = ENTITY_CONFIG[entity];
41+
const response = await fetch(
42+
`${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}${config.endpoint}?sort=recent&size=${size}&page=${page}`,
43+
{
44+
method: 'GET',
45+
headers: {
46+
'Content-Type': 'application/json',
47+
},
48+
next: { revalidate: 3600 },
49+
}
50+
);
51+
const data = await response.json();
52+
return { entityName: entity, count: data.total, list: data.results };
53+
} catch (error) {
54+
console.error(`Error fetching count for ${entity}:`, error);
55+
return { entityName: entity, count: 0, list: [] };
56+
}
57+
}
58+
59+
export const getAllEntityCounts = async (): Promise<Record<string, number>> => {
60+
const counts: Record<string, number> = {};
61+
62+
const countPromises: Promise<{ entityName: string; count: number }>[] = [];
63+
64+
Object.entries(ENTITY_CONFIG).forEach(([entityName, config]) => {
65+
if (config.source === 'graphql' && config.graphqlQuery) {
66+
countPromises.push(getGraphqlEntityCount(entityName, config));
67+
}
68+
if (config.source === 'search' && config.endpoint) {
69+
countPromises.push(getSearchEntityCount(entityName, 5, 1));
70+
}
71+
});
72+
73+
const results = await Promise.all(countPromises);
74+
75+
results.forEach(({ entityName, count }) => {
76+
counts[entityName] = count;
77+
});
78+
79+
return counts;
80+
};

lib/utils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Metadata } from 'next';
2-
import { Metadata } from 'next';
32
import { twMerge, type ClassNameValue } from 'tailwind-merge';
43

54
type MetadataOptions = {
@@ -144,26 +143,12 @@ export function formatDateString(
144143
date.toLocaleDateString('en-US', {
145144
year: 'numeric',
146145
month: 'numeric',
147-
// day: 'numeric',
148-
})
149-
)
150-
.toISOString()
151-
.split('T')[0]
152-
date.toLocaleDateString('en-US', {
153-
year: 'numeric',
154-
month: 'numeric',
155-
// day: 'numeric',
156146
})
157147
)
158148
.toISOString()
159149
.split('T')[0]
160150
: date.toLocaleDateString('en-US', {
161151
month: 'long',
162-
// day: 'numeric',
163-
year: 'numeric',
164-
});
165-
month: 'long',
166-
// day: 'numeric',
167152
year: 'numeric',
168153
});
169154
}

0 commit comments

Comments
 (0)