@misiki/gocommerce-connector 0.3.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/{index-D8lNRhqJ.d.cts → index-DIj1rK1d.d.cts} +49 -6
- package/dist/{index-D8lNRhqJ.d.ts → index-DIj1rK1d.d.ts} +49 -6
- package/dist/index.cjs +49 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +48 -6
- package/dist/index.js.map +1 -1
- package/dist/services/index.cjs +49 -5
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.d.cts +1 -1
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +48 -6
- package/dist/services/index.js.map +1 -1
- package/package.json +2 -1
package/dist/services/index.cjs
CHANGED
|
@@ -212,6 +212,17 @@ var BaseService = class _BaseService {
|
|
|
212
212
|
get creds() {
|
|
213
213
|
return _BaseService._credentials;
|
|
214
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* The store this call is for: the request host's subdomain under
|
|
217
|
+
* `baseDomain`, else the static `store`. Read per call, never stored on the
|
|
218
|
+
* instance, because the server shares one process (and one credentials
|
|
219
|
+
* object) across every concurrent request and each may be for a different host.
|
|
220
|
+
*/
|
|
221
|
+
get store() {
|
|
222
|
+
const { baseDomain, store } = this.creds;
|
|
223
|
+
const host = typeof window !== "undefined" ? window.location?.hostname : requestHost?.();
|
|
224
|
+
return storeForHost(host, baseDomain) ?? store;
|
|
225
|
+
}
|
|
215
226
|
/**
|
|
216
227
|
* The engine's base URL from wherever this code runs. In the browser a
|
|
217
228
|
* configured proxy path wins, because the engine answers no CORS preflight.
|
|
@@ -245,7 +256,8 @@ var BaseService = class _BaseService {
|
|
|
245
256
|
const url = this.engineURL(path, lang ? { lang, ...req.query } : req.query);
|
|
246
257
|
const headers = { Accept: "application/json", ...req.headers };
|
|
247
258
|
if (req.body !== void 0) headers["Content-Type"] = "application/json";
|
|
248
|
-
|
|
259
|
+
const store = this.store;
|
|
260
|
+
if (store) headers["X-Store"] = store;
|
|
249
261
|
if (req.auth) {
|
|
250
262
|
const token = sessionToken();
|
|
251
263
|
if (!token) throw new GoCommerceError("Please sign in to continue.", 401, "unauthorized");
|
|
@@ -347,6 +359,18 @@ var BaseService = class _BaseService {
|
|
|
347
359
|
return body;
|
|
348
360
|
}
|
|
349
361
|
};
|
|
362
|
+
var requestHost;
|
|
363
|
+
function setRequestHost(fn) {
|
|
364
|
+
requestHost = fn;
|
|
365
|
+
}
|
|
366
|
+
function storeForHost(host, baseDomain) {
|
|
367
|
+
if (!host || !baseDomain) return void 0;
|
|
368
|
+
const h = host.toLowerCase().replace(/\.$/, "").replace(/:\d+$/, "");
|
|
369
|
+
const suffix = "." + baseDomain.toLowerCase().replace(/^\.+/, "").replace(/\.$/, "");
|
|
370
|
+
if (!h.endsWith(suffix)) return void 0;
|
|
371
|
+
const slug = h.slice(0, -suffix.length);
|
|
372
|
+
return slug && !slug.includes(".") ? slug : void 0;
|
|
373
|
+
}
|
|
350
374
|
var unreachable = (base, cause) => new GoCommerceError(`Could not reach the commerce engine at ${base}: ${cause?.message ?? cause}`, 0);
|
|
351
375
|
function qs(query) {
|
|
352
376
|
if (!query) return "";
|
|
@@ -948,7 +972,7 @@ function remember(key, load, ttl = TTL_MS) {
|
|
|
948
972
|
// src/services/facts.ts
|
|
949
973
|
var StoreFacts = class extends BaseService {
|
|
950
974
|
key(name) {
|
|
951
|
-
return `${this.engineBase()}|${this.
|
|
975
|
+
return `${this.engineBase()}|${this.store ?? ""}|${name}`;
|
|
952
976
|
}
|
|
953
977
|
/** Enabled plugins by key. A module that is not installed has no key here. */
|
|
954
978
|
plugins() {
|
|
@@ -968,6 +992,10 @@ var StoreFacts = class extends BaseService {
|
|
|
968
992
|
ready() {
|
|
969
993
|
return remember(this.key("ready"), () => this.data("/health/ready"));
|
|
970
994
|
}
|
|
995
|
+
/** The store's own name and how a buyer reaches it, as set in its panel. */
|
|
996
|
+
profile() {
|
|
997
|
+
return remember(this.key("profile"), () => this.data("/api/store"));
|
|
998
|
+
}
|
|
971
999
|
checkoutOptions() {
|
|
972
1000
|
return remember(this.key("checkout"), () => this.data("/api/checkout"));
|
|
973
1001
|
}
|
|
@@ -2378,7 +2406,7 @@ var MenuService = class extends BaseService {
|
|
|
2378
2406
|
/** The header and footer menus, or `null` when the module is not there. Cached: a menu is store-level. */
|
|
2379
2407
|
async engineMenus() {
|
|
2380
2408
|
if (!await this.facts.pluginOn("navigation")) return null;
|
|
2381
|
-
return remember(`${this.engineBase()}|${this.
|
|
2409
|
+
return remember(`${this.engineBase()}|${this.store ?? ""}|menus`, async () => {
|
|
2382
2410
|
const out = [];
|
|
2383
2411
|
for (const handle of ["header", "footer"]) {
|
|
2384
2412
|
try {
|
|
@@ -2415,6 +2443,7 @@ var NewsletterService = class extends BaseService {
|
|
|
2415
2443
|
var newsletterService = new NewsletterService();
|
|
2416
2444
|
|
|
2417
2445
|
// src/services/store-service.ts
|
|
2446
|
+
var isStoreMissing = (err) => err instanceof GoCommerceError && err.status === 404 && (err.code === "store_not_found" || /store not found|no such store/i.test(err.message));
|
|
2418
2447
|
function symbolFor(code) {
|
|
2419
2448
|
try {
|
|
2420
2449
|
const part = new Intl.NumberFormat("en", { style: "currency", currency: code }).formatToParts(0).find((p) => p.type === "currency");
|
|
@@ -2427,6 +2456,14 @@ var StoreService = class extends BaseService {
|
|
|
2427
2456
|
facts = new StoreFacts(this._fetch);
|
|
2428
2457
|
async getStoreByIdOrDomain(_args = {}) {
|
|
2429
2458
|
const base = await readStaticStore();
|
|
2459
|
+
const store = this.store;
|
|
2460
|
+
if (store) {
|
|
2461
|
+
await this.facts.plugins().catch((err) => {
|
|
2462
|
+
if (isStoreMissing(err)) {
|
|
2463
|
+
throw new GoCommerceError(`store not found: no store "${store}" on this engine`, 404, "store_not_found");
|
|
2464
|
+
}
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2430
2467
|
const live = await this.live().catch(() => ({}));
|
|
2431
2468
|
return { ...base, ...live, id: base.id || "gocommerce" };
|
|
2432
2469
|
}
|
|
@@ -2464,17 +2501,22 @@ var StoreService = class extends BaseService {
|
|
|
2464
2501
|
return super.post(path, data);
|
|
2465
2502
|
}
|
|
2466
2503
|
async live() {
|
|
2467
|
-
const [ready, plugins, menus, base] = await Promise.all([
|
|
2504
|
+
const [ready, plugins, menus, base, profile] = await Promise.all([
|
|
2468
2505
|
this.facts.ready(),
|
|
2469
2506
|
this.facts.plugins().catch(() => /* @__PURE__ */ new Map()),
|
|
2470
2507
|
new MenuService(this._fetch).engineMenus(),
|
|
2471
|
-
readStaticStore()
|
|
2508
|
+
readStaticStore(),
|
|
2509
|
+
this.facts.profile().catch(() => null)
|
|
2472
2510
|
]);
|
|
2473
2511
|
const code = ready.currency;
|
|
2474
2512
|
const sameCurrency = (base.currency?.code ?? base.currencyCode) === code;
|
|
2475
2513
|
const flag = (on) => ({ ...on ? { active: true } : { active: false } });
|
|
2476
2514
|
const basePlugins = base.plugins ?? {};
|
|
2477
2515
|
return {
|
|
2516
|
+
// Only what the owner filled in: an empty name keeps the static one.
|
|
2517
|
+
...profile?.name ? { name: profile.name } : {},
|
|
2518
|
+
...profile?.email ? { businessEmail: profile.email } : {},
|
|
2519
|
+
...profile?.phone ? { businessPhone: profile.phone } : {},
|
|
2478
2520
|
currencyCode: code,
|
|
2479
2521
|
currencySymbol: sameCurrency ? base.currencySymbol ?? symbolFor(code) : symbolFor(code),
|
|
2480
2522
|
currency: {
|
|
@@ -3158,10 +3200,12 @@ exports.reelsService = reelsService;
|
|
|
3158
3200
|
exports.regionService = regionService;
|
|
3159
3201
|
exports.reviewService = reviewService;
|
|
3160
3202
|
exports.searchService = searchService;
|
|
3203
|
+
exports.setRequestHost = setRequestHost;
|
|
3161
3204
|
exports.settingService = settingService;
|
|
3162
3205
|
exports.settingsService = settingsService;
|
|
3163
3206
|
exports.stateService = stateService;
|
|
3164
3207
|
exports.staticStore = staticStore;
|
|
3208
|
+
exports.storeForHost = storeForHost;
|
|
3165
3209
|
exports.storeService = storeService;
|
|
3166
3210
|
exports.toPage = toPage;
|
|
3167
3211
|
exports.uploadService = uploadService;
|