@shopkit/data-layer 1.4.5 → 1.4.6

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
@@ -3778,10 +3778,10 @@ var PRODUCT_ENDPOINTS = {
3778
3778
  getProduct: (v = DEFAULT_VERSION) => `/api/${v}/product/details`,
3779
3779
  getProductsByHandles: (v = DEFAULT_VERSION) => `/api/${v}/product/details`,
3780
3780
  getCollections: (v = DEFAULT_VERSION) => `/api/${v}/collections`,
3781
- getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${h}`,
3782
- getCollectionById: (v = DEFAULT_VERSION, id) => `/api/${v}/collections/${id}`,
3781
+ getCollectionByHandle: (v = DEFAULT_VERSION, h) => `/api/${v}/collections/handle/${encodeURIComponent(h)}`,
3782
+ getCollectionById: (v = DEFAULT_VERSION, id) => `/api/${v}/collections/${encodeURIComponent(id)}`,
3783
3783
  getCollectionsByHandles: (v = DEFAULT_VERSION) => `/api/${v}/collections/details`,
3784
- getCollectionFilters: (v = DEFAULT_VERSION, collectionHandle) => `/api/${v}/storefront/collections/${collectionHandle}/filters`
3784
+ getCollectionFilters: (v = DEFAULT_VERSION, collectionHandle) => `/api/${v}/storefront/collections/${encodeURIComponent(collectionHandle)}/filters`
3785
3785
  };
3786
3786
 
3787
3787
  // src/adapters/custom/product/service.ts
@@ -5008,12 +5008,69 @@ var CustomBackendClient = class extends BaseCommerceClient {
5008
5008
  }
5009
5009
  };
5010
5010
 
5011
+ // src/core/normalize-handle.ts
5012
+ function isUnsafeHandle(value) {
5013
+ for (const char of value) {
5014
+ const code = char.codePointAt(0) ?? 0;
5015
+ if (code < 32 || code === 127) {
5016
+ return true;
5017
+ }
5018
+ if (char === "/" || char === "\\" || char === "?" || char === "#" || char === '"') {
5019
+ return true;
5020
+ }
5021
+ }
5022
+ return false;
5023
+ }
5024
+ function decodeHandle(value) {
5025
+ let decoded;
5026
+ try {
5027
+ decoded = decodeURIComponent(value);
5028
+ } catch {
5029
+ return value;
5030
+ }
5031
+ return isUnsafeHandle(decoded) ? value : decoded;
5032
+ }
5033
+ function normalizeParams(params) {
5034
+ if (!params || typeof params !== "object") {
5035
+ return params;
5036
+ }
5037
+ const { handle, handles } = params;
5038
+ const hasHandle = typeof handle === "string";
5039
+ const hasHandles = Array.isArray(handles);
5040
+ if (!hasHandle && !hasHandles) {
5041
+ return params;
5042
+ }
5043
+ const next = { ...params };
5044
+ if (hasHandle) {
5045
+ next.handle = decodeHandle(handle);
5046
+ }
5047
+ if (hasHandles) {
5048
+ next.handles = handles.map(
5049
+ (h) => typeof h === "string" ? decodeHandle(h) : h
5050
+ );
5051
+ }
5052
+ return next;
5053
+ }
5054
+ function withHandleNormalization(client) {
5055
+ return new Proxy(client, {
5056
+ get(target, prop) {
5057
+ const value = Reflect.get(target, prop);
5058
+ if (typeof value !== "function") {
5059
+ return value;
5060
+ }
5061
+ return function(params, ...rest) {
5062
+ return value.call(target, normalizeParams(params), ...rest);
5063
+ };
5064
+ }
5065
+ });
5066
+ }
5067
+
5011
5068
  // src/core/init.ts
5012
5069
  function createShopifyClient(config, logging) {
5013
- return new ShopifyCommerceClient(config, logging);
5070
+ return withHandleNormalization(new ShopifyCommerceClient(config, logging));
5014
5071
  }
5015
5072
  function createCustomClient(config, logging) {
5016
- return new CustomBackendClient(config, logging);
5073
+ return withHandleNormalization(new CustomBackendClient(config, logging));
5017
5074
  }
5018
5075
  function createCommerceClient(clientConfig) {
5019
5076
  switch (clientConfig.type) {