@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/dist/index.d.mts CHANGED
@@ -122,4 +122,23 @@ declare function resolveRequestSignal(options?: {
122
122
  timeoutMs?: number;
123
123
  }, defaultMs?: number): AbortSignal;
124
124
 
125
- export { DEFAULT_REQUEST_TIMEOUT_MS, type IClientConfig, ICommerceClient, ICustomConfig, IShopifyConfig, createCommerceClient, createCustomClient, createShopifyClient, resolveRequestSignal };
125
+ /**
126
+ * The shopper's IP, for forwarding to the OS API gateway (per-shopper rate limits).
127
+ *
128
+ * Read from the RIGHT of X-Forwarded-For: our load balancer appends the TCP peer
129
+ * it saw, so the entry `trustedHops` from the end is the one the shopper cannot
130
+ * forge. Entries to its left, and cf-connecting-ip / true-client-ip / x-real-ip,
131
+ * come from the shopper verbatim (no CDN in front of our pods overwrites them).
132
+ *
133
+ * Only for UNCACHED fetches: Next.js keys its fetch cache on request headers, so a
134
+ * per-shopper header on a cached fetch creates one cache entry per IP.
135
+ */
136
+ interface ShopperIpOptions {
137
+ /** Proxies we run in front of the app that append to X-Forwarded-For. Default 1 (the ALB). */
138
+ trustedHops?: number;
139
+ }
140
+ declare function getShopperIp(headers: Headers, options?: ShopperIpOptions): string | null;
141
+ /** `{ "x-forwarded-for": <shopper IP> }` to spread into an uncached upstream fetch, or `{}`. */
142
+ declare function shopperForwardHeaders(source: Request | Headers, options?: ShopperIpOptions): Record<string, string>;
143
+
144
+ export { DEFAULT_REQUEST_TIMEOUT_MS, type IClientConfig, ICommerceClient, ICustomConfig, IShopifyConfig, type ShopperIpOptions, createCommerceClient, createCustomClient, createShopifyClient, getShopperIp, resolveRequestSignal, shopperForwardHeaders };
package/dist/index.d.ts CHANGED
@@ -122,4 +122,23 @@ declare function resolveRequestSignal(options?: {
122
122
  timeoutMs?: number;
123
123
  }, defaultMs?: number): AbortSignal;
124
124
 
