@shopkit/data-layer 1.6.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;
@@ -5252,6 +5252,23 @@ function createCommerceClient(clientConfig) {
5252
5252
  }
5253
5253
  }
5254
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
+
5255
5272
  // src/models/enums.ts
5256
5273
  var EOrderStatus = /* @__PURE__ */ ((EOrderStatus2) => {
5257
5274
  EOrderStatus2["PENDING"] = "PENDING";
@@ -5310,6 +5327,9 @@ var EFulfillmentStatus = /* @__PURE__ */ ((EFulfillmentStatus2) => {
5310
5327
 
5311
5328
  // src/static-params/index.ts
5312
5329
  var log = logger.child({ module: "static-params" });
5330
+ function skipBuildStaticParams() {
5331
+ return process.env.SKIP_BUILD_STATIC_PARAMS === "true";
5332
+ }
5313
5333
  async function withConcurrency(thunks, limit) {
5314
5334
  const results = new Array(thunks.length);
5315
5335
  let next = 0;
@@ -5356,6 +5376,7 @@ async function traverseCollectionProducts(client, collectionsLimit, productsPerC
5356
5376
  }
5357
5377
  function createCollectionStaticParams(getClient, options) {
5358
5378
  return async function generateStaticParams() {
5379
+ if (skipBuildStaticParams()) return [];
5359
5380
  const limit = options?.limit ?? 250;
5360
5381
  try {
5361
5382
  const result = await getClient().getCollections({ first: limit });
@@ -5375,6 +5396,7 @@ function createCollectionStaticParams(getClient, options) {
5375
5396
  }
5376
5397
  function createProductStaticParams(getClient, options) {
5377
5398
  return async function generateStaticParams() {
5399
+ if (skipBuildStaticParams()) return [];
5378
5400
  const limit = options?.limit ?? 250;
5379
5401
  try {
5380
5402
  const result = await getClient().getProducts({ first: limit });
@@ -5394,6 +5416,7 @@ function createProductStaticParams(getClient, options) {
5394
5416
  }
5395
5417
  function createProductViaCollectionsStaticParams(getClient, options) {
5396
5418
  return async function generateStaticParams() {
5419
+ if (skipBuildStaticParams()) return [];
5397
5420
  try {
5398
5421
  const pairs = await traverseCollectionProducts(
5399
5422
  getClient(),
@@ -5415,6 +5438,7 @@ function createProductViaCollectionsStaticParams(getClient, options) {
5415
5438
  }
5416
5439
  function createCollectionProductStaticParams(getClient, options) {
5417
5440
  return async function generateStaticParams() {
5441
+ if (skipBuildStaticParams()) return [];
5418
5442
  try {
5419
5443
  const pairs = await traverseCollectionProducts(
5420
5444
  getClient(),
@@ -5454,6 +5478,8 @@ exports.createCustomClient = createCustomClient;
5454
5478
  exports.createProductStaticParams = createProductStaticParams;
5455
5479
  exports.createProductViaCollectionsStaticParams = createProductViaCollectionsStaticParams;
5456
5480
  exports.createShopifyClient = createShopifyClient;
5481
+ exports.getShopperIp = getShopperIp;
5457
5482
  exports.resolveRequestSignal = resolveRequestSignal;
5483
+ exports.shopperForwardHeaders = shopperForwardHeaders;
5458
5484
  //# sourceMappingURL=index.js.map
5459
5485
  //# sourceMappingURL=index.js.map