@shopkit/data-layer 1.4.2 → 1.4.4

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.js CHANGED
@@ -159,6 +159,11 @@ function createDomainLogger(domain, adapter, config = {}) {
159
159
  }
160
160
 
161
161
  // src/core/utils.ts
162
+ function isUpstreamUnavailable(error) {
163
+ const status = error?.status;
164
+ if (typeof status !== "number") return true;
165
+ return status >= 500;
166
+ }
162
167
  function formatResponse(params) {
163
168
  const { success, message, data = null, error = null, meta = {} } = params;
164
169
  return {
@@ -3723,9 +3728,11 @@ var BaseCustomService = class {
3723
3728
  statusText: response.statusText,
3724
3729
  duration
3725
3730
  });
3726
- throw new Error(
3731
+ const error = new Error(
3727
3732
  `Request failed: ${response.status} ${response.statusText}`
3728
3733
  );
3734
+ error.status = response.status;
3735
+ throw error;
3729
3736
  }
3730
3737
  this.logger.info("API request successful", {
3731
3738
  method,
@@ -3800,7 +3807,31 @@ var ProductService2 = class extends BaseCustomService {
3800
3807
  PRODUCT_ENDPOINTS.getProducts(options?.version),
3801
3808
  "POST",
3802
3809
  requestParams,
3803
- options
3810
+ {
3811
+ ...options || {},
3812
+ // A list read cannot know which products come back before it runs, so it
3813
+ // takes the broad `products` tag — the one place a broad tag is the correct
3814
+ // answer rather than a blunt one. Per-product reads deliberately omit it
3815
+ // (see getProduct), which is what keeps `revalidateTag("products")` from
3816
+ // becoming a catalog-wide PDP purge: it lands only on the surfaces that
3817
+ // actually render a list — sitemaps, search endpoints, PLP rails.
3818
+ //
3819
+ // This is the producer that tag had been missing. Merchant inventory routes
3820
+ // have been calling `revalidateTag("products")` all along, against nothing:
3821
+ // no read attached it, so those deliveries returned 200 having invalidated
3822
+ // nothing. Same failure the removed `search.reindex` event had, mirrored to
3823
+ // the producer side.
3824
+ //
3825
+ // No `search` tag on a query-bearing read: it would be attached only
3826
+ // alongside `products` and purged only alongside `products`, so it could
3827
+ // never invalidate anything `products` did not already reach. See the note
3828
+ // on CACHE_INVALIDATION_TYPES in @shopkit/webhooks — a tag with no
3829
+ // independent effect is the same dead weight as one with no producer.
3830
+ next: {
3831
+ ...options?.next || {},
3832
+ tags: [cacheTags.CacheTags.products(), ...options?.next?.tags || []]
3833
+ }
3834
+ }
3804
3835
  );
3805
3836
  if (!result || !result.products) {
3806
3837
  return formatResponse({
@@ -3901,6 +3932,14 @@ var ProductService2 = class extends BaseCustomService {
3901
3932
  });
3902
3933
  } catch (error) {
3903
3934
  this.logger.error("Error in getProduct", void 0, { error });
3935
+ if (!isUpstreamUnavailable(error)) {
3936
+ return formatResponse({
3937
+ success: true,
3938
+ message: "Request completed successfully. No product found.",
3939
+ data: null,
3940
+ meta: void 0
3941
+ });
3942
+ }
3904
3943
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
3905
3944
  return formatResponse({
3906
3945
  success: false,
@@ -4114,6 +4153,14 @@ var ProductService2 = class extends BaseCustomService {
4114
4153
  });
4115
4154
  } catch (error) {
4116
4155
  this.logger.error("Error in getCollection", void 0, { error });
4156
+ if (!isUpstreamUnavailable(error)) {
4157
+ return formatResponse({
4158
+ success: true,
4159
+ message: "Request completed successfully. No collection found.",
4160
+ data: null,
4161
+ meta: void 0
4162
+ });
4163
+ }
4117
4164
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
4118
4165
  return formatResponse({
4119
4166
  success: false,
@@ -4167,7 +4214,40 @@ var ProductService2 = class extends BaseCustomService {
4167
4214
  PRODUCT_ENDPOINTS.getCollectionsByHandles(options?.version),
4168
4215
  "POST",
4169
4216
  payload,
4170
- options
4217
+ {
4218
+ ...options || {},
4219
+ // The same tags the single-collection `getCollection` attaches, one set
4220
+ // per requested handle. This is the batch form of that read, so it has to
4221
+ // carry the same invalidation coverage: without it, moving a template from
4222
+ // COLLECTION to COLLECTION_BY_HANDLES silently opts the page out of
4223
+ // webhook invalidation, and it stays stale until its own `revalidate`
4224
+ // window elapses.
4225
+ //
4226
+ // Not hypothetical: home pages render their product grids through this
4227
+ // read, so `revalidateTag(collection:<handle>)` from a product.updated
4228
+ // webhook — which did purge that product's PDP and its collection page —
4229
+ // matched nothing on the home page, and an edited title sat there for the
4230
+ // rest of the ISR window.
4231
+ //
4232
+ // Same rationale and the same deliberate omission of `revalidate` as
4233
+ // getProductsByHandles above. Tagging a POST works: Next records a fetch's
4234
+ // tags on the enclosing page's cache entry before it decides whether the
4235
+ // response itself is cacheable.
4236
+ //
4237
+ // The type guard mirrors getProductsByHandles — CacheTags.collection()
4238
+ // calls .trim() on its argument and this array is widget-supplied, so a
4239
+ // stray null would throw and fail the whole read, not just one tag.
4240
+ next: {
4241
+ ...options?.next || {},
4242
+ tags: [
4243
+ cacheTags.CacheTags.collections(),
4244
+ ...handles.filter(
4245
+ (h) => typeof h === "string" && h.trim() !== ""
4246
+ ).map((h) => cacheTags.CacheTags.collection(h)),
4247
+ ...options?.next?.tags || []
4248
+ ]
4249
+ }
4250
+ }
4171
4251
  );
4172
4252
  if (!result || !result.collections) {
4173
4253
  return formatResponse({
@@ -4225,7 +4305,24 @@ var ProductService2 = class extends BaseCustomService {
4225
4305
  PRODUCT_ENDPOINTS.getCollectionFilters(options?.version, handle),
4226
4306
  "GET",
4227
4307
  void 0,
4228
- options
4308
+ {
4309
+ ...options || {},
4310
+ // Facets are derived from this collection's products — price range, brands,
4311
+ // availability — so anything that invalidates the collection invalidates its
4312
+ // filters. One tag, keyed on the same handle, means the existing
4313
+ // product.updated / collection.updated purges already cover them.
4314
+ //
4315
+ // No broad `collections` tag here, unlike getCollection: a filter set belongs
4316
+ // to exactly one collection, and bulk collection events already purge every
4317
+ // affected handle individually.
4318
+ next: {
4319
+ ...options?.next || {},
4320
+ tags: [
4321
+ cacheTags.CacheTags.collection(handle),
4322
+ ...options?.next?.tags || []
4323
+ ]
4324
+ }
4325
+ }
4229
4326
  );
4230
4327
  if (!result) {
4231
4328
  return formatResponse({