@ecomweb/storefront-kit 0.10.0 → 0.11.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.
Files changed (45) hide show
  1. package/README.md +57 -2
  2. package/dist/api/client.js +32 -1
  3. package/dist/api/client.js.map +1 -1
  4. package/dist/api/proxy.js +29 -4
  5. package/dist/api/proxy.js.map +1 -1
  6. package/dist/api/store-preview-header.js +42 -0
  7. package/dist/api/store-preview-header.js.map +1 -0
  8. package/dist/auth/error-copy.js +2 -1
  9. package/dist/auth/error-copy.js.map +1 -1
  10. package/dist/auth/session-refresh.js +2 -0
  11. package/dist/auth/session-refresh.js.map +1 -1
  12. package/dist/cart/server-context.js +26 -0
  13. package/dist/cart/server-context.js.map +1 -1
  14. package/dist/checkout/use-checkout.js +9 -0
  15. package/dist/checkout/use-checkout.js.map +1 -1
  16. package/dist/index.d.ts +69 -3
  17. package/dist/index.js +14 -0
  18. package/dist/index.js.map +1 -1
  19. package/dist/middleware/create-storefront-middleware.js +58 -11
  20. package/dist/middleware/create-storefront-middleware.js.map +1 -1
  21. package/dist/middleware.d.ts +5 -2
  22. package/dist/provider.js +22 -0
  23. package/dist/provider.js.map +1 -1
  24. package/dist/redirects/lookup-redirect.js +2 -1
  25. package/dist/redirects/lookup-redirect.js.map +1 -1
  26. package/dist/server.d.ts +20 -1
  27. package/dist/server.js +4 -0
  28. package/dist/server.js.map +1 -1
  29. package/dist/stores/get-store-by-host.js +34 -7
  30. package/dist/stores/get-store-by-host.js.map +1 -1
  31. package/dist/stores/metadata.js +17 -0
  32. package/dist/stores/metadata.js.map +1 -1
  33. package/dist/stores/read-store-preview-token.js +10 -0
  34. package/dist/stores/read-store-preview-token.js.map +1 -0
  35. package/dist/stores/resolve-coming-soon.js +30 -0
  36. package/dist/stores/resolve-coming-soon.js.map +1 -0
  37. package/dist/stores/store-coming-soon.js +21 -0
  38. package/dist/stores/store-coming-soon.js.map +1 -0
  39. package/dist/stores/store-preview-cookie.js +64 -0
  40. package/dist/stores/store-preview-cookie.js.map +1 -0
  41. package/dist/stores/store-preview-guard.js +12 -0
  42. package/dist/stores/store-preview-guard.js.map +1 -0
  43. package/dist/stores/store-subdomain.js +10 -5
  44. package/dist/stores/store-subdomain.js.map +1 -1
  45. package/package.json +2 -2
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/stores/store-preview-cookie.ts"],"sourcesContent":["import { NextResponse, type NextRequest } from \"next/server\";\nimport {\n hostOnlyCookieName,\n isHostMappedRequest,\n secureCookies,\n} from \"../auth/cookies\";\nimport { publicNextUrl } from \"../request/public-url\";\nimport {\n THEME_PREVIEW_COOKIE,\n themePreviewCookieName,\n} from \"../theme/preview-cookie\";\n\n// No `next/headers` here: the browser bundle reaches this module (through\n// the API client), so readers take the jar and the request as arguments.\n\n/** Holds a coming soon store's preview token (the dashboard's preview link). */\nexport const STORE_PREVIEW_COOKIE = \"ecw_store_preview\";\n/** The preview link's route under the store's root. */\nexport const STORE_PREVIEW_PATH = \"/preview\";\n/** The preview link's query param carrying the token. */\nexport const STORE_PREVIEW_PARAM = \"token\";\n/** The API's request header (`X-Store-Preview`) that opens a coming soon store. */\nexport const STORE_PREVIEW_HEADER = \"x-store-preview\";\n/**\n * Internal request header carrying a theme preview host's (`p-<slug>`) store\n * preview token from the host lookup to the render, in place of the cookie.\n * Only the middleware sets it, and it deletes any value the client sent on\n * every request it lets through, as it does for `THEME_PREVIEW_HOST_HEADER`,\n * so a reader can trust it on any path the matcher covers. Root `/api/*` is\n * outside the matcher; the backend proxy ignores it there.\n */\nexport const STORE_PREVIEW_TOKEN_HEADER = \"x-ecomweb-store-preview-token\";\n\nconst MAX_TOKEN_LENGTH = 2048;\nconst MAX_AGE_SECONDS = 24 * 60 * 60;\n\n/** The store preview cookie's name at `cookiePath` (see hostOnlyCookieName). */\nexport function storePreviewCookieName(cookiePath: string): string {\n return hostOnlyCookieName(STORE_PREVIEW_COOKIE, cookiePath);\n}\n\n/**\n * The dashboard's preview link (`<store root>/preview?token=<t>`) becomes an\n * httpOnly cookie scoped to the store, then a 307 to the store's home, so the\n * token never stays in the address bar or in shared links. A missing, empty\n * or oversized token clears the cookie instead. The API validates the token\n * on every request. SameSite=Lax: the link is a top-level navigation and the\n * store is never framed during a store preview.\n */\nexport function applyStorePreview(\n req: NextRequest,\n cookiePath: string,\n storeRoot: string\n): NextResponse {\n const token = req.nextUrl.searchParams.get(STORE_PREVIEW_PARAM) ?? \"\";\n const url = publicNextUrl(req);\n url.pathname = storeRoot;\n url.search = \"\";\n const res = NextResponse.redirect(url, 307);\n const valid = token.length > 0 && token.length <= MAX_TOKEN_LENGTH;\n res.cookies.set(storePreviewCookieName(cookiePath), valid ? token : \"\", {\n httpOnly: true,\n secure: secureCookies(),\n sameSite: \"lax\",\n path: cookiePath,\n maxAge: valid ? MAX_AGE_SECONDS : 0,\n });\n return res;\n}\n\n/** Minimal shape shared by `cookies()` and `request.cookies`. */\ninterface CookieReader {\n get(name: string): { value: string } | undefined;\n}\n\n/** Minimal shape shared by `headers()`-backed requests and `NextRequest`. */\ninterface HeaderReader {\n headers: { get(name: string): string | null };\n}\n\n/**\n * The token to send as `X-Store-Preview` for this request, or null. In order:\n * the preview host's token the middleware stamped (`STORE_PREVIEW_TOKEN_HEADER`),\n * the store preview cookie, then the theme preview cookie (`?preview_theme=`\n * on the store's own host), which the API accepts too. Cookies are read under\n * the name their writer used: `/` on a host-mapped request (the `__Host-` name\n * in production, never the bare one a sibling store could plant), else the\n * bare path-scoped name. An empty value counts as none at every step.\n *\n * Trusts `STORE_PREVIEW_TOKEN_HEADER`, so call it only on a request that went\n * through the middleware (server components, route handlers), never on the\n * middleware's own incoming request.\n */\nexport function storePreviewTokenFrom(\n jar: CookieReader,\n request: HeaderReader\n): string | null {\n const stamped = request.headers.get(STORE_PREVIEW_TOKEN_HEADER);\n if (stamped) return stamped;\n const hostMapped = isHostMappedRequest(request);\n const storeCookie = hostMapped\n ? storePreviewCookieName(\"/\")\n : STORE_PREVIEW_COOKIE;\n const themeCookie = hostMapped\n ? themePreviewCookieName(\"/\")\n : THEME_PREVIEW_COOKIE;\n return jar.get(storeCookie)?.value || jar.get(themeCookie)?.value || null;\n}\n\n/**\n * Request option that forbids the kit's API client from attaching the\n * request's ambient preview token (`storePreviewTokenFrom`) as\n * `X-Store-Preview`. Spread it into the SDK call's options (the SDK's\n * `RequestOptions` takes extra keys and the kit's ApiClient hands them to\n * axios unchanged). A plain `getStoreByHost(host)` sets it, because its\n * result is cached per host for every visitor and must be the public answer.\n * An explicit `X-Store-Preview` header in the same options still goes out.\n */\nexport const SKIP_STORE_PREVIEW = Object.freeze({\n skipStorePreview: true,\n} as const);\n\n/** True when request options (or an axios config) carry SKIP_STORE_PREVIEW. */\nexport function skipsStorePreview(options: object | undefined): boolean {\n return (\n (options as { skipStorePreview?: unknown } | undefined)\n ?.skipStorePreview === true\n );\n}\n"],"mappings":"AAAA,SAAS,oBAAsC;AAC/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAMA,MAAM,uBAAuB;AAE7B,MAAM,qBAAqB;AAE3B,MAAM,sBAAsB;AAE5B,MAAM,uBAAuB;AAS7B,MAAM,6BAA6B;AAE1C,MAAM,mBAAmB;AACzB,MAAM,kBAAkB,KAAK,KAAK;AAG3B,SAAS,uBAAuB,YAA4B;AACjE,SAAO,mBAAmB,sBAAsB,UAAU;AAC5D;AAUO,SAAS,kBACd,KACA,YACA,WACc;AACd,QAAM,QAAQ,IAAI,QAAQ,aAAa,IAAI,mBAAmB,KAAK;AACnE,QAAM,MAAM,cAAc,GAAG;AAC7B,MAAI,WAAW;AACf,MAAI,SAAS;AACb,QAAM,MAAM,aAAa,SAAS,KAAK,GAAG;AAC1C,QAAM,QAAQ,MAAM,SAAS,KAAK,MAAM,UAAU;AAClD,MAAI,QAAQ,IAAI,uBAAuB,UAAU,GAAG,QAAQ,QAAQ,IAAI;AAAA,IACtE,UAAU;AAAA,IACV,QAAQ,cAAc;AAAA,IACtB,UAAU;AAAA,IACV,MAAM;AAAA,IACN,QAAQ,QAAQ,kBAAkB;AAAA,EACpC,CAAC;AACD,SAAO;AACT;AAyBO,SAAS,sBACd,KACA,SACe;AACf,QAAM,UAAU,QAAQ,QAAQ,IAAI,0BAA0B;AAC9D,MAAI,QAAS,QAAO;AACpB,QAAM,aAAa,oBAAoB,OAAO;AAC9C,QAAM,cAAc,aAChB,uBAAuB,GAAG,IAC1B;AACJ,QAAM,cAAc,aAChB,uBAAuB,GAAG,IAC1B;AACJ,SAAO,IAAI,IAAI,WAAW,GAAG,SAAS,IAAI,IAAI,WAAW,GAAG,SAAS;AACvE;AAWO,MAAM,qBAAqB,OAAO,OAAO;AAAA,EAC9C,kBAAkB;AACpB,CAAU;AAGH,SAAS,kBAAkB,SAAsC;AACtE,SACG,SACG,qBAAqB;AAE7B;","names":[]}
@@ -0,0 +1,12 @@
1
+ const STORE_PREVIEW_READ_ONLY_MESSAGE = "\u0110ang xem tr\u01B0\u1EDBc: ch\u01B0a th\u1EC3 \u0111\u1EB7t h\xE0ng khi c\u1EEDa h\xE0ng ch\u01B0a m\u1EDF c\u1EEDa.";
2
+ const STORE_PREVIEW_READ_ONLY_NOTICE = {
3
+ kind: "info",
4
+ code: "store_preview.read_only",
5
+ title: "\u0110ang xem tr\u01B0\u1EDBc",
6
+ description: "Ch\u01B0a th\u1EC3 \u0111\u1EB7t h\xE0ng khi c\u1EEDa h\xE0ng ch\u01B0a m\u1EDF c\u1EEDa."
7
+ };
8
+ export {
9
+ STORE_PREVIEW_READ_ONLY_MESSAGE,
10
+ STORE_PREVIEW_READ_ONLY_NOTICE
11
+ };
12
+ //# sourceMappingURL=store-preview-guard.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/stores/store-preview-guard.ts"],"sourcesContent":["import type { Notice } from \"../notices\";\n\n/**\n * What the shopper sees when a store preview refuses a write. A preview is\n * read-only: the API opens GET and HEAD for a preview token and answers\n * every write on a coming soon store with `STORE_COMING_SOON`. So the cart\n * and checkout refuse in the browser first, without sending the request\n * (`isBrowserStorePreview()` in `api/store-preview-header.ts`).\n *\n * `placeOrder` returns the message; the cart writes notify the notice.\n */\nexport const STORE_PREVIEW_READ_ONLY_MESSAGE =\n \"Đang xem trước: chưa thể đặt hàng khi cửa hàng chưa mở cửa.\";\n\nexport const STORE_PREVIEW_READ_ONLY_NOTICE: Notice = {\n kind: \"info\",\n code: \"store_preview.read_only\",\n title: \"Đang xem trước\",\n description: \"Chưa thể đặt hàng khi cửa hàng chưa mở cửa.\",\n};\n"],"mappings":"AAWO,MAAM,kCACX;AAEK,MAAM,iCAAyC;AAAA,EACpD,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,aAAa;AACf;","names":[]}
@@ -1,4 +1,5 @@
1
1
  import { sdk } from "../sdk.js";
