@usequeek/theme-kit 0.1.36 → 0.1.38

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
@@ -250,7 +250,8 @@ Themes set none of these; the storefront host does.
250
250
  | --- | --- |
251
251
  | `QUEEK_API_URL` (or `NEXT_PUBLIC_QUEEK_API_URL`) | Origin of the Queek API for server-side reads. Without it the kit falls back to `http://127.0.0.1:8000/api/v1` and warns once in production builds. |
252
252
  | `NEXT_PUBLIC_QUEEK_AUTH_URL` | Origin of the sign-in service (default `https://auth.usequeek.com`). |
253
- | `STOREFRONT_DOMAIN` | Domain whose subdomains are store slugs (default `queek.com.ng`). |
253
+ | `STOREFRONT_DOMAIN` | The primary store domain: stores live at `{slug}.<domain>` and the URLs the storefront emits use it (default `queek.com.ng`). |
254
+ | `STOREFRONT_LEGACY_DOMAINS` | Comma list of former store domains that still resolve a store (e.g. `queek.com.ng` after a move). The host app redirects them to the primary domain; the kit names the primary one in canonical URLs. Unset = none. |
254
255
  | `CUSTOM_DOMAIN_VENDOR_MAP` | JSON object mapping a custom domain to a store slug. |
255
256
 
256
257
  Browser requests are always same-origin (`/api/v1/client/*`), so the host needs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usequeek/theme-kit",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "The runtime a Queek storefront theme is built on: hooks, stores, page renderer, navigation, shared blocks and locale strings.",
@@ -1,5 +1,5 @@
1
1
  import { cookies, headers } from 'next/headers';
2
- import { resolveCustomDomainVendorSlug, resolveCustomDomainVendorSlugRemote, resolveSubdomainVendorSlug } from '../utils/vendor-host';
2
+ import { resolveCustomDomainVendorSlug, resolveCustomDomainVendorSlugRemote, resolveSubdomainVendorSlug, toPrimaryStoreHost } from '../utils/vendor-host';
3
3
  import { LOCALE_CODE_HEADER, parseLocaleCode, withLocaleQuery, withLocaleTags } from '../utils/locale';
4
4
  import { isMarketPricedPath, readMarketFromCookieString, withMarketQuery } from '../utils/market';
5
5
  import { API_BASE_URL, request, type RequestCachePolicy, type RequestOptions } from './client';
@@ -28,6 +28,9 @@ export async function getRequestOrigin(): Promise<string | undefined> {
28
28
  * storefronts serve at the bare origin; path-based access serves under
29
29
  * `/vendor-slug`). Every canonical URL, sitemap entry, and JSON-LD `url` field
30
30
  * must derive from this instead of re-deriving `basePath` inline.
31
+ *
32
+ * The host is the request's, except that a legacy store domain is named by the
33
+ * primary one (see `STOREFRONT_LEGACY_DOMAINS`).
31
34
  */
32
35
  export async function resolveStoreBaseUrl(vendor: string): Promise<string | null> {
33
36
  const origin = await getRequestOrigin();
@@ -42,7 +45,11 @@ export async function resolveStoreBaseUrl(vendor: string): Promise<string | null
42
45
  );
43
46
  const basePath = isSubdomain ? '' : `/${vendor}`;
44
47
 
45
- return `${origin}${basePath}`;
48
+ // A store reached on a legacy store domain (`STOREFRONT_LEGACY_DOMAINS`) is still
49
+ // addressed by the primary one: the URL we emit must be the one to index and share.
50
+ const canonicalOrigin = origin.replace(/^([a-z][a-z0-9+.-]*:\/\/)(.+)$/i, (_match, scheme: string, authority: string) => `${scheme}${toPrimaryStoreHost(authority)}`);
51
+
52
+ return `${canonicalOrigin}${basePath}`;
46
53
  }
47
54
 
