All files / frontend/src/utils type-guards.ts

13.63% Statements 6/44
2.27% Branches 2/88
20% Functions 1/5
13.63% Lines 6/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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124                      1x 4x     1x           1x                                                                       1x                       1x                                                                                                            
import type { AccordionField } from "@quiqr/types";
 
// Types previously defined in HoForm/types.ts
export interface HugoConfigParsed {
  theme?: string;
  title?: string;
}
export interface QuiqrModelParsed {
  hugover?: string;
}
 
export const isValidAppThemeConfiguration = (config: unknown): config is { interfaceStyle: "quiqr10-light" | "quiqr10-dark" } => {
  return Boolean(typeof config === "object" && "interfaceStyle" in config && (config.interfaceStyle == "quiqr10-dark" || config.interfaceStyle == "quiqr10-light") );
}
 
export const isDynamicFormFieldConfig = (config: unknown): config is { fields : unknown[] } => {
  return typeof config === "object" 
         && 'fields' in config 
         && Array.isArray(config.fields)
}
 
export const isDynamicFormFieldConfigWithValues = (config: unknown): config is { fields : AccordionField[] } => {
  const isValidConfig = isDynamicFormFieldConfig(config);
  const hasValues = isValidConfig && config.fields.length > 0;
  if (!hasValues) {
    return true;
  }
 
  const firstField = config.fields[0];
  const isFieldBase = typeof firstField === 'object' 
                      && 'key' in firstField
                      && typeof firstField.key === 'string'
                      && 'compositeKey' in firstField
                      && typeof firstField.compositeKey === 'string'
                      && 'type' in firstField
                      && typeof firstField.type === 'string'
  return isFieldBase;
}
 
 
export interface HugoSiteDirResponse {
  Screenshot?: string;
  hugoConfigExists?: boolean;
  hugoConfigParsed?: HugoConfigParsed;
  quiqrModelParsed?: QuiqrModelParsed;
  dirName?: string;
  hugoThemesDirExists?: boolean;
  hugoContentDirExists?: boolean;
  hugoDataDirExists?: boolean;
}
 
export interface ErrorWithResponse {
  response: {
    status: number;
  };
}
 
export const isErrorWithResponse = (error: unknown): error is ErrorWithResponse => {
  return (
    error !== null &&
    typeof error === 'object' &&
    'response' in error &&
    error.response !== null &&
    typeof error.response === 'object' &&
    'status' in error.response &&
    typeof error.response.status === 'number'
  );
};
 
export const isHugoSiteDirResponse = (response: unknown): response is HugoSiteDirResponse => {
  if (typeof response !== "object" || response === null) {
    return false;
  }
 
  // Optional fields validation - using proper type narrowing
  if ("Screenshot" in response && response.Screenshot !== undefined && typeof response.Screenshot !== "string") {
    return false;
  }
 
  if ("hugoConfigExists" in response && response.hugoConfigExists !== undefined && typeof response.hugoConfigExists !== "boolean") {
    return false;
  }
 
  if ("hugoConfigParsed" in response && response.hugoConfigParsed !== undefined && response.hugoConfigParsed !== null) {
    const config = response.hugoConfigParsed;
    if (typeof config !== "object") {
      return false;
    }
    if ("theme" in config && config.theme !== undefined && typeof config.theme !== "string") {
      return false;
    }
    if ("title" in config && config.title !== undefined && typeof config.title !== "string") {
      return false;
    }
  }
 
  if ("quiqrModelParsed" in response && response.quiqrModelParsed !== undefined && response.quiqrModelParsed !== null) {
    const model = response.quiqrModelParsed;
    if (typeof model !== "object") {
      return false;
    }
    if ("hugover" in model && model.hugover !== undefined && typeof model.hugover !== "string") {
      return false;
    }
  }
 
  if ("dirName" in response && response.dirName !== undefined && typeof response.dirName !== "string") {
    return false;
  }
 
  if ("hugoThemesDirExists" in response && response.hugoThemesDirExists !== undefined && typeof response.hugoThemesDirExists !== "boolean") {
    return false;
  }
 
  if ("hugoContentDirExists" in response && response.hugoContentDirExists !== undefined && typeof response.hugoContentDirExists !== "boolean") {
    return false;
  }
 
  if ("hugoDataDirExists" in response && response.hugoDataDirExists !== undefined && typeof response.hugoDataDirExists !== "boolean") {
    return false;
  }
 
  return true;
}