ec-client-lib
v0.0.2
Published
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Downloads
3
Readme
React + Vite
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
Currently, two official plugins are available:
- @vitejs/plugin-react uses Babel for Fast Refresh
- @vitejs/plugin-react-swc uses SWC for Fast Refresh
How to use
- Install the npm package using
npm install ec-client-lib
. - Import
useInitiateSession
and use in your component as below:
import { useInitiateSession } from "ec-client-lib";
function Component() {
useInitiateSession(
"http://localhost:3000",
"abcxxxxxxyz",
// callback function to perform something based on the initiate response
(response) => {
if (response.statusCode === 200) {
await fetch("http://localhost:3000/api/success");
} else {
await fetch("http://localhost:3000/api/retry");
}
}
);
// First argument is the API base URL, second argument is the Auth token, third argument is an optional callback function
return <div>Hello world</div>;
}
- To get order details, use
getOrder
function as below:
import { getOrder } from "ec-client-lib";
function Component() {
useEffect(() => {
async function fetchAndConsoleOrder() {
const orderDetails = await getOrder("http://localhost:3000");
console.log("Order details:", orderDetails);
}
fetchAndConsoleOrder();
}, []);
return <div>Hello world</div>;
}
- To update order details, use
updateOrder
function as below:
import { updateOrder } from "ec-client-lib";
function Component() {
async function onClick(event, data) => {
const orderDetails = await updateOrder("http://localhost:3000", data);
console.log("Order details:", orderDetails);
}
return <div>Hello world</div>;
}
- To add an item to cart, use
addToCart
function as below:
import { addToCart } from "ec-client-lib";
async function onClick(event, data) {
const addedItem = await addToCart(
"http://localhost:3000",
data.productVariantId,
data.quantity,
data.orderId
);
return addedItem;
}