48
55
  /**
package/utils/market.ts CHANGED
@@ -150,6 +150,8 @@ export const MARKET_ERROR_CODES = [
150
150
  'market_rate_expired',
151
151
  // 422 at the quote and the order (and cart `market.unavailable`): the market total is zero or below the provider minimum.
152
152
  'market_total_unusable',
153
+ // 422 at the cart code, the quote and the order: a discount code is refused in a fixed-price market (codes are shop-currency money). Remove the code.
154
+ 'market_coupon_unavailable',
153
155
  ] as const;
154
156
 
155
157
  export type MarketErrorCode = (typeof MARKET_ERROR_CODES)[number];
@@ -44,9 +44,98 @@ export function shouldBypassVendorProxy(pathname: string): boolean {
44
44
  return pathname.startsWith('/_next') || pathname.startsWith('/api') || pathname.includes('.');
45
45
  }
46
46
 
47
+ /** The store domain an unset `STOREFRONT_DOMAIN` has always meant. */
48
+ export const DEFAULT_STOREFRONT_DOMAIN = 'queek.com.ng';
49
+
50
+ const HOST_LABEL = /^[a-z0-9]+(-[a-z0-9]+)*$/;
51
+ const DOMAIN_FORMAT = /^[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9]+(-[a-z0-9]+)*)+$/;
52
+
53
+ /**
54
+ * The primary store domain: stores live at `{slug}.<this>` and every URL the
55
+ * storefront emits uses it. `STOREFRONT_DOMAIN`, else `queek.com.ng` — an unset
56
+ * env keeps today's behaviour exactly.
57
+ */
58
+ export function getStorefrontDomain(): string {
59
+ const configured = process.env.STOREFRONT_DOMAIN?.trim().toLowerCase().replace(/^\.+|\.+$/g, '');
60
+
61
+ return configured || DEFAULT_STOREFRONT_DOMAIN;
62
+ }
63
+
64
+ /**
65
+ * Store domains that still RESOLVE a store but are no longer the address:
66
+ * `STOREFRONT_LEGACY_DOMAINS`, a comma list (`queek.com.ng`). Empty when unset,
67
+ * so nothing is a legacy host until the new domain is switched on. The primary
68
+ * domain, duplicates and anything that is not a bare hostname are dropped.
69
+ */
70
+ export function getLegacyStorefrontDomains(): string[] {
71
+ const raw = process.env.STOREFRONT_LEGACY_DOMAINS;
72
+ if (!raw) return [];
73
+
74
+ const primary = getStorefrontDomain();
75
+ const domains = new Set<string>();
76
+ for (const entry of raw.split(',')) {
77
+ const domain = entry.trim().toLowerCase().replace(/^\*?\.+|\.+$/g, '');
78
+ if (domain !== primary && DOMAIN_FORMAT.test(domain)) domains.add(domain);
79
+ }
80
+
81
+ return [...domains];
82
+ }
83
+
84
+ /** Primary first, then the legacy domains: every suffix a store host may end in. */
85
+ export function getStoreDomains(): string[] {
86
+ return [getStorefrontDomain(), ...getLegacyStorefrontDomains()];
87
+ }
88
+
89
+ /** The suffixes (with the leading dot) a hostname is matched against; `.localhost` is the dev host. */
90
+ function storeHostSuffixes(): string[] {
91
+ return [...getStoreDomains().map((domain) => `.${domain}`), '.localhost'];
92
+ }
93
+
94
+ /** The one label in front of a legacy domain, when `hostname` is `<label>.<legacy domain>`. */
95
+ function legacyHostLabel(hostname: string): string | null {
96
+ const host = hostname.toLowerCase();
97
+ for (const domain of getLegacyStorefrontDomains()) {
98
+ const suffix = `.${domain}`;
99
+ if (!host.endsWith(suffix)) continue;
100
+
101
+ const label = host.slice(0, -suffix.length);
102
+
103
+ return HOST_LABEL.test(label) ? label : null;
104
+ }
105
+
106
+ return null;
107
+ }
108
+
109
+ /**
110
+ * Where a request for `{label}.<legacy domain>` belongs: the same label on the
111
+ * primary store domain (`acme.queek.com.ng` → `acme.queek.app`), or `null` when
112
+ * the host is not on a legacy domain (the primary domain, a custom domain,
113
+ * `localhost`, a bare apex, anything else) — so there is never a loop, and with
114
+ * `STOREFRONT_LEGACY_DOMAINS` unset there is never a redirect.
115
+ *
116
+ * It is every single label, not only store slugs: `preview.`, `themes.` and the
117
+ * reserved names move with their domain, so a link to any of them keeps working.
118
+ */
119
+ export function legacyHostRedirectTarget(hostname: string): string | null {
120
+ const label = legacyHostLabel(hostname);
121
+
122
+ return label === null ? null : `${label}.${getStorefrontDomain()}`;
123
+ }
124
+
125
+ /**
126
+ * Re-points a URL's host from a legacy store domain to the primary one, so what
127
+ * the storefront emits (canonical, sitemap, OG, JSON-LD) never names the old
128
+ * address. A URL on any other host is returned unchanged.
129
+ */
130
+ export function toPrimaryStoreHost(host: string): string {
131
+ const [hostname, ...port] = host.split(':');
132
+ const target = legacyHostRedirectTarget(hostname);
133
+
134
+ return target === null ? host : [target, ...port].join(':');
135
+ }
136
+
47
137
  export function resolveSubdomainVendorSlug(hostname: string): string | null {
48
- const storefrontDomain = process.env.STOREFRONT_DOMAIN ?? 'queek.com.ng';
49
- const domains = [`.${storefrontDomain}`, '.localhost'];
138
+ const domains = storeHostSuffixes();
50
139
 
51
140
  const matchedDomain = domains.find((d) => hostname.endsWith(d));
52
141
  if (!matchedDomain) {
@@ -132,8 +221,7 @@ function logRemoteFailure(reason: string): void {
132
221
  }
133
222
 
134
223
  export function isPreviewSubdomain(hostname: string): boolean {
135
- const storefrontDomain = process.env.STOREFRONT_DOMAIN ?? 'queek.com.ng';
136
- const domains = [`.${storefrontDomain}`, '.localhost'];
224
+ const domains = storeHostSuffixes();
137
225
 
138
226
  return domains.some((d) => {
139
227
  if (!hostname.endsWith(d)) return false;