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