@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
31 lines (30 loc) • 1.48 kB
JavaScript
const MEDUSA_PRODUCT_ATTRIBUTES_PAGE_SIZE = 100;
const resolvePageSize = (pageSize)=>{
if (void 0 === pageSize) return MEDUSA_PRODUCT_ATTRIBUTES_PAGE_SIZE;
if (!(Number.isInteger(pageSize) && pageSize >= 1 && pageSize <= 100)) throw new Error("Product Attribute page size must be between 1 and 100.");
return pageSize;
};
function createMedusaProductAttributeService(sdk, config) {
const productsPath = config?.productsPath ?? "/store/products";
const pageSize = resolvePageSize(config?.pageSize);
return {
getProductAttributes: async (params, signal)=>{
if (!params.productId) throw new Error("Product id is required for Product Attributes.");
const productAttributes = [];
let offset = 0;
while(true){
const response = await sdk.client.fetch(`${productsPath}/${encodeURIComponent(params.productId)}/product-attributes`, {
query: {
limit: pageSize,
offset
},
signal
});
productAttributes.push(...response.product_attributes);
if (0 === response.product_attributes.length || productAttributes.length >= response.count) return productAttributes;
offset += response.product_attributes.length;
}
}
};
}
export { MEDUSA_PRODUCT_ATTRIBUTES_PAGE_SIZE, createMedusaProductAttributeService };