All files / src/utils/typeGuards searchanise.ts

100% Statements 66/66
100% Branches 8/8
100% Functions 5/5
100% Lines 66/66

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 2061x   1x 1x 1x 1x 1x 1x 1x   1x                                                                             1x 1x 15x 15x 7x 7x 8x 8x   1x 1x 1x 1x 1x 1x 1x 1x   1x                                                   1x 1x 14x 14x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                             1x 1x 20x 20x   1x 1x 1x 1x   1x                               1x 1x 38x 38x   1x 1x 1x 1x   1x                           1x 1x 36x 36x  
import * as v from 'valibot';
 
const validSearchResponseSchema = v.object({
  totalItems: v.number(),
  startIndex: v.number(),
  itemsPerPage: v.number(),
  currentItemCount: v.number(),
  items: v.array(v.unknown()),
});
 
/**
 * Type guard to validate if a response from the Searchanise search API is a valid SearchResponse object.
 * Checks for the presence and correct types of all required properties including pagination info,
 * suggestions, and a valid array of item listings.
 *
 * @category Typeguards
 * @param response - The response object to validate
 * @returns Type predicate indicating if the response is a valid SearchResponse
 * @example
 * ```typescript
 * // Valid search response
 * const validResponse = {
 *   totalItems: 100,
 *   startIndex: 0,
 *   itemsPerPage: 20,
 *   currentItemCount: 20,
 *   items: [
 *     {
 *       title: "Sodium Chloride",
 *       price: "29.99",
 *       link: "/products/nacl",
 *       product_id: "12345",
 *       product_code: "CHEM-001",
 *       quantity: "500g",
 *       vendor: "Chemical Supplier",
 *       original_product_id: "12345",
 *       list_price: "39.99",
 *       shopify_variants: []
 *     }
 *   ]
 * };
 *
 * if (isValidSearchResponse(validResponse)) {
 *   console.log(`Found ${validResponse.items.length} items`);
 * } else {
 *   console.error("Invalid search response structure");
 * }
 * ```
 * @source
 */
export function isValidSearchResponse(response: unknown): response is SearchResponse {
  const parsed = v.safeParse(validSearchResponseSchema, response);
  if (!parsed.success) {
    return false;
  }
  return parsed.output.items.every((item) => isItemListing(item));
}
 
const searchaniseVariantSchema = v.object({
  sku: v.string(),
  price: v.string(),
  link: v.string(),
  variant_id: v.string(),
  quantity_total: v.union([v.string(), v.number()]),
  options: v.record(v.string(), v.unknown()),
});
 
/**
 * Type guard to validate if an object is a valid Searchanise product variant.
 * Checks for the presence and correct types of all required variant properties
 * including SKU, price, link, variant ID, quantity, and options.
 *
 * @category Typeguards
 * @param variant - The variant object to validate
 * @returns Type predicate indicating if the object is a valid SearchaniseVariant
 * @example
 * ```typescript
 * // Valid Searchanise variant
 * const validVariant = {
 *   sku: "CHEM-001-500G",
 *   price: "29.99",
 *   link: "/products/nacl?variant=1",
 *   variant_id: "1",
 *   quantity_total: "100",
 *   options: { Model: "500g" }
 * };
 *
 * if (isSearchaniseVariant(validVariant)) {
 *   console.log("Valid variant:", validVariant.sku);
 *   console.log("Price:", validVariant.price);
 * }
 * ```
 * @source
 */
export function isSearchaniseVariant(variant: unknown): variant is SearchaniseVariant {
  return v.safeParse(searchaniseVariantSchema, variant).success;
}
 
const itemListingSchema = v.object({
  title: v.string(),
  price: v.union([v.string(), v.number()]),
  link: v.string(),
  product_id: v.string(),
  product_code: v.string(),
  quantity: v.string(),
  // Absent on non-Shopify storefronts (e.g. Laballey's Magento catalog).
  shopify_variants: v.optional(v.array(searchaniseVariantSchema)),
  vendor: v.optional(v.string()),
  original_product_id: v.string(),
  list_price: v.string(),
});
 
/**
 * Type guard to validate if an object is a valid Searchanise item listing.
 * Checks for the presence and correct types of all required properties including
 * product details, pricing, and an array of valid Searchanise variants.
 *
 * @category Typeguards
 * @param item - The item object to validate
 * @returns Type predicate indicating if the object is a valid ItemListing
 * @example
 * ```typescript
 * // Valid item listing
 * const validItem = {
 *   title: "Sodium Chloride",
 *   price: "29.99",
 *   link: "/products/nacl",
 *   product_id: "12345",
 *   product_code: "CHEM-001",
 *   quantity: "500g",
 *   vendor: "Chemical Supplier",
 *   original_product_id: "12345",
 *   list_price: "39.99",
 *   shopify_variants: [
 *     {
 *       sku: "CHEM-001-500G",
 *       price: "29.99",
 *       link: "/products/nacl?variant=1",
 *       variant_id: "1",
 *       quantity_total: "100",
 *       options: { Model: "500g" }
 *     }
 *   ]
 * };
 *
 * if (isItemListing(validItem)) {
 *   console.log("Valid item listing:", validItem.title);
 *   console.log("Vendor:", validItem.vendor);
 * }
 * ```
 * @source
 */
export function isItemListing(item: unknown): item is ItemListing {
  return v.safeParse(itemListingSchema, item).success;
}
 
const searchaniseApiObjectSchema = v.object({
  host: v.pipe(v.string(), v.minLength(1)),
  api_key: v.pipe(v.string(), v.regex(/^[A-Za-z0-9]{10}$/)),
});
 
/**
 * Type guard for the `window.Searchanise` object that Searchanise-powered storefronts embed in
 * their pages. Requires a non-empty `host` string and a 10-character alphanumeric `api_key`;
 * other fields are ignored.
 * @category Typeguards
 * @group Suppliers
 * @param data - The value to validate (typically parsed from the storefront's homepage)
 * @returns Type predicate indicating whether the value carries a usable `host` and `api_key`
 * @example
 * ```typescript
 * isValidSearchaniseApiObject({ host: 'https://searchserverapi1.com', api_key: '4p4M0R6q0N' });
 * // true
 * isValidSearchaniseApiObject({ host: 'https://searchserverapi1.com', api_key: 'short' });
 * // false
 * ```
 * @source
 */
export function isValidSearchaniseApiObject(data: unknown): data is SearchaniseApiObject {
  return v.safeParse(searchaniseApiObjectSchema, data).success;
}
 
const searchaniseCredentialsSchema = v.object({
  apiKey: v.pipe(v.string(), v.regex(/^[A-Za-z0-9]{10}$/)),
  host: v.pipe(v.string(), v.minLength(1)),
});
 
/**
 * Type guard for cached Searchanise credentials. Requires a 10-character alphanumeric `apiKey`
 * and a non-empty `host` string. It checks shape only; the supplier separately checks the host
 * against its allow-list before using it.
 * @category Typeguards
 * @group Suppliers
 * @param data - The value to validate (typically read back from extension storage)
 * @returns Type predicate indicating whether the value is usable credentials
 * @example
 * ```typescript
 * isSearchaniseCredentials({ apiKey: '4p4M0R6q0N', host: 'searchserverapi1.com' }); // true
 * isSearchaniseCredentials({ apiKey: 'short', host: 'searchserverapi1.com' });       // false
 * ```
 * @source
 */
export function isSearchaniseCredentials(data: unknown): data is SearchaniseCredentials {
  return v.safeParse(searchaniseCredentialsSchema, data).success;
}