UNPKG

storenest-commerce

Version:

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

43 lines (42 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOrder = getOrder; exports.placeOrder = placeOrder; 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 getOrder(orderNumber) { const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/orders/${orderNumber}`, { headers: await getHeaders(), }); if (res.status === 404) throw new Error('Order not found'); if (!res.ok) throw new Error('Failed to fetch order'); return await res.json(); } async function placeOrder(params) { const requestBody = JSON.stringify(params); const res = await fetch(`${(0, config_1.getApiBaseUrl)()}/orders`, { method: 'POST', headers: await getHeaders(requestBody), body: requestBody, }); if (!res.ok) { const error = await res.json().catch(() => ({ error: 'Failed to place order' })); throw new Error(error.error || 'Failed to place order'); } return await res.json(); }