2
+ import { SKIP_STORE_PREVIEW } from "./store-preview-cookie.js";
2
3
  import { TtlCache } from "./ttl-cache.js";
3
4
  const TTL_MS = 6e4;
4
5
  const MAX_REFS = 1e4;
@@ -9,11 +10,15 @@ function clearStoreSubdomainCache() {
9
10
  async function getStoreSubdomain(storeRef) {
10
11
  const cached = byRef.get(storeRef);
11
12
  if (cached !== void 0) return cached;
12
- const store = await sdk.stores.getById(storeRef, {
13
- includeConfig: true,
14
- includeSettings: false,
15
- includeEmbeddedScripts: false
16
- });
13
+ const store = await sdk.stores.getById(
14
+ storeRef,
15
+ {
16
+ includeConfig: true,
17
+ includeSettings: false,
18
+ includeEmbeddedScripts: false
19
+ },
20
+ SKIP_STORE_PREVIEW
21
+ );
17
22
  const subdomain = store?.config?.subdomain || null;
18
23
  byRef.set(storeRef, subdomain, TTL_MS);
19
24
  return subdomain;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/stores/store-subdomain.ts"],"sourcesContent":["import { sdk } from \"../sdk\";\nimport { TtlCache } from \"./ttl-cache\";\n\nconst TTL_MS = 60_000;\nconst MAX_REFS = 10_000;\n\nconst byRef = new TtlCache<string | null>(MAX_REFS);\n\n/** Tests only: forget every cached lookup. */\nexport function clearStoreSubdomainCache(): void {\n byRef.clear();\n}\n\n/**\n * The platform subdomain label of the store `storeRef`, or null when there\n * is no such store or it has none. Throws when the API cannot be reached, so\n * the caller can tell \"no store\" from \"don't know\" (only the former is\n * cached).\n */\nexport async function getStoreSubdomain(\n storeRef: string\n): Promise<string | null> {\n const cached = byRef.get(storeRef);\n if (cached !== undefined) return cached;\n\n const store = await sdk.stores.getById(storeRef, {\n includeConfig: true,\n includeSettings: false,\n includeEmbeddedScripts: false,\n });\n const subdomain = store?.config?.subdomain || null;\n byRef.set(storeRef, subdomain, TTL_MS);\n return subdomain;\n}\n"],"mappings":"AAAA,SAAS,WAAW;AACpB,SAAS,gBAAgB;AAEzB,MAAM,SAAS;AACf,MAAM,WAAW;AAEjB,MAAM,QAAQ,IAAI,SAAwB,QAAQ;AAG3C,SAAS,2BAAiC;AAC/C,QAAM,MAAM;AACd;AAQA,eAAsB,kBACpB,UACwB;AACxB,QAAM,SAAS,MAAM,IAAI,QAAQ;AACjC,MAAI,WAAW,OAAW,QAAO;AAEjC,QAAM,QAAQ,MAAM,IAAI,OAAO,QAAQ,UAAU;AAAA,IAC/C,eAAe;AAAA,IACf,iBAAiB;AAAA,IACjB,wBAAwB;AAAA,EAC1B,CAAC;AACD,QAAM,YAAY,OAAO,QAAQ,aAAa;AAC9C,QAAM,IAAI,UAAU,WAAW,MAAM;AACrC,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../src/stores/store-subdomain.ts"],"sourcesContent":["import { sdk } from \"../sdk\";\nimport { SKIP_STORE_PREVIEW } from \"./store-preview-cookie\";\nimport { TtlCache } from \"./ttl-cache\";\n\nconst TTL_MS = 60_000;\nconst MAX_REFS = 10_000;\n\nconst byRef = new TtlCache<string | null>(MAX_REFS);\n\n/** Tests only: forget every cached lookup. */\nexport function clearStoreSubdomainCache(): void {\n byRef.clear();\n}\n\n/**\n * The platform subdomain label of the store `storeRef`, or null when there\n * is no such store or it has none. Throws when the API cannot be reached, so\n * the caller can tell \"no store\" from \"don't know\" (only the former is\n * cached).\n */\nexport async function getStoreSubdomain(\n storeRef: string\n): Promise<string | null> {\n const cached = byRef.get(storeRef);\n if (cached !== undefined) return cached;\n\n // Cached for every visitor, and called from the middleware, where\n // `next/headers` is the raw incoming request: never with the ambient\n // preview token (as for a plain getStoreByHost).\n const store = await sdk.stores.getById(\n storeRef,\n {\n includeConfig: true,\n includeSettings: false,\n includeEmbeddedScripts: false,\n },\n SKIP_STORE_PREVIEW\n );\n const subdomain = store?.config?.subdomain || null;\n byRef.set(storeRef, subdomain, TTL_MS);\n return subdomain;\n}\n"],"mappings":"AAAA,SAAS,WAAW;AACpB,SAAS,0BAA0B;AACnC,SAAS,gBAAgB;AAEzB,MAAM,SAAS;AACf,MAAM,WAAW;AAEjB,MAAM,QAAQ,IAAI,SAAwB,QAAQ;AAG3C,SAAS,2BAAiC;AAC/C,QAAM,MAAM;AACd;AAQA,eAAsB,kBACpB,UACwB;AACxB,QAAM,SAAS,MAAM,IAAI,QAAQ;AACjC,MAAI,WAAW,OAAW,QAAO;AAKjC,QAAM,QAAQ,MAAM,IAAI,OAAO;AAAA,IAC7B;AAAA,IACA;AAAA,MACE,eAAe;AAAA,MACf,iBAAiB;AAAA,MACjB,wBAAwB;AAAA,IAC1B;AAAA,IACA;AAAA,EACF;AACA,QAAM,YAAY,OAAO,QAAQ,aAAa;AAC9C,QAAM,IAAI,UAAU,WAAW,MAAM;AACrC,SAAO;AACT;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecomweb/storefront-kit",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Headless storefront logic for ecomweb: auth, cart, checkout, API proxy",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -47,7 +47,7 @@
47
47
  "currency.js": "^2.0.4",
48
48
  "server-only": "^0.0.1",
49
49
  "zod": "^4.1.12",
50
- "@ecomweb/sdk": "^0.10.0"
50
+ "@ecomweb/sdk": "^0.11.0"
51
51
  },
52
52
  "peerDependencies": {
53
53
  "next": ">=16",