@shopkit/data-layer 1.4.1 → 1.4.3

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,
@@ -3860,7 +3867,35 @@ var ProductService2 = class extends BaseCustomService {
3860
3867
  PRODUCT_ENDPOINTS.getProduct(options?.version),
3861
3868
  "POST",
3862
3869
  payload,
3863
- options
3870
+ {
3871
+ ...options || {},
3872
+ // Tag the read so `revalidateTag(CacheTags.product(handle))` reaches every
3873
+ // page that rendered this product, at any URL. A product is reachable at
3874
+ // six routes here (/products, /products-b, and the nested
3875
+ // /collections[-b]/[collectionname]/... variants); the webhook knows only
3876
+ // the handle, so it cannot enumerate those paths, and purging the route
3877
+ // pattern would take out every product under every collection.
3878
+ //
3879
+ // Tags apply even though this is a POST: Next records them on the page's
3880
+ // cache entry before it decides whether the fetch itself is cacheable.
3881
+ //
3882
+ // No `revalidate` on purpose — that would cache the response and freeze
3883
+ // `template_suffix`, which drives Page Template assignment and must be
3884
+ // read live. Invalidation and caching are separate knobs; we want only
3885
+ // the first. No broad CacheTags.products() either: callers already fire
3886
+ // `revalidateTag("products")` on routine inventory events, which would
3887
+ // become a catalog-wide purge.
3888
+ //
3889
+ // Handle-keyed to match the taxonomy — the webhook purges by handle, so
3890
+ // an id-keyed tag could never match.
3891
+ next: {
3892
+ ...options?.next || {},
3893
+ tags: [
3894
+ ...handle ? [cacheTags.CacheTags.product(handle)] : [],
3895
+ ...options?.next?.tags || []
3896
+ ]
3897
+ }
3898
+ }
3864
3899
  );
3865
3900
  if (!result || !result.product) {
3866
3901
  return formatResponse({
@@ -3878,6 +3913,14 @@ var ProductService2 = class extends BaseCustomService {
3878
3913
  });
3879
3914
  } catch (error) {
3880
3915
  this.logger.error("Error in getProduct", void 0, { error });
3916
+ if (!isUpstreamUnavailable(error)) {
3917
+ return formatResponse({
3918
+ success: true,
3919
+ message: "Request completed successfully. No product found.",
3920
+ data: null,
3921
+ meta: void 0
3922
+ });
3923
+ }
3881
3924
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
3882
3925
  return formatResponse({
3883
3926
  success: false,
@@ -3910,7 +3953,28 @@ var ProductService2 = class extends BaseCustomService {
3910
3953
  PRODUCT_ENDPOINTS.getProductsByHandles(options?.version),
3911
3954
  "POST",
3912
3955
  payload,
3913
- options
3956
+ {
3957
+ ...options || {},
3958
+ // One tag per requested handle, so a single-product webhook invalidates
3959
+ // every multi-product surface that included it (bundles, FBT rails,
3960
+ // hand-picked grids) — not just that product's own page. Same rationale
3961
+ // and same deliberate omission of `revalidate` as getProduct above.
3962
+ //
3963
+ // The type guard is not defensive padding: CacheTags.product() calls
3964
+ // .trim() on its argument, and this array is widget-supplied, so a stray
3965
+ // null would throw here and fail the whole read. Length/count limits are
3966
+ // Next's job — validateTags() drops over-long tags, caps the list, and
3967
+ // logs what it dropped.
3968
+ next: {
3969
+ ...options?.next || {},
3970
+ tags: [
3971
+ ...handles.filter(
3972
+ (h) => typeof h === "string" && h.trim() !== ""
3973
+ ).map((h) => cacheTags.CacheTags.product(h)),
3974
+ ...options?.next?.tags || []
3975
+ ]
3976
+ }
3977
+ }
3914
3978
  );
3915
3979
  if (!result || !result.products) {
3916
3980
  return formatResponse({
@@ -4035,8 +4099,14 @@ var ProductService2 = class extends BaseCustomService {
4035
4099
  params: queryParams,
4036
4100
  ...options || {},
4037
4101
  // Tag the collection read so a webhook can revalidateTag() it, refreshing
4038
- // the collection page AND every grid built from it. GET only — Next caches
4039
- // GET; the handle keys the entity. See @shopkit/core/cache-tags.
4102
+ // the collection page AND every grid built from it — including the home
4103
+ // page, whose entry carries a tag per collection it renders. The handle
4104
+ // keys the entity. See @shopkit/core/cache-tags.
4105
+ //
4106
+ // ("GET only" used to be noted here as if tagging depended on the method.
4107
+ // It does not — see getProduct above, which tags a POST. The method only
4108
+ // affects whether the RESPONSE is cached, not whether the tag is recorded
4109
+ // on the page.)
4040
4110
  next: {
4041
4111
  ...options?.next || {},
4042
4112
  tags: [
@@ -4064,6 +4134,14 @@ var ProductService2 = class extends BaseCustomService {
4064
4134
  });
4065
4135
  } catch (error) {
4066
4136
  this.logger.error("Error in getCollection", void 0, { error });
4137
+ if (!isUpstreamUnavailable(error)) {
4138
+ return formatResponse({
4139
+ success: true,
4140
+ message: "Request completed successfully. No collection found.",
4141
+ data: null,
4142
+ meta: void 0
4143
+ });
4144
+ }
4067
4145
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
4068
4146
  return formatResponse({
4069
4147
  success: false,