Response object to validate
Type predicate indicating if response is a valid SearchResponse
// Valid search response
const validResponse = {
page: {
search: "sodium chloride",
session_id: "abc123",
key: "search_key",
title: "Search Results",
status: "success"
},
request: {
url: "/en/search/sodium-chloride",
method: "GET",
get: { q: "sodium chloride" },
device: "desktop"
},
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");
}
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.