UNPKG

@selldone/sdk-storefront

Version:

A TypeScript SDK to connect to your shop and build a fully functional storefront and website by simply developing a frontend web application. All backend operations are seamlessly managed by the serverless Selldone solution.

147 lines 8.08 kB
// @ts-nocheck /* * Copyright (c) 2023. Selldone® Business OS™ * * Author: M.Pajuhaan * Web: https://selldone.com * ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ * * All rights reserved. In the weave of time, where traditions and innovations intermingle, this content was crafted. * From the essence of thought, through the corridors of creativity, each word, and sentiment has been molded. * Not just to exist, but to inspire. Like an artist's stroke or a sculptor's chisel, every nuance is deliberate. * Our journey is not just about reaching a destination, but about creating a masterpiece. * Tread carefully, for you're treading on dreams. */ import { CDN, SelldoneCore, SetupService, URLS } from "@selldone/core-js"; import { XapiUser } from "./user/XapiUser"; import { XAPI } from "./apis/XAPI"; import { XapiShop } from "./shop/XapiShop"; import { XapiAuth } from "./auth/XapiAuth"; import { StorefrontAxiosSetup } from "./plugins/axios/StorefrontAxiosSetup"; import { XapiProduct } from "./product/XapiProduct"; import { XapiLottery } from "./lottery/XapiLottery"; import { StorefrontDatabase } from "./database/StorefrontDatabase"; import { XapiCoupon } from "./coupon/XapiCoupon"; import { XapiOffer } from "./offer/XapiOffer"; import { Currency, } from "@selldone/core-js/enums/payment/Currency"; import { XapiBasket } from "./basket/XapiBasket"; import { XapiVendor } from "./vendor/XapiVendor"; import { XapiAvocado } from "./avocado/XapiAvocado"; import { XapiArticle } from "./article/XapiArticle"; import { StorefrontRoutesName } from "@selldone/core-js/enums/route/StorefrontRoutesName"; import { XapiCashback } from "@selldone/sdk-storefront/cashback/XapiCashback"; const SDK_VERSION = "0.02"; //█████████████████████████████████████████████████████████████ //――――――――――― Selldone® Storefront SDK ――――――――――― //█████████████████████████████████████████████████████████████ export class StorefrontSDK { /** * Initializes and sets up the Selldone Storefront SDK. * It configures essential SDK parameters, either by taking them from provided arguments or by * fetching them from meta tags. Additionally, it logs the SDK version to the console and * handles different environments, specifically the back office. * * @param _shop_name Optional shop name. If not provided, the function attempts to retrieve the * shop name from a meta tag with the attribute name "shop-name". * * @throws Will throw an error if the shop name is not provided and is also not available * in the meta tag when not in the backoffice environment. * * @returns void * * @constructor * * @example * // Usage in the backoffice environment * StorefrontSDK.Setup("exampleShop"); * * // Typical usage without providing shop name (relies on meta tag) * StorefrontSDK.Setup(); */ static Setup(_shop_name, options = { cookie_key_access_token: "access_token", }) { console.log("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"); console.log(`┣━━━━ Selldone® Storefront SDK | V${SDK_VERSION} ━━━━┫`); console.log("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"); this.CheckDependencies(); let shop_name; let shop_prefix_address; let custom_home; if (window.$backoffice) { console.style("You are using Storefront SDK within <b='color:#673AB7'>BACKOFFICE ENVIRONMENT</b>. So we initial it automatically compatible with Back Office SDK!"); if (!_shop_name) throw "❌ Please set shop_name in the StorefrontSDK.Setup(...)!"; shop_name = _shop_name; shop_prefix_address = ""; custom_home = null; //――――――――――――――――――――――――― Initialize Resources Origin ――――――――――――――――――――――――― // Define API repositories: window.XAPI = new XAPI(); } else { //――――――――――――――――――――――――― Shop Meta Tags ――――――――――――――――――――――――― shop_name = _shop_name ? _shop_name : SetupService.GetMetaValue("shop-name"); if (!shop_name) throw "❌ The shop name is not specified in the meta tag with the name 'shop-name'."; shop_prefix_address = SetupService.GetMetaValue("shop-prefix-address", ""); custom_home = SetupService.GetMetaValue("custom-home"); //――――――――――――――――――――――――― Axios ――――――――――――――――――――――――― StorefrontAxiosSetup(options.cookie_key_access_token); //――――――――――――――――――――――――― Initialize Resources Origin ――――――――――――――――――――――――― // Define API repositories: window.CDN = new CDN(); window.XAPI = new XAPI(); window.URLS = new URLS(); window.ADDRESS_API = window.XAPI; window.ARTICLE_API = window.XAPI; } //――――――――――――――――――――――――― Create Instance ――――――――――――――――――――――――― const _LOCAL_STORAGE_BASE_PATH = `shop/@${shop_name}/`; const _database = new StorefrontDatabase(shop_name); window.$storefront = { name: shop_name, prefix_url: shop_prefix_address, local_storage_path: _LOCAL_STORAGE_BASE_PATH, database: _database, currency: _database.currency.getCurrency(), home: custom_home, user: new XapiUser(shop_name), shop: new XapiShop(shop_name), auth: new XapiAuth(shop_name), products: new XapiProduct(shop_name), lottery: new XapiLottery(shop_name), coupon: new XapiCoupon(shop_name), offer: new XapiOffer(shop_name), cashback: new XapiCashback(shop_name), basket: new XapiBasket(shop_name), vendor: new XapiVendor(shop_name), avocado: new XapiAvocado(shop_name), article: new XapiArticle(shop_name), routes: StorefrontRoutesName, }; Object.defineProperty(window.$storefront, "currency", { get: function () { return _database.currency.getCurrency(); }, set: function (value) { if (typeof value === "string") { value = Currency[value]; } _database.currency.saveCurrency(value); }, }); window.$storefront.currency = _database.currency.getCurrency(); console.style(`✅ Selldone® Storefront SDK [<b='color:#009688'>@${shop_name}</b>] initialized successfully.`); } static CheckDependencies() { if (!window.CDN) { // ━━━ Selldone Core (gapi,...) ━━━ console.log("⚡ we auto initialized 'SelldoneCore.Setup()'! You can manually do it before initializing the Storefront SDK."); SelldoneCore.Setup(); } } } //# sourceMappingURL=StorefrontSDK.js.map