genuka-api
Version:
Javascript(TS) Package to use Genuka API for a StoreFront website
162 lines (138 loc) • 5.15 kB
JavaScript
import Genuka from "../../src/services/index.ts";
const cfg = {
domain: process.env.GENUKA_DOMAIN || "essilabeauty.com",
version: process.env.GENUKA_VERSION || "2023-11",
baseURL: process.env.GENUKA_BASE_URL, // pris en compte si ton AxiosConfig l'accepte
};
const EMAIL = process.env.GENUKA_CUSTOMER_EMAIL;
const PASSWORD = process.env.GENUKA_CUSTOMER_PASSWORD;
const PRODUCT_ID = process.env.GENUKA_PRODUCT_ID;
const PRODUCT_HANDLE = process.env.GENUKA_PRODUCT_HANDLE;
const COLLECTION_ID = process.env.GENUKA_COLLECTION_ID;
function logStep(title, extra = "") {
console.log(`\n\x1b[36m==> ${title}\x1b[0m ${extra}`);
}
async function step(title, fn) {
logStep(title);
console.time(title);
try {
const out = await fn();
console.timeEnd(title);
return out;
} catch (e) {
console.timeEnd(title);
console.error(
`\x1b[31m[${title}] ERROR:\x1b[0m`,
e?.response?.data || e?.message || e
);
throw e;
}
}
let genuka;
(async () => {
// 1) Init “per-request”
genuka = await step("Initialize via domain", async () => {
const sdk = await Genuka.initialize(cfg); // set companyId (+ shopId si resolveShop dans ton SDK)
return sdk;
});
// 2) Company
await step("Company.retrieve", async () => {
const company = await genuka.company.retrieve();
console.dir(company, { depth: 2 });
return company;
});
// 3) Shops
await step("Shops.list", async () => {
const shops = await genuka.shops.list();
console.dir(shops?.slice?.(0, 2) ?? shops, { depth: 2 });
return shops;
});
// 4) Products
await step("Products.count", async () => {
const count = await genuka.products.count();
console.log({ count });
return count;
});
await step("Products.list (paged)", async () => {
const products = await genuka.products.list({ page: 1, limit: 5 });
console.dir(products, { depth: 2 });
return products;
});
if (PRODUCT_ID) {
await step("Products.retrieve by id", async () => {
const p = await genuka.products.retrieve({ id: PRODUCT_ID });
console.dir({ id: PRODUCT_ID, title: p?.title }, { depth: 2 });
return p;
});
}
if (PRODUCT_HANDLE) {
await step("Products.retrieve by handle", async () => {
const p = await genuka.products.retrieve({ handle: PRODUCT_HANDLE });
console.dir({ handle: PRODUCT_HANDLE, title: p?.title }, { depth: 2 });
return p;
});
}
// 5) Collections
await step("Collections.count", async () => {
const count = await genuka.collections.count();
console.log({ count });
return count;
});
await step("Collections.list", async () => {
const cols = await genuka.collections.list({ page: 1, limit: 5 });
console.dir(cols, { depth: 2 });
return cols;
});
if (COLLECTION_ID) {
await step("Collections.retrieve by id", async () => {
const col = await genuka.collections.retrieve({ id: COLLECTION_ID });
console.dir({ id: COLLECTION_ID, title: col?.title }, { depth: 2 });
return col;
});
await step("Products.list by collection", async () => {
const list = await genuka.products.list({
filter: { collections: COLLECTION_ID },
limit: 3,
});
console.dir(list, { depth: 2 });
return list;
});
}
// 6) Pages/Blogs/Articles (facultatifs)
await step("Pages.count", () => genuka.pages.count());
await step("Pages.list", () => genuka.pages.list({ page: 1, limit: 3 }));
await step("Blogs.count", () => genuka.blogs.count());
await step("Blogs.list", () => genuka.blogs.list({ page: 1, limit: 3 }));
await step("Articles.count", () => genuka.articles.count());
await step("Articles.list", () =>
genuka.articles.list({ page: 1, limit: 3 })
);
// 7) Customer flow (si tu as fourni email/password)
if (EMAIL && PASSWORD) {
const { token } = await step("Customers.login", async () => {
const out = await genuka.customers.login({
email: EMAIL,
password: PASSWORD,
});
return out;
});
await step("Set token", async () => genuka.setToken(token));
await step("Customers.retrieve", async () => {
const me = await genuka.customers.retrieve();
console.dir(me, { depth: 2 });
return me;
});
await step("Orders.list (as customer)", () => genuka.orders.list());
// passer une commande “fake” si ton API de tests le permet (sinon commente)
// await step("Orders.create", () => genuka.orders.create({
// customer: { first_name: "John", last_name: "Doe", phone: "123456789", email: EMAIL },
// shipping: { mode: "delivery", amount: 0, address: { first_name: "John", last_name:"Doe", phone: "123456789", line1: "line1", city:"city", country:"country" } },
// billing: { method: "cash", address: { first_name: "John", last_name:"Doe", phone: "123456789", line1: "line1", city:"city", country:"country" } },
// products: [{ title: "Produit test", price: 2000, quantity: 1 }],
// }));
}
console.log("\n✅ Done.");
})().catch((e) => {
console.error("\n❌ Integration run failed.");
process.exit(1);
});