UNPKG

storenest-commerce

Version:

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

79 lines (78 loc) 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.login = login; exports.register = register; exports.createOrGetGuestUser = createOrGetGuestUser; const config_1 = require("./config"); const utils_1 = require("./utils"); async function getHeaders(isCritical = false, requestBody) { if (isCritical) { const secretKey = (0, config_1.getSecretKey)(); return secretKey ? { 'x-secret-key': secretKey } : undefined; } 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 login(params) { const proxyUrl = (0, config_1.getProxyUrl)(); const isCritical = true; const url = proxyUrl ? `${proxyUrl}/auth/login` : `${(0, config_1.getApiBaseUrl)()}/auth/login`; const requestBody = JSON.stringify(params); const headers = { 'Content-Type': 'application/json', ...(proxyUrl ? {} : await getHeaders(isCritical, requestBody) || {}), }; const res = await fetch(url, { method: 'POST', headers, body: requestBody, }); if (!res.ok) throw new Error('Failed to login'); return await res.json(); } async function register(params) { const proxyUrl = (0, config_1.getProxyUrl)(); const isCritical = true; const url = proxyUrl ? `${proxyUrl}/auth/register` : `${(0, config_1.getApiBaseUrl)()}/auth/register`; const requestBody = JSON.stringify(params); const headers = { 'Content-Type': 'application/json', ...(proxyUrl ? {} : await getHeaders(isCritical, requestBody) || {}), }; const res = await fetch(url, { method: 'POST', headers, body: requestBody, }); if (!res.ok) throw new Error('Failed to register'); return await res.json(); } async function createOrGetGuestUser(params) { const proxyUrl = (0, config_1.getProxyUrl)(); const isCritical = false; // Use API key authentication, not secret key const url = proxyUrl ? `${proxyUrl}/auth/guest` : `${(0, config_1.getApiBaseUrl)()}/auth/guest`; const requestBody = JSON.stringify(params); const headers = { 'Content-Type': 'application/json', ...(proxyUrl ? {} : await getHeaders(isCritical, requestBody) || {}), }; const res = await fetch(url, { method: 'POST', headers, body: requestBody, }); if (!res.ok) throw new Error('Failed to create/get guest user'); return await res.json(); }