Return

All tests / src/helpers searchConsole.ts

100% Statements 9/9
100% Branches 4/4
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33            1x   1x                 3x 3x 2x     1x               1x    
/*
 * The Search Console package alone, not the all-APIs `googleapis` entry point. `googleapis` ships
 * declarations for 339 APIs and `import { google }` loads every one of them into the type checker:
 * with octokit 5 beside it, the type check inside `next build` ran out of the default 2 GB heap.
 * `@googleapis/webmasters` is the same client class and the same `auth` helper, scoped to this API.
 */
import { auth, webmasters, type webmasters_v3 } from '@googleapis/webmasters';
 
export const SITE_URL = 'sc-domain:xabierlameiro.com';
 
/**
 * @description Build an authenticated Search Console (webmasters v3) client using the
 * shared analytics service account. Returns null when the credentials are absent so
 * callers can degrade gracefully instead of throwing.
 *
 * @returns {webmasters_v3.Webmasters | null} - Authenticated client, or null if unconfigured
 */
export const getSearchConsoleClient = (): webmasters_v3.Webmasters | null => {
    if (!process.env.ANALYTICS_CLIENT_EMAIL || !process.env.ANALYTICS_PRIVATE_KEY) {
        return null;
    }
 
    const googleAuth = new auth.GoogleAuth({
        credentials: {
            client_email: process.env.ANALYTICS_CLIENT_EMAIL,
            private_key: process.env.ANALYTICS_PRIVATE_KEY?.replace(/\\n/g, '\n'),
        },
        scopes: 'https://www.googleapis.com/auth/webmasters.readonly',
    });
 
    return webmasters({ version: 'v3', auth: googleAuth });
};