@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.
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,
@@ -3853,7 +3860,35 @@ var ProductService2 = class extends BaseCustomService {
3853
3860
  PRODUCT_ENDPOINTS.getProduct(options?.version),
3854
3861
  "POST",
3855
3862
  payload,
3856
- options
3863
+ {
3864
+ ...options || {},
3865
+ // Tag the read so `revalidateTag(CacheTags.product(handle))` reaches every
3866
+ // page that rendered this product, at any URL. A product is reachable at
3867
+ // six routes here (/products, /products-b, and the nested
3868
+ // /collections[-b]/[collectionname]/... variants); the webhook knows only
3869
+ // the handle, so it cannot enumerate those paths, and purging the route
3870
+ // pattern would take out every product under every collection.
3871
+ //
3872
+ // Tags apply even though this is a POST: Next records them on the page's
3873
+ // cache entry before it decides whether the fetch itself is cacheable.
3874
+ //
3875
+ // No `revalidate` on purpose — that would cache the response and freeze
3876
+ // `template_suffix`, which drives Page Template assignment and must be
3877
+ // read live. Invalidation and caching are separate knobs; we want only
3878
+ // the first. No broad CacheTags.products() either: callers already fire
3879
+ // `revalidateTag("products")` on routine inventory events, which would
3880
+ // become a catalog-wide purge.
3881
+ //
3882
+ // Handle-keyed to match the taxonomy — the webhook purges by handle, so
3883
+ // an id-keyed tag could never match.
3884
+ next: {
3885
+ ...options?.next || {},
3886
+ tags: [
3887
+ ...handle ? [CacheTags.product(handle)] : [],
3888
+ ...options?.next?.tags || []
3889
+ ]
3890
+ }
3891
+ }
3857
3892
  );
3858
3893
  if (!result || !result.product) {
3859
3894
  return formatResponse({
@@ -3871,6 +3906,14 @@ var ProductService2 = class extends BaseCustomService {
3871
3906
  });
3872
3907
  } catch (error) {
3873
3908
  this.logger.error("Error in getProduct", void 0, { error });
3909
+ if (!isUpstreamUnavailable(error)) {
3910
+ return formatResponse({
3911
+ success: true,
3912
+ message: "Request completed successfully. No product found.",
3913
+ data: null,
3914
+ meta: void 0
3915
+ });
3916
+ }
3874
3917
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
3875
3918
  return formatResponse({
3876
3919
  success: false,
@@ -3903,7 +3946,28 @@ var ProductService2 = class extends BaseCustomService {
3903
3946
  PRODUCT_ENDPOINTS.getProductsByHandles(options?.version),
3904
3947
  "POST",
3905
3948
  payload,
3906
- options
3949
+ {
3950
+ ...options || {},
3951
+ // One tag per requested handle, so a single-product webhook invalidates
3952
+ // every multi-product surface that included it (bundles, FBT rails,
3953
+ // hand-picked grids) — not just that product's own page. Same rationale
3954
+ // and same deliberate omission of `revalidate` as getProduct above.
3955
+ //
3956
+ // The type guard is not defensive padding: CacheTags.product() calls
3957
+ // .trim() on its argument, and this array is widget-supplied, so a stray
3958
+ // null would throw here and fail the whole read. Length/count limits are
3959
+ // Next's job — validateTags() drops over-long tags, caps the list, and
3960
+ // logs what it dropped.
3961
+ next: {
3962
+ ...options?.next || {},
3963
+ tags: [
3964
+ ...handles.filter(
3965
+ (h) => typeof h === "string" && h.trim() !== ""
3966
+ ).map((h) => CacheTags.product(h)),
3967
+ ...options?.next?.tags || []
3968
+ ]
3969
+ }
3970
+ }
3907
3971
  );
3908
3972
  if (!result || !result.products) {
3909
3973
  return formatResponse({
@@ -4028,8 +4092,14 @@ var ProductService2 = class extends BaseCustomService {
4028
4092
  params: queryParams,
4029
4093
  ...options || {},
4030
4094
  // Tag the collection read so a webhook can revalidateTag() it, refreshing
4031
- // the collection page AND every grid built from it. GET only — Next caches
4032
- // GET; the handle keys the entity. See @shopkit/core/cache-tags.
4095
+ // the collection page AND every grid built from it — including the home
4096
+ // page, whose entry carries a tag per collection it renders. The handle
4097
+ // keys the entity. See @shopkit/core/cache-tags.
4098
+ //
4099
+ // ("GET only" used to be noted here as if tagging depended on the method.
4100
+ // It does not — see getProduct above, which tags a POST. The method only
4101
+ // affects whether the RESPONSE is cached, not whether the tag is recorded
4102
+ // on the page.)
4033
4103
  next: {
4034
4104
  ...options?.next || {},
4035
4105
  tags: [
@@ -4057,6 +4127,14 @@ var ProductService2 = class extends BaseCustomService {
4057
4127
  });
4058
4128
  } catch (error) {
4059
4129
  this.logger.error("Error in getCollection", void 0, { error });
4130
+ if (!isUpstreamUnavailable(error)) {
4131
+ return formatResponse({
4132
+ success: true,
4133
+ message: "Request completed successfully. No collection found.",
4134
+ data: null,
4135
+ meta: void 0
4136
+ });
4137
+ }
4060
4138
  const safeMessage = error instanceof Error ? error.message : "An unexpected error occurred while processing the request.";
4061
4139
  return formatResponse({
4062
4140
  success: false,