All files / src/utils/typeGuards laboratoriumdiscounter.ts

100% Statements 90/90
100% Branches 9/9
100% Functions 5/5
100% Lines 90/90

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 3611x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                                                                           1x 1x 26x 26x 22x 22x 4x 10x   4x   1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                             1x 1x 15x 15x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                         1x 1x 25x 25x   1x 1x 1x 1x 1x     1x 1x 1x 1x 1x   1x                                                                         1x 1x 19x 19x   1x 1x 1x 1x   1x                                                             1x 1x 20x 20x  
import * as v from 'valibot';
 
const searchResponseOkSchema = v.object({
  page: v.object({
    search: v.string(),
    session_id: v.string(),
    key: v.string(),
    title: v.string(),
    status: v.number(),
  }),
  request: v.object({
    url: v.string(),
    method: v.string(),
    get: v.record(v.string(), v.unknown()),
    device: v.record(v.string(), v.unknown()),
  }),
  collection: v.object({
    products: v.record(v.string(), v.unknown()),
  }),
});
 
/**
 * Type guard to validate if a response from the Laboratorium Discounter search API is valid.
 * Checks for the presence and correct types of all required properties including page info,
 * request details, and a valid collection of products.
 *
 * @category Typeguards
 * @param response - Response object to validate
 * @returns Type predicate indicating if response is a valid SearchResponse
 * @example
 * ```typescript
 * // Valid search response
 * const validResponse = {
 *   page: {
 *     search: "sodium chloride",
 *     session_id: "abc123",
 *     key: "search_key",
 *     title: "Search Results",
 *     status: 200
 *   },
 *   request: {
 *     url: "/en/search/sodium-chloride",
 *     method: "GET",
 *     get: { q: "sodium chloride" },
 *     device: { platform: "osx", type: "webkit", mobile: false }
 *   },
 *   collection: {
 *     products: {
 *       "12345": {
 *         id: 12345,
 *         vid: 67890,
 *         image: 1,
 *         brand: false,
 *         code: "CHEM-001",
 *         ean: "1234567890123",
 *         sku: "SKU-001",
 *         score: 1.0,
 *         available: true,
 *         unit: true,
 *         url: "/products/chemical-1",
 *         title: "Sodium Chloride",
 *         fulltitle: "Sodium Chloride 500g",
 *         variant: "500g",
 *         description: "High purity sodium chloride",
 *         data_01: "Additional info",
 *         price: {
 *           price: 29.99,
 *           price_incl: 29.99,
 *           price_excl: 24.79,
 *           price_old: 39.99,
 *           price_old_incl: 39.99,
 *           price_old_excl: 33.05
 *         }
 *       }
 *     }
 *   }
 * };
 *
 * if (isSearchResponseOk(validResponse)) {
 *   console.log("Valid search response");
 *   console.log("Number of products:", Object.keys(validResponse.collection.products).length);
 *   console.log("Search query:", validResponse.page.search);
 * } else {
 *   console.error("Invalid response structure");
 * }
 *
 * // Invalid search response (missing required properties)
 * const invalidResponse = {
 *   page: { search: "sodium chloride" },
 *   collection: { products: {} }
 *   // Missing request object
 * };
 * if (!isSearchResponseOk(invalidResponse)) {
 *   console.error("Invalid response - missing required properties");
 * }
 *
 * // Invalid search response (wrong types)
 * const wrongTypes = {
 *   page: "not an object",
 *   request: "not an object",
 *   collection: "not an object"
 * };
 * if (!isSearchResponseOk(wrongTypes)) {
 *   console.error("Invalid response - wrong property types");
 * }
 * ```
 * @source
 */
export function isSearchResponseOk(response: unknown): response is SearchResponse {
  const parsed = v.safeParse(searchResponseOkSchema, response);
  if (!parsed.success) {
    return false;
  }
  return Object.values(parsed.output.collection.products).every((product) =>
    isSearchResponseProduct(product),
  );
}
 
const priceObjectSchema = v.object({
  price: v.number(),
  price_incl: v.number(),
  price_excl: v.number(),
  price_old: v.number(),
  price_old_incl: v.number(),
  price_old_excl: v.number(),
});
 
/**
 * Type guard to validate if an object has the correct structure for a Laboratorium Discounter price object.
 * Checks for the presence and correct types of all required price properties including
 * regular prices and old prices (for items on sale).
 *
 * @category Typeguards
 * @param price - Object to validate as PriceObject
 * @returns Type predicate indicating if price is a valid PriceObject
 * @example
 * ```typescript
 * // Valid price object (regular price)
 * const regularPrice = {
 *   price: 29.99,
 *   price_incl: 29.99,
 *   price_excl: 24.79,
 *   price_old: 29.99,
 *   price_old_incl: 29.99,
 *   price_old_excl: 24.79
 * };
 *
 * if (isPriceObject(regularPrice)) {
 *   console.log("Valid regular price:", regularPrice.price);
 * }
 *
 * // Valid price object (sale price)
 * const salePrice = {
 *   price: 29.99,
 *   price_incl: 29.99,
 *   price_excl: 24.79,
 *   price_old: 39.99,
 *   price_old_incl: 39.99,
 *   price_old_excl: 33.05
 * };
 *
 * if (isPriceObject(salePrice)) {
 *   console.log("Valid sale price:", salePrice.price);
 *   console.log("Original price:", salePrice.price_old);
 * }
 *
 * // Invalid price object (missing properties)
 * const missingProps = {
 *   price: 29.99,
 *   price_incl: 29.99
 *   // Missing other required properties
 * };
 * if (!isPriceObject(missingProps)) {
 *   console.error("Invalid price - missing required properties");
 * }
 *
 * // Invalid price object (wrong types)
 * const wrongTypes = {
 *   price: "29.99", // Should be number
 *   price_incl: "29.99", // Should be number
 *   price_excl: "24.79", // Should be number
 *   price_old: "39.99", // Should be number
 *   price_old_incl: "39.99", // Should be number
 *   price_old_excl: "33.05" // Should be number
 * };
 * if (!isPriceObject(wrongTypes)) {
 *   console.error("Invalid price - wrong property types");
 * }
 * ```
 * @source
 */
