ChemPare Documentation - v0.0.0
    Preparing search index...

    Function isValidSearchResponse

    • Type guard to validate if a response from the Shopify 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.

      Parameters

      • response: unknown

        The response object to validate

      Returns response is SearchResponse

      Type predicate indicating if the response is a valid SearchResponse

      // Valid search response
      const validResponse = {
      totalItems: 100,
      startIndex: 0,
      itemsPerPage: 20,
      currentItemCount: 20,
      pageStartIndex: 0,
      totalPages: 5,
      suggestions: ["sodium", "chloride", "nacl"],
      pages: [1, 2, 3, 4, 5],
      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: [
      {
      sku: "CHEM-001-500G",
      price: "29.99",
      link: "/products/nacl?variant=1",
      variant_id: "1",
      quantity_total: "100",
      options: { Model: "500g" }
      }
      ]
      }
      // ... more items
      ]
      };

      if (isValidSearchResponse(validResponse)) {
      console.log(`Found ${validResponse.items.length} items`);
      console.log(`Total pages: ${validResponse.totalPages}`);
      } else {
      console.error("Invalid search response structure");
      }

      // Invalid search response (missing required properties)
      const invalidResponse = {
      items: [],
      totalItems: 0
      // Missing other required properties
      };
      if (!isValidSearchResponse(invalidResponse)) {
      console.error("Invalid response - missing required properties");
      }

      // Invalid search response (wrong types)
      const wrongTypes = {
      totalItems: "100", // Should be number
      itemsPerPage: "20", // Should be number
      items: "not an array" // Should be array
      };
      if (!isValidSearchResponse(wrongTypes)) {
      console.error("Invalid response - wrong property types");
      }