/** * Google Search Console API Client * Handles sitemap submission, URL inspection, and indexing requests */ interface GSCConfig { serviceAccountEmail?: string privateKey?: string siteUrl: string } interface SubmitSitemapResponse { success: boolean message?: string error?: string } interface InspectUrlResponse { success: boolean indexed?: boolean error?: string } /** * Submit sitemap to Google Search Console * Uses the Search Console API v1 */ export async function submitSitemapToGSC( sitemapUrl: string, config: GSCConfig ): Promise { const { serviceAccountEmail, privateKey, siteUrl } = config if (!serviceAccountEmail || !privateKey) { return { success: false, error: "Google Search Console credentials not configured", } } try { // For Next.js, we'll use the API route approach // The actual API call will be made server-side via the API route // This function is a helper that can be called from API routes const response = await fetch( `${process.env.NEXT_PUBLIC_APP_URL || ""}/api/sitemap-submit`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ sitemapUrl, siteUrl, }), } ) if (!response.ok) { const error = await response.json() return { success: false, error: error.message || "Failed to submit sitemap", } } const data = await response.json() return { success: true, message: data.message || "Sitemap submitted successfully", } } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error occurred", } } } /** * Request URL indexing in Google Search Console */ export async function requestIndexing( url: string, config: GSCConfig ): Promise { const { serviceAccountEmail, privateKey } = config if (!serviceAccountEmail || !privateKey) { return { success: false, error: "Google Search Console credentials not configured", } } try { const response = await fetch( `${process.env.NEXT_PUBLIC_APP_URL || ""}/api/request-indexing`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ url, }), } ) if (!response.ok) { const error = await response.json() return { success: false, error: error.message || "Failed to request indexing", } } const data = await response.json() return { success: true, indexed: data.indexed || false, } } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error occurred", } } } /** * Get Google Search Console configuration from environment variables */ export function getGSCConfig(): GSCConfig { return { serviceAccountEmail: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL, privateKey: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, "\n"), siteUrl: process.env.NEXT_PUBLIC_SITE_URL || "https://rockymountainvending.com", } }