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