All files / src/utils/typeGuards sciencelab.ts

100% Statements 7/7
100% Branches 1/1
100% Functions 1/1
100% Lines 7/7

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 251x   1x                               1x 1x 12x   12x 12x  
import { isPopulatedObject } from './common';
 
/**
 * Type guard for the ScienceLab (BigCommerce) product-attributes AJAX response.
 * Narrows an unknown parsed value to {@link ScienceLabAttributeResponse} by
 * requiring that it be an object carrying a `data` object. The variant price is
 * read from `data.price.without_tax.value`, which callers still null-check.
 * @category Typeguards
 * @param value - The parsed AJAX response to test
 * @returns True when `value` carries a `data` object
 * @example
 * ```typescript
 * const parsed = await this.httpPostJson({ path, body });
 * if (isScienceLabAttributeResponse(parsed)) {
 *   const price = parsed.data?.price?.without_tax?.value;
 * }
 * ```
 * @source
 */
export function isScienceLabAttributeResponse(
  value: unknown,
): value is ScienceLabAttributeResponse {
  return isPopulatedObject(value) && isPopulatedObject(value.data);
}