125
- export { DEFAULT_REQUEST_TIMEOUT_MS, type IClientConfig, ICommerceClient, ICustomConfig, IShopifyConfig, createCommerceClient, createCustomClient, createShopifyClient, resolveRequestSignal };
125
+ /**
126
+ * The shopper's IP, for forwarding to the OS API gateway (per-shopper rate limits).
127
+ *
128
+ * Read from the RIGHT of X-Forwarded-For: our load balancer appends the TCP peer
129
+ * it saw, so the entry `trustedHops` from the end is the one the shopper cannot
130
+ * forge. Entries to its left, and cf-connecting-ip / true-client-ip / x-real-ip,
131
+ * come from the shopper verbatim (no CDN in front of our pods overwrites them).
132
+ *
133
+ * Only for UNCACHED fetches: Next.js keys its fetch cache on request headers, so a
134
+ * per-shopper header on a cached fetch creates one cache entry per IP.
135
+ */
136
+ interface ShopperIpOptions {
137
+ /** Proxies we run in front of the app that append to X-Forwarded-For. Default 1 (the ALB). */
138
+ trustedHops?: number;
139
+ }
140
+ declare function getShopperIp(headers: Headers, options?: ShopperIpOptions): string | null;
141
+ /** `{ "x-forwarded-for": <shopper IP> }` to spread into an uncached upstream fetch, or `{}`. */
142
+ declare function shopperForwardHeaders(source: Request | Headers, options?: ShopperIpOptions): Record<string, string>;
143
+
144
+ export { DEFAULT_REQUEST_TIMEOUT_MS, type IClientConfig, ICommerceClient, ICustomConfig, IShopifyConfig, type ShopperIpOptions, createCommerceClient, createCustomClient, createShopifyClient, getShopperIp, resolveRequestSignal, shopperForwardHeaders };
package/dist/index.js CHANGED
@@ -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;
@@ -4387,6 +4387,14 @@ var ProductService2 = class extends BaseCustomService {
4387
4387
  async queryCollections(params, options) {
4388
4388
  try {
4389
4389
  const payload = buildCollectionsQueryPayload(params);
4390
+ if (selectedHandles(params)?.length === 0) {
4391
+ return formatResponse({
4392
+ success: true,
4393
+ message: "No collections selected.",
4394
+ data: [],
4395
+ meta: { pagination: { limit: 0, skip: 0, has_more: false, total: 0, next_cursor: null } }
4396
+ });
4397
+ }
4390
4398
  const handles = handlesNamedBy(payload.filter);
4391
4399
  const result = await this.request(
4392
4400
  PRODUCT_ENDPOINTS.queryCollections(options?.version),
@@ -4480,12 +4488,30 @@ function buildCollectionsQueryPayload(params) {
4480
4488
  if (source[key] !== void 0) payload[key] = source[key];
4481
4489
  }
4482
4490
  const filter = payload.filter && typeof payload.filter === "object" ? payload.filter : {};
4491
+ const handles = selectedHandles(params);
4492
+ if (handles) {
4493
+ payload.filter = { ...filter, handle: { $in: handles } };
4494
+ if (typeof payload.limit === "number" && payload.limit < handles.length) {
4495
+ payload.limit = handles.length;
4496
+ }
4497
+ return payload;
4498
+ }
4483
4499
  const routeHandle = source.useRouteHandle === true ? source.handle : void 0;
4484
4500
  if (typeof routeHandle === "string" && routeHandle.trim() !== "" && filter.handle === void 0) {
4485
4501
  payload.filter = { ...filter, handle: routeHandle };
4486
4502
  }
4487
4503
  return payload;
4488
4504
  }
4505
+ var MAX_COLLECTIONS_PER_QUERY = 10;
4506
+ function selectedHandles(params) {
4507
+ const source = params || {};
4508
+ if (!Array.isArray(source.handles)) return void 0;
4509
+ const filter = source.filter;
4510
+ if (filter && typeof filter === "object" && filter.handle !== void 0) {
4511
+ return void 0;
4512
+ }
4513
+ return source.handles.filter((h) => typeof h === "string").map((h) => h.trim()).filter((h) => h !== "").slice(0, MAX_COLLECTIONS_PER_QUERY);
4514
+ }
4489
4515
  function handlesNamedBy(filter) {
4490
4516
  const handle = filter?.handle;
4491
4517
  const candidates = typeof handle === "string" ? [handle] : handle && typeof handle === "object" ? [
@@ -5226,6 +5252,23 @@ function createCommerceClient(clientConfig) {
5226
5252
  }
5227
5253
  }
5228
5254
 
5255
+ // src/core/shopper-ip.ts
5256
+ var IPV4 = /^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$/;
5257
+ var IPV6 = /^(?=.*:)[0-9a-f:]{2,39}(\.\d{1,3}){0,3}$/i;
5258
+ function getShopperIp(headers, options) {
5259
+ const entries = (headers.get("x-forwarded-for") ?? "").split(",").map((entry2) => entry2.trim()).filter(Boolean);
5260
+ const entry = entries[entries.length - (options?.trustedHops ?? 1)];
5261
+ if (!entry) return null;
5262
+ const candidate = entry.match(/^\[([^\]]+)\](?::\d+)?$/)?.[1] ?? entry.match(/^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/)?.[1] ?? entry;
5263
+ const ip = candidate.toLowerCase().startsWith("::ffff:") && IPV4.test(candidate.slice(7)) ? candidate.slice(7) : candidate;
5264
+ return IPV4.test(ip) || IPV6.test(ip) ? ip : null;
5265
+ }
5266
+ function shopperForwardHeaders(source, options) {
5267
+ const headers = source instanceof Headers ? source : source.headers;
5268
+ const ip = getShopperIp(headers, options);
5269
+ return ip ? { "x-forwarded-for": ip } : {};
5270
+ }
5271
+
5229
5272
  // src/models/enums.ts
5230
5273
  var EOrderStatus = /* @__PURE__ */ ((EOrderStatus2) => {
5231
5274
  EOrderStatus2["PENDING"] = "PENDING";
@@ -5284,6 +5327,9 @@ var EFulfillmentStatus = /* @__PURE__ */ ((EFulfillmentStatus2) => {
5284
5327
 
5285
5328
  // src/static-params/index.ts
5286
5329
  var log = logger.child({ module: "static-params" });
5330
+ function skipBuildStaticParams() {
5331
+ return process.env.SKIP_BUILD_STATIC_PARAMS === "true";
5332
+ }
5287
5333
  async function withConcurrency(thunks, limit) {
5288
5334
  const results = new Array(thunks.length);
5289
5335
  let next = 0;
@@ -5330,6 +5376,7 @@ async function traverseCollectionProducts(client, collectionsLimit, productsPerC
5330
5376
  }
5331
5377
  function createCollectionStaticParams(getClient, options) {
5332
5378
  return async function generateStaticParams() {
5379
+ if (skipBuildStaticParams()) return [];
5333
5380
  const limit = options?.limit ?? 250;
5334
5381
  try {
5335
5382
  const result = await getClient().getCollections({ first: limit });
@@ -5349,6 +5396,7 @@ function createCollectionStaticParams(getClient, options) {
5349
5396
  }
5350
5397
  function createProductStaticParams(getClient, options) {
5351
5398
  return async function generateStaticParams() {
5399
+ if (skipBuildStaticParams()) return [];
5352
5400
  const limit = options?.limit ?? 250;
5353
5401
  try {
5354
5402
  const result = await getClient().getProducts({ first: limit });
@@ -5368,6 +5416,7 @@ function createProductStaticParams(getClient, options) {
5368
5416
  }
5369
5417
  function createProductViaCollectionsStaticParams(getClient, options) {
5370
5418
  return async function generateStaticParams() {
5419
+ if (skipBuildStaticParams()) return [];
5371
5420
  try {
5372
5421
  const pairs = await traverseCollectionProducts(
5373
5422
  getClient(),
@@ -5389,6 +5438,7 @@ function createProductViaCollectionsStaticParams(getClient, options) {
5389
5438
  }
5390
5439
  function createCollectionProductStaticParams(getClient, options) {
5391
5440
  return async function generateStaticParams() {
5441
+ if (skipBuildStaticParams()) return [];
5392
5442
  try {
5393
5443
  const pairs = await traverseCollectionProducts(
5394
5444
  getClient(),
@@ -5428,6 +5478,8 @@ exports.createCustomClient = createCustomClient;
5428
5478
  exports.createProductStaticParams = createProductStaticParams;
5429
5479
  exports.createProductViaCollectionsStaticParams = createProductViaCollectionsStaticParams;
5430
5480
  exports.createShopifyClient = createShopifyClient;
5481
+ exports.getShopperIp = getShopperIp;
5431
5482
  exports.resolveRequestSignal = resolveRequestSignal;
5483
+ exports.shopperForwardHeaders = shopperForwardHeaders;
5432
5484
  //# sourceMappingURL=index.js.map
5433
5485
  //# sourceMappingURL=index.js.map