@ai-growth/nextjs
Version:
Seamlessly integrate Sanity CMS with Next.js applications for automated blog routing and rendering
190 lines • 6.21 kB
TypeScript
import { type SanityClient } from '@sanity/client';
import { type SanityConfig } from './config';
/**
* Connection status result interface
*/
export interface ConnectionStatus {
/** Whether the connection was successful */
connected: boolean;
/** Error message if connection failed */
error?: string;
/** Connection latency in milliseconds */
latency?: number;
/** Timestamp when verification was performed */
timestamp: string;
/** Detected API version */
apiVersion?: string;
/** Environment where verification was performed */
environment: 'server' | 'client';
/** Details about the verification attempt */
details?: {
/** Number of retry attempts made */
retryAttempts?: number;
/** Type of query used for verification */
queryType?: 'system' | 'content' | 'config';
/** Whether timeout occurred */
timedOut?: boolean;
};
}
/**
* Creates a Sanity client instance using the configuration from environment variables.
*
* This function automatically configures the client based on the current environment:
* - Uses CDN in production when no token is available (read-only access)
* - Disables CDN when authenticated or in development (real-time data)
* - Handles both server-side and client-side environments
*
* @param options - Optional configuration overrides
* @param options.useCache - Whether to cache the client instance (default: true)
* @param options.configOverrides - Manual configuration overrides for testing
* @returns Configured Sanity client instance
*
* @example
* ```typescript
* // Basic usage - uses environment configuration
* const client = getSanityClient();
*
* // Disable caching for testing
* const client = getSanityClient({ useCache: false });
*
* // Manual configuration override
* const client = getSanityClient({
* configOverrides: { projectId: 'test-project' }
* });
* ```
*
* @throws {ConfigurationError} When environment configuration is invalid
*/
export declare function getSanityClient(options?: {
useCache?: boolean;
configOverrides?: Partial<SanityConfig>;
}): SanityClient;
/**
* Clears the cached Sanity client instance.
*
* This is useful for testing scenarios where you need to ensure
* a fresh client is created with different configuration.
*
* @example
* ```typescript
* // Clear cache before testing with different config
* clearSanityClientCache();
* const client = getSanityClient();
* ```
*/
export declare function clearSanityClientCache(): void;
/**
* Gets the current cached Sanity client instance without creating a new one.
*
* @returns The cached client instance or null if no client is cached
*
* @example
* ```typescript
* const cachedClient = getCachedSanityClient();
* if (cachedClient) {
* // Use existing client
* } else {
* // Create new client
* const client = getSanityClient();
* }
* ```
*/
export declare function getCachedSanityClient(): SanityClient | null;
/**
* Creates a new Sanity client instance without caching.
*
* This is useful when you need a fresh client instance,
* for example in testing or when working with multiple projects.
*
* @param configOverrides - Manual configuration overrides
* @returns New Sanity client instance
*
* @example
* ```typescript
* // Create fresh client for testing
* const testClient = createSanityClient({
* projectId: 'test-project',
* dataset: 'test'
* });
* ```
*/
export declare function createSanityClient(configOverrides?: Partial<SanityConfig>): SanityClient;
/**
* Verifies the connection to Sanity API by performing a lightweight query.
*
* This function tests the API connection by attempting a minimal query
* to the Sanity Content Lake. It measures latency, handles timeouts,
* and categorizes different types of connection failures.
*
* @param options - Configuration options for connection verification
* @param options.timeout - Request timeout in milliseconds (default: 10000)
* @param options.retryAttempts - Number of retry attempts for transient failures (default: 1)
* @param options.client - Custom client instance to use for testing
* @returns Promise resolving to connection status information
*
* @example
* ```typescript
* // Basic connection verification
* const status = await verifyConnection();
* if (status.connected) {
* console.log(`Connected! Latency: ${status.latency}ms`);
* } else {
* console.error(`Connection failed: ${status.error}`);
* }
*
* // Custom timeout and retry
* const status = await verifyConnection({
* timeout: 5000,
* retryAttempts: 2
* });
* ```
*/
export declare function verifyConnection(options?: {
timeout?: number;
retryAttempts?: number;
client?: SanityClient;
}): Promise<ConnectionStatus>;
/**
* Performs a quick connection health check with default settings.
*
* This is a convenience function that performs connection verification
* with sensible defaults for most use cases.
*
* @returns Promise resolving to boolean indicating connection status
*
* @example
* ```typescript
* if (await isConnected()) {
* console.log('Sanity API is accessible');
* } else {
* console.log('Cannot reach Sanity API');
* }
* ```
*/
export declare function isConnected(): Promise<boolean>;
/**
* Gets detailed connection information including configuration status.
*
* This function provides comprehensive connection diagnostics including
* configuration validation, API accessibility, and environment details.
*
* @param options - Configuration options
* @param options.includeConfig - Whether to include configuration details in response
* @returns Promise resolving to detailed connection information
*
* @example
* ```typescript
* const info = await getConnectionInfo({ includeConfig: true });
* console.log('Environment:', info.environment);
* console.log('Connected:', info.connected);
* if (info.apiVersion) {
* console.log('API Version:', info.apiVersion);
* }
* ```
*/
export declare function getConnectionInfo(options?: {
includeConfig?: boolean;
}): Promise<ConnectionStatus & {
configValid?: boolean;
}>;
//# sourceMappingURL=sanity-client.d.ts.map