@misiki/gocommerce-connector 0.3.1 → 0.4.0
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 +28 -0
- package/dist/{index-D8lNRhqJ.d.cts → index-iPG0fxzc.d.cts} +31 -1
- package/dist/{index-D8lNRhqJ.d.ts → index-iPG0fxzc.d.ts} +31 -1
- package/dist/index.cjs +38 -3
- 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 +37 -4
- package/dist/index.js.map +1 -1
- package/dist/services/index.cjs +38 -3
- 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 +37 -4
- 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() {
|
|
@@ -2378,7 +2402,7 @@ var MenuService = class extends BaseService {
|
|
|
2378
2402
|
/** The header and footer menus, or `null` when the module is not there. Cached: a menu is store-level. */
|
|
2379
2403
|
async engineMenus() {
|
|
2380
2404
|
if (!await this.facts.pluginOn("navigation")) return null;
|
|
2381
|
-
return remember(`${this.engineBase()}|${this.
|
|
2405
|
+
return remember(`${this.engineBase()}|${this.store ?? ""}|menus`, async () => {
|
|
2382
2406
|
const out = [];
|
|
2383
2407
|
for (const handle of ["header", "footer"]) {
|
|
2384
2408
|
try {
|
|
@@ -2415,6 +2439,7 @@ var NewsletterService = class extends BaseService {
|
|
|
2415
2439
|
var newsletterService = new NewsletterService();
|
|
2416
2440
|
|
|
2417
2441
|
// src/services/store-service.ts
|
|
2442
|
+
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
2443
|
function symbolFor(code) {
|
|
2419
2444
|
try {
|
|
2420
2445
|
const part = new Intl.NumberFormat("en", { style: "currency", currency: code }).formatToParts(0).find((p) => p.type === "currency");
|
|
@@ -2427,6 +2452,14 @@ var StoreService = class extends BaseService {
|
|
|
2427
2452
|
facts = new StoreFacts(this._fetch);
|
|
2428
2453
|
async getStoreByIdOrDomain(_args = {}) {
|
|
2429
2454
|
const base = await readStaticStore();
|
|
2455
|
+
const store = this.store;
|
|
2456
|
+
if (store) {
|
|
2457
|
+
await this.facts.plugins().catch((err) => {
|
|
2458
|
+
if (isStoreMissing(err)) {
|
|
2459
|
+
throw new GoCommerceError(`store not found: no store "${store}" on this engine`, 404, "store_not_found");
|
|
2460
|
+
}
|
|
2461
|
+
});
|
|
2462
|
+
}
|
|
2430
2463
|
const live = await this.live().catch(() => ({}));
|
|
2431
2464
|
return { ...base, ...live, id: base.id || "gocommerce" };
|
|
2432
2465
|
}
|
|
@@ -3158,10 +3191,12 @@ exports.reelsService = reelsService;
|
|
|
3158
3191
|
exports.regionService = regionService;
|
|
3159
3192
|
exports.reviewService = reviewService;
|
|
3160
3193
|
exports.searchService = searchService;
|
|
3194
|
+
exports.setRequestHost = setRequestHost;
|
|
3161
3195
|
exports.settingService = settingService;
|
|
3162
3196
|
exports.settingsService = settingsService;
|
|
3163
3197
|
exports.stateService = stateService;
|
|
3164
3198
|
exports.staticStore = staticStore;
|
|
3199
|
+
exports.storeForHost = storeForHost;
|
|
3165
3200
|
exports.storeService = storeService;
|
|
3166
3201
|
exports.toPage = toPage;
|
|
3167
3202
|
exports.uploadService = uploadService;
|