UNPKG

storenest-commerce

Version:

Complete e-commerce SDK for Storenest platform with React components, multi-language support, secure checkout, and enterprise-grade security

54 lines (53 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProducts = getProducts; exports.getProductById = getProductById; exports.getProductsByCategorySlug = getProductsByCategorySlug; const config_1 = require("./config"); const utils_1 = require("./utils"); async function getHeaders(requestBody) { const apiKey = (0, config_1.getApiKey)(); if (!apiKey) { return undefined; } try { const { timestamp, signature, nonce } = await (0, utils_1.generateApiToken)(apiKey, requestBody); const headers = { 'x-api-key': apiKey, 'x-timestamp': timestamp, 'x-signature': signature, 'x-nonce': nonce, 'Content-Type': 'application/json', }; return headers; } catch (error) { return undefined; } } async function getProducts(page = 1, limit = 20) { const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/products?page=${page}&limit=${limit}`, { headers: await getHeaders(), }); if (!res.ok) throw new Error('Failed to fetch products'); return await res.json(); } async function getProductById(productId) { const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/products/${productId}`, { headers: await getHeaders(), }); if (res.status === 404) return null; if (!res.ok) throw new Error('Failed to fetch product'); return await res.json(); } async function getProductsByCategorySlug(categorySlug, page = 1, limit = 20) { const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/products/category/${categorySlug}?page=${page}&limit=${limit}`, { headers: await getHeaders(), }); if (!res.ok) throw new Error('Failed to fetch products by category'); return await res.json(); }