Refactor Google Trends fetching to use a default country code and simplify environment variable validation
This commit is contained in:
parent
0289fc44df
commit
527e46453d
@ -1,7 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { generateObject } from 'ai';
|
import { generateObject } from 'ai';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { geolocation } from '@vercel/functions';
|
|
||||||
import { xai } from '@ai-sdk/xai';
|
import { xai } from '@ai-sdk/xai';
|
||||||
|
|
||||||
export interface TrendingQuery {
|
export interface TrendingQuery {
|
||||||
@ -16,7 +15,7 @@ interface RedditPost {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchGoogleTrends(countryCode: string = 'US'): Promise<TrendingQuery[]> {
|
async function fetchGoogleTrends(): Promise<TrendingQuery[]> {
|
||||||
const fetchTrends = async (geo: string): Promise<TrendingQuery[]> => {
|
const fetchTrends = async (geo: string): Promise<TrendingQuery[]> => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`https://trends.google.com/trends/trendingsearches/daily/rss?geo=${geo}`, {
|
const response = await fetch(`https://trends.google.com/trends/trendingsearches/daily/rss?geo=${geo}`, {
|
||||||
@ -62,7 +61,7 @@ async function fetchGoogleTrends(countryCode: string = 'US'): Promise<TrendingQu
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const trends = await fetchTrends(countryCode);
|
const trends = await fetchTrends("US");
|
||||||
|
|
||||||
return [ ...trends];
|
return [ ...trends];
|
||||||
}
|
}
|
||||||
@ -95,11 +94,11 @@ async function fetchRedditQuestions(): Promise<TrendingQuery[]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchFromMultipleSources(countryCode: string) {
|
async function fetchFromMultipleSources() {
|
||||||
const [googleTrends,
|
const [googleTrends,
|
||||||
// redditQuestions
|
// redditQuestions
|
||||||
] = await Promise.all([
|
] = await Promise.all([
|
||||||
fetchGoogleTrends(countryCode),
|
fetchGoogleTrends(),
|
||||||
// fetchRedditQuestions(),
|
// fetchRedditQuestions(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -112,11 +111,11 @@ async function fetchFromMultipleSources(countryCode: string) {
|
|||||||
|
|
||||||
export async function GET(req: Request) {
|
export async function GET(req: Request) {
|
||||||
try {
|
try {
|
||||||
const countryCode = geolocation(req).countryRegion ?? 'US';
|
const trends = await fetchFromMultipleSources();
|
||||||
const trends = await fetchFromMultipleSources(countryCode);
|
|
||||||
|
|
||||||
if (trends.length === 0) {
|
if (trends.length === 0) {
|
||||||
// Fallback queries if both sources fail
|
// Fallback queries if both sources fail
|
||||||
|
console.error('Both sources failed to fetch trends, returning fallback queries');
|
||||||
return NextResponse.json([
|
return NextResponse.json([
|
||||||
{
|
{
|
||||||
icon: 'sparkles',
|
icon: 'sparkles',
|
||||||
|
|||||||
2
env/client.ts
vendored
2
env/client.ts
vendored
@ -7,7 +7,7 @@ export const clientEnv = createEnv({
|
|||||||
NEXT_PUBLIC_MAPBOX_TOKEN: z.string().min(1),
|
NEXT_PUBLIC_MAPBOX_TOKEN: z.string().min(1),
|
||||||
NEXT_PUBLIC_POSTHOG_KEY: z.string().min(1),
|
NEXT_PUBLIC_POSTHOG_KEY: z.string().min(1),
|
||||||
NEXT_PUBLIC_POSTHOG_HOST: z.string().min(1).url(),
|
NEXT_PUBLIC_POSTHOG_HOST: z.string().min(1).url(),
|
||||||
NEXT_PUBLIC_GOOGLE_MAPS_API_KEY: z.string().min(1).url(),
|
NEXT_PUBLIC_GOOGLE_MAPS_API_KEY: z.string().min(1),
|
||||||
},
|
},
|
||||||
runtimeEnv: {
|
runtimeEnv: {
|
||||||
NEXT_PUBLIC_MAPBOX_TOKEN: process.env.NEXT_PUBLIC_MAPBOX_TOKEN,
|
NEXT_PUBLIC_MAPBOX_TOKEN: process.env.NEXT_PUBLIC_MAPBOX_TOKEN,
|
||||||
|
|||||||
8106
pnpm-lock.yaml
8106
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user