@shopkit/data-layer 1.5.0 → 1.7.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
@@ -5,6 +5,7 @@ A comprehensive, production-ready data layer package for e-commerce applications
5
5
  ## Table of Contents
6
6
 
7
7
  - [Features](#features)
8
+ - [API Gateway Rate Limits](#api-gateway-rate-limits-shopper-ip)
8
9
  - [Installation](#installation)
9
10
  - [Quick Start](#quick-start)
10
11
  - [Configuration](#configuration)
@@ -80,6 +81,36 @@ export const generateStaticParams = createCollectionProductStaticParams(
80
81
  );
81
82
  ```
82
83
 
84
+ **Skipping build-time prerendering.** Set `SKIP_BUILD_STATIC_PARAMS=true` at build time and every
85
+ factory returns `[]` without calling the API; pages render on their first request instead
86
+ (`dynamicParams`). Use it when the API gateway rate-limits the build host: a 429 fails a page render
87
+ (so ISR keeps the last good page), which would otherwise fail a large build.
88
+
89
+ ## API Gateway Rate Limits (shopper IP)
90
+
91
+ The OS API gateway rate-limits per shopper IP and per merchant (`gk-merchant-id`, which the custom
92
+ adapter already sends). A call a storefront server makes **for a shopper** must say who the shopper is,
93
+ or every shopper looks like one client:
94
+
95
+ ```typescript
96
+ import { shopperForwardHeaders } from "@shopkit/data-layer";
97
+
98
+ // In an uncached route handler, e.g. a search proxy:
99
+ const res = await fetch(url, {
100
+ cache: "no-store",
101
+ headers: { "gk-merchant-id": merchantId, ...shopperForwardHeaders(request) },
102
+ });
103
+ ```
104
+
105
+ - `getShopperIp(headers, { trustedHops })` reads the **right-most** `x-forwarded-for` entry — the one our
106
+ load balancer appends (`trustedHops`, default 1). Entries to its left and `cf-connecting-ip` /
107
+ `true-client-ip` / `x-real-ip` come from the shopper and are ignored. A client port is stripped.
108
+ - `shopperForwardHeaders(request)` returns `{ "x-forwarded-for": ip }`, or `{}` when no IP resolves.
109
+ - **Uncached fetches only.** Next.js keys its fetch cache on request headers, so a per-shopper header on a
110
+ cached fetch (`next: { revalidate, tags }`) stores one entry per IP. ISR renders send no shopper IP.
111
+ - A gateway 429 counts as "backend unavailable", like a 5xx: `getProduct` / `getCollection` return
112
+ `success: false` rather than "not found", so a page can keep its last good copy.
113
+
83
114
  ## Installation
84
115
 
85
116
  ```bash
@@ -162,7 +162,7 @@ function createDomainLogger(domain, adapter, config = {}) {
162
162
  function isUpstreamUnavailable(error) {
163
163
  const status = error?.status;
164
164
  if (typeof status !== "number") return true;
165
- return status >= 500;
165
+ return status >= 500 || status === 429;
166
166
  }
167
167
  function formatResponse(params) {
168
168
  const { success, message, data = null, error = null, meta = {} } = params;
@@ -4392,6 +4392,14 @@ var ProductService2 = class extends BaseCustomService {
4392
4392
  async queryCollections(params, options) {
4393
4393
  try {
4394
4394
  const payload = buildCollectionsQueryPayload(params);
4395
+ if (selectedHandles(params)?.length === 0) {
4396
+ return formatResponse({
4397
+ success: true,
4398
+ message: "No collections selected.",
4399
+ data: [],
4400
+ meta: { pagination: { limit: 0, skip: 0, has_more: false, total: 0, next_cursor: null } }
4401
+ });
4402
+ }
4395
4403
  const handles = handlesNamedBy(payload.filter);
4396
4404
  const result = await this.request(
4397
4405
  PRODUCT_ENDPOINTS.queryCollections(options?.version),
@@ -4485,12 +4493,30 @@ function buildCollectionsQueryPayload(params) {
4485
4493
  if (source[key] !== void 0) payload[key] = source[key];
4486
4494
  }
4487
4495
  const filter = payload.filter && typeof payload.filter === "object" ? payload.filter : {};
4496
+ const handles = selectedHandles(params);
4497
+ if (handles) {
4498
+ payload.filter = { ...filter, handle: { $in: handles } };
4499
+ if (typeof payload.limit === "number" && payload.limit < handles.length) {
4500
+ payload.limit = handles.length;
4501
+ }
4502
+ return payload;
4503
+ }
4488
4504
  const routeHandle = source.useRouteHandle === true ? source.handle : void 0;
4489
4505
  if (typeof routeHandle === "string" && routeHandle.trim() !== "" && filter.handle === void 0) {
4490
4506
  payload.filter = { ...filter, handle: routeHandle };
4491
4507
  }
4492
4508
  return payload;
4493
4509
  }
4510
+ var MAX_COLLECTIONS_PER_QUERY = 10;
4511
+ function selectedHandles(params) {
4512
+ const source = params || {};
4513
+ if (!Array.isArray(source.handles)) return void 0;
4514
+ const filter = source.filter;
4515
+ if (filter && typeof filter === "object" && filter.handle !== void 0) {
4516
+ return void 0;
4517
+ }
4518
+ return source.handles.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h !== "").slice(0, MAX_COLLECTIONS_PER_QUERY);
4519
+ }
4494
4520
  function handlesNamedBy(filter) {
4495
4521
  const handle = filter?.handle;
4496
4522
  const candidates = typeof handle === "string" ? [handle] : handle && typeof handle === "object" ? [