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