storenest-commerce
Version:
Complete e-commerce SDK for Storenest platform with React components, multi-language support, secure checkout, and enterprise-grade security
63 lines (62 loc) • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addToCart = addToCart;
exports.getCart = getCart;
exports.removeFromCart = removeFromCart;
exports.updateCartItem = updateCartItem;
const config_1 = require("./config");
const utils_1 = require("./utils");
async function getHeaders(requestBody) {
const apiKey = (0, config_1.getApiKey)();
if (!apiKey)
return undefined;
const { timestamp, signature, nonce } = await (0, utils_1.generateApiToken)(apiKey, requestBody);
return {
'x-api-key': apiKey,
'x-timestamp': timestamp,
'x-signature': signature,
'x-nonce': nonce,
'Content-Type': 'application/json',
};
}
async function addToCart(params) {
const requestBody = JSON.stringify(params);
const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/cart/add`, {
method: 'POST',
headers: await getHeaders(requestBody),
body: requestBody,
});
if (!res.ok)
throw new Error('Failed to add to cart');
return await res.json();
}
async function getCart(userId) {
const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/cart/get?userId=${userId}`, {
headers: await getHeaders(),
});
if (!res.ok)
throw new Error('Failed to fetch cart');
return await res.json();
}
async function removeFromCart(params) {
const requestBody = JSON.stringify(params);
const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/cart/remove`, {
method: 'POST',
headers: await getHeaders(requestBody),
body: requestBody,
});
if (!res.ok)
throw new Error('Failed to remove from cart');
return await res.json();
}
async function updateCartItem(params) {
const requestBody = JSON.stringify(params);
const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/cart/update`, {
method: 'POST',
headers: await getHeaders(requestBody),
body: requestBody,
});
if (!res.ok)
throw new Error('Failed to update cart item');
return await res.json();
}