-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorefrontSDK.ts
252 lines (215 loc) · 9.71 KB
/
StorefrontSDK.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// @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, User} 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 {Shop} from "@selldone/core-js/models/shop/shop.model";
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, type ICurrency,} 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";
//█████████████████████████████████████████████████████████████
//―――――――――――――― Global Types ―――――――――――――――
//█████████████████████████████████████████████████████████████
// Extend the Window interface to recognize the properties you add to the global window object.
declare global {
interface Window {
NativeApp?: boolean;
axios: any;
SelldoneUser?: {
access_token: string;
/**
* The expires_in field specifies the time at which the access token expires, represented in seconds since the token was issued.
*/
expires_in: number;
refresh_token?: string | null;
};
/**
* 🚀 Preloaded Shop Information
*
* When the shop page loads, the backend might already populate `window.shop` with relevant shop data.
*
* Example:
* window.shop = {!! json_encode(\App\Shop\Shop::GetPublicInfo($shop->name, null, false)) !!};
*/
shop: Partial<Shop>;
// APIs
CDN: CDN;
XAPI: XAPI;
URLS: URLS;
ADDRESS_API: XAPI;
ARTICLE_API: XAPI;
// Global SDK Interface
$storefront: {
USER: User | null; // Get current longed in user information
name: string;
prefix_url: string; // Ex. '/@pajuhaan' or ''
local_storage_path: string; // Local storage base path.
database: StorefrontDatabase;
currency: ICurrency; // Current selected currency
home: Shop.Home | null;
user: XapiUser;
shop: XapiShop;
auth: XapiAuth;
products: XapiProduct;
lottery: XapiLottery;
coupon: XapiCoupon;
offer: XapiOffer;
cashback: XapiCashback;
basket: XapiBasket;
vendor: XapiVendor;
avocado: XapiAvocado;
article: XapiArticle;
// Routes Name:
routes: typeof StorefrontRoutesName;
};
}
}
//█████████████████████████████████████████████████████████████
//――――――――――― 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?: string,
options: { cookie_key_access_token?: string } = {
cookie_key_access_token: "access_token",
},
): void {
console.log("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
console.log(`┣━━━━ Selldone® Storefront SDK | V${SDK_VERSION} ━━━━┫`);
console.log("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
this.CheckDependencies();
let shop_name: string;
let shop_prefix_address: string;
let custom_home: Shop.Home | null;
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") as Shop.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: ICurrency | keyof typeof Currency) {
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();
}
}
}