All files / web/src/composables useApiKeyConfig.ts

0% Statements 0/44
100% Branches 1/1
100% Functions 1/1
0% Lines 0/44

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                                                                                                     
import type { ApiKeyConfig } from '@/types/generated/ApiKeyConfig'
 
/**
 * Composable for managing API key configuration
 * Provides utilities to build, extract, and compare ApiKeyConfig objects
 */
export function useApiKeyConfig() {
  /**
   * Build an ApiKeyConfig object from mode and value
   */
  const buildConfig = (mode: 'direct' | 'secret', value: string): ApiKeyConfig | null => {
    if (!value || !value.trim()) {
      return null
    }
 
    return {
      type: mode,
      value: value.trim()
    } as ApiKeyConfig
  }
 
  /**
   * Extract mode and value from ApiKeyConfig
   */
  const extractConfig = (config: ApiKeyConfig | null): { mode: 'direct' | 'secret'; value: string } => {
    if (!config) {
      return { mode: 'direct', value: '' }
    }
 
    return {
      mode: config.type,
      value: config.value
    }
  }
 
  /**
   * Check if two ApiKeyConfig objects are different
   */
  const isConfigChanged = (oldConfig: ApiKeyConfig | null, newConfig: ApiKeyConfig | null): boolean => {
    // Both null
    if (!oldConfig && !newConfig) {
      return false
    }
 
    // One is null
    if (!oldConfig || !newConfig) {
      return true
    }
 
    // Compare type and value
    return oldConfig.type !== newConfig.type || oldConfig.value !== newConfig.value
  }
 
  /**
   * Get display text for API key config
   */
  const getConfigDisplay = (config: ApiKeyConfig | null): string => {
    if (!config) {
      return 'Not configured'
    }
 
    if (config.type === 'direct') {
      return `Direct (${config.value.substring(0, 8)}...)`
    }
 
    return `Secret: ${config.value}`
  }
 
  return {
    buildConfig,
    extractConfig,
    isConfigChanged,
    getConfigDisplay
  }
}