export function isPriceObject(price: unknown): price is PriceObject {
  return v.safeParse(priceObjectSchema, price).success;
}
 
const searchResponseProductSchema = v.object({
  id: v.number(),
  vid: v.number(),
  image: v.number(),
  brand: v.boolean(),
  code: v.string(),
  ean: v.string(),
  sku: v.string(),
  score: v.number(),
  available: v.boolean(),
  unit: v.boolean(),
  url: v.string(),
  title: v.string(),
  fulltitle: v.string(),
  variant: v.string(),
  description: v.string(),
  data_01: v.string(),
  price: priceObjectSchema,
});
 
/**
 * Type guard to validate if an object has the correct structure for a Laboratorium Discounter search response product.
 * Checks for the presence and correct types of all required product properties including
 * basic info, availability, and a valid price object.
 *
 * @category Typeguards
 * @param product - Object to validate as SearchResponseProduct
 * @returns Type predicate indicating if product is a valid SearchResponseProduct
 * @example
 * ```typescript
 * // Valid search response product
 * const validProduct = {
 *   id: 12345,
 *   vid: 67890,
 *   image: 1,
 *   brand: false,
 *   code: "CHEM-001",
 *   ean: "1234567890123",
 *   sku: "SKU-001",
 *   score: 1.0,
 *   available: true,
 *   unit: true,
 *   url: "/products/chemical-1",
 *   title: "Sodium Chloride",
 *   fulltitle: "Sodium Chloride 500g",
 *   variant: "500g",
 *   description: "High purity sodium chloride",
 *   data_01: "Additional info",
 *   price: {
 *     price: 29.99,
 *     price_incl: 29.99,
 *     price_excl: 24.79,
 *     price_old: 39.99,
 *     price_old_incl: 39.99,
 *     price_old_excl: 33.05
 *   }
 * };
 *
 * if (isSearchResponseProduct(validProduct)) {
 *   console.log("Valid product:", validProduct.title);
 *   console.log("Price:", validProduct.price.price);
 *   console.log("Available:", validProduct.available);
 * }
 * ```
 * @source
 */
export function isSearchResponseProduct(product: unknown): product is SearchResponseProduct {
  return v.safeParse(searchResponseProductSchema, product).success;
}
 
const productObjectSchema = v.object({
  product: v.pipe(
    v.record(v.string(), v.unknown()),
    v.check(
      (val) => 'variants' in val && (typeof val.variants === 'object' || val.variants === false),
    ),
  ),
  shop: v.object({
    currencies: v.record(v.string(), v.unknown()),
    currency: v.string(),
  }),
});
 
/**
 * Type guard to validate if an object has the correct structure for a Laboratorium Discounter product object.
 * Checks for the presence of a product object with a variants property that is either
 * an object containing variant information or false.
 *
 * @category Typeguards
 * @param data - Object to validate as ProductObject
 * @returns Type predicate indicating if product is a valid ProductObject
 * @example
 * ```typescript
 * // Valid product object with variants
 * const validProduct = {
 *   product: {
 *     variants: {
 *       "1": { id: 1, title: "500g", price: 29.99 },
 *       "2": { id: 2, title: "1kg", price: 49.99 }
 *     }
 *   },
 *   shop: { currencies: { EUR: {} }, currency: "EUR" }
 * };
 *
 * if (isProductObject(validProduct)) {
 *   console.log("Valid product object with variants");
 *   console.log("Number of variants:", Object.keys(validProduct.product.variants).length);
 * }
 *
 * // Valid product object without variants
 * const noVariants = {
 *   product: { variants: false },
 *   shop: { currencies: { EUR: {} }, currency: "EUR" }
 * };
 *
 * if (isProductObject(noVariants)) {
 *   console.log("Valid product object without variants");
 * }
 * ```
 * @source
 */
export function isProductObject(data: unknown): data is LaboratoriumDiscounterProductObject {
  return v.safeParse(productObjectSchema, data).success;
}
 
const validSearchParamsSchema = v.object({
  limit: v.string(),
  format: v.string(),
});
 
/**
 * Type guard to validate if an object has the correct structure for Laboratorium Discounter search parameters.
 * Checks for the presence and correct types of required parameters including
 * limit (must be a valid number string) and format (must be "json").
 *
 * @category Typeguards
 * @param params - Parameters to validate
 * @returns Type predicate indicating if params are valid SearchParams
 * @example
 * ```typescript
 * // Valid search parameters
 * const validParams = {
 *   limit: "10",
 *   format: "json"
 * };
 *
 * if (isValidSearchParams(validParams)) {
 *   console.log("Valid search parameters");
 *   console.log("Limit:", validParams.limit);
 * }
 *
 * // Invalid search parameters (wrong types)
 * const wrongTypes = {
 *   limit: 10, // Should be string
 *   format: 123 // Should be "json"
 * };
 * if (!isValidSearchParams(wrongTypes)) {
 *   console.error("Invalid parameters - wrong property types");
 * }
 * ```
 * @source
 */
export function isValidSearchParams(params: unknown): params is LaboratoriumDiscounterSearchParams {
  return v.safeParse(validSearchParamsSchema, params).success;
}