UNPKG

@ai-growth/nextjs

Version:

Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering

578 lines 18.4 kB
import { z } from 'zod'; /** * Schema for Sanity CMS configuration */ declare const sanityConfigSchema: z.ZodObject<{ /** * Sanity project ID - unique identifier for your Sanity project * @example "abc123def" */ projectId: z.ZodString; /** * Sanity dataset name - typically 'production', 'development', or 'staging' * @example "production" */ dataset: z.ZodString; /** * Optional Sanity API token for authenticated requests * Required for write operations or accessing private datasets */ apiToken: z.ZodOptional<z.ZodString>; /** * Sanity API version - determines which version of the API to use * @default "2023-05-03" */ apiVersion: z.ZodDefault<z.ZodString>; }, "strip", z.ZodTypeAny, { projectId: string; dataset: string; apiVersion: string; apiToken?: string | undefined; }, { projectId: string; dataset: string; apiToken?: string | undefined; apiVersion?: string | undefined; }>; /** * Schema for CMS routing configuration */ declare const cmsConfigSchema: z.ZodObject<{ /** * Base route path for CMS pages * Must start and end with forward slashes * @default "/cms/" * @example "/blog/", "/content/" */ routePath: z.ZodEffects<z.ZodDefault<z.ZodString>, string, string | undefined>; }, "strip", z.ZodTypeAny, { routePath: string; }, { routePath?: string | undefined; }>; /** * Complete configuration schema for the AI Growth CMS library */ export declare const configSchema: z.ZodObject<{ sanity: z.ZodObject<{ /** * Sanity project ID - unique identifier for your Sanity project * @example "abc123def" */ projectId: z.ZodString; /** * Sanity dataset name - typically 'production', 'development', or 'staging' * @example "production" */ dataset: z.ZodString; /** * Optional Sanity API token for authenticated requests * Required for write operations or accessing private datasets */ apiToken: z.ZodOptional<z.ZodString>; /** * Sanity API version - determines which version of the API to use * @default "2023-05-03" */ apiVersion: z.ZodDefault<z.ZodString>; }, "strip", z.ZodTypeAny, { projectId: string; dataset: string; apiVersion: string; apiToken?: string | undefined; }, { projectId: string; dataset: string; apiToken?: string | undefined; apiVersion?: string | undefined; }>; cms: z.ZodObject<{ /** * Base route path for CMS pages * Must start and end with forward slashes * @default "/cms/" * @example "/blog/", "/content/" */ routePath: z.ZodEffects<z.ZodDefault<z.ZodString>, string, string | undefined>; }, "strip", z.ZodTypeAny, { routePath: string; }, { routePath?: string | undefined; }>; }, "strip", z.ZodTypeAny, { sanity: { projectId: string; dataset: string; apiVersion: string; apiToken?: string | undefined; }; cms: { routePath: string; }; }, { sanity: { projectId: string; dataset: string; apiToken?: string | undefined; apiVersion?: string | undefined; }; cms: { routePath?: string | undefined; }; }>; /** * TypeScript type for the complete configuration */ export type Config = z.infer<typeof configSchema>; /** * TypeScript type for Sanity-specific configuration */ export type SanityConfig = z.infer<typeof sanityConfigSchema>; /** * TypeScript type for CMS-specific configuration */ export type CmsConfig = z.infer<typeof cmsConfigSchema>; /** * Schema for client-safe configuration (browser-accessible) * Only includes non-sensitive data that can be safely exposed to the client */ declare const clientConfigSchema: z.ZodObject<{ cms: z.ZodObject<{ routePath: z.ZodEffects<z.ZodDefault<z.ZodString>, string, string | undefined>; }, "strip", z.ZodTypeAny, { routePath: string; }, { routePath?: string | undefined; }>; sanity: z.ZodObject<{ projectId: z.ZodOptional<z.ZodString>; dataset: z.ZodOptional<z.ZodString>; apiVersion: z.ZodDefault<z.ZodString>; }, "strip", z.ZodTypeAny, { apiVersion: string; projectId?: string | undefined; dataset?: string | undefined; }, { projectId?: string | undefined; dataset?: string | undefined; apiVersion?: string | undefined; }>; }, "strip", z.ZodTypeAny, { sanity: { apiVersion: string; projectId?: string | undefined; dataset?: string | undefined; }; cms: { routePath: string; }; }, { sanity: { projectId?: string | undefined; dataset?: string | undefined; apiVersion?: string | undefined; }; cms: { routePath?: string | undefined; }; }>; /** * TypeScript type for client-safe configuration */ export type ClientConfig = z.infer<typeof clientConfigSchema>; /** * Environment variable names used by the configuration system */ export declare const ENV_VARS: { readonly SANITY_PROJECT_ID: "SANITY_PROJECT_ID"; readonly SANITY_DATASET: "SANITY_DATASET"; readonly SANITY_API_TOKEN: "SANITY_API_TOKEN"; readonly SANITY_API_VERSION: "SANITY_API_VERSION"; readonly CMS_ROUTE_PATH: "CMS_ROUTE_PATH"; readonly NEXT_PUBLIC_SANITY_PROJECT_ID: "NEXT_PUBLIC_SANITY_PROJECT_ID"; readonly NEXT_PUBLIC_SANITY_DATASET: "NEXT_PUBLIC_SANITY_DATASET"; readonly NEXT_PUBLIC_SANITY_API_VERSION: "NEXT_PUBLIC_SANITY_API_VERSION"; readonly NEXT_PUBLIC_CMS_ROUTE_PATH: "NEXT_PUBLIC_CMS_ROUTE_PATH"; }; /** * Error class for configuration validation failures */ export declare class ConfigurationError extends Error { details?: z.ZodError | undefined; constructor(message: string, details?: z.ZodError | undefined); } /** * Loads and validates configuration from environment variables * * @param options Configuration loading options * @param options.useCache Whether to use cached configuration (default: true) * @param options.env Custom environment object (default: process.env) * @returns Validated configuration object * @throws ConfigurationError when validation fails * * @example * ```typescript * // Load configuration with caching * const config = getConfig(); * console.log(config.sanity.projectId); * * // Force reload without cache * const freshConfig = getConfig({ useCache: false }); * * // Use custom environment (useful for testing) * const testConfig = getConfig({ * env: { SANITY_PROJECT_ID: 'test', SANITY_DATASET: 'test' } * }); * ``` */ export declare function getConfig(options?: { useCache?: boolean; env?: Record<string, string | undefined>; }): Config; /** * Clears the configuration cache * Useful for testing or when environment variables change at runtime */ export declare function clearConfigCache(): void; /** * Gets the complete Sanity configuration object * * @param options Configuration loading options * @returns Sanity configuration object with projectId, dataset, apiToken, and apiVersion * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * const sanityConfig = getSanityConfig(); * console.log(sanityConfig.projectId, sanityConfig.dataset); * ``` */ export declare function getSanityConfig(options?: Parameters<typeof getConfig>[0]): SanityConfig; /** * Gets the CMS route path * * @param options Configuration loading options * @returns The base route path for CMS pages (e.g., "/cms/") * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * const routePath = getCmsRoutePath(); * // Use in Next.js router: router.push(`${routePath}posts`) * ``` */ export declare function getCmsRoutePath(options?: Parameters<typeof getConfig>[0]): string; /** * Gets configuration specific to the Sanity client initialization * Optimized for CDN usage with published content when no token is available * * @param options Configuration loading options * @returns Sanity client configuration object * * @example * ```typescript * const clientConfig = getSanityClientConfig(); * const sanityClient = createClient(clientConfig); * ``` */ export declare function getSanityClientConfig(options?: Parameters<typeof getConfig>[0]): { projectId: string; dataset: string; apiVersion: string; token?: string; useCdn?: boolean; }; /** * Checks if a Sanity API token is available * Useful for conditional features that require authentication * * @param options Configuration loading options * @returns true if API token is configured, false otherwise * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * if (isSanityTokenAvailable()) { * // Enable write operations * await client.create(document); * } else { * // Read-only mode * console.log('Running in read-only mode'); * } * ``` */ export declare function isSanityTokenAvailable(options?: Parameters<typeof getConfig>[0]): boolean; /** * Gets the Sanity API version string * * @param options Configuration loading options * @returns The API version string (e.g., "2023-05-03") * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * const apiVersion = getApiVersion(); * // Use in direct API calls: `https://projectId.api.sanity.io/v${apiVersion}/...` * ``` */ export declare function getApiVersion(options?: Parameters<typeof getConfig>[0]): string; /** * Gets the Sanity project ID * * @param options Configuration loading options * @returns The Sanity project ID string * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * const projectId = getProjectId(); * // Use for custom API endpoints or analytics * ``` */ export declare function getProjectId(options?: Parameters<typeof getConfig>[0]): string; /** * Gets the Sanity dataset name * * @param options Configuration loading options * @returns The dataset name (e.g., "production", "development") * @throws ConfigurationError when configuration is invalid * * @example * ```typescript * const dataset = getDataset(); * // Use for environment-specific logic * if (dataset === 'development') { * console.log('Running in development mode'); * } * ``` */ export declare function getDataset(options?: Parameters<typeof getConfig>[0]): string; /** * Detects if the code is running in a browser environment */ export declare function isClient(): boolean; /** * Detects if the code is running in a server environment */ export declare function isServer(): boolean; /** * Gets client-safe configuration for browser environment * Automatically falls back from NEXT_PUBLIC_ variables to regular variables if not found * * @param options Configuration loading options * @returns Client-safe configuration object * * @example * ```typescript * const clientConfig = getClientConfig(); * const routePath = clientConfig.cms.routePath; // '/cms/' * const projectId = clientConfig.sanity.projectId; // Uses NEXT_PUBLIC_SANITY_PROJECT_ID or SANITY_PROJECT_ID * const dataset = clientConfig.sanity.dataset; // Uses NEXT_PUBLIC_SANITY_DATASET or SANITY_DATASET * const apiVersion = clientConfig.sanity.apiVersion; // '2023-05-03' * ``` */ export declare function getClientConfig(options?: { useCache?: boolean; env?: Record<string, string | undefined>; }): ClientConfig; /** * Gets configuration specifically from NEXT_PUBLIC_ environment variables * These are the only variables available in the browser in Next.js * * @param options Configuration loading options * @returns Public configuration object with only NEXT_PUBLIC_ variables * * @example * ```typescript * // Only works with NEXT_PUBLIC_ prefixed variables * const publicConfig = getPublicConfig(); * ``` */ export declare function getPublicConfig(options?: { useCache?: boolean; env?: Record<string, string | undefined>; }): ClientConfig; /** * Clears the client configuration cache * Useful for testing or when environment variables change at runtime */ export declare function clearClientConfigCache(): void; /** * Gets a configuration object appropriate for the current environment * Returns full configuration on server, client-safe configuration in browser * * @param options Configuration loading options * @returns Configuration object appropriate for current environment * * @example * ```typescript * // Automatically chooses the right config based on environment * const config = getEnvironmentConfig(); * * // On server: includes projectId, dataset, apiToken, etc. * // In browser: only includes apiVersion and routePath * ``` */ export declare function getEnvironmentConfig(options?: { useCache?: boolean; env?: Record<string, string | undefined>; }): ClientConfig | Config; /** * Helper function to get CMS route path that works in both environments * Uses client-safe configuration in browser, full configuration on server * * @param options Configuration loading options * @returns CMS route path string * * @example * ```typescript * // Works in both server and client * const routePath = getCmsRoutePathSafe(); * ``` */ export declare function getCmsRoutePathSafe(options?: Parameters<typeof getClientConfig>[0]): string; /** * Helper function to get API version that works in both environments * Uses client-safe configuration in browser, full configuration on server * * @param options Configuration loading options * @returns API version string * * @example * ```typescript * // Works in both server and client * const apiVersion = getApiVersionSafe(); * ``` */ export declare function getApiVersionSafe(options?: Parameters<typeof getClientConfig>[0]): string; /** * Helper function to get project ID that works in both environments * Uses client-safe configuration in browser (if available), full configuration on server * * @param options Configuration loading options * @returns Project ID string or undefined if not available in client environment * * @example * ```typescript * // Works in both server and client * const projectId = getProjectIdSafe(); * if (projectId) { * // Use project ID * } * ``` */ export declare function getProjectIdSafe(options?: Parameters<typeof getClientConfig>[0]): string | undefined; /** * Helper function to get dataset that works in both environments * Uses client-safe configuration in browser (if available), full configuration on server * * @param options Configuration loading options * @returns Dataset string or undefined if not available in client environment * * @example * ```typescript * // Works in both server and client * const dataset = getDatasetSafe(); * if (dataset) { * // Use dataset * } * ``` */ export declare function getDatasetSafe(options?: Parameters<typeof getClientConfig>[0]): string | undefined; /** * Configuration validation result */ export type ConfigValidationResult = { isValid: boolean; errors: string[]; warnings: string[]; config?: Config | ClientConfig; environment: 'server' | 'client'; }; /** * Validates configuration at startup and provides detailed feedback * Useful for verifying all required configuration is present before the application starts * * @param options Validation options * @param options.env Custom environment object (default: process.env) * @param options.throwOnError Whether to throw an error if validation fails (default: false) * @returns Detailed validation result with errors, warnings, and config if valid * * @example * ```typescript * // Validate configuration at app startup * const result = validateConfiguration(); * if (!result.isValid) { * console.error('Configuration errors:', result.errors); * process.exit(1); * } * * // In development, show warnings too * if (result.warnings.length > 0) { * console.warn('Configuration warnings:', result.warnings); * } * ``` */ export declare function validateConfiguration(options?: { env?: Record<string, string | undefined>; throwOnError?: boolean; }): ConfigValidationResult; /** * Gets the current configuration status and health * Provides a quick overview of configuration state without throwing errors * * @param options Configuration options * @returns Configuration status information * * @example * ```typescript * const status = getConfigurationStatus(); * console.log(`Environment: ${status.environment}`); * console.log(`Valid: ${status.isValid}`); * console.log(`Has Token: ${status.hasApiToken}`); * ``` */ export declare function getConfigurationStatus(options?: { env?: Record<string, string | undefined>; }): { environment: 'server' | 'client'; isValid: boolean; hasApiToken: boolean; hasPublicVariables: boolean; configuredVariables: string[]; missingRequiredVariables: string[]; }; /** * Debug utility to display current configuration state * Useful during development to understand what configuration is loaded * * @param options Debug options * @param options.env Custom environment object (default: process.env) * @param options.showValues Whether to show actual values (default: false for security) * @param options.maskSensitive Whether to mask sensitive values (default: true) * @returns Debug information object * * @example * ```typescript * // Safe debugging (masks sensitive values) * const debug = debugConfiguration(); * console.log('Debug Info:', debug); * * // Development debugging (shows values but still masks sensitive) * const debugWithValues = debugConfiguration({ showValues: true }); * console.log('Config Values:', debugWithValues); * ``` */ export declare function debugConfiguration(options?: { env?: Record<string, string | undefined>; showValues?: boolean; maskSensitive?: boolean; }): { environment: string; timestamp: string; configurationStatus: ReturnType<typeof getConfigurationStatus>; validationResult: ConfigValidationResult; environmentVariables: Record<string, string | undefined>; }; export {}; //# sourceMappingURL=config.d.ts.map