@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 CHANGED
@@ -26,6 +26,7 @@ PUBLIC_GOCOMMERCE_API_URL=https://api.shop.example # the engine
26
26
  PUBLIC_GOCOMMERCE_PROXY_PATH=/gocommerce # see "The browser and CORS"
27
27
  # PUBLIC_GOCOMMERCE_CHANNEL=web # a sales channel, if the store has several
28
28
  # PUBLIC_GOCOMMERCE_STORE=acme # under `gocommerce platform`, on a shared API host
29
+ # PUBLIC_GOCOMMERCE_BASE_DOMAIN=shopnx.in # or every store: acme.shopnx.in is store `acme`
29
30
  # PUBLIC_GOCOMMERCE_LANGUAGE=fr # ask for this language (?lang=) on every read
30
31
  ```
31
32
 
@@ -77,6 +78,33 @@ location / { proxy_pass http://storefront:3000; }
77
78
  Serving the storefront and the engine from one origin works too; then leave the
78
79
  proxy path unset and point the API URL at that origin.
79
80
 
81
+ ### One storefront for every store
82
+
83
+ Under `gocommerce platform` a storefront can sell for one store, named in
84
+ `PUBLIC_GOCOMMERCE_STORE`, or for all of them. Set `PUBLIC_GOCOMMERCE_BASE_DOMAIN`
85
+ to the domain the engine hands out subdomains of and point every
86
+ `<slug>.<base domain>` at the one deployment: a request for `acme.shopnx.in` is
87
+ sent to the engine with `X-Store: acme`, by the same rule the engine applies to
88
+ its own host (one label, case-insensitive, a port ignored). A host outside the
89
+ base domain falls back to `PUBLIC_GOCOMMERCE_STORE`, or to no store at all.
90
+
91
+ The browser knows its own host. The server does not, so tell the connector where
92
+ to read it, once, from `src/hooks.server.ts`:
93
+
94
+ ```ts
95
+ import { getRequestEvent } from '$app/server'
96
+ import { setRequestHost } from '@misiki/gocommerce-connector'
97
+
98
+ setRequestHost(() => getRequestEvent().url.hostname)
99
+ ```
100
+
101
+ Store-level facts (plugins, currency, menus) are cached per store, so two stores
102
+ on one process never see each other's. A host under the base domain that names
103
+ a store the engine does not have makes `getStoreByIdOrDomain` throw a
104
+ `GoCommerceError` with status 404 and "store not found" in its message, which
105
+ Svelte Commerce renders as its own "Store not found" page. An engine that is
106
+ down stays best-effort: the storefront renders from its static record.
107
+
80
108
  ## What maps to what
81
109
 
82
110
  | Storefront | Engine |
@@ -31,6 +31,16 @@ interface Credentials {
31
31
  * shared API host. Leave unset when the store has a host of its own.
32
32
  */
33
33
  store?: string;
34
+ /**
35
+ * `PUBLIC_GOCOMMERCE_BASE_DOMAIN` — under `gocommerce platform`, the domain
36
+ * every store gets a subdomain of, `shopnx.in`. One storefront deployment
37
+ * then serves them all: a request for `acme.shopnx.in` is sent with
38
+ * `X-Store: acme`, the same rule the engine applies to its own host. A host
39
+ * outside the base domain falls back to `store`. Server-side the connector
40
+ * learns the request's host through `setRequestHost`; in the browser it is
41
+ * `window.location.hostname`.
42
+ */
43
+ baseDomain?: string;
34
44
  /**
35
45
  * `PUBLIC_GOCOMMERCE_CHANNEL` — the sales channel new carts and the product
36
46
  * listing belong to, when the store sells through more than one.
@@ -1163,6 +1173,13 @@ declare class BaseService {
1163
1173
  static setCredentials(creds: Partial<Credentials>): void;
1164
1174
  static getCredentials(): Credentials;
1165
1175
  protected get creds(): Credentials;
1176
+ /**
1177
+ * The store this call is for: the request host's subdomain under
1178
+ * `baseDomain`, else the static `store`. Read per call, never stored on the
1179
+ * instance, because the server shares one process (and one credentials
1180
+ * object) across every concurrent request and each may be for a different host.
1181
+ */
1182
+ protected get store(): string | undefined;
1166
1183
  /**
1167
1184
  * The engine's base URL from wherever this code runs. In the browser a
1168
1185
  * configured proxy path wins, because the engine answers no CORS preflight.
@@ -1200,6 +1217,19 @@ declare class BaseService {
1200
1217
  delete<T = any>(path: string): Promise<T>;
1201
1218
  private raw;
1202
1219
  }
1220
+ /**
1221
+ * Where the server learns the current request's host, for `baseDomain`. A
1222
+ * SvelteKit storefront registers `() => getRequestEvent().url.hostname` once
1223
+ * at boot; the browser needs nothing, it reads `window.location`.
1224
+ */
1225
+ declare function setRequestHost(fn: () => string | undefined): void;
1226
+ /**
1227
+ * `acme.shopnx.in` under `shopnx.in` is store `acme`; anything else is nobody's.
1228
+ * The engine's own rule (`Stores.ByHost`): case-insensitive, a port and a
1229
+ * trailing dot ignored, and exactly one label, so `a.b.shopnx.in` and
1230
+ * `shopnx.in` itself name no store.
1231
+ */
1232
+ declare function storeForHost(host: string | undefined, baseDomain: string | undefined): string | undefined;
1203
1233
  /** `?a=1&b=2`, dropping what was not asked for. Arrays repeat the key. */
1204
1234
  declare function qs(query?: Query): string;
1205
1235
  /** The family's list shape, from the engine's `{data, meta}`. Totals are the engine's, never invented. */
@@ -6459,4 +6489,4 @@ declare class B2bService extends BaseService {
6459
6489
  }
6460
6490
  declare const b2bService: B2bService;
6461
6491
 
6462
- export { type Menu as $, type Address as A, type B2bPlaced as B, type Cart as C, CountryService as D, CouponService as E, type Credentials as F, type Credit as G, CurrencyService as H, type Customer as I, DealService as J, DemoRequestService as K, type EngineRequest as L, EnquiryService as M, type Envelope as N, type Faq as O, FaqService as P, FeedbackService as Q, type Fulfillment as R, GalleryService as S, HomeService as T, type IdentityAddress as U, InitService as V, type Invitation as W, type Lead as X, type ListMeta as Y, MeilisearchService as Z, type Member as _, AddressService as a, contactService as a$, type MenuItem as a0, MenuService as a1, type MenuSummary as a2, type Money as a3, NewsletterService as a4, type Offer as a5, type Order as a6, type OrderLine as a7, OrderService as a8, PageService as a9, type StorefrontMenu as aA, type StorefrontOrder as aB, type StorefrontProduct as aC, UploadService as aD, UserService as aE, type Variant as aF, type VariantImage as aG, VariantService as aH, VarniCustomDesignService as aI, VarniCustomProductService as aJ, type Vendor as aK, VendorService as aL, WarrantyService as aM, type Wishlist as aN, type WishlistItem as aO, WishlistService as aP, addressService as aQ, authService as aR, autocompleteService as aS, b2bService as aT, bannerService as aU, blogService as aV, cartService as aW, categoryService as aX, chatService as aY, checkoutService as aZ, collectionService as a_, type PaymentIntent as aa, PaymentMethodService as ab, type Plugin as ac, PluginService as ad, PopularSearchService as ae, PopularityService as af, type Product as ag, type ProductOption as ah, ProductService as ai, ProfileService as aj, type Quote as ak, REDIRECT_GATEWAYS as al, ReelsService as am, RegionService as an, type RejectedLine as ao, type Review as ap, type ReviewPage as aq, ReviewService as ar, type SearchDoc as as, type SearchResult as at, SearchService as au, SettingService as av, type ShippingRate as aw, StateService as ax, StoreService as ay, type StorefrontCategory as az, type AppliedDiscount as b, varniCustomProductService as b$, countryService as b0, couponService as b1, currencyExponent as b2, currencyService as b3, dealService as b4, demoRequestService as b5, emptyCart as b6, emptyPage as b7, enquiryService as b8, faqService as b9, reviewService as bA, searchService as bB, settingService as bC, settingsService as bD, stateService as bE, staticStore as bF, storeService as bG, toCart as bH, toCartLine as bI, toCategory as bJ, toCmsPage as bK, toCollection as bL, toEngineAddress as bM, toIdentityAddress as bN, toMajor as bO, toMinor as bP, toOrder as bQ, toPage as bR, toProduct as bS, toRating as bT, toUser as bU, toVariant as bV, toVendor as bW, uploadService as bX, userService as bY, variantService as bZ, varniCustomDesignService as b_, feedbackService as ba, fromEngineAddress as bb, fromIdentityAddress as bc, fromSearchDoc as bd, galleryService as be, homeService as bf, initService as bg, mediaUrl as bh, meilisearchService as bi, menuService as bj, minorToMajor as bk, newsletterService as bl, orderService as bm, pageService as bn, paymentMethodService as bo, pluginService as bp, pluginsService as bq, popularSearchService as br, popularityService as bs, priceOf as bt, productImages as bu, productService as bv, profileService as bw, qs as bx, reelsService as by, regionService as bz, type Approval as c, vendorService as c0, warrantyService as c1, wishlistService as c2, type AuthResponse as d, AuthService as e, AutocompleteService as f, type B2bRole as g, B2bService as h, BannerService as i, BaseService as j, BlogService as k, type CartLine as l, CartService as m, CatalogService as n, type Category as o, CategoryService as p, ChatService as q, type CheckoutOptions as r, type CheckoutResult as s, CheckoutService as t, type CmsPage as u, type Collection as v, CollectionService as w, type Company as x, type CompanyOrder as y, ContactService as z };
6492
+ export { type Menu as $, type Address as A, type B2bPlaced as B, type Cart as C, CountryService as D, CouponService as E, type Credentials as F, type Credit as G, CurrencyService as H, type Customer as I, DealService as J, DemoRequestService as K, type EngineRequest as L, EnquiryService as M, type Envelope as N, type Faq as O, FaqService as P, FeedbackService as Q, type Fulfillment as R, GalleryService as S, HomeService as T, type IdentityAddress as U, InitService as V, type Invitation as W, type Lead as X, type ListMeta as Y, MeilisearchService as Z, type Member as _, AddressService as a, contactService as a$, type MenuItem as a0, MenuService as a1, type MenuSummary as a2, type Money as a3, NewsletterService as a4, type Offer as a5, type Order as a6, type OrderLine as a7, OrderService as a8, PageService as a9, type StorefrontMenu as aA, type StorefrontOrder as aB, type StorefrontProduct as aC, UploadService as aD, UserService as aE, type Variant as aF, type VariantImage as aG, VariantService as aH, VarniCustomDesignService as aI, VarniCustomProductService as aJ, type Vendor as aK, VendorService as aL, WarrantyService as aM, type Wishlist as aN, type WishlistItem as aO, WishlistService as aP, addressService as aQ, authService as aR, autocompleteService as aS, b2bService as aT, bannerService as aU, blogService as aV, cartService as aW, categoryService as aX, chatService as aY, checkoutService as aZ, collectionService as a_, type PaymentIntent as aa, PaymentMethodService as ab, type Plugin as ac, PluginService as ad, PopularSearchService as ae, PopularityService as af, type Product as ag, type ProductOption as ah, ProductService as ai, ProfileService as aj, type Quote as ak, REDIRECT_GATEWAYS as al, ReelsService as am, RegionService as an, type RejectedLine as ao, type Review as ap, type ReviewPage as aq, ReviewService as ar, type SearchDoc as as, type SearchResult as at, SearchService as au, SettingService as av, type ShippingRate as aw, StateService as ax, StoreService as ay, type StorefrontCategory as az, type AppliedDiscount as b, variantService as b$, countryService as b0, couponService as b1, currencyExponent as b2, currencyService as b3, dealService as b4, demoRequestService as b5, emptyCart as b6, emptyPage as b7, enquiryService as b8, faqService as b9, reviewService as bA, searchService as bB, setRequestHost as bC, settingService as bD, settingsService as bE, stateService as bF, staticStore as bG, storeForHost as bH, storeService as bI, toCart as bJ, toCartLine as bK, toCategory as bL, toCmsPage as bM, toCollection as bN, toEngineAddress as bO, toIdentityAddress as bP, toMajor as bQ, toMinor as bR, toOrder as bS, toPage as bT, toProduct as bU, toRating as bV, toUser as bW, toVariant as bX, toVendor as bY, uploadService as bZ, userService as b_, feedbackService as ba, fromEngineAddress as bb, fromIdentityAddress as bc, fromSearchDoc as bd, galleryService as be, homeService as bf, initService as bg, mediaUrl as bh, meilisearchService as bi, menuService as bj, minorToMajor as bk, newsletterService as bl, orderService as bm, pageService as bn, paymentMethodService as bo, pluginService as bp, pluginsService as bq, popularSearchService as br, popularityService as bs, priceOf as bt, productImages as bu, productService as bv, profileService as bw, qs as bx, reelsService as by, regionService as bz, type Approval as c, varniCustomDesignService as c0, varniCustomProductService as c1, vendorService as c2, warrantyService as c3, wishlistService as c4, type AuthResponse as d, AuthService as e, AutocompleteService as f, type B2bRole as g, B2bService as h, BannerService as i, BaseService as j, BlogService as k, type CartLine as l, CartService as m, CatalogService as n, type Category as o, CategoryService as p, ChatService as q, type CheckoutOptions as r, type CheckoutResult as s, CheckoutService as t, type CmsPage as u, type Collection as v, CollectionService as w, type Company as x, type CompanyOrder as y, ContactService as z };
@@ -31,6 +31,16 @@ interface Credentials {
31
31
  * shared API host. Leave unset when the store has a host of its own.
32
32
  */
33
33
  store?: string;
34
+ /**
35
+ * `PUBLIC_GOCOMMERCE_BASE_DOMAIN` — under `gocommerce platform`, the domain
36
+ * every store gets a subdomain of, `shopnx.in`. One storefront deployment
37
+ * then serves them all: a request for `acme.shopnx.in` is sent with
38
+ * `X-Store: acme`, the same rule the engine applies to its own host. A host
39
+ * outside the base domain falls back to `store`. Server-side the connector
40
+ * learns the request's host through `setRequestHost`; in the browser it is
41
+ * `window.location.hostname`.
42
+ */
43
+ baseDomain?: string;
34
44
  /**
35
45
  * `PUBLIC_GOCOMMERCE_CHANNEL` — the sales channel new carts and the product
36
46
  * listing belong to, when the store sells through more than one.
@@ -1163,6 +1173,13 @@ declare class BaseService {
1163
1173
  static setCredentials(creds: Partial<Credentials>): void;
1164
1174
  static getCredentials(): Credentials;
1165
1175
  protected get creds(): Credentials;
1176
+ /**
1177
+ * The store this call is for: the request host's subdomain under
1178
+ * `baseDomain`, else the static `store`. Read per call, never stored on the
1179
+ * instance, because the server shares one process (and one credentials
1180
+ * object) across every concurrent request and each may be for a different host.
1181
+ */
1182
+ protected get store(): string | undefined;
1166
1183
  /**
1167
1184
  * The engine's base URL from wherever this code runs. In the browser a
1168
1185
  * configured proxy path wins, because the engine answers no CORS preflight.
@@ -1200,6 +1217,19 @@ declare class BaseService {
1200
1217
  delete<T = any>(path: string): Promise<T>;
1201
1218
  private raw;
1202
1219
  }
1220
+ /**
1221
+ * Where the server learns the current request's host, for `baseDomain`. A
1222
+ * SvelteKit storefront registers `() => getRequestEvent().url.hostname` once
1223
+ * at boot; the browser needs nothing, it reads `window.location`.
1224
+ */
1225
+ declare function setRequestHost(fn: () => string | undefined): void;
1226
+ /**
1227
+ * `acme.shopnx.in` under `shopnx.in` is store `acme`; anything else is nobody's.
1228
+ * The engine's own rule (`Stores.ByHost`): case-insensitive, a port and a
1229
+ * trailing dot ignored, and exactly one label, so `a.b.shopnx.in` and
1230
+ * `shopnx.in` itself name no store.
1231
+ */
1232
+ declare function storeForHost(host: string | undefined, baseDomain: string | undefined): string | undefined;
1203
1233
  /** `?a=1&b=2`, dropping what was not asked for. Arrays repeat the key. */
1204
1234
  declare function qs(query?: Query): string;
1205
1235
  /** The family's list shape, from the engine's `{data, meta}`. Totals are the engine's, never invented. */
@@ -6459,4 +6489,4 @@ declare class B2bService extends BaseService {
6459
6489
  }
6460
6490
  declare const b2bService: B2bService;
6461
6491
 
6462
- export { type Menu as $, type Address as A, type B2bPlaced as B, type Cart as C, CountryService as D, CouponService as E, type Credentials as F, type Credit as G, CurrencyService as H, type Customer as I, DealService as J, DemoRequestService as K, type EngineRequest as L, EnquiryService as M, type Envelope as N, type Faq as O, FaqService as P, FeedbackService as Q, type Fulfillment as R, GalleryService as S, HomeService as T, type IdentityAddress as U, InitService as V, type Invitation as W, type Lead as X, type ListMeta as Y, MeilisearchService as Z, type Member as _, AddressService as a, contactService as a$, type MenuItem as a0, MenuService as a1, type MenuSummary as a2, type Money as a3, NewsletterService as a4, type Offer as a5, type Order as a6, type OrderLine as a7, OrderService as a8, PageService as a9, type StorefrontMenu as aA, type StorefrontOrder as aB, type StorefrontProduct as aC, UploadService as aD, UserService as aE, type Variant as aF, type VariantImage as aG, VariantService as aH, VarniCustomDesignService as aI, VarniCustomProductService as aJ, type Vendor as aK, VendorService as aL, WarrantyService as aM, type Wishlist as aN, type WishlistItem as aO, WishlistService as aP, addressService as aQ, authService as aR, autocompleteService as aS, b2bService as aT, bannerService as aU, blogService as aV, cartService as aW, categoryService as aX, chatService as aY, checkoutService as aZ, collectionService as a_, type PaymentIntent as aa, PaymentMethodService as ab, type Plugin as ac, PluginService as ad, PopularSearchService as ae, PopularityService as af, type Product as ag, type ProductOption as ah, ProductService as ai, ProfileService as aj, type Quote as ak, REDIRECT_GATEWAYS as al, ReelsService as am, RegionService as an, type RejectedLine as ao, type Review as ap, type ReviewPage as aq, ReviewService as ar, type SearchDoc as as, type SearchResult as at, SearchService as au, SettingService as av, type ShippingRate as aw, StateService as ax, StoreService as ay, type StorefrontCategory as az, type AppliedDiscount as b, varniCustomProductService as b$, countryService as b0, couponService as b1, currencyExponent as b2, currencyService as b3, dealService as b4, demoRequestService as b5, emptyCart as b6, emptyPage as b7, enquiryService as b8, faqService as b9, reviewService as bA, searchService as bB, settingService as bC, settingsService as bD, stateService as bE, staticStore as bF, storeService as bG, toCart as bH, toCartLine as bI, toCategory as bJ, toCmsPage as bK, toCollection as bL, toEngineAddress as bM, toIdentityAddress as bN, toMajor as bO, toMinor as bP, toOrder as bQ, toPage as bR, toProduct as bS, toRating as bT, toUser as bU, toVariant as bV, toVendor as bW, uploadService as bX, userService as bY, variantService as bZ, varniCustomDesignService as b_, feedbackService as ba, fromEngineAddress as bb, fromIdentityAddress as bc, fromSearchDoc as bd, galleryService as be, homeService as bf, initService as bg, mediaUrl as bh, meilisearchService as bi, menuService as bj, minorToMajor as bk, newsletterService as bl, orderService as bm, pageService as bn, paymentMethodService as bo, pluginService as bp, pluginsService as bq, popularSearchService as br, popularityService as bs, priceOf as bt, productImages as bu, productService as bv, profileService as bw, qs as bx, reelsService as by, regionService as bz, type Approval as c, vendorService as c0, warrantyService as c1, wishlistService as c2, type AuthResponse as d, AuthService as e, AutocompleteService as f, type B2bRole as g, B2bService as h, BannerService as i, BaseService as j, BlogService as k, type CartLine as l, CartService as m, CatalogService as n, type Category as o, CategoryService as p, ChatService as q, type CheckoutOptions as r, type CheckoutResult as s, CheckoutService as t, type CmsPage as u, type Collection as v, CollectionService as w, type Company as x, type CompanyOrder as y, ContactService as z };
6492
+ export { type Menu as $, type Address as A, type B2bPlaced as B, type Cart as C, CountryService as D, CouponService as E, type Credentials as F, type Credit as G, CurrencyService as H, type Customer as I, DealService as J, DemoRequestService as K, type EngineRequest as L, EnquiryService as M, type Envelope as N, type Faq as O, FaqService as P, FeedbackService as Q, type Fulfillment as R, GalleryService as S, HomeService as T, type IdentityAddress as U, InitService as V, type Invitation as W, type Lead as X, type ListMeta as Y, MeilisearchService as Z, type Member as _, AddressService as a, contactService as a$, type MenuItem as a0, MenuService as a1, type MenuSummary as a2, type Money as a3, NewsletterService as a4, type Offer as a5, type Order as a6, type OrderLine as a7, OrderService as a8, PageService as a9, type StorefrontMenu as aA, type StorefrontOrder as aB, type StorefrontProduct as aC, UploadService as aD, UserService as aE, type Variant as aF, type VariantImage as aG, VariantService as aH, VarniCustomDesignService as aI, VarniCustomProductService as aJ, type Vendor as aK, VendorService as aL, WarrantyService as aM, type Wishlist as aN, type WishlistItem as aO, WishlistService as aP, addressService as aQ, authService as aR, autocompleteService as aS, b2bService as aT, bannerService as aU, blogService as aV, cartService as aW, categoryService as aX, chatService as aY, checkoutService as aZ, collectionService as a_, type PaymentIntent as aa, PaymentMethodService as ab, type Plugin as ac, PluginService as ad, PopularSearchService as ae, PopularityService as af, type Product as ag, type ProductOption as ah, ProductService as ai, ProfileService as aj, type Quote as ak, REDIRECT_GATEWAYS as al, ReelsService as am, RegionService as an, type RejectedLine as ao, type Review as ap, type ReviewPage as aq, ReviewService as ar, type SearchDoc as as, type SearchResult as at, SearchService as au, SettingService as av, type ShippingRate as aw, StateService as ax, StoreService as ay, type StorefrontCategory as az, type AppliedDiscount as b, variantService as b$, countryService as b0, couponService as b1, currencyExponent as b2, currencyService as b3, dealService as b4, demoRequestService as b5, emptyCart as b6, emptyPage as b7, enquiryService as b8, faqService as b9, reviewService as bA, searchService as bB, setRequestHost as bC, settingService as bD, settingsService as bE, stateService as bF, staticStore as bG, storeForHost as bH, storeService as bI, toCart as bJ, toCartLine as bK, toCategory as bL, toCmsPage as bM, toCollection as bN, toEngineAddress as bO, toIdentityAddress as bP, toMajor as bQ, toMinor as bR, toOrder as bS, toPage as bT, toProduct as bU, toRating as bV, toUser as bW, toVariant as bX, toVendor as bY, uploadService as bZ, userService as b_, feedbackService as ba, fromEngineAddress as bb, fromIdentityAddress as bc, fromSearchDoc as bd, galleryService as be, homeService as bf, initService as bg, mediaUrl as bh, meilisearchService as bi, menuService as bj, minorToMajor as bk, newsletterService as bl, orderService as bm, pageService as bn, paymentMethodService as bo, pluginService as bp, pluginsService as bq, popularSearchService as br, popularityService as bs, priceOf as bt, productImages as bu, productService as bv, profileService as bw, qs as bx, reelsService as by, regionService as bz, type Approval as c, varniCustomDesignService as c0, varniCustomProductService as c1, vendorService as c2, warrantyService as c3, wishlistService as c4, type AuthResponse as d, AuthService as e, AutocompleteService as f, type B2bRole as g, B2bService as h, BannerService as i, BaseService as j, BlogService as k, type CartLine as l, CartService as m, CatalogService as n, type Category as o, CategoryService as p, ChatService as q, type CheckoutOptions as r, type CheckoutResult as s, CheckoutService as t, type CmsPage as u, type Collection as v, CollectionService as w, type Company as x, type CompanyOrder as y, ContactService as z };
package/dist/index.cjs CHANGED
@@ -285,6 +285,17 @@ var BaseService = class _BaseService {
285
285
  get creds() {
286
286
  return _BaseService._credentials;
287
287
  }
288
+ /**
289
+ * The store this call is for: the request host's subdomain under
290
+ * `baseDomain`, else the static `store`. Read per call, never stored on the
291
+ * instance, because the server shares one process (and one credentials
292
+ * object) across every concurrent request and each may be for a different host.
293
+ */
294
+ get store() {
295
+ const { baseDomain, store } = this.creds;
296
+ const host = typeof window !== "undefined" ? window.location?.hostname : requestHost?.();
297
+ return storeForHost(host, baseDomain) ?? store;
298
+ }
288
299
  /**
289
300
  * The engine's base URL from wherever this code runs. In the browser a
290
301
  * configured proxy path wins, because the engine answers no CORS preflight.
@@ -318,7 +329,8 @@ var BaseService = class _BaseService {
318
329
  const url = this.engineURL(path, lang ? { lang, ...req.query } : req.query);
319
330
  const headers = { Accept: "application/json", ...req.headers };
320
331
  if (req.body !== void 0) headers["Content-Type"] = "application/json";
321
- if (this.creds.store) headers["X-Store"] = this.creds.store;
332
+ const store = this.store;
333
+ if (store) headers["X-Store"] = store;
322
334
  if (req.auth) {
323
335
  const token = sessionToken();
324
336
  if (!token) throw new GoCommerceError("Please sign in to continue.", 401, "unauthorized");
@@ -420,6 +432,18 @@ var BaseService = class _BaseService {
420
432
  return body;
421
433
  }
422
434
  };
435
+ var requestHost;
436
+ function setRequestHost(fn) {
437
+ requestHost = fn;
438
+ }
439
+ function storeForHost(host, baseDomain) {
440
+ if (!host || !baseDomain) return void 0;
441
+ const h = host.toLowerCase().replace(/\.$/, "").replace(/:\d+$/, "");
442
+ const suffix = "." + baseDomain.toLowerCase().replace(/^\.+/, "").replace(/\.$/, "");
443
+ if (!h.endsWith(suffix)) return void 0;
444
+ const slug = h.slice(0, -suffix.length);
445
+ return slug && !slug.includes(".") ? slug : void 0;
446
+ }
423
447
  var unreachable = (base, cause) => new GoCommerceError(`Could not reach the commerce engine at ${base}: ${cause?.message ?? cause}`, 0);
424
448
  function qs(query) {
425
449
  if (!query) return "";
@@ -967,7 +991,7 @@ var priceOf = (m) => m ? toMajor(m) : 0;
967
991
  // src/services/facts.ts
968
992
  var StoreFacts = class extends BaseService {
969
993
  key(name) {
970
- return `${this.engineBase()}|${this.creds.store ?? ""}|${name}`;
994
+ return `${this.engineBase()}|${this.store ?? ""}|${name}`;
971
995
  }
972
996
  /** Enabled plugins by key. A module that is not installed has no key here. */
973
997
  plugins() {
@@ -2397,7 +2421,7 @@ var MenuService = class extends BaseService {
2397
2421
  /** The header and footer menus, or `null` when the module is not there. Cached: a menu is store-level. */
2398
2422
  async engineMenus() {
2399
2423
  if (!await this.facts.pluginOn("navigation")) return null;
2400
- return remember(`${this.engineBase()}|${this.creds.store ?? ""}|menus`, async () => {
2424
+ return remember(`${this.engineBase()}|${this.store ?? ""}|menus`, async () => {
2401
2425
  const out = [];
2402
2426
  for (const handle of ["header", "footer"]) {
2403
2427
  try {
@@ -2431,6 +2455,7 @@ var NewsletterService = class extends BaseService {
2431
2455
  var newsletterService = new NewsletterService();
2432
2456
 
2433
2457
  // src/services/store-service.ts
2458
+ var isStoreMissing = (err) => err instanceof GoCommerceError && err.status === 404 && (err.code === "store_not_found" || /store not found|no such store/i.test(err.message));
2434
2459
  function symbolFor(code) {
2435
2460
  try {
2436
2461
  const part = new Intl.NumberFormat("en", { style: "currency", currency: code }).formatToParts(0).find((p) => p.type === "currency");
@@ -2443,6 +2468,14 @@ var StoreService = class extends BaseService {
2443
2468
  facts = new StoreFacts(this._fetch);
2444
2469
  async getStoreByIdOrDomain(_args = {}) {
2445
2470
  const base = await readStaticStore();
2471
+ const store = this.store;
2472
+ if (store) {
2473
+ await this.facts.plugins().catch((err) => {
2474
+ if (isStoreMissing(err)) {
2475
+ throw new GoCommerceError(`store not found: no store "${store}" on this engine`, 404, "store_not_found");
2476
+ }
2477
+ });
2478
+ }
2446
2479
  const live = await this.live().catch(() => ({}));
2447
2480
  return { ...base, ...live, id: base.id || "gocommerce" };
2448
2481
  }
@@ -3189,11 +3222,13 @@ exports.regionService = regionService;
3189
3222
  exports.reviewService = reviewService;
3190
3223
  exports.searchService = searchService;
3191
3224
  exports.serveRestLocally = serveRestLocally;
3225
+ exports.setRequestHost = setRequestHost;
3192
3226
  exports.setStaticStore = setStaticStore;
3193
3227
  exports.settingService = settingService;
3194
3228
  exports.settingsService = settingsService;
3195
3229
  exports.stateService = stateService;
3196
3230
  exports.staticStore = staticStore;
3231
+ exports.storeForHost = storeForHost;
3197
3232
  exports.storeService = storeService;
3198
3233
  exports.toCart = toCart;
3199
3234
  exports.toCartLine = toCartLine;