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